Skip to content

Commit 5017fe0

Browse files
committed
[Clang] Suppress missing architecture error when doing LTO
Summary: The `nvlink-wrapper` can do LTO now, which means we can still create some LLVM-IR without needing an architecture. In the case that we try to invoke `nvlink` internally, that will still fail. This patch simply defers the error until later so we can use `--lto-emit-llvm` to get the IR without specifying an architecture.
1 parent fea5914 commit 5017fe0

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,14 +596,16 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
596596
CmdArgs.push_back("-v");
597597

598598
StringRef GPUArch = Args.getLastArgValue(options::OPT_march_EQ);
599-
if (GPUArch.empty()) {
599+
if (GPUArch.empty() && !C.getDriver().isUsingLTO()) {
600600
C.getDriver().Diag(diag::err_drv_offload_missing_gpu_arch)
601601
<< getToolChain().getArchName() << getShortName();
602602
return;
603603
}
604604

605-
CmdArgs.push_back("-arch");
606-
CmdArgs.push_back(Args.MakeArgString(GPUArch));
605+
if (!GPUArch.empty()) {
606+
CmdArgs.push_back("-arch");
607+
CmdArgs.push_back(Args.MakeArgString(GPUArch));
608+
}
607609

608610
if (Args.hasArg(options::OPT_ptxas_path_EQ))
609611
CmdArgs.push_back(Args.MakeArgString(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
// MISSING: error: must pass in an explicit nvptx64 gpu architecture to 'ptxas'
8585
// MISSING: error: must pass in an explicit nvptx64 gpu architecture to 'nvlink'
8686

87+
// Do not error when performing LTO.
88+
//
89+
// RUN: %clang -target nvptx64-nvidia-cuda -flto %s -### 2>&1 \
90+
// RUN: | FileCheck -check-prefix=MISSING-LTO %s
91+
92+
// MISSING-LTO-NOT: error: must pass in an explicit nvptx64 gpu architecture to 'nvlink'
93+
8794
// RUN: %clang -target nvptx64-nvidia-cuda -flto -c %s -### 2>&1 \
8895
// RUN: | FileCheck -check-prefix=GENERIC %s
8996
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_52 -march=generic -flto -c %s -### 2>&1 \

clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args) {
302302
findProgram(Args, "ptxas", {CudaPath + "/bin", GivenPath});
303303
if (!PTXAsPath)
304304
return PTXAsPath.takeError();
305+
if (!Args.hasArg(OPT_arch))
306+
return createStringError(
307+
"must pass in an explicit nvptx64 gpu architecture to 'ptxas'");
305308

306309
auto TempFileOrErr = createTempFile(
307310
Args, sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")), "cubin");
@@ -694,6 +697,10 @@ Error runNVLink(ArrayRef<StringRef> Files, const ArgList &Args) {
694697
if (!NVLinkPath)
695698
return NVLinkPath.takeError();
696699

700+
if (!Args.hasArg(OPT_arch))
701+
return createStringError(
702+
"must pass in an explicit nvptx64 gpu architecture to 'nvlink'");
703+
697704
ArgStringList NewLinkerArgs;
698705
for (const opt::Arg *Arg : Args) {
699706
// Do not forward arguments only intended for the linker wrapper.

0 commit comments

Comments
 (0)