Skip to content

Commit d5a15f3

Browse files
authored
[Clang][NVPTX] Allow passing arguments to the linker while standalone (#73030)
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 e314622 commit d5a15f3

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

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

626-
// Currently, we only pass the input files to the linker, we do not pass
627-
// any libraries that may be valid only for the host.
628-
if (!II.isFilename())
629-
continue;
630-
631626
// The 'nvlink' application performs RDC-mode linking when given a '.o'
632627
// file and device linking when given a '.cubin' file. We always want to
633628
// perform device linking, so just rename any '.o' files.
634629
// FIXME: This should hopefully be removed if NVIDIA updates their tooling.
635-
auto InputFile = getToolChain().getInputFilename(II);
636-
if (llvm::sys::path::extension(InputFile) != ".cubin") {
637-
// If there are no actions above this one then this is direct input and we
638-
// can copy it. Otherwise the input is internal so a `.cubin` file should
639-
// exist.
640-
if (II.getAction() && II.getAction()->getInputs().size() == 0) {
641-
const char *CubinF =
642-
Args.MakeArgString(getToolChain().getDriver().GetTemporaryPath(
643-
llvm::sys::path::stem(InputFile), "cubin"));
644-
if (llvm::sys::fs::copy_file(InputFile, C.addTempFile(CubinF)))
645-
continue;
630+
if (II.isFilename()) {
631+
auto InputFile = getToolChain().getInputFilename(II);
632+
if (llvm::sys::path::extension(InputFile) != ".cubin") {
633+
// If there are no actions above this one then this is direct input and
634+
// we can copy it. Otherwise the input is internal so a `.cubin` file
635+
// should exist.
636+
if (II.getAction() && II.getAction()->getInputs().size() == 0) {
637+
const char *CubinF =
638+
Args.MakeArgString(getToolChain().getDriver().GetTemporaryPath(
639+
llvm::sys::path::stem(InputFile), "cubin"));
640+
if (llvm::sys::fs::copy_file(InputFile, C.addTempFile(CubinF)))
641+
continue;
646642

647-
CmdArgs.push_back(CubinF);
643+
CmdArgs.push_back(CubinF);
644+
} else {
645+
SmallString<256> Filename(InputFile);
646+
llvm::sys::path::replace_extension(Filename, "cubin");
647+
CmdArgs.push_back(Args.MakeArgString(Filename));
648+
}
648649
} else {
649-
SmallString<256> Filename(InputFile);
650-
llvm::sys::path::replace_extension(Filename, "cubin");
651-
CmdArgs.push_back(Args.MakeArgString(Filename));
650+
CmdArgs.push_back(Args.MakeArgString(InputFile));
652651
}
653-
} else {
654-
CmdArgs.push_back(Args.MakeArgString(InputFile));
652+
} else if (!II.isNothing()) {
653+
II.getInputArg().renderAsInput(Args, CmdArgs);
655654
}
656655
}
657656

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
// LOWERING: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
7070

7171
//
72+
// Test passing arguments directly to nvlink.
73+
//
74+
// RUN: %clang -target nvptx64-nvidia-cuda -Wl,-v -Wl,a,b -### %s 2>&1 \
75+
// RUN: | FileCheck -check-prefix=LINKER-ARGS %s
76+
77+
// LINKER-ARGS: nvlink{{.*}}"-v"{{.*}}"a" "b"
78+
7279
// Tests for handling a missing architecture.
7380
//
7481
// RUN: not %clang -target nvptx64-nvidia-cuda %s -### 2>&1 \
@@ -80,4 +87,4 @@
8087
// RUN: %clang -target nvptx64-nvidia-cuda -flto -c %s -### 2>&1 \
8188
// RUN: | FileCheck -check-prefix=GENERIC %s
8289

83-
// GENERIC-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"
90+
// GENERIC-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
454454
Triple.isAMDGPU() ? Args.MakeArgString("-mcpu=" + Arch)
455455
: Args.MakeArgString("-march=" + Arch),
456456
Args.MakeArgString("-" + OptLevel),
457-
"-Wl,--no-undefined",
458457
};
459458

459+
if (!Triple.isNVPTX())
460+
CmdArgs.push_back("-Wl,--no-undefined");
461+
460462
for (StringRef InputFile : InputFiles)
461463
CmdArgs.push_back(InputFile);
462464

0 commit comments

Comments
 (0)