1 cmake_minimum_required(VERSION 3.20.0)
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
120 add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
121 target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
122 # These were not properly reported in early LLVM and we don't need them
123 target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
126 set( r600--_devices cedar cypress barts cayman )
127 set( amdgcn--_devices tahiti )
128 set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
129 set( amdgcn--amdhsa_devices none )
130 set( clspv--_devices none )
131 set( clspv64--_devices none )
132 set( nvptx--_devices none )
133 set( nvptx64--_devices none )
134 set( nvptx--nvidiacl_devices none )
135 set( nvptx64--nvidiacl_devices none )
136 set( spirv-mesa3d-_devices none )
137 set( spirv64-mesa3d-_devices none )
140 set( cedar_aliases palm sumo sumo2 redwood juniper )
141 set( cypress_aliases hemlock )
142 set( barts_aliases turks caicos )
143 set( cayman_aliases aruba )
144 set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
145 mullins tonga iceland carrizo fiji stoney polaris10 polaris11 )
147 # Support for gfx9 was added in LLVM 5.0 (r295554)
148 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "4.99.99" )
149 set( tahiti_aliases ${tahiti_aliases} gfx900 gfx902 )
152 # Support for Vega12 and Vega20 was added in LLVM 7 (r331215)
153 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "6.99.99" )
154 set( tahiti_aliases ${tahiti_aliases} gfx904 gfx906 )
158 configure_file( libclc.pc.in libclc.pc @ONLY )
159 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
160 install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" )
162 if( ENABLE_RUNTIME_SUBNORMAL )
163 add_library( subnormal_use_default STATIC
164 generic/lib/subnormal_use_default.ll )
165 add_library( subnormal_disable STATIC
166 generic/lib/subnormal_disable.ll )
167 install( TARGETS subnormal_use_default subnormal_disable ARCHIVE
168 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
171 find_package( Python3 REQUIRED COMPONENTS Interpreter )
172 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
175 COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
176 DEPENDS ${script_loc} )
177 add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
181 foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
182 message( "BUILDING ${t}" )
183 string( REPLACE "-" ";" TRIPLE ${t} )
184 list( GET TRIPLE 0 ARCH )
185 list( GET TRIPLE 1 VENDOR )
186 list( GET TRIPLE 2 OS )
190 if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND
191 NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64)
192 LIST( APPEND dirs generic )
195 if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
196 list( APPEND dirs amdgpu )
200 if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
206 # Enumerate SOURCES* files
208 foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
209 foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
210 file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
211 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
212 # Prepend the location to give higher priority to
213 # specialized implementation
215 set( source_list ${file_loc} ${source_list} )
220 # Add the generated convert.cl here to prevent adding
221 # the one listed in SOURCES
222 if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" )
223 set( rel_files convert.cl )
224 set( objects convert.cl )
225 if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND
226 NOT ${ARCH} STREQUAL "clspv64" )
227 list( APPEND rel_files generic/lib/subnormal_use_default.ll )
234 foreach( l ${source_list} )
235 file( READ ${l} file_list )
236 string( REPLACE "\n" ";" file_list ${file_list} )
237 get_filename_component( dir ${l} DIRECTORY )
238 foreach( f ${file_list} )
239 list( FIND objects ${f} found )
241 list( APPEND objects ${f} )
242 list( APPEND rel_files ${dir}/${f} )
243 # FIXME: This should really go away
244 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
245 get_filename_component( fdir ${src_loc} DIRECTORY )
247 set_source_files_properties( ${dir}/${f}
248 PROPERTIES COMPILE_FLAGS "-I ${fdir}" )
253 foreach( d ${${t}_devices} )
254 # Some targets don't have a specific GPU to target
255 if( ${d} STREQUAL "none" OR ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
257 set( arch_suffix "${t}" )
259 set( mcpu "-mcpu=${d}" )
260 set( arch_suffix "${d}-${t}" )
262 message( " DEVICE: ${d} ( ${${d}_aliases} )" )
264 if ( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
265 if( ${ARCH} STREQUAL "spirv" )
270 set( build_flags -O0 -finline-hint-functions )
272 set( spvflags --spirv-max-version=1.1 )
273 elseif( ${ARCH} STREQUAL "clspv" )
275 set( build_flags "-Wno-unknown-assumption")
277 elseif( ${ARCH} STREQUAL "clspv64" )
279 set( build_flags "-Wno-unknown-assumption")
286 add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
287 # Make sure we depend on the pseudo target to prevent
288 # multiple invocations
289 add_dependencies( builtins.link.${arch_suffix}
290 generate_convert.cl )
291 # CMake will turn this include into absolute path
292 target_include_directories( builtins.link.${arch_suffix} PRIVATE
294 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
296 string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
297 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
298 ${CLC_TARGET_DEFINE} )
299 target_compile_options( builtins.link.${arch_suffix} PRIVATE -target
300 ${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
301 set_target_properties( builtins.link.${arch_suffix} PROPERTIES
302 LINKER_LANGUAGE CLC )
304 set( obj_suffix ${arch_suffix}.bc )
307 add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
308 COMMAND ${LLVM_OPT} ${opt_flags} -o
309 "builtins.opt.${obj_suffix}"
310 "builtins.link.${obj_suffix}"
311 DEPENDS "builtins.link.${arch_suffix}" )
312 add_custom_target( "opt.${obj_suffix}" ALL
313 DEPENDS "builtins.opt.${obj_suffix}" )
315 if( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
316 set( spv_suffix ${arch_suffix}.spv )
317 add_custom_command( OUTPUT "${spv_suffix}"
318 COMMAND ${LLVM_SPIRV} ${spvflags}
320 "builtins.link.${obj_suffix}"
321 DEPENDS "builtins.link.${arch_suffix}" )
322 add_custom_target( "prepare-${spv_suffix}" ALL
323 DEPENDS "${spv_suffix}" )
324 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
325 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
329 add_custom_command( OUTPUT "${obj_suffix}"
330 COMMAND prepare_builtins -o
332 "builtins.opt.${obj_suffix}"
333 DEPENDS "opt.${obj_suffix}"
334 "builtins.opt.${obj_suffix}"
336 add_custom_target( "prepare-${obj_suffix}" ALL
337 DEPENDS "${obj_suffix}" )
339 # nvptx-- targets don't include workitem builtins
340 if( NOT ${t} MATCHES ".*ptx.*--$" )
341 add_test( NAME external-calls-${obj_suffix}
342 COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
343 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
346 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
347 foreach( a ${${d}_aliases} )
348 set( alias_suffix "${a}-${t}.bc" )
349 add_custom_target( ${alias_suffix} ALL
350 COMMAND ${CMAKE_COMMAND} -E
351 create_symlink ${obj_suffix}
353 DEPENDS "prepare-${obj_suffix}" )
354 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )