Skip to content

Commit 575390d

Browse files
committed
[libc] Rework the GPU build to be a regular target
Summary: This is a massive patch because it reworks the entire build and everything that depends on it. This is not split up because various bots would fail otherwise. I will attempt to describe the necessary changes here. This patch completely reworks how the GPU build is built and targeted. Previously, we used a standard runtimes build and handled both NVPTX and AMDGPU in a single build via multi-targeting. This added a lot of divergence in the build system and prevented us from doing various things like building for the CPU / GPU at the same time, or exporting the startup libraries or running tests without a full rebuild. The new appraoch is to handle the GPU builds as strict cross-compiling runtimes. The first step required #81557 to allow the `LIBC` target to build for the GPU without touching the other targets. This means that the GPU uses all the same handling as the other builds in `libc`. The new expected way to build the GPU libc is with `LLVM_LIBC_RUNTIME_TARGETS=amdgcn-amd-amdhsa;nvptx64-nvidia-cuda`. The second step was reworking how we generated the embedded GPU library by moving it into the library install step. Where we previously had one `libcgpu.a` we now have `libcgpu-amdgpu.a` and `libcgpu-nvptx.a`. This patch includes the necessary clang / OpenMP changes to make that not break the bots when this lands. We unfortunately still require that the NVPTX target has an `internal` target for tests. This is because the NVPTX target needs to do LTO for the provided version (The offloading toolchain can handle it) but cannot use it for the native toolchain which is used for making tests. This approach is vastly suprerior in every way, allowing us to treat the GPU as a standard cross-compiling target. We can now install the GPU utilities to do things like use the offload tests and other fun things. Depends on #81557
1 parent 9dbedca commit 575390d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+579
-664
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,41 @@ static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
10871087
"llvm-libc-decls");
10881088
bool HasLibC = llvm::sys::fs::exists(LibCDecls) &&
10891089
llvm::sys::fs::is_directory(LibCDecls);
1090-
if (Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC)) {
1091-
CmdArgs.push_back("-lcgpu");
1092-
CmdArgs.push_back("-lmgpu");
1090+
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC))
1091+
return;
1092+
1093+
// We don't have access to the offloading toolchains here, so determine from
1094+
// the arguments if we have any active NVPTX or AMDGPU toolchains.
1095+
llvm::DenseSet<const char *> Libraries;
1096+
if (const Arg *Targets = Args.getLastArg(options::OPT_fopenmp_targets_EQ)) {
1097+
if (llvm::any_of(Targets->getValues(),
1098+
[](auto S) { return llvm::Triple(S).isAMDGPU(); })) {
1099+
Libraries.insert("-lcgpu-amdgpu");
1100+
Libraries.insert("-lmgpu-amdgpu");
1101+
}
1102+
if (llvm::any_of(Targets->getValues(),
1103+
[](auto S) { return llvm::Triple(S).isNVPTX(); })) {
1104+
Libraries.insert("-lcgpu-nvptx");
1105+
Libraries.insert("-lmgpu-nvptx");
1106+
}
10931107
}
1108+
1109+
for (StringRef Arch : Args.getAllArgValues(options::OPT_offload_arch_EQ)) {
1110+
if (llvm::any_of(llvm::split(Arch, ","), [](StringRef Str) {
1111+
return IsAMDGpuArch(StringToCudaArch(Str));
1112+
})) {
1113+
Libraries.insert("-lcgpu-amdgpu");
1114+
Libraries.insert("-lmgpu-amdgpu");
1115+
}
1116+
if (llvm::any_of(llvm::split(Arch, ","), [](StringRef Str) {
1117+
return IsNVIDIAGpuArch(StringToCudaArch(Str));
1118+
})) {
1119+
Libraries.insert("-lcgpu-nvptx");
1120+
Libraries.insert("-lmgpu-nvptx");
1121+
}
1122+
}
1123+
1124+
llvm::append_range(CmdArgs, Libraries);
10941125
}
10951126

10961127
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,

