1 if(NOT LIBC_TARGET_OS_IS_GPU)
3 "libc build: Invalid attempt to set up GPU architectures.")
6 # Ensure the compiler is a valid clang when building the GPU target.
7 set(req_ver "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
8 if(LLVM_VERSION_MAJOR AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" AND
9 ${CMAKE_CXX_COMPILER_VERSION} VERSION_EQUAL "${req_ver}"))
10 message(FATAL_ERROR "Cannot build libc for GPU. CMake compiler "
11 "'${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}' "
12 " is not 'Clang ${req_ver}'.")
14 if(NOT LLVM_LIBC_FULL_BUILD)
15 message(FATAL_ERROR "LLVM_LIBC_FULL_BUILD must be enabled to build libc for "
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}")
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")
30 # Identify the program used to package multiple images into a single binary.
31 get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY)
32 if(TARGET clang-offload-packager)
33 get_target_property(LIBC_CLANG_OFFLOAD_PACKAGER clang-offload-packager LOCATION)
35 find_program(LIBC_CLANG_OFFLOAD_PACKAGER
36 NAMES clang-offload-packager NO_DEFAULT_PATH
37 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
38 if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
39 message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU "
44 # Identify llvm-link program so we can merge the output IR into a single blob.
46 get_target_property(LIBC_LLVM_LINK llvm-link LOCATION)
48 find_program(LIBC_LLVM_LINK
49 NAMES llvm-link NO_DEFAULT_PATH
50 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
51 if(NOT LIBC_LLVM_LINK)
52 message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build")
56 # Optionally set up a job pool to limit the number of GPU tests run in parallel.
57 # This is sometimes necessary as running too many tests in parallel can cause
58 # the GPU or driver to run out of resources.
59 set(LIBC_GPU_TEST_JOBS "" CACHE STRING "Number of jobs to use for GPU tests")
60 if(LIBC_GPU_TEST_JOBS)
61 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=${LIBC_GPU_TEST_JOBS})
62 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)
64 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=1)
65 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)
68 set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")
69 if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
70 check_cxx_compiler_flag(-mcpu=native PLATFORM_HAS_GPU)
71 elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
72 check_cxx_compiler_flag(-march=native PLATFORM_HAS_GPU)
75 set(gpu_test_architecture "")
76 if(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU
77 AND NOT "AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
78 set(LIBC_GPU_TESTS_DISABLED TRUE)
79 message(STATUS "AMDGPU backend is not available, tests will not be built")
80 elseif(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU
81 AND NOT "NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
82 set(LIBC_GPU_TESTS_DISABLED TRUE)
83 message(STATUS "NVPTX backend is not available, tests will not be built")
84 elseif(LIBC_GPU_TEST_ARCHITECTURE)
85 set(LIBC_GPU_TESTS_DISABLED FALSE)
86 set(gpu_test_architecture ${LIBC_GPU_TEST_ARCHITECTURE})
87 message(STATUS "Using user-specified GPU architecture for testing: "
88 "'${gpu_test_architecture}'")
89 elseif(PLATFORM_HAS_GPU)
90 set(LIBC_GPU_TESTS_DISABLED FALSE)
91 set(gpu_test_architecture "native")
92 message(STATUS "Using GPU architecture detected on the system for testing: "
95 set(LIBC_GPU_TESTS_DISABLED TRUE)
96 message(STATUS "No GPU architecture detected or provided, tests will not be "
99 set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")
101 # The NVPTX backend cannot currently handle objects created in debug mode.
102 if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX AND CMAKE_BUILD_TYPE STREQUAL "Debug")
103 set(LIBC_GPU_TESTS_DISABLED TRUE)
106 # Identify the GPU loader utility used to run tests.
107 set(LIBC_GPU_LOADER_EXECUTABLE "" CACHE STRING "Executable for the GPU loader.")
108 if(LIBC_GPU_LOADER_EXECUTABLE)
109 set(gpu_loader_executable ${LIBC_GPU_LOADER_EXECUTABLE})
110 elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
111 find_program(LIBC_AMDHSA_LOADER_EXECUTABLE
112 NAMES amdhsa-loader NO_DEFAULT_PATH
113 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
114 if(LIBC_AMDHSA_LOADER_EXECUTABLE)
115 set(gpu_loader_executable ${LIBC_AMDHSA_LOADER_EXECUTABLE})
117 elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
118 find_program(LIBC_NVPTX_LOADER_EXECUTABLE
119 NAMES nvptx-loader NO_DEFAULT_PATH
120 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
121 if(LIBC_NVPTX_LOADER_EXECUTABLE)
122 set(gpu_loader_executable ${LIBC_NVPTX_LOADER_EXECUTABLE})
125 if(NOT TARGET libc.utils.gpu.loader AND gpu_loader_executable)
126 add_custom_target(libc.utils.gpu.loader)
127 set_target_properties(
128 libc.utils.gpu.loader
130 EXECUTABLE "${gpu_loader_executable}"
134 if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
135 # The AMDGPU environment uses different code objects to encode the ABI for
136 # kernel calls and intrinsic functions. We want to specify this manually to
137 # conform to whatever the test suite was built to handle.
138 set(LIBC_GPU_CODE_OBJECT_VERSION 5)
141 if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
142 # FIXME: This is a hack required to keep the CUDA package from trying to find
143 # pthreads. We only link the CUDA driver, so this is unneeded.
144 add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
146 find_package(CUDAToolkit QUIET)
147 if(CUDAToolkit_FOUND)
148 get_filename_component(LIBC_CUDA_ROOT "${CUDAToolkit_BIN_DIR}" DIRECTORY ABSOLUTE)