Skip to content

Commit fcb5586

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 256b676 commit fcb5586

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,17 @@ 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+
// If the user's toolchain has the 'include/amdgcn-amd-amdhsa/` path, we
638+
// assume it supports the standard C libraries for the GPU and include them.
639+
bool HasLibC = getToolChain().getStdlibIncludePath().has_value();
640+
if (!Args.hasArg(options::OPT_nogpulib) &&
641+
!Args.hasArg(options::OPT_nolibc) &&
642+
Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC)) {
643+
CmdArgs.push_back("-lc");
644+
CmdArgs.push_back("-lm");
645+
}
646+
636647
CmdArgs.push_back("-o");
637648
CmdArgs.push_back(Output.getFilename());
638649
C.addCommand(std::make_unique<Command>(

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,7 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
12571257
addOpenMPDeviceLibC(C, Args, CmdArgs);
12581258

12591259
addArchSpecificRPath(TC, Args, CmdArgs);
1260+
12601261
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
12611262

12621263
return true;

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,16 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
617617
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
618618
C.getDriver().getLTOMode() == LTOK_Thin);
619619

620+
// If the user's toolchain has the 'include/nvptx64-nvidia-cuda/` path, we
621+
// assume it supports the standard C libraries for the GPU and include them.
622+
bool HasLibC = getToolChain().getStdlibIncludePath().has_value();
623+
if (!Args.hasArg(options::OPT_nogpulib) &&
624+
!Args.hasArg(options::OPT_nolibc) &&
625+
Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC)) {
626+
CmdArgs.push_back("-lc");
627+
CmdArgs.push_back("-lm");
628+
}
629+
620630
// Add paths for the default clang library path.
621631
SmallString<256> DefaultLibPath =
622632
llvm::sys::path::parent_path(TC.getDriver().Dir);

0 commit comments

Comments
 (0)