clang/test/Driver/openmp-offload-gpu.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,21 @@
394394
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
395395
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
396396
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
397-
// RUN: --offload-arch=sm_52 -gpulibc -nogpuinc %s 2>&1 \
397+
// RUN: --offload-arch=sm_52,gfx90a -gpulibc -nogpuinc %s 2>&1 \
398398
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
399-
// LIBC-GPU: "-lcgpu"{{.*}}"-lmgpu"
399+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
400+
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
401+
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
402+
// RUN: -fopenmp-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -gpulibc -nogpuinc %s 2>&1 \
403+
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
404+
// LIBC-GPU-DAG: "-lcgpu-amdgpu"
405+
// LIBC-GPU-DAG: "-lmgpu-amdgpu"
406+
// LIBC-GPU-DAG: "-lcgpu-nvptx"
407+
// LIBC-GPU-DAG: "-lmgpu-nvptx"
400408

401409
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
402410
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
403411
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
404412
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
405413
// RUN: | FileCheck --check-prefix=NO-LIBC-GPU %s
406-
// NO-LIBC-GPU-NOT: "-lcgpu"{{.*}}"-lmgpu"
414+
// NO-LIBC-GPU-NOT: -lmgpu{{.*}}-lcgpu

libc/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ set(LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LL
4343
CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
4444
)
4545

46-
if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
46+
if(LLVM_LIBC_FULL_BUILD OR LLVM_LIBC_GPU_BUILD)
4747
if(NOT LIBC_HDRGEN_EXE)
4848
# We need to set up hdrgen first since other targets depend on it.
4949
add_subdirectory(utils/LibcTableGenUtil)
@@ -77,7 +77,7 @@ if(LIBC_HDRGEN_ONLY OR NEED_LIBC_HDRGEN)
7777
# to build libc-hdrgen and return.
7878

7979
# Always make the RPC server availible to other projects for GPU mode.
80-
if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
80+
if(LLVM_LIBC_GPU_BUILD)
8181
add_subdirectory(utils/gpu/server)
8282
endif()
8383
return()
@@ -118,7 +118,7 @@ if(COMMAND_RETURN_CODE EQUAL 0)
118118
message(STATUS "Set COMPILER_RESOURCE_DIR to "
119119
"${COMPILER_RESOURCE_DIR} using --print-resource-dir")
120120
else()
121-
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
121+
if (LIBC_TARGET_OS_IS_GPU)
122122
message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
123123
else()
124124
set(COMPILER_RESOURCE_DIR OFF)
@@ -216,11 +216,7 @@ foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
216216
load_libc_config(${config_path}/config.json ${cmd_line_conf})
217217
endforeach()
218218

219-
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
220-
set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
221-
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/gpu-none-llvm)
222-
set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
223-
elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
219+
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
224220
set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
225221
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
226222
set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
@@ -235,7 +231,11 @@ else()
235231
set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
236232
set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
237233
endif()
238-
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
234+
if(LIBC_TARGET_OS_IS_GPU)
235+
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
236+
else()
237+
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
238+
endif()
239239
endif()
240240

241241
if(LIBC_TARGET_TRIPLE)
@@ -247,7 +247,7 @@ else()
247247
set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX})
248248
endif()
249249

250-
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
250+
if(LIBC_TARGET_OS_IS_GPU)
251251
include(prepare_libc_gpu_build)
252252
set(LIBC_ENABLE_UNITTESTS OFF)
253253
endif()

libc/cmake/modules/LLVMLibCArchitectures.cmake

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@
66
# platform.
77
# ------------------------------------------------------------------------------
88

