Skip to content

Commit 4415c4d

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 4415c4d

File tree

10 files changed

+32
-46
lines changed

10 files changed

+32
-46
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -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);

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)