Skip to content

Commit e152215

Browse files
committed
[SYCL][libclc] Fix cross builds
When performing cross builds, we need native versions of various tools, we cannot assume the cross builds that are part of the current build are executable. LLVM provides the setup_host_tool function to handle this, either picking up versions of tools from LLVM_NATIVE_TOOL_DIR, or implicitly building native versions as needed. Use this in more places. This applies the changes from LLVM #97392 and #97811 and adapts them to DPC++, and makes the same changes in other places that are only needed for DPC++.
1 parent 3ac3e1b commit e152215

File tree

12 files changed

+74
-34
lines changed

12 files changed

+74
-34
lines changed

clang/tools/clang-offload-bundler/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ add_clang_tool(clang-offload-bundler
1818
intrinsics_gen
1919
)
2020

21+
setup_host_tool(clang-offload-bundler CLANG_OFFLOAD_BUNDLER
22+
clang-offload-bundler_exe clang-offload-bundler_target)
23+
2124
set(CLANG_OFFLOAD_BUNDLER_LIB_DEPS
2225
clangBasic
2326
clangDriver

clang/tools/clang-offload-packager/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ add_clang_tool(clang-offload-packager
1212
${tablegen_deps}
1313
)
1414

15+
setup_host_tool(clang-offload-packager CLANG_OFFLOAD_PACKAGER_EXE
16+
clang-offload-packager_exe clang-offload-packager_target)
17+
1518
clang_target_link_libraries(clang-offload-packager
1619
PRIVATE
1720
clangBasic

libclc/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI
6969

7070
# Import required tools
7171
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
72-
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
72+
foreach( tool IN ITEMS clang llvm-as llvm-link llvm-spirv opt )
7373
find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
7474
set( ${tool}_exe ${LLVM_TOOL_${tool}} )
7575
set( ${tool}_target )
@@ -92,6 +92,7 @@ else()
9292
get_host_tool_path( clang CLANG clang_exe clang_target )
9393
get_host_tool_path( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
9494
get_host_tool_path( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
95+
get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
9596
get_host_tool_path( opt OPT opt_exe opt_target )
9697
endif()
9798
endif()
@@ -113,13 +114,15 @@ if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
113114

114115
# If we've requested a custom binary directory, there are some otherwise
115116
# optional tools which we want to ensure are present.
116-
if( NOT TARGET libclc::llvm-spirv OR NOT TARGET libclc::libclc-remangler )
117-
message( FATAL_ERROR "libclc toolchain incomplete!" )
118-
endif()
117+
foreach( tool IN ITEMS llvm-spirv libclc-remangler )
118+
if( NOT EXISTS "${${tool}_exe}" AND "${${tool}_target}" STREQUAL "" )
119+
message( FATAL_ERROR "libclc toolchain incomplete!" )
120+
endif()
121+
endforeach()
119122
endif()
120123

121124
foreach( tool IN ITEMS clang opt llvm-as llvm-link )
122-
if( NOT EXISTS "${${tool}_exe}" AND "${tool}_target" STREQUAL "" )
125+
if( NOT EXISTS "${${tool}_exe}" AND "${${tool}_target}" STREQUAL "" )
123126
message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
124127
endif()
125128
endforeach()

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,13 @@ function(add_libclc_builtin_set)
456456
"${LIBCLC_LIBRARY_OUTPUT_INTDIR}/remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" )
457457
add_custom_command( OUTPUT "${builtins_remangle_path}"
458458
COMMAND ${CMAKE_COMMAND} -E make_directory ${LIBCLC_LIBRARY_OUTPUT_INTDIR}
459-
COMMAND libclc::libclc-remangler
459+
COMMAND ${libclc-remangler_exe}
460460
-o "${builtins_remangle_path}"
461461
--long-width=${long_width}
462462
--char-signedness=${signedness}
463463
--input-ir=${builtins_lib}
464464
${dummy_in}
465-
DEPENDS ${builtins_lib} libclc::libclc-remangler ${dummy_in})
465+
DEPENDS ${builtins_lib} ${libclc-remangler_target} ${dummy_in})
466466
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" ALL
467467
DEPENDS "${builtins_remangle_path}" "${dummy_in}")
468468
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}"
@@ -489,12 +489,12 @@ function(add_libclc_builtin_set)
489489
math(EXPR libclc-remangler-test-no "${libclc-remangler-test-no}+1")
490490
set(current-test "libclc-remangler-test-${obj_suffix}-${libclc-remangler-test-no}")
491491
add_custom_target(${current-test}
492-
COMMAND libclc::libclc-remangler
492+
COMMAND ${libclc-remangler_exe}
493493
--long-width=l32
494494
--char-signedness=signed
495495
--input-ir=${target-ir}
496496
${dummy_in} -t -o -
497-
DEPENDS ${builtins_lib} "${dummy_in}" libclc::libclc-remangler)
497+
DEPENDS ${builtins_lib} "${dummy_in}" ${libclc-remangler_target})
498498
list(APPEND libclc-remangler-tests ${current-test})
499499
endforeach()
500500
endif()