9-
if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
10-
# We set the generic target and OS to "gpu" here. More specific defintions
11-
# for the exact target GPU are set up in prepare_libc_gpu_build.cmake.
12-
set(LIBC_TARGET_OS "gpu")
13-
set(LIBC_TARGET_ARCHITECTURE_IS_GPU TRUE)
14-
set(LIBC_TARGET_ARCHITECTURE "gpu")
15-
if(LIBC_TARGET_TRIPLE)
16-
message(WARNING "LIBC_TARGET_TRIPLE is ignored as LIBC_GPU_BUILD is on. ")
17-
endif()
18-
return()
19-
endif()
20-
219
if(MSVC)
2210
# If the compiler is visual c++ or equivalent, we will assume a host build.
2311
set(LIBC_TARGET_OS ${CMAKE_HOST_SYSTEM_NAME})
@@ -59,6 +47,10 @@ function(get_arch_and_system_from_triple triple arch_var sys_var)
5947
set(target_arch "riscv32")
6048
elseif(target_arch MATCHES "^riscv64")
6149
set(target_arch "riscv64")
50+
elseif(target_arch MATCHES "^amdgcn")
51+
set(target_arch "amdgpu")
52+
elseif(target_arch MATCHES "^nvptx64")
53+
set(target_arch "nvptx")
6254
else()
6355
return()
6456
endif()
@@ -75,6 +67,12 @@ function(get_arch_and_system_from_triple triple arch_var sys_var)
7567
set(target_sys "darwin")
7668
endif()
7769

70+
# Setting OS name for GPU architectures.
71+
list(GET triple_comps -1 gpu_target_sys)
72+
if(gpu_target_sys MATCHES "^amdhsa" OR gpu_target_sys MATCHES "^cuda")
73+
set(target_sys "gpu")
74+
endif()
75+
7876
set(${sys_var} ${target_sys} PARENT_SCOPE)
7977
endfunction(get_arch_and_system_from_triple)
8078

@@ -156,6 +154,10 @@ elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv64")
156154
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv32")
157155
set(LIBC_TARGET_ARCHITECTURE_IS_RISCV32 TRUE)
158156
set(LIBC_TARGET_ARCHITECTURE "riscv")
157+
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "amdgpu")
158+
set(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU TRUE)
159+
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "nvptx")
160+
set(LIBC_TARGET_ARCHITECTURE_IS_NVPTX TRUE)
159161
else()
160162
message(FATAL_ERROR
161163
"Unsupported libc target architecture ${LIBC_TARGET_ARCHITECTURE}")
@@ -178,6 +180,8 @@ elseif(LIBC_TARGET_OS STREQUAL "darwin")
178180
set(LIBC_TARGET_OS_IS_DARWIN TRUE)
179181
elseif(LIBC_TARGET_OS STREQUAL "windows")
180182
set(LIBC_TARGET_OS_IS_WINDOWS TRUE)
183+
elseif(LIBC_TARGET_OS STREQUAL "gpu")
184+
set(LIBC_TARGET_OS_IS_GPU TRUE)
181185
else()
182186
message(FATAL_ERROR
183187
"Unsupported libc target operating system ${LIBC_TARGET_OS}")

