Skip to content

Commit ba8c965

Browse files
authored
[Clang] Do not implicitly link C libraries for the GPU targets (llvm#109052)
Summary: I initially thought that it would be convenient to automatically link these libraries like they are for standard C/C++ targets. However, this created issues when trying to use C++ as a GPU target. This patch moves the logic to now implicitly pass it as part of the offloading toolchain instead, if found. This means that the user needs to set the target toolchain for the link job for automatic detection, but can still be done manually via `-Xoffload-linker -lc`.
1 parent 0d736e2 commit ba8c965

File tree

9 files changed

+25
-29
lines changed

9 files changed

+25
-29
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,6 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
648648
Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
649649
}
650650

651-
addGPULibraries(getToolChain(), Args, CmdArgs);
652-
653651
CmdArgs.push_back("-o");
654652
CmdArgs.push_back(Output.getFilename());
655653
C.addCommand(std::make_unique<Command>(
@@ -1089,4 +1087,4 @@ bool AMDGPUToolChain::shouldSkipSanitizeOption(
10891087
return true;
10901088
}
10911089
return false;
1092-
}
1090+
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9223,6 +9223,25 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92239223
A->claim();
92249224
}
92259225

9226+
// Pass in the C library for GPUs if present and not disabled.
9227+
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r, options::OPT_nogpulib,
9228+
options::OPT_nodefaultlibs, options::OPT_nolibc,
9229+
options::OPT_nogpulibc)) {
9230+
forAllAssociatedToolChains(C, JA, getToolChain(), [&](const ToolChain &TC) {
9231+
// The device C library is only available for NVPTX and AMDGPU targets
9232+
// currently.
9233+
if (!TC.getTriple().isNVPTX() && !TC.getTriple().isAMDGPU())
9234+
return;
9235+
bool HasLibC = TC.getStdlibIncludePath().has_value();
9236+
if (HasLibC) {
9237+
CmdArgs.push_back(Args.MakeArgString(
9238+
"--device-linker=" + TC.getTripleString() + "=" + "-lc"));
9239+
CmdArgs.push_back(Args.MakeArgString(
9240+
"--device-linker=" + TC.getTripleString() + "=" + "-lm"));
9241+
}
9242+
});
9243+
}
9244+
92269245
// If we disable the GPU C library support it needs to be forwarded to the
92279246
// link job.
92289247
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true))

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,6 @@ void tools::addLinkerCompressDebugSectionsOption(
510510
}
511511
}
512512

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

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ 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-
4138
void claimNoWarnArgs(const llvm::opt::ArgList &Args);
4239

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

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,6 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
635635
for (StringRef Feature : Features)
636636
CmdArgs.append({"--feature", Args.MakeArgString(Feature)});
637637

638-
addGPULibraries(getToolChain(), Args, CmdArgs);
639-
640638
// Add paths for the default clang library path.
641639
SmallString<256> DefaultLibPath =
642640
llvm::sys::path::parent_path(TC.getDriver().Dir);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,4 @@
377377
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
378378
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
379379
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
380-
// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-compiler=-nolibc"
380+
// LIBC-GPU-NOT: clang-linker-wrapper{{.*}}"--device-linker"

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ if(LIBC_TARGET_TRIPLE)
2121
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
2222
endif()
2323
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
24-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib -nostdlib")
24+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
2525
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
2626
set(CMAKE_REQUIRED_FLAGS
27-
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument -nostdlib")
27+
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
2828
endif()
2929

3030
# Optionally set up a job pool to limit the number of GPU tests run in parallel.

libcxx/cmake/caches/AMDGPU.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ set(LIBCXX_ADDITIONAL_COMPILE_FLAGS
3333
"-nogpulib;-flto;-fconvergent-functions;-Xclang;-mcode-object-version=none" CACHE STRING "")
3434
set(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS
3535
"-nogpulib;-flto;-fconvergent-functions;-Xclang;-mcode-object-version=none" CACHE STRING "")
36-
set(CMAKE_REQUIRED_FLAGS "-nogpulib -nodefaultlibs" CACHE STRING "")
36+
set(CMAKE_REQUIRED_FLAGS "-nogpulib" CACHE STRING "")

libcxx/cmake/caches/NVPTX.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ set(LIBCXX_ADDITIONAL_COMPILE_FLAGS
3333
"-nogpulib;-flto;-fconvergent-functions;--cuda-feature=+ptx63" CACHE STRING "")
3434
set(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS
3535
"-nogpulib;-flto;-fconvergent-functions;--cuda-feature=+ptx63" CACHE STRING "")
36-
set(CMAKE_REQUIRED_FLAGS "-nogpulib -nodefaultlibs -flto -c" CACHE STRING "")
36+
set(CMAKE_REQUIRED_FLAGS "-nogpulib -flto -c" CACHE STRING "")

0 commit comments

Comments
 (0)