112 lines · plain
1if(NOT LIBC_TARGET_OS_IS_GPU)2 message(FATAL_ERROR3 "libc build: Invalid attempt to set up GPU architectures.")4endif()5 6# Ensure the compiler is a valid clang when building the GPU target.7set(req_ver "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")8if(LLVM_VERSION_MAJOR AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" AND9 ${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}'.")13endif()14if(NOT LLVM_LIBC_FULL_BUILD)15 message(FATAL_ERROR "LLVM_LIBC_FULL_BUILD must be enabled to build libc for "16 "GPU.")17endif()18 19# Set the required flags globally so standard CMake utilities can compile.20if(LIBC_TARGET_TRIPLE)21 set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")22endif()23 24# Optionally set up a job pool to limit the number of GPU tests run in parallel.25# This is sometimes necessary as running too many tests in parallel can cause26# the GPU or driver to run out of resources.27set(LIBC_GPU_TEST_JOBS "" CACHE STRING "Number of jobs to use for GPU tests")28if(LIBC_GPU_TEST_JOBS)29 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=${LIBC_GPU_TEST_JOBS})30 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)31else()32 set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=1)33 set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)34endif()35 36set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")37if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)38 check_cxx_compiler_flag(-mcpu=native PLATFORM_HAS_GPU)39elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)40 check_cxx_compiler_flag(-march=native PLATFORM_HAS_GPU)41endif()42 43set(gpu_test_architecture "")44if(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU45 AND NOT "AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)46 set(LIBC_GPU_TESTS_DISABLED TRUE)47 message(STATUS "AMDGPU backend is not available, tests will not be built")48elseif(DEFINED LLVM_TARGETS_TO_BUILD AND LIBC_TARGET_ARCHITECTURE_IS_NVPTX49 AND NOT "NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)50 set(LIBC_GPU_TESTS_DISABLED TRUE)51 message(STATUS "NVPTX backend is not available, tests will not be built")52elseif(LIBC_GPU_TEST_ARCHITECTURE)53 set(LIBC_GPU_TESTS_DISABLED FALSE)54 set(gpu_test_architecture ${LIBC_GPU_TEST_ARCHITECTURE})55 message(STATUS "Using user-specified GPU architecture for testing: "56 "'${gpu_test_architecture}'")57elseif(PLATFORM_HAS_GPU)58 set(LIBC_GPU_TESTS_DISABLED FALSE)59 set(gpu_test_architecture "native")60 message(STATUS "Using GPU architecture detected on the system for testing: "61 "'native'")62else()63 set(LIBC_GPU_TESTS_DISABLED TRUE)64 message(STATUS "No GPU architecture detected or provided, tests will not be "65 "built")66endif()67set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")68 69# Identify the GPU loader utility used to run tests.70set(LIBC_GPU_LOADER_EXECUTABLE "" CACHE STRING "Executable for the GPU loader.")71if(LIBC_GPU_LOADER_EXECUTABLE)72 set(gpu_loader_executable ${LIBC_GPU_LOADER_EXECUTABLE})73elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)74 find_program(LIBC_AMDHSA_LOADER_EXECUTABLE75 NAMES amdhsa-loader NO_DEFAULT_PATH76 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})77 if(LIBC_AMDHSA_LOADER_EXECUTABLE)78 set(gpu_loader_executable ${LIBC_AMDHSA_LOADER_EXECUTABLE})79 endif()80elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)81 find_program(LIBC_NVPTX_LOADER_EXECUTABLE82 NAMES nvptx-loader NO_DEFAULT_PATH83 PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})84 if(LIBC_NVPTX_LOADER_EXECUTABLE)85 set(gpu_loader_executable ${LIBC_NVPTX_LOADER_EXECUTABLE})86 endif()87endif()88if(NOT TARGET libc.utils.gpu.loader AND gpu_loader_executable)89 add_custom_target(libc.utils.gpu.loader)90 set_target_properties(91 libc.utils.gpu.loader92 PROPERTIES93 EXECUTABLE "${gpu_loader_executable}"94 )95endif()96 97# The AMDGPU environment uses different code objects to encode the ABI for98# kernel calls and intrinsic functions. We want to expose this to conform to99# whatever the test suite was built to handle.100set(LIBC_GPU_CODE_OBJECT_VERSION "6" CACHE STRING "AMDGPU Code object ABI to use")101 102if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)103 # FIXME: This is a hack required to keep the CUDA package from trying to find104 # pthreads. We only link the CUDA driver, so this is unneeded.105 add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)106 107 find_package(CUDAToolkit QUIET)108 if(CUDAToolkit_FOUND)109 get_filename_component(LIBC_CUDA_ROOT "${CUDAToolkit_BIN_DIR}" DIRECTORY ABSOLUTE)110 endif()111endif()112