blob: 9858ae905983f329c26d86734a613dfae975c9e7 [file] [log] [blame]
Mark de Wevercbaa3592023-05-24 16:12:321cmake_minimum_required(VERSION 3.20.0)
Jan Vesely2ce1d092018-11-27 16:07:192
Tom Stellardb2647872022-11-23 06:56:553project( libclc VERSION 0.2.0 LANGUAGES CXX C)
4
Tom Stellard409f42b2023-01-27 18:50:015set(CMAKE_CXX_STANDARD 17)
6
Fraser Cormack72f98812024-04-11 16:09:077# Add path for custom modules
8list( INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake/modules" )
9
10set( LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
11set( LIBCLC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
12set( LIBCLC_OBJFILE_DIR ${LIBCLC_BINARY_DIR}/obj.libclc.dir )
13
14include( AddLibclc )
15
Jan Veselye25db172019-01-07 20:20:3716include( GNUInstallDirs )
Jan Vesely814fb652020-02-15 03:29:0417set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
18 amdgcn-amdhsa/lib/SOURCES;
19 amdgcn/lib/SOURCES;
20 amdgcn-mesa3d/lib/SOURCES;
21 amdgpu/lib/SOURCES;
Alan Baker21427b82020-12-07 20:54:1422 clspv/lib/SOURCES;
Kévin Petitec0a8802022-01-05 16:32:0823 clspv64/lib/SOURCES;
Jan Vesely814fb652020-02-15 03:29:0424 generic/lib/SOURCES;
25 ptx/lib/SOURCES;
26 ptx-nvidiacl/lib/SOURCES;
Dave Airliec37145c2020-08-17 20:45:0427 r600/lib/SOURCES;
28 spirv/lib/SOURCES;
29 spirv64/lib/SOURCES
Jan Vesely814fb652020-02-15 03:29:0430)
Jan Vesely2ce1d092018-11-27 16:07:1931
Fraser Cormack338ecfb2024-04-04 16:54:5432set( LIBCLC_MIN_LLVM 3.9.0 )
Jan Vesely2ce1d092018-11-27 16:07:1933
34set( LIBCLC_TARGETS_TO_BUILD "all"
Fraser Cormack93d51192024-04-16 15:55:5335 CACHE STRING "Semicolon-separated list of libclc targets to build, or 'all'." )
Jan Vesely2ce1d092018-11-27 16:07:1936
Fraser Cormack92539502024-03-18 14:37:0437option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF )
Jan Vesely2ce1d092018-11-27 16:07:1938
Fraser Cormack72f98812024-04-11 16:09:0739if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
40 # Out-of-tree configuration
41 set( LIBCLC_STANDALONE_BUILD TRUE )
Jan Vesely2ce1d092018-11-27 16:07:1942
Fraser Cormack72f98812024-04-11 16:09:0743 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
44 include(AddLLVM)
Tom Stellardb2647872022-11-23 06:56:5545
Fraser Cormack72f98812024-04-11 16:09:0746 message( STATUS "libclc LLVM version: ${LLVM_PACKAGE_VERSION}" )
47
48 if( LLVM_PACKAGE_VERSION VERSION_LESS LIBCLC_MIN_LLVM )
49 message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
50 endif()
51
52 # Import required tools as targets
Fraser Cormack0aeeff32024-04-16 15:46:3753 if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
54 foreach( tool IN ITEMS clang llvm-as llvm-link opt )
55 find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
56 add_executable( libclc::${tool} IMPORTED GLOBAL )
57 set_target_properties( libclc::${tool} PROPERTIES IMPORTED_LOCATION ${LLVM_TOOL_${tool}} )
58 endforeach()
59 endif()
Fraser Cormack72f98812024-04-11 16:09:0760else()
61 # In-tree configuration
62 set( LIBCLC_STANDALONE_BUILD FALSE )
63
64 set( LLVM_PACKAGE_VERSION ${LLVM_VERSION} )
65
66 # Note that we check this later (for both build types) but we can provide a
67 # more useful error message when built in-tree. We assume that LLVM tools are
68 # always available so don't warn here.
69 if( NOT clang IN_LIST LLVM_ENABLE_PROJECTS )
70 message(FATAL_ERROR "Clang is not enabled, but is required to build libclc in-tree")
71 endif()
72
Fraser Cormack0aeeff32024-04-16 15:46:3773 if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
74 foreach( tool IN ITEMS clang llvm-as llvm-link opt )
75 add_executable(libclc::${tool} ALIAS ${tool})
76 endforeach()
77 endif()
78endif()
79
80if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
81 message( WARNING "Using custom LLVM tools to build libclc: "
82 "${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}, "
83 " ensure the tools are up to date." )
84 # Note - use a differently named variable than LLVM_TOOL_${tool} as above, as
85 # the variable name is used to cache the result of find_program. If we used
86 # the same name, a user wouldn't be able to switch a build between default
87 # and custom tools.
Fraser Cormack9d1112862024-04-16 15:48:5988 foreach( tool IN ITEMS clang llvm-as llvm-link opt )
Fraser Cormack0aeeff32024-04-16 15:46:3789 find_program( LLVM_CUSTOM_TOOL_${tool} ${tool}
90 PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
91 add_executable( libclc::${tool} IMPORTED GLOBAL )
92 set_target_properties( libclc::${tool} PROPERTIES
93 IMPORTED_LOCATION ${LLVM_CUSTOM_TOOL_${tool}} )
Fraser Cormack72f98812024-04-11 16:09:0794 endforeach()
Jan Vesely2ce1d092018-11-27 16:07:1995endif()
96
Fraser Cormackdc74c692024-04-16 15:53:1897foreach( tool IN ITEMS clang opt llvm-as llvm-link )
98 if( NOT TARGET libclc::${tool} )
99 message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
100 endif()
101endforeach()
Jan Vesely2ce1d092018-11-27 16:07:19102
Fraser Cormack72f98812024-04-11 16:09:07103# llvm-spirv is an optional dependency, used to build spirv-* targets.
104find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
105
Fraser Cormack06f54e72024-04-16 14:30:20106if( LLVM_SPIRV )
107 add_executable( libclc::llvm-spirv IMPORTED GLOBAL )
108 set_target_properties( libclc::llvm-spirv PROPERTIES IMPORTED_LOCATION ${LLVM_SPIRV} )
109endif()
110
Fraser Cormack61efea72024-04-04 09:12:33111# List of all targets. Note that some are added dynamically below.
112set( LIBCLC_TARGETS_ALL
113 amdgcn--
114 amdgcn--amdhsa
115 clspv--
116 clspv64--
117 r600--
118 nvptx--
119 nvptx64--
120 nvptx--nvidiacl
121 nvptx64--nvidiacl
122)
123
124# mesa3d environment is only available since LLVM 4.0
Fraser Cormack338ecfb2024-04-04 16:54:54125if( LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL 4.0.0 )
Fraser Cormack61efea72024-04-04 09:12:33126 list( APPEND LIBCLC_TARGETS_ALL amdgcn-mesa-mesa3d )
127endif()
128
129# spirv-mesa3d and spirv64-mesa3d targets can only be built with the (optional)
130# llvm-spirv external tool.
Fraser Cormack06f54e72024-04-16 14:30:20131if( TARGET libclc::llvm-spirv )
Fraser Cormack61efea72024-04-04 09:12:33132 list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
133endif()
134
135if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
136 set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
137endif()
138
Dave Airliec37145c2020-08-17 20:45:04139list( SORT LIBCLC_TARGETS_TO_BUILD )
140
Fraser Cormack61efea72024-04-04 09:12:33141# Verify that the user hasn't requested mesa3d targets without an available
142# llvm-spirv tool.
Dave Airliec37145c2020-08-17 20:45:04143if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
Fraser Cormack06f54e72024-04-16 14:30:20144 if( NOT TARGET libclc::llvm-spirv )
Fraser Cormack92539502024-03-18 14:37:04145 message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
146 endif()
Dave Airliec37145c2020-08-17 20:45:04147endif()
148
Jan Vesely2ce1d092018-11-27 16:07:19149# Construct LLVM version define
Tom Stellardb2647872022-11-23 06:56:55150set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_VERSION_MAJOR}0${LLVM_VERSION_MINOR}" )
Jan Vesely2ce1d092018-11-27 16:07:19151
Jan Vesely2ce1d092018-11-27 16:07:19152# This needs to be set before any target that needs it
Tom Stellard8040e3a2023-01-27 19:15:54153# We need to use LLVM_INCLUDE_DIRS here, because if we are linking to an
154# llvm build directory, this includes $src/llvm/include which is where all the
155# headers are not $build/include/ which is what LLVM_INCLUDE_DIR is set to.
156include_directories( ${LLVM_INCLUDE_DIRS} )
Jan Vesely2ce1d092018-11-27 16:07:19157
158# Setup prepare_builtins tools
Tom Stellardb2647872022-11-23 06:56:55159set(LLVM_LINK_COMPONENTS
160 BitReader
161 BitWriter
162 Core
Fraser Cormack61efea72024-04-04 09:12:33163 IRReader
Thomas Debesse382b56d2023-05-22 10:20:59164 Support
Tom Stellardb2647872022-11-23 06:56:55165)
Fraser Cormack72f98812024-04-11 16:09:07166if( LIBCLC_STANDALONE_BUILD )
Fraser Cormack8461d902024-04-08 10:01:58167 add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
168else()
169 add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp )
170endif()
Jan Vesely2ce1d092018-11-27 16:07:19171target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
Tom Stellardb2647872022-11-23 06:56:55172# These were not properly reported in early LLVM and we don't need them
173target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
Jan Vesely2ce1d092018-11-27 16:07:19174
175# Setup arch devices
176set( r600--_devices cedar cypress barts cayman )
177set( amdgcn--_devices tahiti )
178set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
179set( amdgcn--amdhsa_devices none )
Alan Baker21427b82020-12-07 20:54:14180set( clspv--_devices none )
Kévin Petitec0a8802022-01-05 16:32:08181set( clspv64--_devices none )
Jan Vesely2ce1d092018-11-27 16:07:19182set( nvptx--_devices none )
183set( nvptx64--_devices none )
184set( nvptx--nvidiacl_devices none )
185set( nvptx64--nvidiacl_devices none )
Dave Airliec37145c2020-08-17 20:45:04186set( spirv-mesa3d-_devices none )
187set( spirv64-mesa3d-_devices none )
Jan Vesely2ce1d092018-11-27 16:07:19188
189# Setup aliases
190set( cedar_aliases palm sumo sumo2 redwood juniper )
191set( cypress_aliases hemlock )
192set( barts_aliases turks caicos )
193set( cayman_aliases aruba )
194set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
Fraser Cormack92539502024-03-18 14:37:04195 mullins tonga tongapro iceland carrizo fiji stoney polaris10 polaris11
196 gfx602 gfx705 gfx805
197 gfx900 gfx902 gfx904 gfx906 gfx908 gfx909 gfx90a gfx90c gfx940 gfx941 gfx942
198 gfx1010 gfx1011 gfx1012 gfx1013
199 gfx1030 gfx1031 gfx1032 gfx1033 gfx1034 gfx1035 gfx1036
200 gfx1100 gfx1101 gfx1102 gfx1103
Shilei Tian1ca00552024-06-06 16:16:11201 gfx1150 gfx1151 gfx1152
Fraser Cormack92539502024-03-18 14:37:04202 gfx1200 gfx1201
Zoltán Böszörményi262735b2024-01-22 08:32:13203)
Jan Vesely2ce1d092018-11-27 16:07:19204
205# pkg-config file
206configure_file( libclc.pc.in libclc.pc @ONLY )
John Ericsonddcc02d2021-12-11 01:54:42207install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
208install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" )
Jan Vesely2ce1d092018-11-27 16:07:19209
210if( ENABLE_RUNTIME_SUBNORMAL )
Fraser Cormack9d1112862024-04-16 15:48:59211 foreach( file IN ITEMS subnormal_use_default subnormal_disable )
Fraser Cormack72f98812024-04-11 16:09:07212 link_bc(
213 TARGET ${file}
214 INPUTS ${PROJECT_SOURCE_DIR}/generic/lib/${file}.ll
215 )
216 install( FILES $<TARGET_PROPERTY:${file},TARGET_FILE> ARCHIVE
217 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
218 endforeach()
Jan Vesely2ce1d092018-11-27 16:07:19219endif()
220
Aaron Puchert1c1a8102020-10-01 20:31:30221find_package( Python3 REQUIRED COMPONENTS Interpreter )
Fraser Cormack61efea72024-04-04 09:12:33222file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
Jan Vesely2ce1d092018-11-27 16:07:19223add_custom_command(
Fraser Cormack92539502024-03-18 14:37:04224 OUTPUT convert.cl
225 COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
226 DEPENDS ${script_loc} )
Jan Vesely2ce1d092018-11-27 16:07:19227add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
228
Romaric Jodinb6193a22024-03-14 19:56:34229add_custom_command(
Fraser Cormack92539502024-03-18 14:37:04230 OUTPUT clspv-convert.cl
231 COMMAND ${Python3_EXECUTABLE} ${script_loc} --clspv > clspv-convert.cl
232 DEPENDS ${script_loc} )
Romaric Jodinb6193a22024-03-14 19:56:34233add_custom_target( "clspv-generate_convert.cl" DEPENDS clspv-convert.cl )
234
Jan Vesely2ce1d092018-11-27 16:07:19235enable_testing()
236
237foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
Fraser Cormacke251f562024-03-28 15:11:30238 message( STATUS "libclc target '${t}' is enabled" )
Fraser Cormack92539502024-03-18 14:37:04239 string( REPLACE "-" ";" TRIPLE ${t} )
240 list( GET TRIPLE 0 ARCH )
241 list( GET TRIPLE 1 VENDOR )
242 list( GET TRIPLE 2 OS )
Jan Vesely2ce1d092018-11-27 16:07:19243
Fraser Cormack92539502024-03-18 14:37:04244 set( dirs )
Dave Airliec37145c2020-08-17 20:45:04245
Fraser Cormack92539502024-03-18 14:37:04246 if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND
247 NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64)
248 LIST( APPEND dirs generic )
249 endif()
Dave Airliec37145c2020-08-17 20:45:04250
Fraser Cormack92539502024-03-18 14:37:04251 if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
252 list( APPEND dirs amdgpu )
253 endif()
Jan Vesely2ce1d092018-11-27 16:07:19254
Fraser Cormack338ecfb2024-04-04 16:54:54255 # nvptx is special
Fraser Cormack92539502024-03-18 14:37:04256 if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
257 set( DARCH ptx )
258 else()
259 set( DARCH ${ARCH} )
260 endif()
Jan Vesely2ce1d092018-11-27 16:07:19261
Fraser Cormack92539502024-03-18 14:37:04262 # Enumerate SOURCES* files
263 set( source_list )
264 foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
265 foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
266 file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
Fraser Cormack61efea72024-04-04 09:12:33267 file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${file_loc} loc )
Fraser Cormack92539502024-03-18 14:37:04268 # Prepend the location to give higher priority to
269 # specialized implementation
270 if( EXISTS ${loc} )
271 set( source_list ${file_loc} ${source_list} )
272 endif()
273 endforeach()
274 endforeach()
Jan Vesely2ce1d092018-11-27 16:07:19275
Fraser Cormack338ecfb2024-04-04 16:54:54276 # Add the generated convert.cl here to prevent adding the one listed in
277 # SOURCES
Fraser Cormack72f98812024-04-11 16:09:07278 set( objects ) # A "set" of already-added input files
279 set( rel_files ) # Source directory input files, relative to the root dir
280 set( gen_files ) # Generated binary input files, relative to the binary dir
Fraser Cormack92539502024-03-18 14:37:04281 if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" )
282 if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND
283 NOT ${ARCH} STREQUAL "clspv64" )
Fraser Cormack72f98812024-04-11 16:09:07284 list( APPEND gen_files convert.cl )
285 list( APPEND objects convert.cl )
Fraser Cormack92539502024-03-18 14:37:04286 list( APPEND rel_files generic/lib/subnormal_use_default.ll )
287 elseif(${ARCH} STREQUAL "clspv" OR ${ARCH} STREQUAL "clspv64")
Fraser Cormack72f98812024-04-11 16:09:07288 list( APPEND gen_files clspv-convert.cl )
289 list( APPEND objects clspv-convert.cl )
Fraser Cormack92539502024-03-18 14:37:04290 endif()
Fraser Cormack92539502024-03-18 14:37:04291 endif()
Jan Vesely2ce1d092018-11-27 16:07:19292
Fraser Cormack92539502024-03-18 14:37:04293 foreach( l ${source_list} )
294 file( READ ${l} file_list )
295 string( REPLACE "\n" ";" file_list ${file_list} )
296 get_filename_component( dir ${l} DIRECTORY )
297 foreach( f ${file_list} )
Fraser Cormack72f98812024-04-11 16:09:07298 # Only add each file once, so that targets can 'specialize' builtins
299 if( NOT ${f} IN_LIST objects )
Fraser Cormack92539502024-03-18 14:37:04300 list( APPEND objects ${f} )
301 list( APPEND rel_files ${dir}/${f} )
Fraser Cormack92539502024-03-18 14:37:04302 endif()
303 endforeach()
304 endforeach()
Jan Vesely2ce1d092018-11-27 16:07:19305
Fraser Cormack92539502024-03-18 14:37:04306 foreach( d ${${t}_devices} )
Fraser Cormack72f98812024-04-11 16:09:07307 get_libclc_device_info(
308 TRIPLE ${t}
309 DEVICE ${d}
310 CPU cpu
311 ARCH_SUFFIX arch_suffix
312 CLANG_TRIPLE clang_triple
313 )
314
315 set( mcpu )
316 if( NOT "${cpu}" STREQUAL "" )
317 set( mcpu "-mcpu=${cpu}" )
Fraser Cormack92539502024-03-18 14:37:04318 endif()
Fraser Cormack72f98812024-04-11 16:09:07319
Fraser Cormacke251f562024-03-28 15:11:30320 message( STATUS " device: ${d} ( ${${d}_aliases} )" )
Jan Vesely2ce1d092018-11-27 16:07:19321
Fraser Cormack72f98812024-04-11 16:09:07322 if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
Fraser Cormack92539502024-03-18 14:37:04323 set( build_flags -O0 -finline-hint-functions )
324 set( opt_flags )
325 set( spvflags --spirv-max-version=1.1 )
Fraser Cormack72f98812024-04-11 16:09:07326 elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
Fraser Cormack92539502024-03-18 14:37:04327 set( build_flags "-Wno-unknown-assumption")
328 set( opt_flags -O3 )
329 else()
330 set( build_flags )
331 set( opt_flags -O3 )
332 endif()
Dave Airliec37145c2020-08-17 20:45:04333
Fraser Cormack72f98812024-04-11 16:09:07334 set( LIBCLC_ARCH_OBJFILE_DIR "${LIBCLC_OBJFILE_DIR}/${arch_suffix}" )
335 file( MAKE_DIRECTORY ${LIBCLC_ARCH_OBJFILE_DIR} )
336
337 string( TOUPPER "CLC_${ARCH}" CLC_TARGET_DEFINE )
338
339 list( APPEND build_flags
340 -D__CLC_INTERNAL
341 -D${CLC_TARGET_DEFINE}
342 -I${PROJECT_SOURCE_DIR}/generic/include
343 # FIXME: Fix libclc to not require disabling this noisy warning
344 -Wno-bitwise-conditional-parentheses
345 )
346
347 set( bytecode_files "" )
348 foreach( file IN LISTS gen_files rel_files )
349 # We need to take each file and produce an absolute input file, as well
350 # as a unique architecture-specific output file. We deal with a mix of
351 # different input files, which makes this trickier.
352 if( ${file} IN_LIST gen_files )
353 # Generated files are given just as file names, which we must make
354 # absolute to the binary directory.
355 set( input_file ${CMAKE_CURRENT_BINARY_DIR}/${file} )
Fraser Cormacka0f81912024-04-16 12:12:52356 set( output_file "${LIBCLC_ARCH_OBJFILE_DIR}/${file}.bc" )
Fraser Cormack72f98812024-04-11 16:09:07357 else()
358 # Other files are originally relative to each SOURCE file, which are
359 # then make relative to the libclc root directory. We must normalize
360 # the path (e.g., ironing out any ".."), then make it relative to the
361 # root directory again, and use that relative path component for the
362 # binary path.
363 get_filename_component( abs_path ${file} ABSOLUTE BASE_DIR ${PROJECT_SOURCE_DIR} )
364 file( RELATIVE_PATH root_rel_path ${PROJECT_SOURCE_DIR} ${abs_path} )
365 set( input_file ${PROJECT_SOURCE_DIR}/${file} )
Fraser Cormacka0f81912024-04-16 12:12:52366 set( output_file "${LIBCLC_ARCH_OBJFILE_DIR}/${root_rel_path}.bc" )
Fraser Cormack72f98812024-04-11 16:09:07367 endif()
368
369 get_filename_component( file_dir ${file} DIRECTORY )
370
371 compile_to_bc(
372 TRIPLE ${clang_triple}
373 INPUT ${input_file}
374 OUTPUT ${output_file}
375 EXTRA_OPTS "${mcpu}" -fno-builtin -nostdlib
376 "${build_flags}" -I${PROJECT_SOURCE_DIR}/${file_dir}
377 )
378 list( APPEND bytecode_files ${output_file} )
379 endforeach()
380
Fraser Cormack61efea72024-04-04 09:12:33381 set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
Jan Vesely2ce1d092018-11-27 16:07:19382
Fraser Cormack72f98812024-04-11 16:09:07383 link_bc(
384 TARGET ${builtins_link_lib_tgt}
385 INPUTS ${bytecode_files}
386 )
Jan Vesely2ce1d092018-11-27 16:07:19387
Fraser Cormack72f98812024-04-11 16:09:07388 set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> )
Jan Vesely2ce1d092018-11-27 16:07:19389
Fraser Cormack72f98812024-04-11 16:09:07390 if( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
Fraser Cormack92539502024-03-18 14:37:04391 set( spv_suffix ${arch_suffix}.spv )
Fraser Cormack72f98812024-04-11 16:09:07392 add_custom_command( OUTPUT ${spv_suffix}
Fraser Cormack06f54e72024-04-16 14:30:20393 COMMAND libclc::llvm-spirv ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
Fraser Cormack3d118f92024-04-16 12:59:59394 DEPENDS ${builtins_link_lib}
Fraser Cormack72f98812024-04-11 16:09:07395 )
Fraser Cormack92539502024-03-18 14:37:04396 add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
397 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
398 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
399 else()
Fraser Cormack72f98812024-04-11 16:09:07400 set( builtins_opt_lib_tgt builtins.opt.${arch_suffix} )
401
402 # Add opt target
403 add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
404 COMMAND libclc::opt ${opt_flags} -o ${builtins_opt_lib_tgt}.bc
405 ${builtins_link_lib}
Fraser Cormack3d118f92024-04-16 12:59:59406 DEPENDS libclc::opt ${builtins_link_lib}
Fraser Cormack72f98812024-04-11 16:09:07407 )
408 add_custom_target( ${builtins_opt_lib_tgt}
409 ALL DEPENDS ${builtins_opt_lib_tgt}.bc
410 )
411 set_target_properties( ${builtins_opt_lib_tgt}
412 PROPERTIES TARGET_FILE ${builtins_opt_lib_tgt}.bc
413 )
414
Fraser Cormack3d118f92024-04-16 12:59:59415 set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> )
416
Fraser Cormack92539502024-03-18 14:37:04417 # Add prepare target
Fraser Cormack72f98812024-04-11 16:09:07418 set( obj_suffix ${arch_suffix}.bc )
419 add_custom_command( OUTPUT ${obj_suffix}
Fraser Cormack3d118f92024-04-16 12:59:59420 COMMAND prepare_builtins -o ${obj_suffix} ${builtins_opt_lib}
421 DEPENDS ${builtins_opt_lib} prepare_builtins )
Fraser Cormack72f98812024-04-11 16:09:07422 add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )
Dave Airliec37145c2020-08-17 20:45:04423
Fraser Cormack92539502024-03-18 14:37:04424 # nvptx-- targets don't include workitem builtins
Fraser Cormack72f98812024-04-11 16:09:07425 if( NOT clang_triple MATCHES ".*ptx.*--$" )
Fraser Cormack92539502024-03-18 14:37:04426 add_test( NAME external-calls-${obj_suffix}
427 COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
Fraser Cormack61efea72024-04-04 09:12:33428 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )
Fraser Cormack92539502024-03-18 14:37:04429 endif()
Dave Airliec37145c2020-08-17 20:45:04430
Fraser Cormack92539502024-03-18 14:37:04431 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
432 foreach( a ${${d}_aliases} )
Fraser Cormack72f98812024-04-11 16:09:07433 set( alias_suffix "${a}-${clang_triple}.bc" )
Fraser Cormack92539502024-03-18 14:37:04434 add_custom_target( ${alias_suffix} ALL
435 COMMAND ${CMAKE_COMMAND} -E create_symlink ${obj_suffix} ${alias_suffix}
Fraser Cormack72f98812024-04-11 16:09:07436 DEPENDS prepare-${obj_suffix} )
Fraser Cormack92539502024-03-18 14:37:04437 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
438 endforeach( a )
439 endif()
440 endforeach( d )
Jan Vesely2ce1d092018-11-27 16:07:19441endforeach( t )