[libcxx-commits] [clang] [libc] [libcxx] [Clang] Do not implicitly link C libraries for the GPU targets (PR #109052)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 17 14:14:34 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Joseph Huber (jhuber6)
<details>
<summary>Changes</summary>
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`.
---
Full diff: https://github.com/llvm/llvm-project/pull/109052.diff
9 Files Affected:
- (modified) clang/lib/Driver/ToolChains/AMDGPU.cpp (+1-3)
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+19)
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (-16)
- (modified) clang/lib/Driver/ToolChains/CommonArgs.h (-3)
- (modified) clang/lib/Driver/ToolChains/Cuda.cpp (-2)
- (modified) clang/test/Driver/openmp-offload-gpu.c (+1-1)
- (modified) libc/cmake/modules/prepare_libc_gpu_build.cmake (+2-2)
- (modified) libcxx/cmake/caches/AMDGPU.cmake (+1-1)
- (modified) libcxx/cmake/caches/NVPTX.cmake (+1-1)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 74f70573c5feb8..2c85d21ebd738c 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -648,8 +648,6 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
}
- addGPULibraries(getToolChain(), Args, CmdArgs);
-
CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename());
C.addCommand(std::make_unique<Command>(
@@ -1089,4 +1087,4 @@ bool AMDGPUToolChain::shouldSkipSanitizeOption(
return true;
}
return false;
-}
\ No newline at end of file
+}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 3fe4ce5d893b8d..196330e74e8392 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9223,6 +9223,25 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
A->claim();
}
+ // Pass in the C library for GPUs if present and not disabled.
+ if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r, options::OPT_nogpulib,
+ options::OPT_nodefaultlibs, options::OPT_nolibc,
+ options::OPT_nogpulibc)) {
+ forAllAssociatedToolChains(C, JA, getToolChain(), [&](const ToolChain &TC) {
+ // The device C library is only available for NVPTX and AMDGPU targets
+ // currently.
+ if (!TC.getTriple().isNVPTX() && !TC.getTriple().isAMDGPU())
+ return;
+ bool HasLibC = TC.getStdlibIncludePath().has_value();
+ if (HasLibC) {
+ CmdArgs.push_back(Args.MakeArgString(
+ "--device-linker=" + TC.getTripleString() + "=" + "-lc"));
+ CmdArgs.push_back(Args.MakeArgString(
+ "--device-linker=" + TC.getTripleString() + "=" + "-lm"));
+ }
+ });
+ }
+
// If we disable the GPU C library support it needs to be forwarded to the
// link job.
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true))
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 502aba2ce4aa9c..043d9e48764439 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -510,22 +510,6 @@ void tools::addLinkerCompressDebugSectionsOption(
}
}
-void tools::addGPULibraries(const ToolChain &TC, const llvm::opt::ArgList &Args,
- llvm::opt::ArgStringList &CmdArgs) {
- if (Args.hasArg(options::OPT_nostdlib, options::OPT_r,
- options::OPT_nodefaultlibs, options::OPT_nolibc,
- options::OPT_nogpulibc))
- return;
-
- // If the user's toolchain has the 'include/<triple>/` path, we assume it
- // supports the standard C libraries for the GPU and include them.
- bool HasLibC = TC.getStdlibIncludePath().has_value();
- if (HasLibC) {
- CmdArgs.push_back("-lc");
- CmdArgs.push_back("-lm");
- }
-}
-
void tools::AddTargetFeature(const ArgList &Args,
std::vector<StringRef> &Features,
OptSpecifier OnOpt, OptSpecifier OffOpt,
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index 0c97398dfcfa34..8695d3fe5b55b8 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -35,9 +35,6 @@ void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
-void addGPULibraries(const ToolChain &TC, const llvm::opt::ArgList &Args,
- llvm::opt::ArgStringList &CmdArgs);
-
void claimNoWarnArgs(const llvm::opt::ArgList &Args);
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index ef44ffa5594daf..509cd87b28c37e 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -635,8 +635,6 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
for (StringRef Feature : Features)
CmdArgs.append({"--feature", Args.MakeArgString(Feature)});
- addGPULibraries(getToolChain(), Args, CmdArgs);
-
// Add paths for the default clang library path.
SmallString<256> DefaultLibPath =
llvm::sys::path::parent_path(TC.getDriver().Dir);
diff --git a/clang/test/Driver/openmp-offload-gpu.c b/clang/test/Driver/openmp-offload-gpu.c
index ef6cbdded6a6f2..f6e2245dcdbc05 100644
--- a/clang/test/Driver/openmp-offload-gpu.c
+++ b/clang/test/Driver/openmp-offload-gpu.c
@@ -377,4 +377,4 @@
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
-// LIBC-GPU: clang-linker-wrapper{{.*}}"--device-compiler=-nolibc"
+// LIBC-GPU-NOT: clang-linker-wrapper{{.*}}"--device-linker"
diff --git a/libc/cmake/modules/prepare_libc_gpu_build.cmake b/libc/cmake/modules/prepare_libc_gpu_build.cmake
index 14ae8f6e9eecd7..e20591b80e6f29 100644
--- a/libc/cmake/modules/prepare_libc_gpu_build.cmake
+++ b/libc/cmake/modules/prepare_libc_gpu_build.cmake
@@ -21,10 +21,10 @@ if(LIBC_TARGET_TRIPLE)
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
endif()
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
- set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib -nostdlib")
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
set(CMAKE_REQUIRED_FLAGS
- "${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument -nostdlib")
+ "${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
endif()
# Optionally set up a job pool to limit the number of GPU tests run in parallel.
diff --git a/libcxx/cmake/caches/AMDGPU.cmake b/libcxx/cmake/caches/AMDGPU.cmake
index 0cd2eebfb9c16a..7443470b2e8a8e 100644
--- a/libcxx/cmake/caches/AMDGPU.cmake
+++ b/libcxx/cmake/caches/AMDGPU.cmake
@@ -33,4 +33,4 @@ set(LIBCXX_ADDITIONAL_COMPILE_FLAGS
"-nogpulib;-flto;-fconvergent-functions;-Xclang;-mcode-object-version=none" CACHE STRING "")
set(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS
"-nogpulib;-flto;-fconvergent-functions;-Xclang;-mcode-object-version=none" CACHE STRING "")
-set(CMAKE_REQUIRED_FLAGS "-nogpulib -nodefaultlibs" CACHE STRING "")
+set(CMAKE_REQUIRED_FLAGS "-nogpulib" CACHE STRING "")
diff --git a/libcxx/cmake/caches/NVPTX.cmake b/libcxx/cmake/caches/NVPTX.cmake
index 47a24a349e996e..3685ddcbb66624 100644
--- a/libcxx/cmake/caches/NVPTX.cmake
+++ b/libcxx/cmake/caches/NVPTX.cmake
@@ -33,4 +33,4 @@ set(LIBCXX_ADDITIONAL_COMPILE_FLAGS
"-nogpulib;-flto;-fconvergent-functions;--cuda-feature=+ptx63" CACHE STRING "")
set(LIBCXXABI_ADDITIONAL_COMPILE_FLAGS
"-nogpulib;-flto;-fconvergent-functions;--cuda-feature=+ptx63" CACHE STRING "")
-set(CMAKE_REQUIRED_FLAGS "-nogpulib -nodefaultlibs -flto -c" CACHE STRING "")
+set(CMAKE_REQUIRED_FLAGS "-nogpulib -flto -c" CACHE STRING "")
``````````
</details>
https://github.com/llvm/llvm-project/pull/109052
More information about the libcxx-commits
mailing list