1 cmake_minimum_required(VERSION 3.13.4)
3 project( libclc VERSION 0.2.0 LANGUAGES CXX C)
5 set(CMAKE_CXX_STANDARD 17)
7 include( GNUInstallDirs )
8 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
9 amdgcn-amdhsa/lib/SOURCES;
11 amdgcn-mesa3d/lib/SOURCES;
17 ptx-nvidiacl/lib/SOURCES;
24 set( LIBCLC_TARGETS_ALL
38 set( LIBCLC_MIN_LLVM "3.9.0" )
40 set( LIBCLC_TARGETS_TO_BUILD "all"
41 CACHE STRING "Semicolon-separated list of targets to build, or 'all'." )
43 option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support."
46 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
49 message( "LLVM version: ${LLVM_PACKAGE_VERSION}" )
51 if( ${LLVM_PACKAGE_VERSION} VERSION_LESS ${LIBCLC_MIN_LLVM} )
52 message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
55 # mesa3d environment is only available since LLVM 4.0
56 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.0" )
57 set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
60 if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
61 set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
64 find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
65 find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
66 find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
67 find_program( LLVM_OPT opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
68 find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
71 message( "clang: ${LLVM_CLANG}" )
72 message( "llvm-as: ${LLVM_AS}" )
73 message( "llvm-link: ${LLVM_LINK}" )
74 message( "opt: ${LLVM_OPT}" )
75 message( "llvm-spirv: ${LLVM_SPIRV}" )
77 if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
78 message( FATAL_ERROR "toolchain incomplete!" )
81 list( SORT LIBCLC_TARGETS_TO_BUILD )
83 if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
85 message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
89 set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
90 set( CMAKE_CLC_COMPILER ${LLVM_CLANG} )
91 set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
92 set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
93 set( CMAKE_LLAsm_COMPILER ${LLVM_AS} )
94 set( CMAKE_LLAsm_ARCHIVE ${LLVM_LINK} )
96 # Construct LLVM version define
97 set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_VERSION_MAJOR}0${LLVM_VERSION_MINOR}" )
100 # LLVM 13 enables standard includes by default
101 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "12.99.99" )
102 set( CMAKE_LLAsm_FLAGS "${CMAKE_LLAsm_FLAGS} -cl-no-stdinc")
103 set( CMAKE_CLC_FLAGS "${CMAKE_CLC_FLAGS} -cl-no-stdinc")
106 enable_language( CLC LLAsm )
107 # This needs to be set before any target that needs it
108 # We need to use LLVM_INCLUDE_DIRS here, because if we are linking to an
109 # llvm build directory, this includes $src/llvm/include which is where all the
110 # headers are not $build/include/ which is what LLVM_INCLUDE_DIR is set to.
111 include_directories( ${LLVM_INCLUDE_DIRS} )
113 # Setup prepare_builtins tools
114 set(LLVM_LINK_COMPONENTS
119 add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
120 target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
121 # These were not properly reported in early LLVM and we don't need them
122 target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
125 set( r600--_devices cedar cypress barts cayman )
126 set( amdgcn--_devices tahiti )
127 set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
128 set( amdgcn--amdhsa_devices none )
129 set( clspv--_devices none )
130 set( clspv64--_devices none )
131 set( nvptx--_devices none )
132 set( nvptx64--_devices none )
133 set( nvptx--nvidiacl_devices none )
134 set( nvptx64--nvidiacl_devices none )
135 set( spirv-mesa3d-_devices none )
136 set( spirv64-mesa3d-_devices none )
139 set( cedar_aliases palm sumo sumo2 redwood juniper )
140 set( cypress_aliases hemlock )
141 set( barts_aliases turks caicos )
142 set( cayman_aliases aruba )
143 set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
144 mullins tonga iceland carrizo fiji stoney polaris10 polaris11 )
146 # Support for gfx9 was added in LLVM 5.0 (r295554)
147 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "4.99.99" )
148 set( tahiti_aliases ${tahiti_aliases} gfx900 gfx902 )
151 # Support for Vega12 and Vega20 was added in LLVM 7 (r331215)
152 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "6.99.99" )
153 set( tahiti_aliases ${tahiti_aliases} gfx904 gfx906 )
157 configure_file( libclc.pc.in libclc.pc @ONLY )
158 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
159 install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" )
161 if( ENABLE_RUNTIME_SUBNORMAL )
162 add_library( subnormal_use_default STATIC
163 generic/lib/subnormal_use_default.ll )
164 add_library( subnormal_disable STATIC
165 generic/lib/subnormal_disable.ll )
166 install( TARGETS subnormal_use_default subnormal_disable ARCHIVE
167 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
170 find_package( Python3 REQUIRED COMPONENTS Interpreter )
171 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
174 COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
175 DEPENDS ${script_loc} )
176 add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
180 foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
181 message( "BUILDING ${t}" )
182 string( REPLACE "-" ";" TRIPLE ${t} )
183 list( GET TRIPLE 0 ARCH )
184 list( GET TRIPLE 1 VENDOR )
185 list( GET TRIPLE 2 OS )
189 if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND
190 NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64)
191 LIST( APPEND dirs generic )
194 if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
195 list( APPEND dirs amdgpu )
199 if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
205 # Enumerate SOURCES* files
207 foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
208 foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
209 file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
210 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
211 # Prepend the location to give higher priority to
212 # specialized implementation
214 set( source_list ${file_loc} ${source_list} )
219 # Add the generated convert.cl here to prevent adding
220 # the one listed in SOURCES
221 if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" )
222 set( rel_files convert.cl )
223 set( objects convert.cl )
224 if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND
225 NOT ${ARCH} STREQUAL "clspv64" )
226 list( APPEND rel_files generic/lib/subnormal_use_default.ll )
233 foreach( l ${source_list} )
234 file( READ ${l} file_list )
235 string( REPLACE "\n" ";" file_list ${file_list} )
236 get_filename_component( dir ${l} DIRECTORY )
237 foreach( f ${file_list} )
238 list( FIND objects ${f} found )
240 list( APPEND objects ${f} )
241 list( APPEND rel_files ${dir}/${f} )
242 # FIXME: This should really go away
243 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
244 get_filename_component( fdir ${src_loc} DIRECTORY )
246 set_source_files_properties( ${dir}/${f}
247 PROPERTIES COMPILE_FLAGS "-I ${fdir}" )
252 foreach( d ${${t}_devices} )
253 # Some targets don't have a specific GPU to target
254 if( ${d} STREQUAL "none" OR ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
256 set( arch_suffix "${t}" )
258 set( mcpu "-mcpu=${d}" )
259 set( arch_suffix "${d}-${t}" )
261 message( " DEVICE: ${d} ( ${${d}_aliases} )" )
263 if ( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
264 if( ${ARCH} STREQUAL "spirv" )
269 set( build_flags -O0 -finline-hint-functions )
271 set( spvflags --spirv-max-version=1.1 )
272 elseif( ${ARCH} STREQUAL "clspv" )
276 elseif( ${ARCH} STREQUAL "clspv64" )
285 add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
286 # Make sure we depend on the pseudo target to prevent
287 # multiple invocations
288 add_dependencies( builtins.link.${arch_suffix}
289 generate_convert.cl )
290 # CMake will turn this include into absolute path
291 target_include_directories( builtins.link.${arch_suffix} PRIVATE
293 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
295 string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
296 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
297 ${CLC_TARGET_DEFINE} )
298 target_compile_options( builtins.link.${arch_suffix} PRIVATE -target
299 ${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
300 set_target_properties( builtins.link.${arch_suffix} PROPERTIES
301 LINKER_LANGUAGE CLC )
303 set( obj_suffix ${arch_suffix}.bc )
306 add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
307 COMMAND ${LLVM_OPT} ${opt_flags} -o
308 "builtins.opt.${obj_suffix}"
309 "builtins.link.${obj_suffix}"
310 DEPENDS "builtins.link.${arch_suffix}" )
311 add_custom_target( "opt.${obj_suffix}" ALL
312 DEPENDS "builtins.opt.${obj_suffix}" )
314 if( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
315 set( spv_suffix ${arch_suffix}.spv )
316 add_custom_command( OUTPUT "${spv_suffix}"
317 COMMAND ${LLVM_SPIRV} ${spvflags}
319 "builtins.link.${obj_suffix}"
320 DEPENDS "builtins.link.${arch_suffix}" )
321 add_custom_target( "prepare-${spv_suffix}" ALL
322 DEPENDS "${spv_suffix}" )
323 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
324 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
328 add_custom_command( OUTPUT "${obj_suffix}"
329 COMMAND prepare_builtins -o
331 "builtins.opt.${obj_suffix}"
332 DEPENDS "opt.${obj_suffix}"
333 "builtins.opt.${obj_suffix}"
335 add_custom_target( "prepare-${obj_suffix}" ALL
336 DEPENDS "${obj_suffix}" )
338 # nvptx-- targets don't include workitem builtins
339 if( NOT ${t} MATCHES ".*ptx.*--$" )
340 add_test( NAME external-calls-${obj_suffix}
341 COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
342 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
345 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
346 foreach( a ${${d}_aliases} )
347 set( alias_suffix "${a}-${t}.bc" )
348 add_custom_target( ${alias_suffix} ALL
349 COMMAND ${CMAKE_COMMAND} -E
350 create_symlink ${obj_suffix}
352 DEPENDS "prepare-${obj_suffix}" )
353 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )