Skip to content

Commit 4f516aa

Browse files
authored
[Clang] Make the GPU toolchains implicitly link -lm and -lc (#98170)
Summary: The previous patches (The other commits in this chain) allow the offloading toolchain to directly invoke the device linker. Because of this, we can now just have the toolchain implicitly include `-lc` and `-lm` like a standard target does. This removes the old handling that went through the fat binary `-lcgpu`.
1 parent adc59f4 commit 4f516aa

File tree

6 files changed

+32
-65
lines changed

6 files changed

+32
-65
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
633633
else if (Args.hasArg(options::OPT_mcpu_EQ))
634634
CmdArgs.push_back(Args.MakeArgString(
635635
"-plugin-opt=mcpu=" + Args.getLastArgValue(options::OPT_mcpu_EQ)));
636+
637+
addGPULibraries(getToolChain(), Args, CmdArgs);
638+
636639
CmdArgs.push_back("-o");
637640
CmdArgs.push_back(Output.getFilename());
638641
C.addCommand(std::make_unique<Command>(

clang/lib/Driver/ToolChains/Clang.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -9111,6 +9111,11 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
91119111
A->claim();
91129112
}
91139113

9114+
// If we disable the GPU C library support it needs to be forwarded to the
9115+
// link job.
9116+
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true))
9117+
CmdArgs.push_back("--device-linker=-nolibc");
9118+
91149119
// Add the linker arguments to be forwarded by the wrapper.
91159120
CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") +
91169121
LinkCommand->getExecutable()));

clang/lib/Driver/ToolChains/CommonArgs.cpp

+17-39
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,22 @@ void tools::addLinkerCompressDebugSectionsOption(
509509
}
510510
}
511511

512+
void tools::addGPULibraries(const ToolChain &TC, const llvm::opt::ArgList &Args,
513+
llvm::opt::ArgStringList &CmdArgs) {
514+
if (Args.hasArg(options::OPT_nostdlib, options::OPT_r,
515+
options::OPT_nodefaultlibs, options::OPT_nolibc,
516+
options::OPT_nogpulibc))
517+
return;
518+
519+
// If the user's toolchain has the 'include/<triple>/` path, we assume it
520+
// supports the standard C libraries for the GPU and include them.
521+
bool HasLibC = TC.getStdlibIncludePath().has_value();
522+
if (HasLibC) {
523+
CmdArgs.push_back("-lc");
524+
CmdArgs.push_back("-lm");
525+
}
526+
}
527+
512528
void tools::AddTargetFeature(const ArgList &Args,
513529
std::vector<StringRef> &Features,
514530
OptSpecifier OnOpt, OptSpecifier OffOpt,
@@ -1145,42 +1161,6 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11451161
}
11461162
}
11471163

