Skip to content

Commit

Permalink
[SYCL][libclc] Fix cross builds (#16244)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
hvdijk authored Dec 19, 2024
1 parent ef4d66a commit 2444c91
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 34 deletions.
3 changes: 3 additions & 0 deletions clang/tools/clang-offload-bundler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ add_clang_tool(clang-offload-bundler
intrinsics_gen
)

setup_host_tool(clang-offload-bundler CLANG_OFFLOAD_BUNDLER
clang-offload-bundler_exe clang-offload-bundler_target)

set(CLANG_OFFLOAD_BUNDLER_LIB_DEPS
clangBasic
clangDriver
Expand Down
3 changes: 3 additions & 0 deletions clang/tools/clang-offload-packager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ add_clang_tool(clang-offload-packager
${tablegen_deps}
)

setup_host_tool(clang-offload-packager CLANG_OFFLOAD_PACKAGER_EXE
clang-offload-packager_exe clang-offload-packager_target)

clang_target_link_libraries(clang-offload-packager
PRIVATE
clangBasic
Expand Down
13 changes: 8 additions & 5 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI

# Import required tools
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
foreach( tool IN ITEMS clang llvm-as llvm-link llvm-spirv opt )
find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
set( ${tool}_exe ${LLVM_TOOL_${tool}} )
set( ${tool}_target )
Expand All @@ -93,6 +93,7 @@ else()
get_host_tool_path( clang CLANG clang_exe clang_target )
get_host_tool_path( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
get_host_tool_path( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
get_host_tool_path( opt OPT opt_exe opt_target )
endif()
endif()
Expand All @@ -114,13 +115,15 @@ if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )

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

foreach( tool IN ITEMS clang opt llvm-as llvm-link )
if( NOT EXISTS "${${tool}_exe}" AND "${tool}_target" STREQUAL "" )
if( NOT EXISTS "${${tool}_exe}" AND "${${tool}_target}" STREQUAL "" )
message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
endif()
endforeach()
Expand Down
8 changes: 4 additions & 4 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@ function(add_libclc_builtin_set)
"${LIBCLC_LIBRARY_OUTPUT_INTDIR}/remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" )
add_custom_command( OUTPUT "${builtins_remangle_path}"
COMMAND ${CMAKE_COMMAND} -E make_directory ${LIBCLC_LIBRARY_OUTPUT_INTDIR}
COMMAND libclc::libclc-remangler
COMMAND ${libclc-remangler_exe}
-o "${builtins_remangle_path}"
--long-width=${long_width}
--char-signedness=${signedness}
--input-ir=${builtins_lib}
${dummy_in}
DEPENDS ${builtins_lib} libclc::libclc-remangler ${dummy_in})
DEPENDS ${builtins_lib} ${libclc-remangler_target} ${dummy_in})
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" ALL
DEPENDS "${builtins_remangle_path}" "${dummy_in}")
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}"
Expand All @@ -489,12 +489,12 @@ function(add_libclc_builtin_set)
math(EXPR libclc-remangler-test-no "${libclc-remangler-test-no}+1")
set(current-test "libclc-remangler-test-${obj_suffix}-${libclc-remangler-test-no}")
add_custom_target(${current-test}
COMMAND libclc::libclc-remangler
COMMAND ${libclc-remangler_exe}
--long-width=l32
--char-signedness=signed
--input-ir=${target-ir}
${dummy_in} -t -o -
DEPENDS ${builtins_lib} "${dummy_in}" libclc::libclc-remangler)
DEPENDS ${builtins_lib} "${dummy_in}" ${libclc-remangler_target})
list(APPEND libclc-remangler-tests ${current-test})
endforeach()
endif()
Expand Down
9 changes: 3 additions & 6 deletions libclc/utils/libclc-remangler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set(LLVM_LINK_COMPONENTS

add_clang_tool(libclc-remangler LibclcRemangler.cpp)

setup_host_tool( libclc-remangler LIBCLC_REMANGLER
libclc-remangler_exe libclc-remangler_target )

target_include_directories(libclc-remangler PRIVATE
${CMAKE_SOURCE_DIR}/../clang/include
${CMAKE_BINARY_DIR}/tools/clang/include)
Expand All @@ -24,9 +27,3 @@ clang_target_link_libraries(libclc-remangler
clangSerialization
LLVMOption
)

# If we've not already imported a libclc-remangler tool from an external build,
# set it up now.
if(NOT TARGET libclc::libclc-remangler)
add_executable(libclc::libclc-remangler ALIAS libclc-remangler)
endif()
19 changes: 19 additions & 0 deletions libdevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ else()
will not build libdevice sanitizer")
endif()

if(NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR})
get_host_tool_path(clang CLANG clang_exe clang_target)
get_host_tool_path(llvm-ar LLVM_AR llvm-ar_exe llvm-ar_target)
get_host_tool_path(append-file APPEND_FILE append-file_exe append-file_target)
get_host_tool_path(clang-offload-bundler CLANG_OFFLOAD_BUNDLER clang-offload-bundler_exe clang-offload-bundler_target)
get_host_tool_path(clang-offload-packager CLANG_OFFLOAD_PACKAGER clang-offload-packager_exe clang-offload-packager_target)
get_host_tool_path(file-table-tform FILE_TABLE_TFORM file-table-tform_exe file-table-tform_target)
get_host_tool_path(llvm-foreach LLVM_FOREACH llvm-foreach_exe llvm-foreach_target)
get_host_tool_path(llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target)
get_host_tool_path(sycl-post-link SYCL_POST_LINK sycl-post-link_exe sycl-post-link_target)
else()
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)
find_program(LLVM_CUSTOM_TOOL_${tool} ${tool}
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
set(${tool}_exe ${LLVM_CUSTOM_TOOL_${tool}})
set(${tool}_target)
endforeach()
endif()

# Build libdevice for SYCL.
include(SYCLLibdevice)

Expand Down
42 changes: 23 additions & 19 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ set(install_dest_obj lib${LLVM_LIBDIR_SUFFIX})
set(install_dest_obj-new-offload lib${LLVM_LIBDIR_SUFFIX})
set(install_dest_bc lib${LLVM_LIBDIR_SUFFIX})

set(clang $<TARGET_FILE:clang>)
set(llvm-ar $<TARGET_FILE:llvm-ar>)
set(llvm-link $<TARGET_FILE:llvm-link>)
set(llvm-opt $<TARGET_FILE:opt>)

string(CONCAT sycl_targets_opt
"-fsycl-targets="
"spir64_x86_64-unknown-unknown,"
Expand Down Expand Up @@ -55,6 +50,7 @@ set(compile_opts
# we declare all functions as 'static'.
-Wno-undefined-internal
-sycl-std=2020
--target=${LLVM_HOST_TRIPLE}
)

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

add_custom_command(
OUTPUT ${devicelib-file}
COMMAND ${clang} ${ARG_OPTS}
COMMAND ${clang_exe} -I ${PROJECT_BINARY_DIR}/include
${ARG_OPTS}
${CMAKE_CURRENT_SOURCE_DIR}/${ARG_SRC} -o ${devicelib-file}
MAIN_DEPENDENCY ${ARG_SRC}
DEPENDS ${ARG_DEPENDENCIES}
Expand Down Expand Up @@ -224,20 +221,26 @@ function(add_devicelibs filename)
endforeach()
endfunction()

# Set up the dependency lists for the libdevice libraries
set(crt_obj_deps wrapper.h device.h spirv_vars.h sycl-compiler)
set(complex_obj_deps device_complex.h device.h sycl-compiler)
set(cmath_obj_deps device_math.h device.h sycl-compiler)
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)
set(itt_obj_deps device_itt.h spirv_vars.h device.h sycl-compiler)
set(bfloat16_obj_deps sycl-headers sycl-compiler)
# For native builds, sycl-compiler will already include everything we need.
# For cross builds, we also need native versions of the tools.
set(sycl-compiler_deps
sycl-compiler ${clang_target} ${append-file_target}
${clang-offload-bundler_target} ${clang-offload-packager_target}
${file-table-tform_target} ${llvm-foreach_target} ${llvm-spirv_target}
${sycl-post-link_target})
set(crt_obj_deps wrapper.h device.h spirv_vars.h ${sycl-compiler_deps})
set(complex_obj_deps device_complex.h device.h ${sycl-compiler_deps})
set(cmath_obj_deps device_math.h device.h ${sycl-compiler_deps})
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})
set(itt_obj_deps device_itt.h spirv_vars.h device.h ${sycl-compiler_deps})
set(bfloat16_obj_deps sycl-headers ${sycl-compiler_deps})
if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
set(asan_obj_deps
device.h atomic.hpp spirv_vars.h
${UR_SANITIZER_INCLUDE_DIR}/asan/asan_libdevice.hpp
include/asan_rtl.hpp
include/spir_global_var.hpp
sycl-compiler)
${sycl-compiler_deps})