libc/cmake/modules/LLVMLibCCheckMPFR.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ set(LLVM_LIBC_MPFR_INSTALL_PATH "" CACHE PATH "Path to where MPFR is installed (
22

33
if(LLVM_LIBC_MPFR_INSTALL_PATH)
44
set(LIBC_TESTS_CAN_USE_MPFR TRUE)
5-
elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
5+
elseif(LIBC_TARGET_OS_IS_GPU)
66
set(LIBC_TESTS_CAN_USE_MPFR FALSE)
77
else()
88
try_compile(

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,22 @@ function(_get_common_compile_options output_var flags)
8282
list(APPEND compile_options "/EHs-c-")
8383
list(APPEND compile_options "/GR-")
8484
endif()
85-
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
85+
if (LIBC_TARGET_OS_IS_GPU)
8686
list(APPEND compile_options "-nogpulib")
8787
list(APPEND compile_options "-fvisibility=hidden")
8888
list(APPEND compile_options "-fconvergent-functions")
89+
list(APPEND compile_options "-flto")
90+
91+
if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
92+
list(APPEND compile_options "-Wno-unknown-cuda-version")
93+
list(APPEND compile_options "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false")
94+
list(APPEND compile_options "--cuda-feature=+ptx63")
95+
if(LIBC_CUDA_ROOT)
96+
list(APPEND compile_options "--cuda-path=${LIBC_CUDA_ROOT}")
97+
endif()
98+
elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
99+
list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none")
100+
endif()
89101

90102
# Manually disable all standard include paths and include the resource
91103
# directory to prevent system headers from being included.
@@ -138,73 +150,21 @@ function(_get_common_test_compile_options output_var flags)
138150
set(${output_var} ${compile_options} PARENT_SCOPE)
139151
endfunction()
140152

141-
# Obtains NVPTX specific arguments for compilation.
142-
# The PTX feature is primarily based on the CUDA toolchain version. We want to
143-
# be able to target NVPTX without an existing CUDA installation, so we need to
144-
# set this manually. This simply sets the PTX feature to the minimum required
145-
# for the features we wish to use on that target. The minimum PTX features used
146-
# here roughly corresponds to the CUDA 9.0 release.
147-
# Adjust as needed for desired PTX features.
148-
function(get_nvptx_compile_options output_var gpu_arch)
149-
set(nvptx_options "")
150-
list(APPEND nvptx_options "-march=${gpu_arch}")
151-
list(APPEND nvptx_options "-Wno-unknown-cuda-version")
152-
list(APPEND nvptx_options "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false")
153-
if(${gpu_arch} STREQUAL "sm_35")
154-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
155-
elseif(${gpu_arch} STREQUAL "sm_37")
156-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
157-
elseif(${gpu_arch} STREQUAL "sm_50")
158-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
159-
elseif(${gpu_arch} STREQUAL "sm_52")
160-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
161-
elseif(${gpu_arch} STREQUAL "sm_53")
162-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
163-
elseif(${gpu_arch} STREQUAL "sm_60")
164-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
165-
elseif(${gpu_arch} STREQUAL "sm_61")
166-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
167-
elseif(${gpu_arch} STREQUAL "sm_62")
168-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
169-
elseif(${gpu_arch} STREQUAL "sm_70")
170-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
171-
elseif(${gpu_arch} STREQUAL "sm_72")
172-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
173-
elseif(${gpu_arch} STREQUAL "sm_75")
174-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
175-
elseif(${gpu_arch} STREQUAL "sm_80")
176-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
177-
elseif(${gpu_arch} STREQUAL "sm_86")
178-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
179-
elseif(${gpu_arch} STREQUAL "sm_89")
180-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
181-
elseif(${gpu_arch} STREQUAL "sm_90")
182-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
183-
else()
184-
message(FATAL_ERROR "Unknown Nvidia GPU architecture '${gpu_arch}'")
185-
endif()
186-
187-
if(LIBC_CUDA_ROOT)
188-
list(APPEND nvptx_options "--cuda-path=${LIBC_CUDA_ROOT}")
189-
endif()
190-
set(${output_var} ${nvptx_options} PARENT_SCOPE)
191-
endfunction()
192-
193153
function(_get_hermetic_test_compile_options output_var flags)
194154
_get_compile_options_from_flags(compile_flags ${flags})
195155
list(APPEND compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
196156
${flags} -fpie -ffreestanding -fno-exceptions -fno-rtti)
197157

198158
# The GPU build requires overriding the default CMake triple and architecture.
199-
if(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU)
159+
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
200160
list(APPEND compile_options
201161
-nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto
202-
--target=${LIBC_GPU_TARGET_TRIPLE}
203162
-mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION})
204-
elseif(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
205-
get_nvptx_compile_options(nvptx_options ${LIBC_GPU_TARGET_ARCHITECTURE})
163+
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
206164
list(APPEND compile_options
207-
-nogpulib ${nvptx_options} -fno-use-cxa-atexit --target=${LIBC_GPU_TARGET_TRIPLE})
165+
"SHELL:-mllvm -nvptx-emit-init-fini-kernel=false"
166+
--cuda-path=${LIBC_CUDA_ROOT}
167+
-nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit)
208168
endif()
209169
set(${output_var} ${compile_options} PARENT_SCOPE)
210170
endfunction()

libc/cmake/modules/LLVMLibCHeaderRules.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function(add_gen_header target_name)
139139
${hdrgen_deps}
140140
)
141141

142-
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
142+
if(LIBC_TARGET_OS_IS_GPU)
143143
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls)
144144
set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path})
145145
add_custom_command(

0 commit comments

Comments
 (0)