libclc/utils/libclc-remangler/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ set(LLVM_LINK_COMPONENTS
1111

1212
add_clang_tool(libclc-remangler LibclcRemangler.cpp)
1313

14+
setup_host_tool( libclc-remangler LIBCLC_REMANGLER
15+
libclc-remangler_exe libclc-remangler_target )
16+
1417
target_include_directories(libclc-remangler PRIVATE
1518
${CMAKE_SOURCE_DIR}/../clang/include
1619
${CMAKE_BINARY_DIR}/tools/clang/include)
@@ -24,9 +27,3 @@ clang_target_link_libraries(libclc-remangler
2427
clangSerialization
2528
LLVMOption
2629
)
27-
28-
# If we've not already imported a libclc-remangler tool from an external build,
29-
# set it up now.
30-
if(NOT TARGET libclc::libclc-remangler)
31-
add_executable(libclc::libclc-remangler ALIAS libclc-remangler)
32-
endif()

libdevice/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ else()
1919
will not build libdevice sanitizer")
2020
endif()
2121

22+
if(NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR})
23+
get_host_tool_path(clang CLANG clang_exe clang_target)
24+
get_host_tool_path(llvm-ar LLVM_AR llvm-ar_exe llvm-ar_target)
25+
get_host_tool_path(append-file APPEND_FILE append-file_exe append-file_target)
26+
get_host_tool_path(clang-offload-bundler CLANG_OFFLOAD_BUNDLER clang-offload-bundler_exe clang-offload-bundler_target)
27+
get_host_tool_path(clang-offload-packager CLANG_OFFLOAD_PACKAGER clang-offload-packager_exe clang-offload-packager_target)
28+
get_host_tool_path(file-table-tform FILE_TABLE_TFORM file-table-tform_exe file-table-tform_target)
29+
get_host_tool_path(llvm-foreach LLVM_FOREACH llvm-foreach_exe llvm-foreach_target)
30+
get_host_tool_path(llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target)
31+
get_host_tool_path(sycl-post-link SYCL_POST_LINK sycl-post-link_exe sycl-post-link_target)
32+
else()
33+
foreach(tool IN ITEMS clang llvm-ar append-file clang-offload-bundler clang-offload-packager file-table-tform llvm-foreach llvm-spirv sycl-post-link)
34+
find_program(LLVM_CUSTOM_TOOL_${tool} ${tool}
35+
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
36+
set(${tool}_exe ${LLVM_CUSTOM_TOOL_${tool}})
37+
set(${tool}_target)
38+
endforeach()
39+
endif()
40+
2241
# Build libdevice for SYCL.
2342
include(SYCLLibdevice)
2443

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ set(install_dest_obj lib${LLVM_LIBDIR_SUFFIX})
2222
set(install_dest_obj-new-offload lib${LLVM_LIBDIR_SUFFIX})
2323
set(install_dest_bc lib${LLVM_LIBDIR_SUFFIX})
2424

25-
set(clang $<TARGET_FILE:clang>)
26-
set(llvm-ar $<TARGET_FILE:llvm-ar>)
27-
set(llvm-link $<TARGET_FILE:llvm-link>)
28-
set(llvm-opt $<TARGET_FILE:opt>)
29-
3025
string(CONCAT sycl_targets_opt
3126
"-fsycl-targets="
3227
"spir64_x86_64-unknown-unknown,"
@@ -55,6 +50,7 @@ set(compile_opts
5550
# we declare all functions as 'static'.
5651
-Wno-undefined-internal
5752
-sycl-std=2020
53+
--target=${LLVM_HOST_TRIPLE}
5854
)
5955

6056
set(SYCL_LIBDEVICE_GCC_TOOLCHAIN "" CACHE PATH "Path to GCC installation")
@@ -146,7 +142,8 @@ function(compile_lib_ext filename)
146142

147143
add_custom_command(
148144
OUTPUT ${devicelib-file}
149-
COMMAND ${clang} ${ARG_OPTS}
145+
COMMAND ${clang_exe} -I ${PROJECT_BINARY_DIR}/include
146+
${ARG_OPTS}
150147
${CMAKE_CURRENT_SOURCE_DIR}/${ARG_SRC} -o ${devicelib-file}
151148
MAIN_DEPENDENCY ${ARG_SRC}
152149
DEPENDS ${ARG_DEPENDENCIES}
@@ -224,20 +221,26 @@ function(add_devicelibs filename)
224221
endforeach()
225222
endfunction()
226223

227-
# Set up the dependency lists for the libdevice libraries
228-
set(crt_obj_deps wrapper.h device.h spirv_vars.h sycl-compiler)
229-
set(complex_obj_deps device_complex.h device.h sycl-compiler)
230-
set(cmath_obj_deps device_math.h device.h sycl-compiler)
231-
set(imf_obj_deps device_imf.hpp imf_half.hpp imf_bf16.hpp imf_rounding_op.hpp imf_impl_utils.hpp device.h sycl-compiler)
232-
set(itt_obj_deps device_itt.h spirv_vars.h device.h sycl-compiler)
233-
set(bfloat16_obj_deps sycl-headers sycl-compiler)
224+
# For native builds, sycl-compiler will already include everything we need.
225+
# For cross builds, we also need native versions of the tools.
226+
set(sycl-compiler_deps
227+
sycl-compiler ${clang_target} ${append-file_target}
228+
${clang-offload-bundler_target} ${clang-offload-packager_target}
229+
${file-table-tform_target} ${llvm-foreach_target} ${llvm-spirv_target}
230+
${sycl-post-link_target})
231+
set(crt_obj_deps wrapper.h device.h spirv_vars.h ${sycl-compiler_deps})
232+
set(complex_obj_deps device_complex.h device.h ${sycl-compiler_deps})
233+
set(cmath_obj_deps device_math.h device.h ${sycl-compiler_deps})
234+
set(imf_obj_deps device_imf.hpp imf_half.hpp imf_bf16.hpp imf_rounding_op.hpp imf_impl_utils.hpp device.h ${sycl-compiler_deps})
235+
set(itt_obj_deps device_itt.h spirv_vars.h device.h ${sycl-compiler_deps})
236+
set(bfloat16_obj_deps sycl-headers ${sycl-compiler_deps})
234237
if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
235238
set(asan_obj_deps
236239
device.h atomic.hpp spirv_vars.h
237240
${UR_SANITIZER_INCLUDE_DIR}/asan/asan_libdevice.hpp
238241
include/asan_rtl.hpp
239242
include/spir_global_var.hpp
240-
sycl-compiler)
243+
${sycl-compiler_deps})
241244

242245
set(sanitizer_generic_compile_opts ${compile_opts}
243246
-fno-sycl-instrument-device-code
@@ -427,6 +430,7 @@ set(imf_fp64_fallback_src ${imf_fallback_src_dir}/imf_fp64_fallback.cpp)
427430
set(imf_bf16_fallback_src ${imf_fallback_src_dir}/imf_bf16_fallback.cpp)
428431

429432
set(imf_host_cxx_flags -c
433+
--target=${LLVM_HOST_TRIPLE}
430434
-D__LIBDEVICE_HOST_IMPL__
431435
)
432436

@@ -520,13 +524,13 @@ function(add_lib_imf name)
520524

521525
add_custom_command(
522526
OUTPUT ${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
523-
COMMAND ${clang} ${compile_opts} ${ARG_EXTRA_OPTS}
527+
COMMAND ${clang_exe} ${compile_opts} ${ARG_EXTRA_OPTS}
524528
-I ${CMAKE_CURRENT_SOURCE_DIR}/imf
525529
${imf_${ARG_DTYPE}_fallback_src}
526530
-o
527531
${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
528532
DEPENDS ${imf_fallback_${ARG_DTYPE}_deps}
529-
get_imf_fallback_${ARG_DTYPE} sycl-compiler
533+
get_imf_fallback_${ARG_DTYPE} ${sycl-compiler_deps}
530534
VERBATIM)
531535

532536
add_custom_target(${ARG_TGT_NAME}
@@ -620,7 +624,7 @@ foreach(dtype IN ITEMS bf16 fp32 fp64)
620624
endif()
621625
add_custom_command(
622626
OUTPUT ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
623-
COMMAND ${clang} ${${ftype}_host_compile_opts}
627+
COMMAND ${clang_exe} ${${ftype}_host_compile_opts}
624628
${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
625629
-o ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
626630
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
@@ -637,7 +641,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
637641
DEPENDS ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}})
638642
add_custom_command(
639643
OUTPUT ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
640-
COMMAND ${llvm-ar} rcs
644+
COMMAND ${llvm-ar_exe} rcs
641645
${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
642646
${${ftype}_binary_dir}/imf-fp32-host.${${ftype}-suffix}
643647
${${ftype}_binary_dir}/fallback-imf-fp32-host.${${ftype}-suffix}
@@ -648,7 +652,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
648652
DEPENDS imf_fp32_host_${ftype} imf_fallback_fp32_host_${ftype}
649653
DEPENDS imf_fp64_host_${ftype} imf_fallback_fp64_host_${ftype}
650654
DEPENDS imf_bf16_host_${ftype} imf_fallback_bf16_host_${ftype}
651-
DEPENDS sycl-compiler
655+
DEPENDS ${llvm-ar_target}
652656
VERBATIM)
653657
add_dependencies(libsycldevice-obj imf_host_${ftype})
654658

llvm/tools/append-file/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ add_llvm_tool(append-file
99
DEPENDS
1010
intrinsics_gen
1111
)
12+
13+
setup_host_tool(append-file APPEND_FILE append_file_exe append_file_target)

llvm/tools/file-table-tform/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ set(LLVM_LINK_COMPONENTS
55
add_llvm_tool(file-table-tform
66
file-table-tform.cpp
77
)
8+
9+
setup_host_tool(file-table-tform FILE_TABLE_TFORM file-table-tform_exe file-table-tform_target)

llvm/tools/llvm-ar/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ add_llvm_tool(llvm-ar
1919
GENERATE_DRIVER
2020
)
2121

22+
setup_host_tool(llvm-ar LLVM_AR llvm_ar_exe llvm_ar_target)
23+
2224
add_llvm_tool_symlink(llvm-ranlib llvm-ar)
2325
add_llvm_tool_symlink(llvm-lib llvm-ar)
2426
add_llvm_tool_symlink(llvm-dlltool llvm-ar)

0 commit comments

Comments
 (0)