set(sanitizer_generic_compile_opts ${compile_opts}
-fno-sycl-instrument-device-code
Expand Down Expand Up @@ -442,6 +445,7 @@ set(imf_fp64_fallback_src ${imf_fallback_src_dir}/imf_fp64_fallback.cpp)
set(imf_bf16_fallback_src ${imf_fallback_src_dir}/imf_bf16_fallback.cpp)

set(imf_host_cxx_flags -c
--target=${LLVM_HOST_TRIPLE}
-D__LIBDEVICE_HOST_IMPL__
)

Expand Down Expand Up @@ -535,13 +539,13 @@ function(add_lib_imf name)

add_custom_command(
OUTPUT ${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
COMMAND ${clang} ${compile_opts} ${ARG_EXTRA_OPTS}
COMMAND ${clang_exe} ${compile_opts} ${ARG_EXTRA_OPTS}
-I ${CMAKE_CURRENT_SOURCE_DIR}/imf
${imf_${ARG_DTYPE}_fallback_src}
-o
${ARG_DIR}/${name}.${${ARG_FTYPE}-suffix}
DEPENDS ${imf_fallback_${ARG_DTYPE}_deps}
get_imf_fallback_${ARG_DTYPE} sycl-compiler
get_imf_fallback_${ARG_DTYPE} ${sycl-compiler_deps}
VERBATIM)

add_custom_target(${ARG_TGT_NAME}
Expand Down Expand Up @@ -635,7 +639,7 @@ foreach(dtype IN ITEMS bf16 fp32 fp64)
endif()
add_custom_command(
OUTPUT ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
COMMAND ${clang} ${${ftype}_host_compile_opts}
COMMAND ${clang_exe} ${${ftype}_host_compile_opts}
${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
-o ${${ftype}_binary_dir}/imf-${dtype}-host.${${ftype}-suffix}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${wrapper_name}
Expand All @@ -652,7 +656,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
DEPENDS ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}})
add_custom_command(
OUTPUT ${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
COMMAND ${llvm-ar} rcs
COMMAND ${llvm-ar_exe} rcs
${${ftype}_binary_dir}/${devicelib_host_static_${ftype}}
${${ftype}_binary_dir}/imf-fp32-host.${${ftype}-suffix}
${${ftype}_binary_dir}/fallback-imf-fp32-host.${${ftype}-suffix}
Expand All @@ -663,7 +667,7 @@ foreach(ftype IN ITEMS obj obj-new-offload)
DEPENDS imf_fp32_host_${ftype} imf_fallback_fp32_host_${ftype}
DEPENDS imf_fp64_host_${ftype} imf_fallback_fp64_host_${ftype}
DEPENDS imf_bf16_host_${ftype} imf_fallback_bf16_host_${ftype}
DEPENDS sycl-compiler
DEPENDS ${llvm-ar_target}
VERBATIM)
add_dependencies(libsycldevice-obj imf_host_${ftype})

Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/append-file/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ add_llvm_tool(append-file
DEPENDS
intrinsics_gen
)

setup_host_tool(append-file APPEND_FILE append_file_exe append_file_target)
2 changes: 2 additions & 0 deletions llvm/tools/file-table-tform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ set(LLVM_LINK_COMPONENTS
add_llvm_tool(file-table-tform
file-table-tform.cpp
)

setup_host_tool(file-table-tform FILE_TABLE_TFORM file-table-tform_exe file-table-tform_target)
2 changes: 2 additions & 0 deletions llvm/tools/llvm-ar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_llvm_tool(llvm-ar
GENERATE_DRIVER
)

setup_host_tool(llvm-ar LLVM_AR llvm_ar_exe llvm_ar_target)

add_llvm_tool_symlink(llvm-ranlib llvm-ar)
add_llvm_tool_symlink(llvm-lib llvm-ar)
add_llvm_tool_symlink(llvm-dlltool llvm-ar)
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-foreach/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ set(LLVM_LINK_COMPONENTS
add_llvm_tool(llvm-foreach
llvm-foreach.cpp
)

setup_host_tool(llvm-foreach LLVM_FOREACH llvm-foreach_exe llvm-foreach_target)
3 changes: 3 additions & 0 deletions llvm/tools/sycl-post-link/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ add_llvm_tool(sycl-post-link
LLVMGenXIntrinsics
)

setup_host_tool(sycl-post-link SYCL_POST_LINK
sycl-post-link_exe sycl-post-link_target)

target_link_libraries(sycl-post-link PRIVATE LLVMGenXIntrinsics)

0 comments on commit 2444c91

Please sign in to comment.