Skip to content

Commit d4c4180

Browse files
authored
[Clang] Add a flag to include GPU startup files (#112025)
Summary: The C library for GPUs provides the ability to target regular C/C++ programs by providing the C library and a file containing kernels that call the `main` function. This is mostly used for unit tests, this patch provides a quick way to add them without needing to know the paths. I currently do this explicitly, but according to the libc++ contributors we don't want to need to specify these paths manually. See the discussion in #104515. I just default to `lib/` if the target-specific one isn't found because the linker will handle giving a reasonable error message if it's not found. Basically the use-case looks like this. ```console $ clang test.c --target=amdgcn-amd-amdhsa -mcpu=native -startfiles -stdlib $ amdhsa-loader a.out PASS! ```
1 parent 7152bf3 commit d4c4180

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5633,6 +5633,7 @@ def noprebind : Flag<["-"], "noprebind">;
56335633
def noprofilelib : Flag<["-"], "noprofilelib">;
56345634
def noseglinkedit : Flag<["-"], "noseglinkedit">;
56355635
def nostartfiles : Flag<["-"], "nostartfiles">, Group<Link_Group>;
5636+
def startfiles : Flag<["-"], "startfiles">, Group<Link_Group>;
56365637
def nostdinc : Flag<["-"], "nostdinc">,
56375638
Visibility<[ClangOption, CLOption, DXCOption]>, Group<IncludePath_Group>,
56385639
HelpText<"Disable both standard system #include directories and builtin #include directories">;
@@ -5645,6 +5646,9 @@ def nostdincxx : Flag<["-"], "nostdinc++">, Visibility<[ClangOption, CC1Option]>
56455646
def nostdlib : Flag<["-"], "nostdlib">,
56465647
Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
56475648
Group<Link_Group>;
5649+
def stdlib : Flag<["-"], "stdlib">,
5650+
Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
5651+
Group<Link_Group>;
56485652
def nostdlibxx : Flag<["-"], "nostdlib++">;
56495653
def object : Flag<["-"], "object">;
56505654
def o : JoinedOrSeparate<["-"], "o">,

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,17 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
648648
Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
649649
}
650650

651+
if (Args.hasArg(options::OPT_stdlib))
652+
CmdArgs.append({"-lc", "-lm"});
653+
if (Args.hasArg(options::OPT_startfiles)) {
654+
std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
655+
if (!IncludePath)
656+
IncludePath = "/lib";
657+
SmallString<128> P(*IncludePath);
658+
llvm::sys::path::append(P, "crt1.o");
659+
CmdArgs.push_back(Args.MakeArgString(P));
660+
}
661+
651662
CmdArgs.push_back("-o");
652663
CmdArgs.push_back(Output.getFilename());
653664
C.addCommand(std::make_unique<Command>(

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,17 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
643643
llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
644644
CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
645645

646+
if (Args.hasArg(options::OPT_stdlib))
647+
CmdArgs.append({"-lc", "-lm"});
648+
if (Args.hasArg(options::OPT_startfiles)) {
649+
std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
650+
if (!IncludePath)
651+
IncludePath = "/lib";
652+
SmallString<128> P(*IncludePath);
653+
llvm::sys::path::append(P, "crt1.o");
654+
CmdArgs.push_back(Args.MakeArgString(P));
655+
}
656+
646657
C.addCommand(std::make_unique<Command>(
647658
JA, *this,
648659
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
3333
// RUN: -r %s 2>&1 | FileCheck -check-prefixes=RELO %s
3434
// RELO-NOT: -shared
35+
36+
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
37+
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
38+
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"

clang/test/Driver/cuda-cross-compiling.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@
105105
// RUN: | FileCheck -check-prefix=FEATURE %s
106106

107107
// FEATURE: clang-nvlink-wrapper{{.*}}"--plugin-opt=-mattr=+ptx63"
108+
109+
//
110+
// Test including the libc startup files and libc
111+
//
112+
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -stdlib -startfiles \
113+
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
114+
115+
// STARTUP: clang-nvlink-wrapper{{.*}}"-lc" "-lm" "{{.*}}crt1.o"

0 commit comments

Comments
 (0)