Skip to content

Commit 2bf58f5

Browse files
authored
[Clang] Suppress missing architecture error when doing LTO (#100652)
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 6740d70 commit 2bf58f5

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
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.

libc/cmake/modules/LLVMLibCLibraryRules.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function(add_bitcode_entrypoint_library target_name base_target_name)
113113

114114
add_executable(${target_name} ${objects})
115115
target_link_options(${target_name} PRIVATE
116-
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
116+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
117117
endfunction(add_bitcode_entrypoint_library)
118118

119119
# A rule to build a library from a collection of entrypoint objects.

libc/startup/gpu/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function(add_startup_object name)
3434
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
3535
RUNTIME_OUTPUT_NAME ${name}.o)
3636
target_link_options(${fq_target_name}.exe PRIVATE
37-
"-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
37+
"-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
3838
endif()
3939
endfunction()
4040

0 commit comments

Comments
 (0)