Skip to content

Commit 6c9916d

Browse files
authored
[libc] Configure CMAKE_REQUIRED_FLAGS so the GPU can use flag checks (#95424)
Summary: This patch adds `CMAKE_REQUIRED_FLAGS` for the GPU build so checks like `check_cxx_compiler_flags` work as expected. This is required because we need to hack around the potential lack of `nvlink` and `ptxas` for NVPTX targets and the fact that the AMDGPU target needs `-nogpulib` to avoid errors on lack of ROCm. This makes a few of the checks pass and also allows us to just check `-mcpu=native` for architecture detection instead of finding the tools manually.
1 parent 5ed5d72 commit 6c9916d

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ if(NOT LLVM_LIBC_FULL_BUILD)
1616
"GPU.")
1717
endif()
1818

19+
# Set the required flags globally so standard CMake utilities can compile.
20+
if(LIBC_TARGET_TRIPLE)
21+
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
22+
endif()
23+
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
24+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
25+
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
26+
set(CMAKE_REQUIRED_FLAGS
27+
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
28+
endif()
29+
1930
# Identify the program used to package multiple images into a single binary.
2031
get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY)
2132
if(TARGET clang-offload-packager)
@@ -56,39 +67,9 @@ endif()
5667

5768
set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")
5869
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
59-
# Identify any locally installed AMD GPUs on the system using 'amdgpu-arch'.
60-
if(TARGET amdgpu-arch)
61-
get_target_property(LIBC_AMDGPU_ARCH amdgpu-arch LOCATION)
62-
else()
63-
find_program(LIBC_AMDGPU_ARCH
64-
NAMES amdgpu-arch NO_DEFAULT_PATH
65-
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
66-
endif()
67-
if(LIBC_AMDGPU_ARCH)
68-
execute_process(COMMAND ${LIBC_AMDGPU_ARCH}
69-
OUTPUT_VARIABLE arch_tool_output
70-
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
71-
if(arch_tool_output MATCHES "^gfx[0-9]+")
72-
set(PLATFORM_HAS_GPU TRUE)
73-
endif()
74-
endif()
70+
check_cxx_compiler_flag(-mcpu=native PLATFORM_HAS_GPU)
7571
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
76-
# Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'.
77-
if(TARGET nvptx-arch)
78-
get_target_property(LIBC_NVPTX_ARCH nvptx-arch LOCATION)
79-
else()
80-
find_program(LIBC_NVPTX_ARCH
81-
NAMES nvptx-arch NO_DEFAULT_PATH
82-
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
83-
endif()
84-
if(LIBC_NVPTX_ARCH)
85-
execute_process(COMMAND ${LIBC_NVPTX_ARCH}
86-
OUTPUT_VARIABLE arch_tool_output
87-
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
88-
if(arch_tool_output MATCHES "^sm_[0-9]+")
89-
set(PLATFORM_HAS_GPU TRUE)
90-
endif()
91-
endif()
72+
check_cxx_compiler_flag(-march=native PLATFORM_HAS_GPU)
9273
endif()
9374

9475
set(gpu_test_architecture "")

llvm/runtimes/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,15 @@ if(build_runtimes)
496496
if(TARGET amdhsa-loader)
497497
list(APPEND extra_cmake_args
498498
"-DRUNTIMES_amdgcn-amd-amdhsa_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:amdhsa-loader>")
499-
list(APPEND extra_deps amdhsa-loader amdgpu-arch)
499+
list(APPEND extra_deps amdhsa-loader)
500500
endif()
501501
list(APPEND extra_cmake_args "-DRUNTIMES_amdgcn-amd-amdhsa_LLVM_LIBC_FULL_BUILD=ON")
502502
endif()
503503
if("libc" IN_LIST RUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES)
504504
if(TARGET nvptx-loader)
505505
list(APPEND extra_cmake_args
506506
"-DRUNTIMES_nvptx64-nvidia-cuda_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:nvptx-loader>")
507-
list(APPEND extra_deps nvptx-loader nvptx-arch)
507+
list(APPEND extra_deps nvptx-loader)
508508
endif()
509509
list(APPEND extra_cmake_args "-DRUNTIMES_nvptx64-nvidia-cuda_LLVM_LIBC_FULL_BUILD=ON")
510510
endif()

0 commit comments

Comments
 (0)