1148-
/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the
1149-
/// LLVM C library for GPUs.
1150-
static void addOpenMPDeviceLibC(const Compilation &C, const ArgList &Args,
1151-
ArgStringList &CmdArgs) {
1152-
if (Args.hasArg(options::OPT_nogpulib) || Args.hasArg(options::OPT_nolibc))
1153-
return;
1154-
1155-
// Check the resource directory for the LLVM libc GPU declarations. If it's
1156-
// found we can assume that LLVM was built with support for the GPU libc.
1157-
SmallString<256> LibCDecls(C.getDriver().ResourceDir);
1158-
llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers",
1159-
"llvm-libc-decls");
1160-
bool HasLibC = llvm::sys::fs::exists(LibCDecls) &&
1161-
llvm::sys::fs::is_directory(LibCDecls);
1162-
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC))
1163-
return;
1164-
1165-
SmallVector<const ToolChain *> ToolChains;
1166-
auto TCRange = C.getOffloadToolChains(Action::OFK_OpenMP);
1167-
for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
1168-
ToolChains.push_back(TI->second);
1169-
1170-
if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
1171-
return TC->getTriple().isAMDGPU();
1172-
})) {
1173-
CmdArgs.push_back("-lcgpu-amdgpu");
1174-
CmdArgs.push_back("-lmgpu-amdgpu");
1175-
}
1176-
if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
1177-
return TC->getTriple().isNVPTX();
1178-
})) {
1179-
CmdArgs.push_back("-lcgpu-nvptx");
1180-
CmdArgs.push_back("-lmgpu-nvptx");
1181-
}
1182-
}
1183-
11841164
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
11851165
const ArgList &Args,
11861166
ArgStringList &CmdArgs) {
@@ -1253,10 +1233,8 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
12531233
if (IsOffloadingHost && !Args.hasArg(options::OPT_nogpulib))
12541234
CmdArgs.push_back("-lomptarget.devicertl");
12551235

1256-
if (IsOffloadingHost)
1257-
addOpenMPDeviceLibC(C, Args, CmdArgs);
1258-
12591236
addArchSpecificRPath(TC, Args, CmdArgs);
1237+
12601238
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
12611239

12621240
return true;

clang/lib/Driver/ToolChains/CommonArgs.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
3535
const llvm::opt::ArgList &Args,
3636
llvm::opt::ArgStringList &CmdArgs);
3737

38+
void addGPULibraries(const ToolChain &TC, const llvm::opt::ArgList &Args,
39+
llvm::opt::ArgStringList &CmdArgs);
40+
3841
void claimNoWarnArgs(const llvm::opt::ArgList &Args);
3942

4043
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,

clang/lib/Driver/ToolChains/Cuda.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
625625
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
626626
C.getDriver().getLTOMode() == LTOK_Thin);
627627

628+
addGPULibraries(getToolChain(), Args, CmdArgs);
629+
628630
// Add paths for the default clang library path.
629631
SmallString<256> DefaultLibPath =
630632
llvm::sys::path::parent_path(TC.getDriver().Dir);

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

+2-26
Original file line numberDiff line numberDiff line change
@@ -372,33 +372,9 @@
372372
// XARCH-DEVICE: "-cc1" "-triple" "nvptx64-nvidia-cuda"{{.*}}"-O3"
373373
// XARCH-DEVICE-NOT: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-O3"
374374

375-
//
376-
// Check that `-gpulibc` includes the LLVM C libraries for the GPU.
377-
//
378-
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
379-
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
380-
// RUN: --libomptarget-amdgpu-bc-path=%S/Inputs/hip_dev_lib/libomptarget-amdgpu-gfx803.bc \
381-
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
382-
// RUN: --rocm-path=%S/Inputs/rocm \
383-
// RUN: --offload-arch=sm_52,gfx803 -gpulibc -nogpuinc %s 2>&1 \
384-
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
385-
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
386-
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
387-
// RUN: --libomptarget-amdgpu-bc-path=%S/Inputs/hip_dev_lib/libomptarget-amdgpu-gfx803.bc \
388-
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
389-
// RUN: --rocm-path=%S/Inputs/rocm \
390-
// RUN: -Xopenmp-target=nvptx64-nvidia-cuda -march=sm_52 \
391-
// RUN: -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx803 \
392-
// RUN: -fopenmp-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -gpulibc -nogpuinc %s 2>&1 \
393-
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
394-
// LIBC-GPU-DAG: "-lcgpu-amdgpu"
395-
// LIBC-GPU-DAG: "-lmgpu-amdgpu"
396-
// LIBC-GPU-DAG: "-lcgpu-nvptx"
397-
// LIBC-GPU-DAG: "-lmgpu-nvptx"
398-
399375
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
400376
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
401377
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
402378
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
403-
// RUN: | FileCheck --check-prefix=NO-LIBC-GPU %s
404-
// NO-LIBC-GPU-NOT: -lmgpu{{.*}}-lcgpu
379+
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
380+
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-linker=-nolibc"

0 commit comments

Comments
 (0)