FindComputeCpp.cmake 16 KB

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