Skip to content

Commit 9da4d74

Browse files
authored
[Clang] Always pass detected CUDA path to 'clang-nvlink-wrapper' (#152789)
Summary: We always want to use the detected path. The clang driver's detection is far more sophisticated so we should use that whenever possible. Also update the usage so we properly fall back to path instead of incorrectly using the `/bin` if not provided.
1 parent 5a5e8ba commit 9da4d74

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,12 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
611611
CmdArgs.push_back(Args.MakeArgString(
612612
"--pxtas-path=" + Args.getLastArgValue(options::OPT_ptxas_path_EQ)));
613613

614-
if (Args.hasArg(options::OPT_cuda_path_EQ))
615-
CmdArgs.push_back(Args.MakeArgString(
616-
"--cuda-path=" + Args.getLastArgValue(options::OPT_cuda_path_EQ)));
614+
if (Args.hasArg(options::OPT_cuda_path_EQ) || TC.CudaInstallation.isValid()) {
615+
StringRef CudaPath = Args.getLastArgValue(
616+
options::OPT_cuda_path_EQ,
617+
llvm::sys::path::parent_path(TC.CudaInstallation.getBinPath()));
618+
CmdArgs.push_back(Args.MakeArgString("--cuda-path=" + CudaPath));
619+
}
617620

618621
// Add paths specified in LIBRARY_PATH environment variable as -L options.
619622
addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@
104104
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
105105

106106
// STARTUP: clang-nvlink-wrapper{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
107+
108+
//
109+
// Test cuda path handling
110+
//
111+
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_52 --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
112+
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=PATH %s
113+
114+
// PATH: clang-nvlink-wrapper{{.*}}"--cuda-path={{.*}}/Inputs/CUDA/usr/local/cuda"

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,16 @@ struct Symbol {
286286
};
287287

288288
Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args) {
289-
std::string CudaPath = Args.getLastArgValue(OPT_cuda_path_EQ).str();
290-
std::string GivenPath = Args.getLastArgValue(OPT_ptxas_path_EQ).str();
291-
Expected<std::string> PTXAsPath =
292-
findProgram(Args, "ptxas", {CudaPath + "/bin", GivenPath});
289+
SmallVector<StringRef, 1> SearchPaths;
290+
if (Arg *A = Args.getLastArg(OPT_cuda_path_EQ))
291+
SearchPaths.push_back(Args.MakeArgString(A->getValue() + Twine("/bin")));
292+
if (Arg *A = Args.getLastArg(OPT_ptxas_path_EQ))
293+
SearchPaths.push_back(Args.MakeArgString(A->getValue()));
294+
295+
Expected<std::string> PTXAsPath = findProgram(Args, "ptxas", SearchPaths);
293296
if (!PTXAsPath)
294297
return PTXAsPath.takeError();
298+
295299
if (!Args.hasArg(OPT_arch))
296300
return createStringError(
297301
"must pass in an explicit nvptx64 gpu architecture to 'ptxas'");
@@ -691,9 +695,11 @@ Error runNVLink(ArrayRef<StringRef> Files, const ArgList &Args) {
691695
if (Args.hasArg(OPT_lto_emit_asm) || Args.hasArg(OPT_lto_emit_llvm))
692696
return Error::success();
693697

694-
std::string CudaPath = Args.getLastArgValue(OPT_cuda_path_EQ).str();
695-
Expected<std::string> NVLinkPath =
696-
findProgram(Args, "nvlink", {CudaPath + "/bin"});
698+
SmallVector<StringRef, 1> SearchPaths;
699+
if (Arg *A = Args.getLastArg(OPT_cuda_path_EQ))
700+
SearchPaths.push_back(Args.MakeArgString(A->getValue() + Twine("/bin")));
701+
702+
Expected<std::string> NVLinkPath = findProgram(Args, "nvlink", SearchPaths);
697703
if (!NVLinkPath)
698704
return NVLinkPath.takeError();
699705

0 commit comments

Comments
 (0)