Skip to content

Commit 46e96f6

Browse files
committed
[OpenMP] Use loaded offloading toolchains to add libraries
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.
1 parent 9937952 commit 46e96f6

File tree

10 files changed

+61
-74
lines changed

10 files changed

+61
-74
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
876876
// LowerMatrixIntrinsicsPass, which is transitively called by
877877
// buildThinLTODefaultPipeline under EnableMatrix.
878878
if ((IsThinLTO || IsFatLTO || IsUnifiedLTO) &&
879-
Args.hasArg(options::OPT_fenable_matrix))
879+
Args.hasArg(options::OPT_fenable_matrix))
880880
CmdArgs.push_back(
881881
Args.MakeArgString(Twine(PluginOptPrefix) + "-enable-matrix"));
882882

@@ -1075,53 +1075,38 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
10751075

10761076
/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the
10771077
/// LLVM C library for GPUs.
1078-
static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
1078+
static void addOpenMPDeviceLibC(const Compilation &C, const ArgList &Args,
10791079
ArgStringList &CmdArgs) {
10801080
if (Args.hasArg(options::OPT_nogpulib) || Args.hasArg(options::OPT_nolibc))
10811081
return;
10821082

10831083
// Check the resource directory for the LLVM libc GPU declarations. If it's
10841084
// found we can assume that LLVM was built with support for the GPU libc.
1085-
SmallString<256> LibCDecls(TC.getDriver().ResourceDir);
1085+
SmallString<256> LibCDecls(C.getDriver().ResourceDir);
10861086
llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers",
10871087
"llvm-libc-decls");
10881088
bool HasLibC = llvm::sys::fs::exists(LibCDecls) &&
10891089
llvm::sys::fs::is_directory(LibCDecls);
10901090
if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC))
10911091
return;
10921092

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-
}
1107-
}
1093+
SmallVector<const ToolChain *> ToolChains;
1094+
auto TCRange = C.getOffloadToolChains(Action::OFK_OpenMP);
1095+
for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
1096+
ToolChains.push_back(TI->second);
11081097

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-
}
1098+
if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
1099+
return TC->getTriple().isAMDGPU();
1100+
})) {
1101+
CmdArgs.push_back("-lcgpu-amdgpu");
1102+
CmdArgs.push_back("-lmgpu-amdgpu");
1103+
}
1104+
if (llvm::any_of(ToolChains, [](const ToolChain *TC) {
1105+
return TC->getTriple().isNVPTX();
1106+
})) {
1107+
CmdArgs.push_back("-lcgpu-nvptx");
1108+
CmdArgs.push_back("-lmgpu-nvptx");
11221109
}
1123-
1124-
llvm::append_range(CmdArgs, Libraries);
11251110
}
11261111

11271112
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
@@ -1153,9 +1138,10 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
11531138
}
11541139
}
11551140

1156-
bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
1157-
const ArgList &Args, bool ForceStaticHostRuntime,
1158-
bool IsOffloadingHost, bool GompNeedsRT) {
1141+
bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
1142+
const ToolChain &TC, const ArgList &Args,
1143+
bool ForceStaticHostRuntime, bool IsOffloadingHost,
1144+
bool GompNeedsRT) {
11591145
if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
11601146
options::OPT_fno_openmp, false))
11611147
return false;
@@ -1187,7 +1173,7 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
11871173
CmdArgs.push_back("-Bdynamic");
11881174

11891175
if (RTKind == Driver::OMPRT_GOMP && GompNeedsRT)
1190-
CmdArgs.push_back("-lrt");
1176+
CmdArgs.push_back("-lrt");
11911177

11921178
if (IsOffloadingHost)
11931179
CmdArgs.push_back("-lomptarget");
@@ -1196,7 +1182,7 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
11961182
CmdArgs.push_back("-lomptarget.devicertl");
11971183

11981184
if (IsOffloadingHost)
1199-
addOpenMPDeviceLibC(TC, Args, CmdArgs);
1185+
addOpenMPDeviceLibC(C, Args, CmdArgs);
12001186

