[clang] d4c4180 - [Clang] Add a flag to include GPU startup files (#112025)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 28 07:17:23 PDT 2024
Author: Joseph Huber
Date: 2024-10-28T07:17:19-07:00
New Revision: d4c41804175e9cb37266c410cafe9caaac1819ca
URL: https://github.com/llvm/llvm-project/commit/d4c41804175e9cb37266c410cafe9caaac1819ca
DIFF: https://github.com/llvm/llvm-project/commit/d4c41804175e9cb37266c410cafe9caaac1819ca.diff
LOG: [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 https://github.com/llvm/llvm-project/pull/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!
```
Added:
Modified:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/AMDGPU.cpp
clang/lib/Driver/ToolChains/Cuda.cpp
clang/test/Driver/amdgpu-toolchain.c
clang/test/Driver/cuda-cross-compiling.c
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5df6ddd5e6a0c5..7b28e8b4c31ec1 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5633,6 +5633,7 @@ def noprebind : Flag<["-"], "noprebind">;
def noprofilelib : Flag<["-"], "noprofilelib">;
def noseglinkedit : Flag<["-"], "noseglinkedit">;
def nostartfiles : Flag<["-"], "nostartfiles">, Group<Link_Group>;
+def startfiles : Flag<["-"], "startfiles">, Group<Link_Group>;
def nostdinc : Flag<["-"], "nostdinc">,
Visibility<[ClangOption, CLOption, DXCOption]>, Group<IncludePath_Group>,
HelpText<"Disable both standard system #include directories and builtin #include directories">;
@@ -5645,6 +5646,9 @@ def nostdincxx : Flag<["-"], "nostdinc++">, Visibility<[ClangOption, CC1Option]>
def nostdlib : Flag<["-"], "nostdlib">,
Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
Group<Link_Group>;
+def stdlib : Flag<["-"], "stdlib">,
+ Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
+ Group<Link_Group>;
def nostdlibxx : Flag<["-"], "nostdlib++">;
def object : Flag<["-"], "object">;
def o : JoinedOrSeparate<["-"], "o">,
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 2c85d21ebd738c..a8061ffd9321f5 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -648,6 +648,17 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
}
+ if (Args.hasArg(options::OPT_stdlib))
+ CmdArgs.append({"-lc", "-lm"});
+ if (Args.hasArg(options::OPT_startfiles)) {
+ std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
+ if (!IncludePath)
+ IncludePath = "/lib";
+ SmallString<128> P(*IncludePath);
+ llvm::sys::path::append(P, "crt1.o");
+ CmdArgs.push_back(Args.MakeArgString(P));
+ }
+
CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename());
C.addCommand(std::make_unique<Command>(
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 412b379304b1e6..ddd5ea248ca0cc 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -643,6 +643,17 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
+ if (Args.hasArg(options::OPT_stdlib))
+ CmdArgs.append({"-lc", "-lm"});
+ if (Args.hasArg(options::OPT_startfiles)) {
+ std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
+ if (!IncludePath)
+ IncludePath = "/lib";
+ SmallString<128> P(*IncludePath);
+ llvm::sys::path::append(P, "crt1.o");
+ CmdArgs.push_back(Args.MakeArgString(P));
+ }
+
C.addCommand(std::make_unique<Command>(
JA, *this,
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
diff --git a/clang/test/Driver/amdgpu-toolchain.c b/clang/test/Driver/amdgpu-toolchain.c
index b60d31bae62700..c1c5aa8e90e686 100644
--- a/clang/test/Driver/amdgpu-toolchain.c
+++ b/clang/test/Driver/amdgpu-toolchain.c
@@ -32,3 +32,7 @@
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
// RUN: -r %s 2>&1 | FileCheck -check-prefixes=RELO %s
// RELO-NOT: -shared
+
+// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
+// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
+// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
diff --git a/clang/test/Driver/cuda-cross-compiling.c b/clang/test/Driver/cuda-cross-compiling.c
index 126e9e9fc83d57..baf37048300315 100644
--- a/clang/test/Driver/cuda-cross-compiling.c
+++ b/clang/test/Driver/cuda-cross-compiling.c
@@ -105,3 +105,11 @@
// RUN: | FileCheck -check-prefix=FEATURE %s
// FEATURE: clang-nvlink-wrapper{{.*}}"--plugin-opt=-mattr=+ptx63"
+
+//
+// Test including the libc startup files and libc
+//
+// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -stdlib -startfiles \
+// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
+
+// STARTUP: clang-nvlink-wrapper{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
More information about the cfe-commits
mailing list