Skip to content

Commit 61efea7

Browse files
authored
[libclc] Fix a couple of issues preventing in-tree builds (#87505)
libclc is mentioned in the list of LLVM_ENABLE_PROJECTS but it isn't actually possible to build it in-tree for various reasons. Users currently have to build it via LLVM_ENABLE_EXTERNAL_PROJECTS, which isn't very well documented. We can't properly build in-tree because the current system needs to "see" clang and other tools at CMake configuration time. The general idea is that we could fix this in the future by moving the compilation and linking of bitcode libraries to custom commands, which would remove the dependency on CMake configuration and would allow us to build libclc after clang and other tools are built in-tree. Since that's a bigger change, it is being left for later. Note that with this commit it's *still* not possible to properly build in-tree - this commit just fixes a few little things that are in the way. We are now able to build in-tree in the sense that it can be built as a regular LLVM sub-project, but the tools it uses to compile the libraries are still picked up from a pre-existing installation of LLVM, and not from tools built during the same build as libclc. The things fixed by this commit include: * Its use of CMAKE_SOURCE_DIR (i.e., assuming it was the top-level project) * These have been converted to PROJECT_SOURCE_DIR - should have no consequences for out-of-tree builds. * Its prepare_builtins tool insisting on linking against the dynamic LLVM.so. * This has been turned from an "llvm executable" into an "llvm utility" which links against the static libraries. * It was also missing a link component for the IRReader library. * Assuming an output path for its builtin libraries (dependent on the working directory) * This has been changed to query CMake for the library target's output file. * The spirv-mesa3d and spirv64-mesa3d targets were enabled by default (or when asking to build 'all' libclc targets), when they require llvm-spirv as an external dependency. * They are now only built when the user explicitly asks for them, or when llvm-spirv is available and the user asks for 'all'.
1 parent 1273591 commit 61efea7

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

libclc/CMakeLists.txt

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
2020
spirv64/lib/SOURCES
2121
)
2222

23-
# List of all targets
24-
set( LIBCLC_TARGETS_ALL
25-
amdgcn--
26-
amdgcn--amdhsa
27-
clspv--
28-
clspv64--
29-
r600--
30-
nvptx--
31-
nvptx64--
32-
nvptx--nvidiacl
33-
nvptx64--nvidiacl
34-
spirv-mesa3d-
35-
spirv64-mesa3d-
36-
)
37-
3823
set( LIBCLC_MIN_LLVM "3.9.0" )
3924

4025
set( LIBCLC_TARGETS_TO_BUILD "all"
@@ -51,15 +36,6 @@ if( ${LLVM_PACKAGE_VERSION} VERSION_LESS ${LIBCLC_MIN_LLVM} )
5136
message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
5237
endif()
5338

54-
# mesa3d environment is only available since LLVM 4.0
55-
if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.0" )
56-
set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
57-
endif()
58-
59-
if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
60-
set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
61-
endif()
62-
6339
find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
6440
find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
6541
find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
@@ -76,15 +52,45 @@ if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
7652
message( FATAL_ERROR "libclc toolchain incomplete!" )
7753
endif()
7854

55+
# List of all targets. Note that some are added dynamically below.
56+
set( LIBCLC_TARGETS_ALL
57+
amdgcn--
58+
amdgcn--amdhsa
59+
clspv--
60+
clspv64--
61+
r600--
62+
nvptx--
63+
nvptx64--
64+
nvptx--nvidiacl
65+
nvptx64--nvidiacl
66+
)
67+
68+
# mesa3d environment is only available since LLVM 4.0
69+
if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.0" )
70+
list( APPEND LIBCLC_TARGETS_ALL amdgcn-mesa-mesa3d )
71+
endif()
72+
73+
# spirv-mesa3d and spirv64-mesa3d targets can only be built with the (optional)
74+
# llvm-spirv external tool.
75+
if( LLVM_SPIRV )
76+
list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
77+
endif()
78+
79+
if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
80+
set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
81+
endif()
82+
7983
list( SORT LIBCLC_TARGETS_TO_BUILD )
8084

85+
# Verify that the user hasn't requested mesa3d targets without an available
86+
# llvm-spirv tool.
8187
if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
8288
if( NOT LLVM_SPIRV )
8389
message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
8490
endif()
8591
endif()
8692

87-
set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
93+
set( CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )
8894
set( CMAKE_CLC_COMPILER ${LLVM_CLANG} )
8995
set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
9096
set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
@@ -113,9 +119,10 @@ set(LLVM_LINK_COMPONENTS
113119
BitReader
114120
BitWriter
115121
Core
122+
IRReader
116123
Support
117124
)
118-
add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
125+
add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp )
119126
target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
120127
# These were not properly reported in early LLVM and we don't need them
121128
target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
@@ -165,7 +172,7 @@ if( ENABLE_RUNTIME_SUBNORMAL )
165172
endif()
166173

