Skip to content

Commit 2444c91

Browse files
authored
[SYCL][libclc] Fix cross builds (#16244)
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++. Tested with a suitable cross compilation toolchain file: ```console $ mkdir build $ cat >build/aarch64-linux.cmake <<EOF set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR aarch64) set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc" CACHE STRING "") set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++" CACHE STRING "") set(PKG_CONFIG_EXECUTABLE "aarch64-linux-gnu-pkg-config" CACHE STRING "") set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) EOF $ python3 buildbot/configure.py -o build/aarch64-linux \ --cmake-opt=--toolchain=$PWD/build/aarch64-linux.cmake \ --cmake-opt=-DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu ```
1 parent ef4d66a commit 2444c91

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
@@ -70,7 +70,7 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI
7070

7171
# Import required tools
7272
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
73-
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
73+
foreach( tool IN ITEMS clang llvm-as llvm-link llvm-spirv opt )
7474
find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
7575
set( ${tool}_exe ${LLVM_TOOL_${tool}} )
7676
set( ${tool}_target )
@@ -93,6 +93,7 @@ else()
9393
get_host_tool_path( clang CLANG clang_exe clang_target )
9494
get_host_tool_path( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
9595
get_host_tool_path( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
96+
get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
9697
get_host_tool_path( opt OPT opt_exe opt_target )
9798
endif()
9899
endif()
@@ -114,13 +115,15 @@ if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
114115

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

122125
foreach( tool IN ITEMS clang opt llvm-as llvm-link )
123-
if( NOT EXISTS "${${tool}_exe}" AND "${tool}_target" STREQUAL "" )
126+
if( NOT EXISTS "${${tool}_exe}" AND "${${tool}_target}" STREQUAL "" )
124127
message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
125128
endif()
126129
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
@@ -442,6 +445,7 @@ set(imf_fp64_fallback_src ${imf_fallback_src_dir}/imf_fp64_fallback.cpp)
442445
set(imf_bf16_fallback_src ${imf_fallback_src_dir}/imf_bf16_fallback.cpp)
443446

444447
set(imf_host_cxx_flags -c
448+
--target=${LLVM_HOST_TRIPLE}
445449
-D__LIBDEVICE_HOST_IMPL__
446450
)
447451

@@ -535,13 +539,13 @@ function(add_lib_imf name)
535539

536540
add_custom_command(
537541
OUTPUT ${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
538-
COMMAND ${clang} ${compile_opts} ${ARG_EXTRA_OPTS}
542+
COMMAND ${clang_exe} ${compile_opts} ${ARG_EXTRA_OPTS}
539543
-I ${CMAKE_CURRENT_SOURCE_DIR}/imf
540544
${imf_${ARG_DTYPE}_fallback_src}
541545
-o
542546
${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
543547
DEPENDS ${imf_fallback_${ARG_DTYPE}_deps}
544-
get_imf_fallback_${ARG_DTYPE} sycl-compiler
548+
get_imf_fallback_${ARG_DTYPE} ${sycl-compiler_deps}
545549
VERBATIM)
546550

547551
add_custom_target(${ARG_TGT_NAME}
@@ -635,7 +639,7 @@ foreach(dtype IN ITEMS bf16 fp32 fp64)
635639
endif()
636640
add_custom_command(
637641
OUTPUT ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
638-
COMMAND ${clang} ${${ftype}_host_compile_opts}
642+
COMMAND ${clang_exe} ${${ftype}_host_compile_opts}
639643
${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
640644
-o ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
641645
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
@@ -652,7 +656,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
652656
DEPENDS ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}})
653657
add_custom_command(
654658
OUTPUT ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
655-
COMMAND ${llvm-ar} rcs
659+
COMMAND ${llvm-ar_exe} rcs
656660
${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
657661
${${ftype}_binary_dir}/imf-fp32-host.${${ftype}-suffix}
658662
${${ftype}_binary_dir}/fallback-imf-fp32-host.${${ftype}-suffix}
@@ -663,7 +667,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
663667
DEPENDS imf_fp32_host_${ftype} imf_fallback_fp32_host_${ftype}
664668
DEPENDS imf_fp64_host_${ftype} imf_fallback_fp64_host_${ftype}
665669
DEPENDS imf_bf16_host_${ftype} imf_fallback_bf16_host_${ftype}
666-
DEPENDS sycl-compiler
670+
DEPENDS ${llvm-ar_target}
667671
VERBATIM)
668672
add_dependencies(libsycldevice-obj imf_host_${ftype})
669673

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)