FindComputeCpp.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #.rst:
  2. # FindComputeCpp
  3. #---------------
  4. #
  5. # Copyright 2016-2018 Codeplay Software Ltd.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use these files except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #########################
  20. # FindComputeCpp.cmake
  21. #########################
  22. #
  23. # Tools for finding and building with ComputeCpp.
  24. #
  25. # User must define ComputeCpp_DIR pointing to the ComputeCpp
  26. # installation.
  27. #
  28. # Latest version of this file can be found at:
  29. # https://github.com/codeplaysoftware/computecpp-sdk
  30. cmake_minimum_required(VERSION 3.4.3)
  31. include(FindPackageHandleStandardArgs)
  32. include(ComputeCppIRMap)
  33. set(COMPUTECPP_USER_FLAGS "" CACHE STRING "User flags for compute++")
  34. separate_arguments(COMPUTECPP_USER_FLAGS)
  35. mark_as_advanced(COMPUTECPP_USER_FLAGS)
  36. set(COMPUTECPP_BITCODE "spir64" CACHE STRING
  37. "Bitcode type to use as SYCL target in compute++")
  38. mark_as_advanced(COMPUTECPP_BITCODE)
  39. include(CMakeFindDependencyMacro)
  40. find_dependency(OpenCL REQUIRED)
  41. # Find ComputeCpp package
  42. if(DEFINED ComputeCpp_DIR)
  43. set(computecpp_find_hint ${ComputeCpp_DIR})
  44. elseif(DEFINED ENV{COMPUTECPP_DIR})
  45. set(computecpp_find_hint $ENV{COMPUTECPP_DIR})
  46. endif()
  47. # Used for running executables on the host
  48. set(computecpp_host_find_hint ${computecpp_find_hint})
  49. if(CMAKE_CROSSCOMPILING)
  50. # ComputeCpp_HOST_DIR is used to find executables that are run on the host
  51. if(DEFINED ComputeCpp_HOST_DIR)
  52. set(computecpp_host_find_hint ${ComputeCpp_HOST_DIR})
  53. elseif(DEFINED ENV{COMPUTECPP_HOST_DIR})
  54. set(computecpp_host_find_hint $ENV{COMPUTECPP_HOST_DIR})
  55. endif()
  56. endif()
  57. find_program(ComputeCpp_DEVICE_COMPILER_EXECUTABLE compute++
  58. HINTS ${computecpp_host_find_hint}
  59. PATH_SUFFIXES bin
  60. NO_SYSTEM_ENVIRONMENT_PATH)
  61. find_program(ComputeCpp_INFO_EXECUTABLE computecpp_info
  62. HINTS ${computecpp_host_find_hint}
  63. PATH_SUFFIXES bin
  64. NO_SYSTEM_ENVIRONMENT_PATH)
  65. find_library(COMPUTECPP_RUNTIME_LIBRARY
  66. NAMES ComputeCpp ComputeCpp_vs2015
  67. HINTS ${computecpp_find_hint}
  68. PATH_SUFFIXES lib
  69. DOC "ComputeCpp Runtime Library")
  70. find_library(COMPUTECPP_RUNTIME_LIBRARY_DEBUG
  71. NAMES ComputeCpp_d ComputeCpp ComputeCpp_vs2015_d
  72. HINTS ${computecpp_find_hint}
  73. PATH_SUFFIXES lib
  74. DOC "ComputeCpp Debug Runtime Library")
  75. find_path(ComputeCpp_INCLUDE_DIRS
  76. NAMES "CL/sycl.hpp"
  77. HINTS ${computecpp_find_hint}/include
  78. DOC "The ComputeCpp include directory")
  79. get_filename_component(ComputeCpp_INCLUDE_DIRS ${ComputeCpp_INCLUDE_DIRS} ABSOLUTE)
  80. get_filename_component(computecpp_canonical_root_dir "${ComputeCpp_INCLUDE_DIRS}/.." ABSOLUTE)
  81. set(ComputeCpp_ROOT_DIR "${computecpp_canonical_root_dir}" CACHE PATH
  82. "The root of the ComputeCpp install")
  83. if(NOT ComputeCpp_INFO_EXECUTABLE)
  84. message(WARNING "Can't find computecpp_info - check ComputeCpp_DIR")
  85. else()
  86. execute_process(COMMAND ${ComputeCpp_INFO_EXECUTABLE} "--dump-version"
  87. OUTPUT_VARIABLE ComputeCpp_VERSION
  88. RESULT_VARIABLE ComputeCpp_INFO_EXECUTABLE_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE)
  89. if(NOT ComputeCpp_INFO_EXECUTABLE_RESULT EQUAL "0")
  90. message(WARNING "Package version - Error obtaining version!")
  91. endif()
  92. execute_process(COMMAND ${ComputeCpp_INFO_EXECUTABLE} "--dump-is-supported"
  93. OUTPUT_VARIABLE COMPUTECPP_PLATFORM_IS_SUPPORTED
  94. RESULT_VARIABLE ComputeCpp_INFO_EXECUTABLE_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE)
  95. if(NOT ComputeCpp_INFO_EXECUTABLE_RESULT EQUAL "0")
  96. message(WARNING "platform - Error checking platform support!")
  97. else()
  98. mark_as_advanced(COMPUTECPP_PLATFORM_IS_SUPPORTED)
  99. if (COMPUTECPP_PLATFORM_IS_SUPPORTED)
  100. message(STATUS "platform - your system can support ComputeCpp")
  101. else()
  102. message(STATUS "platform - your system is not officially supported")
  103. endif()
  104. endif()
  105. endif()
  106. find_package_handle_standard_args(ComputeCpp
  107. REQUIRED_VARS ComputeCpp_ROOT_DIR
  108. ComputeCpp_DEVICE_COMPILER_EXECUTABLE
  109. ComputeCpp_INFO_EXECUTABLE
  110. COMPUTECPP_RUNTIME_LIBRARY
  111. COMPUTECPP_RUNTIME_LIBRARY_DEBUG
  112. ComputeCpp_INCLUDE_DIRS
  113. VERSION_VAR ComputeCpp_VERSION)
  114. mark_as_advanced(ComputeCpp_ROOT_DIR
  115. ComputeCpp_DEVICE_COMPILER_EXECUTABLE
  116. ComputeCpp_INFO_EXECUTABLE
  117. COMPUTECPP_RUNTIME_LIBRARY
  118. COMPUTECPP_RUNTIME_LIBRARY_DEBUG
  119. ComputeCpp_INCLUDE_DIRS
  120. ComputeCpp_VERSION)
  121. if(NOT ComputeCpp_FOUND)
  122. return()
  123. endif()
  124. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS -O2 -mllvm -inline-threshold=1000 -intelspirmetadata)
  125. mark_as_advanced(COMPUTECPP_DEVICE_COMPILER_FLAGS)
  126. if(CMAKE_CROSSCOMPILING)
  127. if(NOT COMPUTECPP_DONT_USE_TOOLCHAIN)
  128. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS --gcc-toolchain=${COMPUTECPP_TOOLCHAIN_DIR})
  129. endif()
  130. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS --sysroot=${COMPUTECPP_SYSROOT_DIR})
  131. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS -target ${COMPUTECPP_TARGET_TRIPLE})
  132. endif()
  133. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS -sycl-target ${COMPUTECPP_BITCODE})
  134. message(STATUS "compute++ flags - ${COMPUTECPP_DEVICE_COMPILER_FLAGS}")
  135. include(ComputeCppCompilerChecks)
  136. if(NOT TARGET OpenCL::OpenCL)
  137. add_library(OpenCL::OpenCL UNKNOWN IMPORTED)
  138. set_target_properties(OpenCL::OpenCL PROPERTIES
  139. IMPORTED_LOCATION "${OpenCL_LIBRARIES}"
  140. INTERFACE_INCLUDE_DIRECTORIES "${OpenCL_INCLUDE_DIRS}"
  141. )
  142. endif()
  143. if(NOT TARGET ComputeCpp::ComputeCpp)
  144. add_library(ComputeCpp::ComputeCpp UNKNOWN IMPORTED)
  145. set_target_properties(ComputeCpp::ComputeCpp PROPERTIES
  146. IMPORTED_LOCATION_DEBUG "${COMPUTECPP_RUNTIME_LIBRARY_DEBUG}"
  147. IMPORTED_LOCATION_RELWITHDEBINFO "${COMPUTECPP_RUNTIME_LIBRARY}"
  148. IMPORTED_LOCATION "${COMPUTECPP_RUNTIME_LIBRARY}"
  149. INTERFACE_INCLUDE_DIRECTORIES "${ComputeCpp_INCLUDE_DIRS}"
  150. INTERFACE_LINK_LIBRARIES "OpenCL::OpenCL"
  151. )
  152. endif()
  153. # This property allows targets to specify that their sources should be
  154. # compiled with the integration header included after the user's
  155. # sources, not before (e.g. when an enum is used in a kernel name, this
  156. # is not technically valid SYCL code but can work with ComputeCpp)
  157. define_property(
  158. TARGET PROPERTY COMPUTECPP_INCLUDE_AFTER
  159. BRIEF_DOCS "Include integration header after user source"
  160. FULL_DOCS "Changes compiler arguments such that the source file is
  161. actually the integration header, and the .cpp file is included on
  162. the command line so that it is seen by the compiler first. Enables
  163. non-standards-conformant SYCL code to compile with ComputeCpp."
  164. )
  165. define_property(
  166. TARGET PROPERTY INTERFACE_COMPUTECPP_FLAGS
  167. BRIEF_DOCS "Interface compile flags to provide compute++"
  168. FULL_DOCS "Set additional compile flags to pass to compute++ when compiling
  169. any target which links to this one."
  170. )
  171. define_property(
  172. SOURCE PROPERTY COMPUTECPP_SOURCE_FLAGS
  173. BRIEF_DOCS "Source file compile flags for compute++"
  174. FULL_DOCS "Set additional compile flags for compiling the SYCL integration
  175. header for the given source file."
  176. )
  177. ####################
  178. # __build_ir
  179. ####################
  180. #
  181. # Adds a custom target for running compute++ and adding a dependency for the
  182. # resulting integration header and kernel binary.
  183. #
  184. # TARGET : Name of the target.
  185. # SOURCE : Source file to be compiled.
  186. # COUNTER : Counter included in name of custom target. Different counter
  187. # values prevent duplicated names of custom target when source files with
  188. # the same name, but located in different directories, are used for the
  189. # same target.
  190. #
  191. function(__build_ir)
  192. set(options)
  193. set(one_value_args
  194. TARGET
  195. SOURCE
  196. COUNTER
  197. )
  198. set(multi_value_args)
  199. cmake_parse_arguments(SDK_BUILD_IR
  200. "${options}"
  201. "${one_value_args}"
  202. "${multi_value_args}"
  203. ${ARGN}
  204. )
  205. get_filename_component(sourceFileName ${SDK_BUILD_IR_SOURCE} NAME)
  206. # Set the path to the integration header.
  207. # The .sycl filename must depend on the target so that different targets
  208. # using the same source file will be generated with a different rule.
  209. set(baseSyclName ${CMAKE_CURRENT_BINARY_DIR}/${SDK_BUILD_IR_TARGET}_${sourceFileName})
  210. set(outputSyclFile ${baseSyclName}.sycl)
  211. set(outputDeviceFile ${baseSyclName}.${IR_MAP_${COMPUTECPP_BITCODE}})
  212. set(depFileName ${baseSyclName}.sycl.d)
  213. set(include_directories "$<TARGET_PROPERTY:${SDK_BUILD_IR_TARGET},INCLUDE_DIRECTORIES>")
  214. set(compile_definitions "$<TARGET_PROPERTY:${SDK_BUILD_IR_TARGET},COMPILE_DEFINITIONS>")
  215. set(generated_include_directories
  216. $<$<BOOL:${include_directories}>:-I\"$<JOIN:${include_directories},\"\t-I\">\">)
  217. set(generated_compile_definitions
  218. $<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},\t-D>>)
  219. # Obtain language standard of the file
  220. set(device_compiler_cxx_standard)
  221. get_target_property(targetCxxStandard ${SDK_BUILD_IR_TARGET} CXX_STANDARD)
  222. if (targetCxxStandard MATCHES 17)
  223. set(device_compiler_cxx_standard "-std=c++1z")
  224. elseif (targetCxxStandard MATCHES 14)
  225. set(device_compiler_cxx_standard "-std=c++14")
  226. elseif (targetCxxStandard MATCHES 11)
  227. set(device_compiler_cxx_standard "-std=c++11")
  228. elseif (targetCxxStandard MATCHES 98)
  229. message(FATAL_ERROR "SYCL applications cannot be compiled using C++98")
  230. else ()
  231. set(device_compiler_cxx_standard "")
  232. endif()
  233. get_property(source_compile_flags
  234. SOURCE ${SDK_BUILD_IR_SOURCE}
  235. PROPERTY COMPUTECPP_SOURCE_FLAGS
  236. )
  237. separate_arguments(source_compile_flags)
  238. if(source_compile_flags)
  239. list(APPEND computecpp_source_flags ${source_compile_flags})
  240. endif()
  241. list(APPEND COMPUTECPP_DEVICE_COMPILER_FLAGS
  242. ${device_compiler_cxx_standard}
  243. ${COMPUTECPP_USER_FLAGS}
  244. ${computecpp_source_flags}
  245. )
  246. set(ir_dependencies ${SDK_BUILD_IR_SOURCE})
  247. get_target_property(target_libraries ${SDK_BUILD_IR_TARGET} LINK_LIBRARIES)
  248. if(target_libraries)
  249. foreach(library ${target_libraries})
  250. if(TARGET ${library})
  251. list(APPEND ir_dependencies ${library})
  252. endif()
  253. endforeach()
  254. endif()
  255. # Depfile support was only added in CMake 3.7
  256. # CMake throws an error if it is unsupported by the generator (i. e. not ninja)
  257. if((NOT CMAKE_VERSION VERSION_LESS 3.7.0) AND
  258. CMAKE_GENERATOR MATCHES "Ninja")
  259. file(RELATIVE_PATH relOutputFile ${CMAKE_BINARY_DIR} ${outputDeviceFile})
  260. set(generate_depfile -MMD -MF ${depFileName} -MT ${relOutputFile})
  261. set(enable_depfile DEPFILE ${depFileName})
  262. endif()
  263. # Add custom command for running compute++
  264. add_custom_command(
  265. OUTPUT ${outputDeviceFile} ${outputSyclFile}
  266. COMMAND ${ComputeCpp_DEVICE_COMPILER_EXECUTABLE}
  267. ${COMPUTECPP_DEVICE_COMPILER_FLAGS}
  268. ${generated_include_directories}
  269. ${generated_compile_definitions}
  270. -sycl-ih ${outputSyclFile}
  271. -o ${outputDeviceFile}
  272. -c ${SDK_BUILD_IR_SOURCE}
  273. ${generate_depfile}
  274. DEPENDS ${ir_dependencies}
  275. IMPLICIT_DEPENDS CXX ${SDK_BUILD_IR_SOURCE}
  276. ${enable_depfile}
  277. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  278. COMMENT "Building ComputeCpp integration header file ${outputSyclFile}")
  279. # Name: (user-defined name)_(source file)_(counter)_ih
  280. set(headerTargetName
  281. ${SDK_BUILD_IR_TARGET}_${sourceFileName}_${SDK_BUILD_IR_COUNTER}_ih)
  282. if(NOT MSVC)
  283. # Add a custom target for the generated integration header
  284. add_custom_target(${headerTargetName} DEPENDS ${outputDeviceFile} ${outputSyclFile})
  285. add_dependencies(${SDK_BUILD_IR_TARGET} ${headerTargetName})
  286. endif()
  287. # This property can be set on a per-target basis to indicate that the
  288. # integration header should appear after the main source listing
  289. get_target_property(includeAfter ${SDK_ADD_SYCL_TARGET} COMPUTECPP_INCLUDE_AFTER)
  290. if(includeAfter)
  291. # Change the source file to the integration header - e.g.
  292. # g++ -c source_file_name.cpp.sycl
  293. get_target_property(current_sources ${SDK_BUILD_IR_TARGET} SOURCES)
  294. # Remove absolute path to source file
  295. list(REMOVE_ITEM current_sources ${SDK_BUILD_IR_SOURCE})
  296. # Remove relative path to source file
  297. string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" ""
  298. rel_source_file ${SDK_BUILD_IR_SOURCE}
  299. )
  300. list(REMOVE_ITEM current_sources ${rel_source_file})
  301. # Add SYCL header to source list
  302. list(APPEND current_sources ${outputSyclFile})
  303. set_property(TARGET ${SDK_BUILD_IR_TARGET}
  304. PROPERTY SOURCES ${current_sources})
  305. # CMake/gcc don't know what language a .sycl file is, so tell them
  306. set_property(SOURCE ${outputSyclFile} PROPERTY LANGUAGE CXX)
  307. set(includedFile ${SDK_BUILD_IR_SOURCE})
  308. set(cppFile ${outputSyclFile})
  309. else()
  310. set_property(SOURCE ${outputSyclFile} PROPERTY HEADER_FILE_ONLY ON)
  311. set(includedFile ${outputSyclFile})
  312. set(cppFile ${SDK_BUILD_IR_SOURCE})
  313. endif()
  314. # Force inclusion of the integration header for the host compiler
  315. if(MSVC)
  316. # Group SYCL files inside Visual Studio
  317. source_group("SYCL" FILES ${outputSyclFile})
  318. if(includeAfter)
  319. # Allow the source file to be edited using Visual Studio.
  320. # It will be added as a header file so it won't be compiled.
  321. set_property(SOURCE ${SDK_BUILD_IR_SOURCE} PROPERTY HEADER_FILE_ONLY true)
  322. endif()
  323. # Add both source and the sycl files to the VS solution.
  324. target_sources(${SDK_BUILD_IR_TARGET} PUBLIC ${SDK_BUILD_IR_SOURCE} ${outputSyclFile})
  325. set(forceIncludeFlags "/FI${includedFile} /TP")
  326. else()
  327. set(forceIncludeFlags "-include ${includedFile} -x c++")
  328. endif()
  329. set_property(
  330. SOURCE ${cppFile}
  331. APPEND_STRING PROPERTY COMPILE_FLAGS "${forceIncludeFlags}"
  332. )
  333. endfunction(__build_ir)
  334. #######################
  335. # add_sycl_to_target
  336. #######################
  337. #
  338. # Adds a SYCL compilation custom command associated with an existing
  339. # target and sets a dependancy on that new command.
  340. #
  341. # TARGET : Name of the target to add SYCL to.
  342. # SOURCES : Source files to be compiled for SYCL.
  343. #
  344. function(add_sycl_to_target)
  345. set(options)
  346. set(one_value_args
  347. TARGET
  348. )
  349. set(multi_value_args
  350. SOURCES
  351. )
  352. cmake_parse_arguments(SDK_ADD_SYCL
  353. "${options}"
  354. "${one_value_args}"
  355. "${multi_value_args}"
  356. ${ARGN}
  357. )
  358. set_target_properties(${SDK_ADD_SYCL_TARGET} PROPERTIES LINKER_LANGUAGE CXX)
  359. # If the CXX compiler is set to compute++ enable the driver.
  360. get_filename_component(cmakeCxxCompilerFileName "${CMAKE_CXX_COMPILER}" NAME)
  361. if("${cmakeCxxCompilerFileName}" STREQUAL "compute++")
  362. if(MSVC)
  363. message(FATAL_ERROR "The compiler driver is not supported by this system,
  364. revert the CXX compiler to your default host compiler.")
  365. endif()
  366. get_target_property(includeAfter ${SDK_ADD_SYCL_TARGET} COMPUTECPP_INCLUDE_AFTER)
  367. if(includeAfter)
  368. list(APPEND COMPUTECPP_USER_FLAGS -fsycl-ih-last)
  369. endif()
  370. list(INSERT COMPUTECPP_DEVICE_COMPILER_FLAGS 0 -sycl-driver)
  371. # Prepend COMPUTECPP_DEVICE_COMPILER_FLAGS and append COMPUTECPP_USER_FLAGS
  372. foreach(prop COMPILE_OPTIONS INTERFACE_COMPILE_OPTIONS)
  373. get_target_property(target_compile_options ${SDK_ADD_SYCL_TARGET} ${prop})
  374. if(NOT target_compile_options)
  375. set(target_compile_options "")
  376. endif()
  377. set_property(
  378. TARGET ${SDK_ADD_SYCL_TARGET}
  379. PROPERTY ${prop}
  380. ${COMPUTECPP_DEVICE_COMPILER_FLAGS}
  381. ${target_compile_options}
  382. ${COMPUTECPP_USER_FLAGS}
  383. )
  384. endforeach()
  385. else()
  386. set(fileCounter 0)
  387. list(INSERT COMPUTECPP_DEVICE_COMPILER_FLAGS 0 -sycl)
  388. # Add custom target to run compute++ and generate the integration header
  389. foreach(sourceFile ${SDK_ADD_SYCL_SOURCES})
  390. if(NOT IS_ABSOLUTE ${sourceFile})
  391. set(sourceFile "${CMAKE_CURRENT_SOURCE_DIR}/${sourceFile}")
  392. endif()
  393. __build_ir(
  394. TARGET ${SDK_ADD_SYCL_TARGET}
  395. SOURCE ${sourceFile}
  396. COUNTER ${fileCounter}
  397. )
  398. MATH(EXPR fileCounter "${fileCounter} + 1")
  399. endforeach()
  400. endif()
  401. set_property(TARGET ${SDK_ADD_SYCL_TARGET}
  402. APPEND PROPERTY LINK_LIBRARIES ComputeCpp::ComputeCpp)
  403. set_property(TARGET ${SDK_ADD_SYCL_TARGET}
  404. APPEND PROPERTY INTERFACE_LINK_LIBRARIES ComputeCpp::ComputeCpp)
  405. endfunction(add_sycl_to_target)