Skip to content

Commit 640ba3f

Browse files
authored
[libc] Fix standard cross build targeting the GPU (#82724)
Summary: The GPU target has recently been changed to support standard `libc` build rules. This means we should be able to build for it both in `LLVM_ENABLE_PROJECTS` mode, or targeting the runtimes directory directly as in the LLVM `libc` documentation. Previously this failed because the version check on the compiler was too strict and the `--target=` options were not being set on the link jobs unless in CMake cross compiliation mode. This patch fixes those so the following config should work now to build the GPU target directly if using NVPTX. ``` cmake ../runtimes -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang \ -DLLVM_ENABLE_RUNTIMES=libc -DLLVM_RUNTIMES_TARGET=nvptx64-nvidia-cuda \ -DLLVM_DEFAULT_TARGET_TRIPLE=nvptx64-nvidia-cuda \ -DLIBC_HDRGEN_EXE=/path/to/hdrgen/libc-hdrgen \ -DLLVM_LIBC_FULL_BUILD=ON -GNinja ```
1 parent 0352d5e commit 640ba3f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,15 @@ function(add_integration_test test_name)
463463

464464
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
465465
target_link_options(${fq_build_target_name} PRIVATE
466+
${LIBC_COMPILE_OPTIONS_DEFAULT}
466467
-mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto
467468
"-Wl,-mllvm,-amdgpu-lower-global-ctor-dtor=0" -nostdlib -static
468469
"-Wl,-mllvm,-amdhsa-code-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}")
469470
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
470471
# We need to use the internal object versions for NVPTX.
471472
set(internal_suffix ".__internal__")
472473
target_link_options(${fq_build_target_name} PRIVATE
474+
${LIBC_COMPILE_OPTIONS_DEFAULT}
473475
"-Wl,--suppress-stack-size-warning"
474476
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
475477
"--cuda-path=${LIBC_CUDA_ROOT}")
@@ -644,13 +646,15 @@ function(add_libc_hermetic_test test_name)
644646

645647
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
646648
target_link_options(${fq_build_target_name} PRIVATE
649+
${LIBC_COMPILE_OPTIONS_DEFAULT}
647650
-mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto
648651
"-Wl,-mllvm,-amdgpu-lower-global-ctor-dtor=0" -nostdlib -static
649652
"-Wl,-mllvm,-amdhsa-code-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}")
650653
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
651654
# We need to use the internal object versions for NVPTX.
652655
set(internal_suffix ".__internal__")
653656
target_link_options(${fq_build_target_name} PRIVATE
657+
${LIBC_COMPILE_OPTIONS_DEFAULT}
654658
"-Wl,--suppress-stack-size-warning"
655659
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
656660
"--cuda-path=${LIBC_CUDA_ROOT}")

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ endif()
55

66
# Ensure the compiler is a valid clang when building the GPU target.
77
set(req_ver "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
8-
if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" AND
9-
${CMAKE_CXX_COMPILER_VERSION} VERSION_EQUAL "${req_ver}"))
8+
if(LLVM_VERSION_MAJOR AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" AND
9+
${CMAKE_CXX_COMPILER_VERSION} VERSION_EQUAL "${req_ver}"))
1010
message(FATAL_ERROR "Cannot build libc for GPU. CMake compiler "
1111
"'${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}' "
1212
" is not 'Clang ${req_ver}'.")

0 commit comments

Comments
 (0)