167174
find_package( Python3 REQUIRED COMPONENTS Interpreter )
168-
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
175+
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
169176
add_custom_command(
170177
OUTPUT convert.cl
171178
COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
@@ -210,7 +217,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
210217
foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
211218
foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
212219
file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
213-
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
220+
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${file_loc} loc )
214221
# Prepend the location to give higher priority to
215222
# specialized implementation
216223
if( EXISTS ${loc} )
@@ -246,7 +253,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
246253
list( APPEND objects ${f} )
247254
list( APPEND rel_files ${dir}/${f} )
248255
# FIXME: This should really go away
249-
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
256+
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${dir}/${f} src_loc )
250257
get_filename_component( fdir ${src_loc} DIRECTORY )
251258

252259
set_source_files_properties( ${dir}/${f}
@@ -288,53 +295,56 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
288295
set( opt_flags -O3 )
289296
endif()
290297

291-
add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
298+
set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
299+
add_library( ${builtins_link_lib_tgt} STATIC ${rel_files} )
292300
# Make sure we depend on the pseudo target to prevent
293301
# multiple invocations
294-
add_dependencies( builtins.link.${arch_suffix} generate_convert.cl )
295-
add_dependencies( builtins.link.${arch_suffix} clspv-generate_convert.cl )
302+
add_dependencies( ${builtins_link_lib_tgt} generate_convert.cl )
303+
add_dependencies( ${builtins_link_lib_tgt} clspv-generate_convert.cl )
296304
# CMake will turn this include into absolute path
297-
target_include_directories( builtins.link.${arch_suffix} PRIVATE
305+
target_include_directories( ${builtins_link_lib_tgt} PRIVATE
298306
"generic/include" )
299-
target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
307+
target_compile_definitions( ${builtins_link_lib_tgt} PRIVATE
300308
"__CLC_INTERNAL" )
301309
string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
302-
target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
310+
target_compile_definitions( ${builtins_link_lib_tgt} PRIVATE
303311
${CLC_TARGET_DEFINE} )
304-
target_compile_options( builtins.link.${arch_suffix} PRIVATE -target
312+
target_compile_options( ${builtins_link_lib_tgt} PRIVATE -target
305313
${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
306-
set_target_properties( builtins.link.${arch_suffix} PROPERTIES
314+
set_target_properties( ${builtins_link_lib_tgt} PROPERTIES
307315
LINKER_LANGUAGE CLC )
308316

309317
set( obj_suffix ${arch_suffix}.bc )
318+
set( builtins_opt_lib_tgt builtins.opt.${obj_suffix} )
310319

311320
# Add opt target
312-
add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
313-
COMMAND ${LLVM_OPT} ${opt_flags} -o "builtins.opt.${obj_suffix}" "builtins.link.${obj_suffix}"
314-
DEPENDS "builtins.link.${arch_suffix}" )
321+
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}
322+
COMMAND ${LLVM_OPT} ${opt_flags} -o ${builtins_opt_lib_tgt}
323+
$<TARGET_FILE:${builtins_link_lib_tgt}>
324+
DEPENDS ${builtins_link_lib_tgt} )
315325
add_custom_target( "opt.${obj_suffix}" ALL
316-
DEPENDS "builtins.opt.${obj_suffix}" )
326+
DEPENDS ${builtins_opt_lib_tgt} )
317327

318328
if( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
319329
set( spv_suffix ${arch_suffix}.spv )
320330
add_custom_command( OUTPUT "${spv_suffix}"
321-
COMMAND ${LLVM_SPIRV} ${spvflags} -o "${spv_suffix}" "builtins.link.${obj_suffix}"
322-
DEPENDS "builtins.link.${arch_suffix}" )
331+
COMMAND ${LLVM_SPIRV} ${spvflags} -o "${spv_suffix}" ${builtins_opt_lib_tgt}
332+
DEPENDS ${builtins_link_lib_tgt} )
323333
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
324334
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
325335
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
326336
else()
327337
# Add prepare target
328338
add_custom_command( OUTPUT "${obj_suffix}"
329-
COMMAND prepare_builtins -o "${obj_suffix}" "builtins.opt.${obj_suffix}"
330-
DEPENDS "opt.${obj_suffix}" "builtins.opt.${obj_suffix}" prepare_builtins )
339+
COMMAND prepare_builtins -o "${obj_suffix}" ${builtins_opt_lib_tgt}
340+
DEPENDS "opt.${obj_suffix}" ${builtins_opt_lib_tgt} prepare_builtins )
331341
add_custom_target( "prepare-${obj_suffix}" ALL DEPENDS "${obj_suffix}" )
332342

333343
# nvptx-- targets don't include workitem builtins
334344
if( NOT ${t} MATCHES ".*ptx.*--$" )
335345
add_test( NAME external-calls-${obj_suffix}
336346
COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
337-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
347+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )
338348
endif()
339349

340350
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )

0 commit comments

Comments
 (0)