Skip to content

Commit f0682b7

Browse files
committed
[Clang] Add a flag to include GPU startup files
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 llvm#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 -gpustartfiles $ amdhsa-loader a.out PASS! ```
1 parent 7f06d8a commit f0682b7

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,9 @@ defm offload_via_llvm : BoolFOption<"offload-via-llvm",
13161316
BothFlags<[], [ClangOption], " LLVM/Offload as portable offloading runtime.">>;
13171317
}
13181318

1319+
def gpustartfiles : Flag<["-"], "gpustartfiles">, Group<Link_Group>,
1320+
HelpText<"Link the GPU C startup utilities automatically, used for testing.">;
1321+
13191322
// CUDA options
13201323
let Group = cuda_Group in {
13211324
def cuda_include_ptx_EQ : Joined<["--"], "cuda-include-ptx=">,

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,15 @@ 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_gpustartfiles)) {
652+
auto IncludePath = getToolChain().getStdlibPath();
653+
if (!IncludePath)
654+
IncludePath = "/lib";
655+
SmallString<128> P(*IncludePath);
656+
llvm::sys::path::append(P, "crt1.o");
657+
CmdArgs.append({"-lc", "-lm", Args.MakeArgString(P)});
658+
}
659+
651660
CmdArgs.push_back("-o");
652661
CmdArgs.push_back(Output.getFilename());
653662
C.addCommand(std::make_unique<Command>(

clang/lib/Driver/ToolChains/Cuda.cpp

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

644+
if (Args.hasArg(options::OPT_gpustartfiles)) {
645+
auto IncludePath = getToolChain().getStdlibPath();
646+
if (!IncludePath)
647+
IncludePath = "/lib";
648+
SmallString<128> P(*IncludePath);
649+
llvm::sys::path::append(P, "crt1.o");
650+
CmdArgs.append({"-lc", "-lm", Args.MakeArgString(P)});
651+
}
652+
644653
C.addCommand(std::make_unique<Command>(
645654
JA, *this,
646655
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,

clang/test/Driver/gpustartfiles.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -gpustartfiles \
2+
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=NVPTX %s
3+
// NVPTX: clang-nvlink-wrapper{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
4+
//
5+
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -gpustartfiles \
6+
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=AMDGPU %s
7+
// AMDGPU: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"

0 commit comments

Comments
 (0)