[clang] [OpenMP] Use loaded offloading toolchains to add libraries (PR #87108)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 29 13:30:01 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Joseph Huber (jhuber6)
<details>
<summary>Changes</summary>
Summary:
We want to pass these GPU libraries by default if a certain offloading
toolchain is loaded for OpenMP. Previously I parsed this from the
arguments because it's only available in the compilation. This doesn't
really work for `native` and it's extra effort, so this patch just
passes in the `Compilation` as an extr argument and uses that. Tests
should be unaffected.
---
Full diff: https://github.com/llvm/llvm-project/pull/87108.diff
10 Files Affected:
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+51-64)
- (modified) clang/lib/Driver/ToolChains/CommonArgs.h (+2-2)
- (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/DragonFly.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/FreeBSD.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/Haiku.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/NetBSD.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/OpenBSD.cpp (+1-1)
- (modified) clang/lib/Driver/ToolChains/Solaris.cpp (+1-1)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index ace4fb99581e38..60d14762ba37ba 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -876,7 +876,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
// LowerMatrixIntrinsicsPass, which is transitively called by
// buildThinLTODefaultPipeline under EnableMatrix.
if ((IsThinLTO || IsFatLTO || IsUnifiedLTO) &&
- Args.hasArg(options::OPT_fenable_matrix))
+ Args.hasArg(options::OPT_fenable_matrix))
CmdArgs.push_back(
Args.MakeArgString(Twine(PluginOptPrefix) + "-enable-matrix"));
@@ -1075,14 +1075,14 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the
/// LLVM C library for GPUs.
-static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
+static void addOpenMPDeviceLibC(const Compilation &C, const ArgList &Args,
ArgStringList &CmdArgs) {
if (Args.hasArg(options::OPT_nogpulib) || Args.hasArg(options::OPT_nolibc))
return;
// Check the resource directory for the LLVM libc GPU declarations. If it's
// found we can assume that LLVM was built with support for the GPU libc.
- SmallString<256> LibCDecls(TC.getDriver().ResourceDir);
+ SmallString<256> LibCDecls(C.getDriver().ResourceDir);
llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers",
"llvm-libc-decls");
bool HasLibC = llvm::sys::fs::exists(LibCDecls) &&
@@ -1090,38 +1090,23 @@ static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC))
return;
- // We don't have access to the offloading toolchains here, so determine from
- // the arguments if we have any active NVPTX or AMDGPU toolchains.
- llvm::DenseSet<const char *> Libraries;
- if (const Arg *Targets = Args.getLastArg(options::OPT_fopenmp_targets_EQ)) {
- if (llvm::any_of(Targets->getValues(),
- [](auto S) { return llvm::Triple(S).isAMDGPU(); })) {
- Libraries.insert("-lcgpu-amdgpu");
- Libraries.insert("-lmgpu-amdgpu");
- }
- if (llvm::any_of(Targets->getValues(),
- [](auto S) { return llvm::Triple(S).isNVPTX(); })) {
- Libraries.insert("-lcgpu-nvptx");
- Libraries.insert("-lmgpu-nvptx");
- }
- }
+ SmallVector<const ToolChain *> ToolChains;
+ auto TCRange = C.getOffloadToolChains(Action::OFK_OpenMP);
+ for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
+ ToolChains.push_back(TI->second);
- for (StringRef Arch : Args.getAllArgValues(options::OPT_offload_arch_EQ)) {
- if (llvm::any_of(llvm::split(Arch, ","), [](StringRef Str) {
- return IsAMDGpuArch(StringToCudaArch(Str));
- })) {
- Libraries.insert("-lcgpu-amdgpu");
- Libraries.insert("-lmgpu-amdgpu");
- }
- if (llvm::any_of(llvm::split(Arch, ","), [](StringRef Str) {
- return IsNVIDIAGpuArch(StringToCudaArch(Str));
- })) {
- Libraries.insert("-lcgpu-nvptx");
- Libraries.insert("-lmgpu-nvptx");
- }
+ if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
+ return TC->getTriple().isAMDGPU();
+ })) {
+ CmdArgs.push_back("-lcgpu-amdgpu");
+ CmdArgs.push_back("-lmgpu-amdgpu");
+ }
+ if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
+ return TC->getTriple().isNVPTX();
+ })) {
+ CmdArgs.push_back("-lcgpu-nvptx");
+ CmdArgs.push_back("-lmgpu-nvptx");
}
-
- llvm::append_range(CmdArgs, Libraries);
}
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
@@ -1153,9 +1138,10 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
}
}
-bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
- const ArgList &Args, bool ForceStaticHostRuntime,
- bool IsOffloadingHost, bool GompNeedsRT) {
+bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
+ const ToolChain &TC, const ArgList &Args,
+ bool ForceStaticHostRuntime, bool IsOffloadingHost,
+ bool GompNeedsRT) {
if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
options::OPT_fno_openmp, false))
return false;
@@ -1187,7 +1173,7 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
CmdArgs.push_back("-Bdynamic");
if (RTKind == Driver::OMPRT_GOMP && GompNeedsRT)
- CmdArgs.push_back("-lrt");
+ CmdArgs.push_back("-lrt");
if (IsOffloadingHost)
CmdArgs.push_back("-lomptarget");
@@ -1196,7 +1182,7 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
CmdArgs.push_back("-lomptarget.devicertl");
if (IsOffloadingHost)
- addOpenMPDeviceLibC(TC, Args, CmdArgs);
+ addOpenMPDeviceLibC(C, Args, CmdArgs);
addArchSpecificRPath(TC, Args, CmdArgs);
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
@@ -1357,10 +1343,12 @@ static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
bool IsShared, bool IsWhole) {
// Wrap any static runtimes that must be forced into executable in
// whole-archive.
- if (IsWhole) CmdArgs.push_back("--whole-archive");
+ if (IsWhole)
+ CmdArgs.push_back("--whole-archive");
CmdArgs.push_back(TC.getCompilerRTArgString(
Args, Sanitizer, IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static));
- if (IsWhole) CmdArgs.push_back("--no-whole-archive");
+ if (IsWhole)
+ CmdArgs.push_back("--no-whole-archive");
if (IsShared) {
addArchSpecificRPath(TC, Args, CmdArgs);
@@ -1427,8 +1415,7 @@ void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
TC.getTriple().getOS() != llvm::Triple::RTEMS)
CmdArgs.push_back("-ldl");
// Required for backtrace on some OSes
- if (TC.getTriple().isOSFreeBSD() ||
- TC.getTriple().isOSNetBSD() ||
+ if (TC.getTriple().isOSFreeBSD() || TC.getTriple().isOSNetBSD() ||
TC.getTriple().isOSOpenBSD())
CmdArgs.push_back("-lexecinfo");
// There is no libresolv on Android, FreeBSD, OpenBSD, etc. On musl
@@ -1640,7 +1627,8 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty();
}
-bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
+bool tools::addXRayRuntime(const ToolChain &TC, const ArgList &Args,
+ ArgStringList &CmdArgs) {
if (Args.hasArg(options::OPT_shared))
return false;
@@ -1665,8 +1653,7 @@ void tools::linkXRayRuntimeDeps(const ToolChain &TC,
CmdArgs.push_back("-lrt");
CmdArgs.push_back("-lm");
- if (!TC.getTriple().isOSFreeBSD() &&
- !TC.getTriple().isOSNetBSD() &&
+ if (!TC.getTriple().isOSFreeBSD() && !TC.getTriple().isOSNetBSD() &&
!TC.getTriple().isOSOpenBSD())
CmdArgs.push_back("-ldl");
}
@@ -1957,19 +1944,19 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
bool EmbeddedPISupported;
switch (Triple.getArch()) {
- case llvm::Triple::arm:
- case llvm::Triple::armeb:
- case llvm::Triple::thumb:
- case llvm::Triple::thumbeb:
- EmbeddedPISupported = true;
- break;
- default:
- EmbeddedPISupported = false;
- break;
+ case llvm::Triple::arm:
+ case llvm::Triple::armeb:
+ case llvm::Triple::thumb:
+ case llvm::Triple::thumbeb:
+ EmbeddedPISupported = true;
+ break;
+ default:
+ EmbeddedPISupported = false;
+ break;
}
bool ROPI = false, RWPI = false;
- Arg* LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi);
+ Arg *LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi);
if (LastROPIArg && LastROPIArg->getOption().matches(options::OPT_fropi)) {
if (!EmbeddedPISupported)
ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target)
@@ -1998,7 +1985,7 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
if (ABIName == "n64")
PIC = true;
// When targettng MIPS with -mno-abicalls, it's always static.
- if(Args.hasArg(options::OPT_mno_abicalls))
+ if (Args.hasArg(options::OPT_mno_abicalls))
return std::make_tuple(llvm::Reloc::Static, 0U, false);
// Unlike other architectures, MIPS, even with -fPIC/-mxgot/multigot,
// does not use PIC level 2 for historical reasons.
@@ -2160,7 +2147,8 @@ enum class LibGccType { UnspecifiedLibGcc, StaticLibGcc, SharedLibGcc };
static LibGccType getLibGccType(const ToolChain &TC, const Driver &D,
const ArgList &Args) {
if (Args.hasArg(options::OPT_static_libgcc) ||
- Args.hasArg(options::OPT_static) || Args.hasArg(options::OPT_static_pie) ||
+ Args.hasArg(options::OPT_static) ||
+ Args.hasArg(options::OPT_static_pie) ||
// The Android NDK only provides libunwind.a, not libunwind.so.
TC.getTriple().isAndroid())
return LibGccType::StaticLibGcc;
@@ -2528,11 +2516,10 @@ static void GetSDLFromOffloadArchive(
return;
StringRef Prefix = isBitCodeSDL ? "libbc-" : "lib";
- std::string OutputLib =
- D.GetTemporaryPath(Twine(Prefix + llvm::sys::path::filename(Lib) + "-" +
- Arch + "-" + Target)
- .str(),
- "a");
+ std::string OutputLib = D.GetTemporaryPath(
+ Twine(Prefix + llvm::sys::path::filename(Lib) + "-" + Arch + "-" + Target)
+ .str(),
+ "a");
C.addTempFile(C.getArgs().MakeArgString(OutputLib));
@@ -2745,8 +2732,8 @@ void tools::addMachineOutlinerArgs(const Driver &D,
}
};
- if (Arg *A = Args.getLastArg(options::OPT_moutline,
- options::OPT_mno_outline)) {
+ if (Arg *A =
+ Args.getLastArg(options::OPT_moutline, options::OPT_mno_outline)) {
if (A->getOption().matches(options::OPT_moutline)) {
// We only support -moutline in AArch64 and ARM targets right now. If
// we're not compiling for these, emit a warning and ignore the flag.
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index bb37be4bd6ea05..5581905db31144 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -111,8 +111,8 @@ void addOpenMPRuntimeLibraryPath(const ToolChain &TC,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
/// Returns true, if an OpenMP runtime has been added.
-bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
- const llvm::opt::ArgList &Args,
+bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs,
+ const ToolChain &TC, const llvm::opt::ArgList &Args,
bool ForceStaticHostRuntime = false,
bool IsOffloadingHost = false, bool GompNeedsRT = false);
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index c7682c7f1d3379..caf6c4a444fdce 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -686,7 +686,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
- addOpenMPRuntime(CmdArgs, getToolChain(), Args);
+ addOpenMPRuntime(C, CmdArgs, getToolChain(), Args);
if (isObjCRuntimeLinked(Args) &&
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
diff --git a/clang/lib/Driver/ToolChains/DragonFly.cpp b/clang/lib/Driver/ToolChains/DragonFly.cpp
index b59a172bd6ae86..1dbc46763c1156 100644
--- a/clang/lib/Driver/ToolChains/DragonFly.cpp
+++ b/clang/lib/Driver/ToolChains/DragonFly.cpp
@@ -136,7 +136,7 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX()) {
if (ToolChain.ShouldLinkCXXStdlib(Args))
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index c5757ddebb0f3e..a8ee6540001ee4 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -295,7 +295,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
!Args.hasArg(options::OPT_static);
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX()) {
if (ToolChain.ShouldLinkCXXStdlib(Args))
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index a9c9d2475809d7..dedbfac6cb25d2 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -598,7 +598,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// FIXME: Only pass GompNeedsRT = true for platforms with libgomp that
// require librt. Most modern Linux platforms do, but some may not.
- if (addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP,
+ if (addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP,
JA.isHostOffloading(Action::OFK_OpenMP),
/* GompNeedsRT= */ true))
// OpenMP runtimes implies pthreads when using the GNU toolchain.
diff --git a/clang/lib/Driver/ToolChains/Haiku.cpp b/clang/lib/Driver/ToolChains/Haiku.cpp
index 30464e2229e65b..346652a7e4bd8e 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -107,7 +107,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_r)) {
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 0eec8fddabd5db..d54f2288294949 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -311,7 +311,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_r)) {
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX()) {
if (ToolChain.ShouldLinkCXXStdlib(Args))
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 6da6728585df93..e20d9fb1cfc417 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -221,7 +221,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_r)) {
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX()) {
if (ToolChain.ShouldLinkCXXStdlib(Args))
diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp
index 5d7f0ae2a392a6..7126e018ca5b6f 100644
--- a/clang/lib/Driver/ToolChains/Solaris.cpp
+++ b/clang/lib/Driver/ToolChains/Solaris.cpp
@@ -211,7 +211,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// Use the static OpenMP runtime with -static-openmp
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
!Args.hasArg(options::OPT_static);
- addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+ addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
if (D.CCCIsCXX()) {
if (ToolChain.ShouldLinkCXXStdlib(Args))
``````````
</details>
https://github.com/llvm/llvm-project/pull/87108
More information about the cfe-commits
mailing list