Skip to content

Commit 69811ec

Browse files
committed
[Clang] Make the GPU toolchains implicitly link -lm and -lc
Summary: The previous patches (The other commits in this chain) allow the offloading toolchain to directly invoke the device linker. Because of this, we can now just have the toolchain implicitly include `-lc` and `-lm` like a standard target does. This removes the old handling that went through the fat binary `-lcgpu`.
1 parent 541a631 commit 69811ec

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
633633
else if (Args.hasArg(options::OPT_mcpu_EQ))
634634
CmdArgs.push_back(Args.MakeArgString(
635635
"-plugin-opt=mcpu=" + Args.getLastArgValue(options::OPT_mcpu_EQ)));
636+
637+
addGPULibraries(getToolChain(), Args, CmdArgs);
638+
636639
CmdArgs.push_back("-o");
637640
CmdArgs.push_back(Output.getFilename());
638641
C.addCommand(std::make_unique<Command>(

clang/lib/Driver/ToolChains/CommonArgs.cpp

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

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

12591275
addArchSpecificRPath(TC, Args, CmdArgs);
1276+
12601277
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
12611278

12621279
return true;

clang/lib/Driver/ToolChains/CommonArgs.h

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

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

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
625625
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
626626
C.getDriver().getLTOMode() == LTOK_Thin);
627627

628+
addGPULibraries(getToolChain(), Args, CmdArgs);
629+
628630
// Add paths for the default clang library path.
629631
SmallString<256> DefaultLibPath =
630632
llvm::sys::path::parent_path(TC.getDriver().Dir);

0 commit comments

Comments
 (0)