Skip to content

Commit ee43e8f

Browse files
committed
[Clang][NVPTX] Allow passing arguments to the linker while standalone
Summary: We support standalone compilation for the NVPTX architecture using 'nvlink' as our linker. Because of the special handling required to transform input files to cubins, as nvlink expects for some reason, we didn't use the standard AddLinkerInput method. However, this also meant that we weren't forwarding options passed with -Wl to the linker. Add this support in for the standalone toolchain path. Revived from https://reviews.llvm.org/D149978
1 parent 8341a40 commit ee43e8f

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -611,35 +611,34 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
611611
continue;
612612
}
613613

614-
// Currently, we only pass the input files to the linker, we do not pass
615-
// any libraries that may be valid only for the host.
616-
if (!II.isFilename())
617-
continue;
618-
619614
// The 'nvlink' application performs RDC-mode linking when given a '.o'
620615
// file and device linking when given a '.cubin' file. We always want to
621616
// perform device linking, so just rename any '.o' files.
622617
// FIXME: This should hopefully be removed if NVIDIA updates their tooling.
623-
auto InputFile = getToolChain().getInputFilename(II);
624-
if (llvm::sys::path::extension(InputFile) != ".cubin") {
625-
// If there are no actions above this one then this is direct input and we
626-
// can copy it. Otherwise the input is internal so a `.cubin` file should
627-
// exist.
628-
if (II.getAction() && II.getAction()->getInputs().size() == 0) {
629-
const char *CubinF =
630-
Args.MakeArgString(getToolChain().getDriver().GetTemporaryPath(
631-
llvm::sys::path::stem(InputFile), "cubin"));
632-
if (llvm::sys::fs::copy_file(InputFile, C.addTempFile(CubinF)))
633-
continue;
618+
if (II.isFilename()) {
619+
auto InputFile = getToolChain().getInputFilename(II);
620+
if (llvm::sys::path::extension(InputFile) != ".cubin") {
621+
// If there are no actions above this one then this is direct input and
622+
// we can copy it. Otherwise the input is internal so a `.cubin` file
623+
// should exist.
624+
if (II.getAction() && II.getAction()->getInputs().size() == 0) {
625+
const char *CubinF =
626+
Args.MakeArgString(getToolChain().getDriver().GetTemporaryPath(
627+
llvm::sys::path::stem(InputFile), "cubin"));
628+
if (llvm::sys::fs::copy_file(InputFile, C.addTempFile(CubinF)))
629+
continue;
634630

635-
CmdArgs.push_back(CubinF);
631+
CmdArgs.push_back(CubinF);
632+
} else {
633+
SmallString<256> Filename(InputFile);
634+
llvm::sys::path::replace_extension(Filename, "cubin");
635+
CmdArgs.push_back(Args.MakeArgString(Filename));
636+
}
636637
} else {
637-
SmallString<256> Filename(InputFile);
638-
llvm::sys::path::replace_extension(Filename, "cubin");
639-
CmdArgs.push_back(Args.MakeArgString(Filename));
638+
CmdArgs.push_back(Args.MakeArgString(InputFile));
640639
}
641-
} else {
642-
CmdArgs.push_back(Args.MakeArgString(InputFile));
640+
} else if (!II.isNothing()) {
641+
II.getInputArg().renderAsInput(Args, CmdArgs);
643642
}
644643
}
645644

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@
7777
// RUN: | FileCheck -check-prefix=LOWERING %s
7878

7979
// LOWERING: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
80+
81+
//
82+
// Test passing arguments directly to nvlink.
83+
//
84+
// RUN: %clang -target nvptx64-nvidia-cuda -Wl,-v -Wl,a,b -### %s 2>&1 \
85+
// RUN: | FileCheck -check-prefix=LINKER-ARGS %s
86+
87+
// LINKER-ARGS: nvlink{{.*}}"-v"{{.*}}"a" "b"

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,11 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
385385
Triple.isAMDGPU() ? Args.MakeArgString("-mcpu=" + Arch)
386386
: Args.MakeArgString("-march=" + Arch),
387387
Args.MakeArgString("-" + OptLevel),
388-
"-Wl,--no-undefined",
389388
};
390389

390+
if (!Triple.isNVPTX())
391+
CmdArgs.push_back("-Wl,--no-undefined");
392+
391393
for (StringRef InputFile : InputFiles)
392394
CmdArgs.push_back(InputFile);
393395

0 commit comments

Comments
 (0)