Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / cmake / modules / prepare_libc_gpu_build.cmake
blob0b6067f69775c4512008c8e41afaa6d5b715e437
1 if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
2   message(FATAL_ERROR
3           "libc build: Invalid attempt to set up GPU architectures.")
4 endif()
6 # Set up the target architectures to build the GPU libc for.
7 set(all_amdgpu_architectures "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906"
8                              "gfx908;gfx90a;gfx90c;gfx940;gfx1010;gfx1030"
9                              "gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036"
10                              "gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151")
11 set(all_nvptx_architectures "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
12                             "sm_70;sm_72;sm_75;sm_80;sm_86;sm_89;sm_90")
13 set(all_gpu_architectures
14     "${all_amdgpu_architectures};${all_nvptx_architectures}")
15 set(LIBC_GPU_ARCHITECTURES "all" CACHE STRING
16     "List of GPU architectures to build the libc for.")
18 # Ensure the compiler is a valid clang when building the GPU target.
19 set(req_ver "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
20 if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" AND
21         ${CMAKE_CXX_COMPILER_VERSION} VERSION_EQUAL "${req_ver}"))
22   message(FATAL_ERROR "Cannot build libc for GPU. CMake compiler "
23                       "'${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}' "
24                       " is not 'Clang ${req_ver}'.")
25 endif()
26 if(NOT LLVM_LIBC_FULL_BUILD)
27   message(FATAL_ERROR "LLVM_LIBC_FULL_BUILD must be enabled to build libc for "
28                       "GPU.")
29 endif()
31 # Identify any locally installed AMD GPUs on the system using 'amdgpu-arch'.
32 find_program(LIBC_AMDGPU_ARCH
33              NAMES amdgpu-arch NO_DEFAULT_PATH
34              PATHS ${LLVM_BINARY_DIR}/bin /opt/rocm/llvm/bin/)
36 # Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'.
37 find_program(LIBC_NVPTX_ARCH
38              NAMES nvptx-arch NO_DEFAULT_PATH
39              PATHS ${LLVM_BINARY_DIR}/bin)
41 # Get the list of all natively supported GPU architectures.
42 set(detected_gpu_architectures "")
43 foreach(arch_tool ${LIBC_NVPTX_ARCH} ${LIBC_AMDGPU_ARCH})
44   if(arch_tool)
45     execute_process(COMMAND ${arch_tool}
46                     OUTPUT_VARIABLE arch_tool_output
47                     ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
48     string(REPLACE "\n" ";" arch_list "${arch_tool_output}")
49     list(APPEND detected_gpu_architectures "${arch_list}")
50   endif()
51 endforeach()
52 list(REMOVE_DUPLICATES detected_gpu_architectures)
54 if(LIBC_GPU_ARCHITECTURES STREQUAL "all")
55   set(LIBC_GPU_ARCHITECTURES ${all_gpu_architectures})
56 elseif(LIBC_GPU_ARCHITECTURES STREQUAL "native")
57   if(NOT detected_gpu_architectures)
58     message(FATAL_ERROR "No GPUs found on the system when using 'native'")
59   endif()
60   set(LIBC_GPU_ARCHITECTURES ${detected_gpu_architectures})
61 endif()
62 message(STATUS "Building libc for the following GPU architecture(s): "
63                "${LIBC_GPU_ARCHITECTURES}")
65 # Identify the program used to package multiple images into a single binary.
66 find_program(LIBC_CLANG_OFFLOAD_PACKAGER
67              NAMES clang-offload-packager NO_DEFAULT_PATH
68              PATHS ${LLVM_BINARY_DIR}/bin)
69 if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
70   message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU "
71                       "build")
72 endif()
74 # Optionally set up a job pool to limit the number of GPU tests run in parallel.
75 # This is sometimes necessary as running too many tests in parallel can cause
76 # the GPU or driver to run out of resources.
77 set(LIBC_GPU_TEST_JOBS "" CACHE STRING "Number of jobs to use for GPU tests")
78 if(LIBC_GPU_TEST_JOBS)
79   set_property(GLOBAL PROPERTY JOB_POOLS LIBC_GPU_TEST_POOL=${LIBC_GPU_TEST_JOBS})
80   set(LIBC_HERMETIC_TEST_JOB_POOL JOB_POOL LIBC_GPU_TEST_POOL)
81 endif()
83 set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")
85 set(gpu_test_architecture "")
86 if(LIBC_GPU_TEST_ARCHITECTURE)
87   set(gpu_test_architecture ${LIBC_GPU_TEST_ARCHITECTURE})
88   message(STATUS "Using user-specified GPU architecture for testing: "
89                  "'${gpu_test_architecture}'")
90 elseif(detected_gpu_architectures)
91   list(GET detected_gpu_architectures 0 gpu_test_architecture)
92   message(STATUS "Using GPU architecture detected on the system for testing: "
93                  "'${gpu_test_architecture}'")
94 else()
95   message(STATUS "No GPU architecture set for testing. GPU tests will not be "
96                  "availibe. Set 'LIBC_GPU_TEST_ARCHITECTURE' to override.")
97   return()
98 endif()
100 if("${gpu_test_architecture}" IN_LIST all_amdgpu_architectures)
101   set(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU TRUE)
102   set(LIBC_GPU_TARGET_TRIPLE "amdgcn-amd-amdhsa")
103   set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")
104 elseif("${gpu_test_architecture}" IN_LIST all_nvptx_architectures)
105   set(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX TRUE)
106   set(LIBC_GPU_TARGET_TRIPLE "nvptx64-nvidia-cuda")
107   set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")
108 else()
109   message(FATAL_ERROR "Unknown GPU architecture '${gpu_test_architecture}'")
110 endif()
112 if(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
113   find_package(CUDAToolkit QUIET)
114   if(CUDAToolkit_FOUND)
115     get_filename_component(LIBC_CUDA_ROOT "${CUDAToolkit_BIN_DIR}" DIRECTORY ABSOLUTE)
116   endif()
117 endif()
119 if(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU)
120   # The AMDGPU environment uses different code objects to encode the ABI for
121   # kernel calls and intrinsic functions. We want to specify this manually to
122   # conform to whatever the test suite was built to handle.
123   # FIXME: The test suite currently hangs when compiled targeting version five.
124   # This occurrs during traversal of the callback array in the startup code. We
125   # deliberately use version four until this can be addressed.
126   set(LIBC_GPU_CODE_OBJECT_VERSION 4)
127 endif()