12011187
addArchSpecificRPath(TC, Args, CmdArgs);
12021188
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
@@ -1357,10 +1343,12 @@ static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
13571343
bool IsShared, bool IsWhole) {
13581344
// Wrap any static runtimes that must be forced into executable in
13591345
// whole-archive.
1360-
if (IsWhole) CmdArgs.push_back("--whole-archive");
1346+
if (IsWhole)
1347+
CmdArgs.push_back("--whole-archive");
13611348
CmdArgs.push_back(TC.getCompilerRTArgString(
13621349
Args, Sanitizer, IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static));
1363-
if (IsWhole) CmdArgs.push_back("--no-whole-archive");
1350+
if (IsWhole)
1351+
CmdArgs.push_back("--no-whole-archive");
13641352

13651353
if (IsShared) {
13661354
addArchSpecificRPath(TC, Args, CmdArgs);
@@ -1427,8 +1415,7 @@ void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
14271415
TC.getTriple().getOS() != llvm::Triple::RTEMS)
14281416
CmdArgs.push_back("-ldl");
14291417
// Required for backtrace on some OSes
1430-
if (TC.getTriple().isOSFreeBSD() ||
1431-
TC.getTriple().isOSNetBSD() ||
1418+
if (TC.getTriple().isOSFreeBSD() || TC.getTriple().isOSNetBSD() ||
14321419
TC.getTriple().isOSOpenBSD())
14331420
CmdArgs.push_back("-lexecinfo");
14341421
// There is no libresolv on Android, FreeBSD, OpenBSD, etc. On musl
@@ -1640,7 +1627,8 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
16401627
return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty();
16411628
}
16421629

1643-
bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
1630+
bool tools::addXRayRuntime(const ToolChain &TC, const ArgList &Args,
1631+
ArgStringList &CmdArgs) {
16441632
if (Args.hasArg(options::OPT_shared))
16451633
return false;
16461634

@@ -1665,8 +1653,7 @@ void tools::linkXRayRuntimeDeps(const ToolChain &TC,
16651653
CmdArgs.push_back("-lrt");
16661654
CmdArgs.push_back("-lm");
16671655

1668-
if (!TC.getTriple().isOSFreeBSD() &&
1669-
!TC.getTriple().isOSNetBSD() &&
1656+
if (!TC.getTriple().isOSFreeBSD() && !TC.getTriple().isOSNetBSD() &&
16701657
!TC.getTriple().isOSOpenBSD())
16711658
CmdArgs.push_back("-ldl");
16721659
}
@@ -1957,19 +1944,19 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
19571944

19581945
bool EmbeddedPISupported;
19591946
switch (Triple.getArch()) {
1960-
case llvm::Triple::arm:
1961-
case llvm::Triple::armeb:
1962-
case llvm::Triple::thumb:
1963-
case llvm::Triple::thumbeb:
1964-
EmbeddedPISupported = true;
1965-
break;
1966-
default:
1967-
EmbeddedPISupported = false;
1968-
break;
1947+
case llvm::Triple::arm:
1948+
case llvm::Triple::armeb:
1949+
case llvm::Triple::thumb:
1950+
case llvm::Triple::thumbeb:
1951+
EmbeddedPISupported = true;
1952+
break;
1953+
default:
1954+
EmbeddedPISupported = false;
1955+
break;
19691956
}
19701957

19711958
bool ROPI = false, RWPI = false;
1972-
Arg* LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi);
1959+
Arg *LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi);
19731960
if (LastROPIArg && LastROPIArg->getOption().matches(options::OPT_fropi)) {
19741961
if (!EmbeddedPISupported)
19751962
ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target)
@@ -1998,7 +1985,7 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
19981985
if (ABIName == "n64")
19991986
PIC = true;
20001987
// When targettng MIPS with -mno-abicalls, it's always static.
2001-
if(Args.hasArg(options::OPT_mno_abicalls))
1988+
if (Args.hasArg(options::OPT_mno_abicalls))
20021989
return std::make_tuple(llvm::Reloc::Static, 0U, false);
20031990
// Unlike other architectures, MIPS, even with -fPIC/-mxgot/multigot,
20041991
// does not use PIC level 2 for historical reasons.
@@ -2160,7 +2147,8 @@ enum class LibGccType { UnspecifiedLibGcc, StaticLibGcc, SharedLibGcc };
21602147
static LibGccType getLibGccType(const ToolChain &TC, const Driver &D,
21612148
const ArgList &Args) {
21622149
if (Args.hasArg(options::OPT_static_libgcc) ||
2163-
Args.hasArg(options::OPT_static) || Args.hasArg(options::OPT_static_pie) ||
2150+
Args.hasArg(options::OPT_static) ||
2151+
Args.hasArg(options::OPT_static_pie) ||
21642152
// The Android NDK only provides libunwind.a, not libunwind.so.
21652153
TC.getTriple().isAndroid())
21662154
return LibGccType::StaticLibGcc;
@@ -2528,11 +2516,10 @@ static void GetSDLFromOffloadArchive(
25282516
return;
25292517

25302518
StringRef Prefix = isBitCodeSDL ? "libbc-" : "lib";
2531-
std::string OutputLib =
2532-
D.GetTemporaryPath(Twine(Prefix + llvm::sys::path::filename(Lib) + "-" +
2533-
Arch + "-" + Target)
2534-
.str(),
2535-
"a");
2519+
std::string OutputLib = D.GetTemporaryPath(
2520+
Twine(Prefix + llvm::sys::path::filename(Lib) + "-" + Arch + "-" + Target)
2521+
.str(),
2522+
"a");
25362523

25372524
C.addTempFile(C.getArgs().MakeArgString(OutputLib));
25382525

@@ -2745,8 +2732,8 @@ void tools::addMachineOutlinerArgs(const Driver &D,
27452732
}
27462733
};
27472734

2748-
if (Arg *A = Args.getLastArg(options::OPT_moutline,
2749-
options::OPT_mno_outline)) {
2735+
if (Arg *A =
2736+
Args.getLastArg(options::OPT_moutline, options::OPT_mno_outline)) {
27502737
if (A->getOption().matches(options::OPT_moutline)) {
27512738
// We only support -moutline in AArch64 and ARM targets right now. If
27522739
// we're not compiling for these, emit a warning and ignore the flag.

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ void addOpenMPRuntimeLibraryPath(const ToolChain &TC,
111111
const llvm::opt::ArgList &Args,
112112
llvm::opt::ArgStringList &CmdArgs);
113113
/// Returns true, if an OpenMP runtime has been added.
114-
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
115-
const llvm::opt::ArgList &Args,
114+
bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs,
115+
const ToolChain &TC, const llvm::opt::ArgList &Args,
116116
bool ForceStaticHostRuntime = false,
117117
bool IsOffloadingHost = false, bool GompNeedsRT = false);
118118

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
686686
}
687687

688688
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
689-
addOpenMPRuntime(CmdArgs, getToolChain(), Args);
689+
addOpenMPRuntime(C, CmdArgs, getToolChain(), Args);
690690

691691
if (isObjCRuntimeLinked(Args) &&
692692
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {

clang/lib/Driver/ToolChains/DragonFly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
136136

137137
// Use the static OpenMP runtime with -static-openmp
138138
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
139-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
139+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
140140

141141
if (D.CCCIsCXX()) {
142142
if (ToolChain.ShouldLinkCXXStdlib(Args))

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
295295
// Use the static OpenMP runtime with -static-openmp
296296
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
297297
!Args.hasArg(options::OPT_static);
298-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
298+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
299299

300300
if (D.CCCIsCXX()) {
301301
if (ToolChain.ShouldLinkCXXStdlib(Args))

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
598598

599599
// FIXME: Only pass GompNeedsRT = true for platforms with libgomp that
600600
// require librt. Most modern Linux platforms do, but some may not.
601-
if (addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP,
601+
if (addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP,
602602
JA.isHostOffloading(Action::OFK_OpenMP),
603603
/* GompNeedsRT= */ true))
604604
// OpenMP runtimes implies pthreads when using the GNU toolchain.

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
107107
options::OPT_r)) {
108108
// Use the static OpenMP runtime with -static-openmp
109109
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
110-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
110+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
111111

112112
if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
113113
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
311311
options::OPT_r)) {
312312
// Use the static OpenMP runtime with -static-openmp
313313
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
314-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
314+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
315315

316316
if (D.CCCIsCXX()) {
317317
if (ToolChain.ShouldLinkCXXStdlib(Args))

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
221221
options::OPT_r)) {
222222
// Use the static OpenMP runtime with -static-openmp
223223
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
224-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
224+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
225225

226226
if (D.CCCIsCXX()) {
227227
if (ToolChain.ShouldLinkCXXStdlib(Args))

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
211211
// Use the static OpenMP runtime with -static-openmp
212212
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
213213
!Args.hasArg(options::OPT_static);
214-
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
214+
addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
215215

216216
if (D.CCCIsCXX()) {
217217
if (ToolChain.ShouldLinkCXXStdlib(Args))

0 commit comments

Comments
 (0)