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 # Optionally set up a job pool to limit the number of GPU tests run in parallel.
31 # This is sometimes necessary as running too many tests in parallel can cause
32 # the GPU or driver to run out of resources.
33 set(LIBC_GPU_TEST_JOBS "" CACHE STRING "Number of jobs to use for GPU tests")
34 if(LIBC_GPU_TEST_JOBS)
35 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=${LIBC_GPU_TEST_JOBS})
36 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)
38 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=1)
39 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)
42 set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")
43 if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
44 check_cxx_compiler_flag(-mcpu=native PLATFORM_HAS_GPU)
45 elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
46 check_cxx_compiler_flag(-march=native PLATFORM_HAS_GPU)
49 set(gpu_test_architecture "")
50 if(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU
51 AND NOT "AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
52 set(LIBC_GPU_TESTS_DISABLED TRUE)
53 message(STATUS "AMDGPU backend is not available, tests will not be built")
54 elseif(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_NVPTX
55 AND NOT "NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
56 set(LIBC_GPU_TESTS_DISABLED TRUE)
57 message(STATUS "NVPTX backend is not available, tests will not be built")
58 elseif(LIBC_GPU_TEST_ARCHITECTURE)
59 set(LIBC_GPU_TESTS_DISABLED FALSE)
60 set(gpu_test_architecture ${LIBC_GPU_TEST_ARCHITECTURE})
61 message(STATUS "Using user-specified GPU architecture for testing: "
62 "'${gpu_test_architecture}'")
63 elseif(PLATFORM_HAS_GPU)
64 set(LIBC_GPU_TESTS_DISABLED FALSE)
65 set(gpu_test_architecture "native")
66 message(STATUS "Using GPU architecture detected on the system for testing: "
69 set(LIBC_GPU_TESTS_DISABLED TRUE)
70 message(STATUS "No GPU architecture detected or provided, tests will not be "
73 set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")
75 # Identify the GPU loader utility used to run tests.
76 set(LIBC_GPU_LOADER_EXECUTABLE "" CACHE STRING "Executable for the GPU loader.")
77 if(LIBC_GPU_LOADER_EXECUTABLE)
78 set(gpu_loader_executable ${LIBC_GPU_LOADER_EXECUTABLE})
79 elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
80 find_program(LIBC_AMDHSA_LOADER_EXECUTABLE
81 NAMES amdhsa-loader NO_DEFAULT_PATH
82 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
83 if(LIBC_AMDHSA_LOADER_EXECUTABLE)
84 set(gpu_loader_executable ${LIBC_AMDHSA_LOADER_EXECUTABLE})
86 elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
87 find_program(LIBC_NVPTX_LOADER_EXECUTABLE
88 NAMES nvptx-loader NO_DEFAULT_PATH
89 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
90 if(LIBC_NVPTX_LOADER_EXECUTABLE)
91 set(gpu_loader_executable ${LIBC_NVPTX_LOADER_EXECUTABLE})
94 if(NOT TARGET libc.utils.gpu.loader AND gpu_loader_executable)
95 add_custom_target(libc.utils.gpu.loader)
96 set_target_properties(
99 EXECUTABLE "${gpu_loader_executable}"
103 if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
104 # The AMDGPU environment uses different code objects to encode the ABI for
105 # kernel calls and intrinsic functions. We want to specify this manually to
106 # conform to whatever the test suite was built to handle.
107 set(LIBC_GPU_CODE_OBJECT_VERSION 5)
110 if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
111 # FIXME: This is a hack required to keep the CUDA package from trying to find
112 # pthreads. We only link the CUDA driver, so this is unneeded.
113 add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
115 find_package(CUDAToolkit QUIET)
116 if(CUDAToolkit_FOUND)
117 get_filename_component(LIBC_CUDA_ROOT "${CUDAToolkit_BIN_DIR}" DIRECTORY ABSOLUTE)