xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Cuda.cpp (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Cuda.h"
10 #include "CommonArgs.h"
11 #include "InputInfo.h"
12 #include "clang/Basic/Cuda.h"
13 #include "clang/Config/config.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/Distro.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/DriverDiagnostic.h"
18 #include "clang/Driver/Options.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/Option/ArgList.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Process.h"
25 #include "llvm/Support/Program.h"
26 #include "llvm/Support/TargetParser.h"
27 #include "llvm/Support/VirtualFileSystem.h"
28 #include <system_error>
29 
30 using namespace clang::driver;
31 using namespace clang::driver::toolchains;
32 using namespace clang::driver::tools;
33 using namespace clang;
34 using namespace llvm::opt;
35 
36 namespace {
37 struct CudaVersionInfo {
38   std::string DetectedVersion;
39   CudaVersion Version;
40 };
41 // Parses the contents of version.txt in an CUDA installation.  It should
42 // contain one line of the from e.g. "CUDA Version 7.5.2".
43 CudaVersionInfo parseCudaVersionFile(llvm::StringRef V) {
44   V = V.trim();
45   if (!V.startswith("CUDA Version "))
46     return {V.str(), CudaVersion::UNKNOWN};
47   V = V.substr(strlen("CUDA Version "));
48   SmallVector<StringRef,4> VersionParts;
49   V.split(VersionParts, '.');
50   return {"version.txt: " + V.str() + ".",
51           VersionParts.size() < 2
52               ? CudaVersion::UNKNOWN
53               : CudaStringToVersion(
54                     join_items(".", VersionParts[0], VersionParts[1]))};
55 }
56 
57 CudaVersion getCudaVersion(uint32_t raw_version) {
58   if (raw_version < 7050)
59     return CudaVersion::CUDA_70;
60   if (raw_version < 8000)
61     return CudaVersion::CUDA_75;
62   if (raw_version < 9000)
63     return CudaVersion::CUDA_80;
64   if (raw_version < 9010)
65     return CudaVersion::CUDA_90;
66   if (raw_version < 9020)
67     return CudaVersion::CUDA_91;
68   if (raw_version < 10000)
69     return CudaVersion::CUDA_92;
70   if (raw_version < 10010)
71     return CudaVersion::CUDA_100;
72   if (raw_version < 10020)
73     return CudaVersion::CUDA_101;
74   if (raw_version < 11000)
75     return CudaVersion::CUDA_102;
76   if (raw_version < 11010)
77     return CudaVersion::CUDA_110;
78   return CudaVersion::LATEST;
79 }
80 
81 CudaVersionInfo parseCudaHFile(llvm::StringRef Input) {
82   // Helper lambda which skips the words if the line starts with them or returns
83   // None otherwise.
84   auto StartsWithWords =
85       [](llvm::StringRef Line,
86          const SmallVector<StringRef, 3> words) -> llvm::Optional<StringRef> {
87     for (StringRef word : words) {
88       if (!Line.consume_front(word))
89         return {};
90       Line = Line.ltrim();
91     }
92     return Line;
93   };
94 
95   Input = Input.ltrim();
96   while (!Input.empty()) {
97     if (auto Line =
98             StartsWithWords(Input.ltrim(), {"#", "define", "CUDA_VERSION"})) {
99       uint32_t RawVersion;
100       Line->consumeInteger(10, RawVersion);
101       return {"cuda.h: CUDA_VERSION=" + Twine(RawVersion).str() + ".",
102               getCudaVersion(RawVersion)};
103     }
104     // Find next non-empty line.
105     Input = Input.drop_front(Input.find_first_of("\n\r")).ltrim();
106   }
107   return {"cuda.h: CUDA_VERSION not found.", CudaVersion::UNKNOWN};
108 }
109 } // namespace
110 
111 void CudaInstallationDetector::WarnIfUnsupportedVersion() {
112   if (DetectedVersionIsNotSupported)
113     D.Diag(diag::warn_drv_unknown_cuda_version)
114         << DetectedVersion
115         << CudaVersionToString(CudaVersion::LATEST_SUPPORTED);
116 }
117 
118 CudaInstallationDetector::CudaInstallationDetector(
119     const Driver &D, const llvm::Triple &HostTriple,
120     const llvm::opt::ArgList &Args)
121     : D(D) {
122   struct Candidate {
123     std::string Path;
124     bool StrictChecking;
125 
126     Candidate(std::string Path, bool StrictChecking = false)
127         : Path(Path), StrictChecking(StrictChecking) {}
128   };
129   SmallVector<Candidate, 4> Candidates;
130 
131   // In decreasing order so we prefer newer versions to older versions.
132   std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
133   auto &FS = D.getVFS();
134 
135   if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) {
136     Candidates.emplace_back(
137         Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
138   } else if (HostTriple.isOSWindows()) {
139     for (const char *Ver : Versions)
140       Candidates.emplace_back(
141           D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
142           Ver);
143   } else {
144     if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) {
145       // Try to find ptxas binary. If the executable is located in a directory
146       // called 'bin/', its parent directory might be a good guess for a valid
147       // CUDA installation.
148       // However, some distributions might installs 'ptxas' to /usr/bin. In that
149       // case the candidate would be '/usr' which passes the following checks
150       // because '/usr/include' exists as well. To avoid this case, we always
151       // check for the directory potentially containing files for libdevice,
152       // even if the user passes -nocudalib.
153       if (llvm::ErrorOr<std::string> ptxas =
154               llvm::sys::findProgramByName("ptxas")) {
155         SmallString<256> ptxasAbsolutePath;
156         llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath);
157 
158         StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath);
159         if (llvm::sys::path::filename(ptxasDir) == "bin")
160           Candidates.emplace_back(
161               std::string(llvm::sys::path::parent_path(ptxasDir)),
162               /*StrictChecking=*/true);
163       }
164     }
165 
166     Candidates.emplace_back(D.SysRoot + "/usr/local/cuda");
167     for (const char *Ver : Versions)
168       Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
169 
170     Distro Dist(FS, llvm::Triple(llvm::sys::getProcessTriple()));
171     if (Dist.IsDebian() || Dist.IsUbuntu())
172       // Special case for Debian to have nvidia-cuda-toolkit work
173       // out of the box. More info on http://bugs.debian.org/882505
174       Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
175   }
176 
177   bool NoCudaLib = Args.hasArg(options::OPT_nogpulib);
178 
179   for (const auto &Candidate : Candidates) {
180     InstallPath = Candidate.Path;
181     if (InstallPath.empty() || !FS.exists(InstallPath))
182       continue;
183 
184     BinPath = InstallPath + "/bin";
185     IncludePath = InstallPath + "/include";
186     LibDevicePath = InstallPath + "/nvvm/libdevice";
187 
188     if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
189       continue;
190     bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
191     if (CheckLibDevice && !FS.exists(LibDevicePath))
192       continue;
193 
194     // On Linux, we have both lib and lib64 directories, and we need to choose
195     // based on our triple.  On MacOS, we have only a lib directory.
196     //
197     // It's sufficient for our purposes to be flexible: If both lib and lib64
198     // exist, we choose whichever one matches our triple.  Otherwise, if only
199     // lib exists, we use it.
200     if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64"))
201       LibPath = InstallPath + "/lib64";
202     else if (FS.exists(InstallPath + "/lib"))
203       LibPath = InstallPath + "/lib";
204     else
205       continue;
206 
207     CudaVersionInfo VersionInfo = {"", CudaVersion::UNKNOWN};
208     if (auto VersionFile = FS.getBufferForFile(InstallPath + "/version.txt"))
209       VersionInfo = parseCudaVersionFile((*VersionFile)->getBuffer());
210     // If version file didn't give us the version, try to find it in cuda.h
211     if (VersionInfo.Version == CudaVersion::UNKNOWN)
212       if (auto CudaHFile = FS.getBufferForFile(InstallPath + "/include/cuda.h"))
213         VersionInfo = parseCudaHFile((*CudaHFile)->getBuffer());
214     // As the last resort, make an educated guess between CUDA-7.0, (which had
215     // no version.txt file and had old-style libdevice bitcode ) and an unknown
216     // recent CUDA version (no version.txt, new style bitcode).
217     if (VersionInfo.Version == CudaVersion::UNKNOWN) {
218       VersionInfo.Version = (FS.exists(LibDevicePath + "/libdevice.10.bc"))
219                                 ? Version = CudaVersion::LATEST
220                                 : Version = CudaVersion::CUDA_70;
221       VersionInfo.DetectedVersion =
222           "No version found in version.txt or cuda.h.";
223     }
224 
225     Version = VersionInfo.Version;
226     DetectedVersion = VersionInfo.DetectedVersion;
227 
228     // TODO(tra): remove the warning once we have all features of 10.2
229     // and 11.0 implemented.
230     DetectedVersionIsNotSupported = Version > CudaVersion::LATEST_SUPPORTED;
231 
232     if (Version >= CudaVersion::CUDA_90) {
233       // CUDA-9+ uses single libdevice file for all GPU variants.
234       std::string FilePath = LibDevicePath + "/libdevice.10.bc";
235       if (FS.exists(FilePath)) {
236         for (int Arch = (int)CudaArch::SM_30, E = (int)CudaArch::LAST; Arch < E;
237              ++Arch) {
238           CudaArch GpuArch = static_cast<CudaArch>(Arch);
239           if (!IsNVIDIAGpuArch(GpuArch))
240             continue;
241           std::string GpuArchName(CudaArchToString(GpuArch));
242           LibDeviceMap[GpuArchName] = FilePath;
243         }
244       }
245     } else {
246       std::error_code EC;
247       for (llvm::vfs::directory_iterator LI = FS.dir_begin(LibDevicePath, EC),
248                                          LE;
249            !EC && LI != LE; LI = LI.increment(EC)) {
250         StringRef FilePath = LI->path();
251         StringRef FileName = llvm::sys::path::filename(FilePath);
252         // Process all bitcode filenames that look like
253         // libdevice.compute_XX.YY.bc
254         const StringRef LibDeviceName = "libdevice.";
255         if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
256           continue;
257         StringRef GpuArch = FileName.slice(
258             LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
259         LibDeviceMap[GpuArch] = FilePath.str();
260         // Insert map entries for specific devices with this compute
261         // capability. NVCC's choice of the libdevice library version is
262         // rather peculiar and depends on the CUDA version.
263         if (GpuArch == "compute_20") {
264           LibDeviceMap["sm_20"] = std::string(FilePath);
265           LibDeviceMap["sm_21"] = std::string(FilePath);
266           LibDeviceMap["sm_32"] = std::string(FilePath);
267         } else if (GpuArch == "compute_30") {
268           LibDeviceMap["sm_30"] = std::string(FilePath);
269           if (Version < CudaVersion::CUDA_80) {
270             LibDeviceMap["sm_50"] = std::string(FilePath);
271             LibDeviceMap["sm_52"] = std::string(FilePath);
272             LibDeviceMap["sm_53"] = std::string(FilePath);
273           }
274           LibDeviceMap["sm_60"] = std::string(FilePath);
275           LibDeviceMap["sm_61"] = std::string(FilePath);
276           LibDeviceMap["sm_62"] = std::string(FilePath);
277         } else if (GpuArch == "compute_35") {
278           LibDeviceMap["sm_35"] = std::string(FilePath);
279           LibDeviceMap["sm_37"] = std::string(FilePath);
280         } else if (GpuArch == "compute_50") {
281           if (Version >= CudaVersion::CUDA_80) {
282             LibDeviceMap["sm_50"] = std::string(FilePath);
283             LibDeviceMap["sm_52"] = std::string(FilePath);
284             LibDeviceMap["sm_53"] = std::string(FilePath);
285           }
286         }
287       }
288     }
289 
290     // Check that we have found at least one libdevice that we can link in if
291     // -nocudalib hasn't been specified.
292     if (LibDeviceMap.empty() && !NoCudaLib)
293       continue;
294 
295     IsValid = true;
296     break;
297   }
298 }
299 
300 void CudaInstallationDetector::AddCudaIncludeArgs(
301     const ArgList &DriverArgs, ArgStringList &CC1Args) const {
302   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
303     // Add cuda_wrappers/* to our system include path.  This lets us wrap
304     // standard library headers.
305     SmallString<128> P(D.ResourceDir);
306     llvm::sys::path::append(P, "include");
307     llvm::sys::path::append(P, "cuda_wrappers");
308     CC1Args.push_back("-internal-isystem");
309     CC1Args.push_back(DriverArgs.MakeArgString(P));
310   }
311 
312   if (DriverArgs.hasArg(options::OPT_nogpuinc))
313     return;
314 
315   if (!isValid()) {
316     D.Diag(diag::err_drv_no_cuda_installation);
317     return;
318   }
319 
320   CC1Args.push_back("-internal-isystem");
321   CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
322   CC1Args.push_back("-include");
323   CC1Args.push_back("__clang_cuda_runtime_wrapper.h");
324 }
325 
326 void CudaInstallationDetector::CheckCudaVersionSupportsArch(
327     CudaArch Arch) const {
328   if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
329       ArchsWithBadVersion[(int)Arch])
330     return;
331 
332   auto MinVersion = MinVersionForCudaArch(Arch);
333   auto MaxVersion = MaxVersionForCudaArch(Arch);
334   if (Version < MinVersion || Version > MaxVersion) {
335     ArchsWithBadVersion[(int)Arch] = true;
336     D.Diag(diag::err_drv_cuda_version_unsupported)
337         << CudaArchToString(Arch) << CudaVersionToString(MinVersion)
338         << CudaVersionToString(MaxVersion) << InstallPath
339         << CudaVersionToString(Version);
340   }
341 }
342 
343 void CudaInstallationDetector::print(raw_ostream &OS) const {
344   if (isValid())
345     OS << "Found CUDA installation: " << InstallPath << ", version "
346        << CudaVersionToString(Version) << "\n";
347 }
348 
349 namespace {
350 /// Debug info level for the NVPTX devices. We may need to emit different debug
351 /// info level for the host and for the device itselfi. This type controls
352 /// emission of the debug info for the devices. It either prohibits disable info
353 /// emission completely, or emits debug directives only, or emits same debug
354 /// info as for the host.
355 enum DeviceDebugInfoLevel {
356   DisableDebugInfo,        /// Do not emit debug info for the devices.
357   DebugDirectivesOnly,     /// Emit only debug directives.
358   EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
359                            /// host.
360 };
361 } // anonymous namespace
362 
363 /// Define debug info level for the NVPTX devices. If the debug info for both
364 /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
365 /// only debug directives are requested for the both host and device
366 /// (-gline-directvies-only), or the debug info only for the device is disabled
367 /// (optimization is on and --cuda-noopt-device-debug was not specified), the
368 /// debug directves only must be emitted for the device. Otherwise, use the same
369 /// debug info level just like for the host (with the limitations of only
370 /// supported DWARF2 standard).
371 static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
372   const Arg *A = Args.getLastArg(options::OPT_O_Group);
373   bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) ||
374                         Args.hasFlag(options::OPT_cuda_noopt_device_debug,
375                                      options::OPT_no_cuda_noopt_device_debug,
376                                      /*Default=*/false);
377   if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
378     const Option &Opt = A->getOption();
379     if (Opt.matches(options::OPT_gN_Group)) {
380       if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0))
381         return DisableDebugInfo;
382       if (Opt.matches(options::OPT_gline_directives_only))
383         return DebugDirectivesOnly;
384     }
385     return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
386   }
387   return willEmitRemarks(Args) ? DebugDirectivesOnly : DisableDebugInfo;
388 }
389 
390 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
391                                     const InputInfo &Output,
392                                     const InputInfoList &Inputs,
393                                     const ArgList &Args,
394                                     const char *LinkingOutput) const {
395   const auto &TC =
396       static_cast<const toolchains::CudaToolChain &>(getToolChain());
397   assert(TC.getTriple().isNVPTX() && "Wrong platform");
398 
399   StringRef GPUArchName;
400   // If this is an OpenMP action we need to extract the device architecture
401   // from the -march=arch option. This option may come from -Xopenmp-target
402   // flag or the default value.
403   if (JA.isDeviceOffloading(Action::OFK_OpenMP)) {
404     GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
405     assert(!GPUArchName.empty() && "Must have an architecture passed in.");
406   } else
407     GPUArchName = JA.getOffloadingArch();
408 
409   // Obtain architecture from the action.
410   CudaArch gpu_arch = StringToCudaArch(GPUArchName);
411   assert(gpu_arch != CudaArch::UNKNOWN &&
412          "Device action expected to have an architecture.");
413 
414   // Check that our installation's ptxas supports gpu_arch.
415   if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
416     TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
417   }
418 
419   ArgStringList CmdArgs;
420   CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
421   DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
422   if (DIKind == EmitSameDebugInfoAsHost) {
423     // ptxas does not accept -g option if optimization is enabled, so
424     // we ignore the compiler's -O* options if we want debug info.
425     CmdArgs.push_back("-g");
426     CmdArgs.push_back("--dont-merge-basicblocks");
427     CmdArgs.push_back("--return-at-end");
428   } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
429     // Map the -O we received to -O{0,1,2,3}.
430     //
431     // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
432     // default, so it may correspond more closely to the spirit of clang -O2.
433 
434     // -O3 seems like the least-bad option when -Osomething is specified to
435     // clang but it isn't handled below.
436     StringRef OOpt = "3";
437     if (A->getOption().matches(options::OPT_O4) ||
438         A->getOption().matches(options::OPT_Ofast))
439       OOpt = "3";
440     else if (A->getOption().matches(options::OPT_O0))
441       OOpt = "0";
442     else if (A->getOption().matches(options::OPT_O)) {
443       // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
444       OOpt = llvm::StringSwitch<const char *>(A->getValue())
445                  .Case("1", "1")
446                  .Case("2", "2")
447                  .Case("3", "3")
448                  .Case("s", "2")
449                  .Case("z", "2")
450                  .Default("2");
451     }
452     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
453   } else {
454     // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
455     // to no optimizations, but ptxas's default is -O3.
456     CmdArgs.push_back("-O0");
457   }
458   if (DIKind == DebugDirectivesOnly)
459     CmdArgs.push_back("-lineinfo");
460 
461   // Pass -v to ptxas if it was passed to the driver.
462   if (Args.hasArg(options::OPT_v))
463     CmdArgs.push_back("-v");
464 
465   CmdArgs.push_back("--gpu-name");
466   CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
467   CmdArgs.push_back("--output-file");
468   CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
469   for (const auto& II : Inputs)
470     CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
471 
472   for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
473     CmdArgs.push_back(Args.MakeArgString(A));
474 
475   bool Relocatable = false;
476   if (JA.isOffloading(Action::OFK_OpenMP))
477     // In OpenMP we need to generate relocatable code.
478     Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
479                                options::OPT_fnoopenmp_relocatable_target,
480                                /*Default=*/true);
481   else if (JA.isOffloading(Action::OFK_Cuda))
482     Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
483                                options::OPT_fno_gpu_rdc, /*Default=*/false);
484 
485   if (Relocatable)
486     CmdArgs.push_back("-c");
487 
488   const char *Exec;
489   if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
490     Exec = A->getValue();
491   else
492     Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
493   C.addCommand(std::make_unique<Command>(
494       JA, *this,
495       ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
496                           "--options-file"},
497       Exec, CmdArgs, Inputs, Output));
498 }
499 
500 static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
501   bool includePTX = true;
502   for (Arg *A : Args) {
503     if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
504           A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
505       continue;
506     A->claim();
507     const StringRef ArchStr = A->getValue();
508     if (ArchStr == "all" || ArchStr == gpu_arch) {
509       includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
510       continue;
511     }
512   }
513   return includePTX;
514 }
515 
516 // All inputs to this linker must be from CudaDeviceActions, as we need to look
517 // at the Inputs' Actions in order to figure out which GPU architecture they
518 // correspond to.
519 void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
520                                  const InputInfo &Output,
521                                  const InputInfoList &Inputs,
522                                  const ArgList &Args,
523                                  const char *LinkingOutput) const {
524   const auto &TC =
525       static_cast<const toolchains::CudaToolChain &>(getToolChain());
526   assert(TC.getTriple().isNVPTX() && "Wrong platform");
527 
528   ArgStringList CmdArgs;
529   if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
530     CmdArgs.push_back("--cuda");
531   CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32");
532   CmdArgs.push_back(Args.MakeArgString("--create"));
533   CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
534   if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
535     CmdArgs.push_back("-g");
536 
537   for (const auto& II : Inputs) {
538     auto *A = II.getAction();
539     assert(A->getInputs().size() == 1 &&
540            "Device offload action is expected to have a single input");
541     const char *gpu_arch_str = A->getOffloadingArch();
542     assert(gpu_arch_str &&
543            "Device action expected to have associated a GPU architecture!");
544     CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
545 
546     if (II.getType() == types::TY_PP_Asm &&
547         !shouldIncludePTX(Args, gpu_arch_str))
548       continue;
549     // We need to pass an Arch of the form "sm_XX" for cubin files and
550     // "compute_XX" for ptx.
551     const char *Arch = (II.getType() == types::TY_PP_Asm)
552                            ? CudaArchToVirtualArchString(gpu_arch)
553                            : gpu_arch_str;
554     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
555                                          Arch + ",file=" + II.getFilename()));
556   }
557 
558   for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
559     CmdArgs.push_back(Args.MakeArgString(A));
560 
561   const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
562   C.addCommand(std::make_unique<Command>(
563       JA, *this,
564       ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
565                           "--options-file"},
566       Exec, CmdArgs, Inputs, Output));
567 }
568 
569 void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
570                                        const InputInfo &Output,
571                                        const InputInfoList &Inputs,
572                                        const ArgList &Args,
573                                        const char *LinkingOutput) const {
574   const auto &TC =
575       static_cast<const toolchains::CudaToolChain &>(getToolChain());
576   assert(TC.getTriple().isNVPTX() && "Wrong platform");
577 
578   ArgStringList CmdArgs;
579 
580   // OpenMP uses nvlink to link cubin files. The result will be embedded in the
581   // host binary by the host linker.
582   assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
583          "CUDA toolchain not expected for an OpenMP host device.");
584 
585   if (Output.isFilename()) {
586     CmdArgs.push_back("-o");
587     CmdArgs.push_back(Output.getFilename());
588   } else
589     assert(Output.isNothing() && "Invalid output.");
590   if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
591     CmdArgs.push_back("-g");
592 
593   if (Args.hasArg(options::OPT_v))
594     CmdArgs.push_back("-v");
595 
596   StringRef GPUArch =
597       Args.getLastArgValue(options::OPT_march_EQ);
598   assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
599 
600   CmdArgs.push_back("-arch");
601   CmdArgs.push_back(Args.MakeArgString(GPUArch));
602 
603   // Add paths specified in LIBRARY_PATH environment variable as -L options.
604   addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
605 
606   // Add paths for the default clang library path.
607   SmallString<256> DefaultLibPath =
608       llvm::sys::path::parent_path(TC.getDriver().Dir);
609   llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
610   CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
611 
612   for (const auto &II : Inputs) {
613     if (II.getType() == types::TY_LLVM_IR ||
614         II.getType() == types::TY_LTO_IR ||
615         II.getType() == types::TY_LTO_BC ||
616         II.getType() == types::TY_LLVM_BC) {
617       C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
618           << getToolChain().getTripleString();
619       continue;
620     }
621 
622     // Currently, we only pass the input files to the linker, we do not pass
623     // any libraries that may be valid only for the host.
624     if (!II.isFilename())
625       continue;
626 
627     const char *CubinF = C.addTempFile(
628         C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
629 
630     CmdArgs.push_back(CubinF);
631   }
632 
633   const char *Exec =
634       Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
635   C.addCommand(std::make_unique<Command>(
636       JA, *this,
637       ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
638                           "--options-file"},
639       Exec, CmdArgs, Inputs, Output));
640 }
641 
642 /// CUDA toolchain.  Our assembler is ptxas, and our "linker" is fatbinary,
643 /// which isn't properly a linker but nonetheless performs the step of stitching
644 /// together object files from the assembler into a single blob.
645 
646 CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
647                              const ToolChain &HostTC, const ArgList &Args,
648                              const Action::OffloadKind OK)
649     : ToolChain(D, Triple, Args), HostTC(HostTC),
650       CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
651   if (CudaInstallation.isValid()) {
652     CudaInstallation.WarnIfUnsupportedVersion();
653     getProgramPaths().push_back(std::string(CudaInstallation.getBinPath()));
654   }
655   // Lookup binaries into the driver directory, this is used to
656   // discover the clang-offload-bundler executable.
657   getProgramPaths().push_back(getDriver().Dir);
658 }
659 
660 std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
661   // Only object files are changed, for example assembly files keep their .s
662   // extensions. CUDA also continues to use .o as they don't use nvlink but
663   // fatbinary.
664   if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object))
665     return ToolChain::getInputFilename(Input);
666 
667   // Replace extension for object files with cubin because nvlink relies on
668   // these particular file names.
669   SmallString<256> Filename(ToolChain::getInputFilename(Input));
670   llvm::sys::path::replace_extension(Filename, "cubin");
671   return std::string(Filename.str());
672 }
673 
674 void CudaToolChain::addClangTargetOptions(
675     const llvm::opt::ArgList &DriverArgs,
676     llvm::opt::ArgStringList &CC1Args,
677     Action::OffloadKind DeviceOffloadingKind) const {
678   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
679 
680   StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
681   assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
682   assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
683           DeviceOffloadingKind == Action::OFK_Cuda) &&
684          "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
685 
686   if (DeviceOffloadingKind == Action::OFK_Cuda) {
687     CC1Args.push_back("-fcuda-is-device");
688 
689     if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
690                            options::OPT_fno_cuda_approx_transcendentals, false))
691       CC1Args.push_back("-fcuda-approx-transcendentals");
692 
693     if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
694                            false))
695       CC1Args.push_back("-fgpu-rdc");
696   }
697 
698   if (DriverArgs.hasArg(options::OPT_nogpulib))
699     return;
700 
701   std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
702 
703   if (LibDeviceFile.empty()) {
704     if (DeviceOffloadingKind == Action::OFK_OpenMP &&
705         DriverArgs.hasArg(options::OPT_S))
706       return;
707 
708     getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
709     return;
710   }
711 
712   CC1Args.push_back("-mlink-builtin-bitcode");
713   CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
714 
715   std::string CudaVersionStr;
716 
717   // New CUDA versions often introduce new instructions that are only supported
718   // by new PTX version, so we need to raise PTX level to enable them in NVPTX
719   // back-end.
720   const char *PtxFeature = nullptr;
721   switch (CudaInstallation.version()) {
722 #define CASE_CUDA_VERSION(CUDA_VER, PTX_VER)                                   \
723   case CudaVersion::CUDA_##CUDA_VER:                                           \
724     CudaVersionStr = #CUDA_VER;                                                \
725     PtxFeature = "+ptx" #PTX_VER;                                              \
726     break;
727     CASE_CUDA_VERSION(110, 70);
728     CASE_CUDA_VERSION(102, 65);
729     CASE_CUDA_VERSION(101, 64);
730     CASE_CUDA_VERSION(100, 63);
731     CASE_CUDA_VERSION(92, 61);
732     CASE_CUDA_VERSION(91, 61);
733     CASE_CUDA_VERSION(90, 60);
734 #undef CASE_CUDA_VERSION
735   default:
736     // If unknown CUDA version, we take it as CUDA 8.0. Same assumption is also
737     // made in libomptarget/deviceRTLs.
738     CudaVersionStr = "80";
739     PtxFeature = "+ptx42";
740   }
741   CC1Args.append({"-target-feature", PtxFeature});
742   if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr,
743                          options::OPT_fno_cuda_short_ptr, false))
744     CC1Args.append({"-mllvm", "--nvptx-short-ptr"});
745 
746   if (CudaInstallation.version() >= CudaVersion::UNKNOWN)
747     CC1Args.push_back(DriverArgs.MakeArgString(
748         Twine("-target-sdk-version=") +
749         CudaVersionToString(CudaInstallation.version())));
750 
751   if (DeviceOffloadingKind == Action::OFK_OpenMP) {
752     SmallVector<StringRef, 8> LibraryPaths;
753     // Add user defined library paths from LIBRARY_PATH.
754     llvm::Optional<std::string> LibPath =
755         llvm::sys::Process::GetEnv("LIBRARY_PATH");
756     if (LibPath) {
757       SmallVector<StringRef, 8> Frags;
758       const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
759       llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
760       for (StringRef Path : Frags)
761         LibraryPaths.emplace_back(Path.trim());
762     }
763 
764     // Add path to lib / lib64 folder.
765     SmallString<256> DefaultLibPath =
766         llvm::sys::path::parent_path(getDriver().Dir);
767     llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
768     LibraryPaths.emplace_back(DefaultLibPath.c_str());
769 
770     // First check whether user specifies bc library
771     if (const Arg *A =
772             DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_bc_path_EQ)) {
773       std::string LibOmpTargetName(A->getValue());
774       if (llvm::sys::fs::exists(LibOmpTargetName)) {
775         CC1Args.push_back("-mlink-builtin-bitcode");
776         CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetName));
777       } else {
778         getDriver().Diag(diag::err_drv_omp_offload_target_bcruntime_not_found)
779             << LibOmpTargetName;
780       }
781     } else {
782       bool FoundBCLibrary = false;
783 
784       std::string LibOmpTargetName = "libomptarget-nvptx-cuda_" +
785                                      CudaVersionStr + "-" + GpuArch.str() +
786                                      ".bc";
787 
788       for (StringRef LibraryPath : LibraryPaths) {
789         SmallString<128> LibOmpTargetFile(LibraryPath);
790         llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
791         if (llvm::sys::fs::exists(LibOmpTargetFile)) {
792           CC1Args.push_back("-mlink-builtin-bitcode");
793           CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
794           FoundBCLibrary = true;
795           break;
796         }
797       }
798       if (!FoundBCLibrary)
799         getDriver().Diag(diag::err_drv_omp_offload_target_missingbcruntime)
800             << LibOmpTargetName;
801     }
802   }
803 }
804 
805 llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType(
806     const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
807     const llvm::fltSemantics *FPType) const {
808   if (JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
809     if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
810         DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
811                            options::OPT_fno_cuda_flush_denormals_to_zero,
812                            false))
813       return llvm::DenormalMode::getPreserveSign();
814   }
815 
816   assert(JA.getOffloadingDeviceKind() != Action::OFK_Host);
817   return llvm::DenormalMode::getIEEE();
818 }
819 
820 bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
821   const Option &O = A->getOption();
822   return (O.matches(options::OPT_gN_Group) &&
823           !O.matches(options::OPT_gmodules)) ||
824          O.matches(options::OPT_g_Flag) ||
825          O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) ||
826          O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) ||
827          O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) ||
828          O.matches(options::OPT_gdwarf_5) ||
829          O.matches(options::OPT_gcolumn_info);
830 }
831 
832 void CudaToolChain::adjustDebugInfoKind(
833     codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
834   switch (mustEmitDebugInfo(Args)) {
835   case DisableDebugInfo:
836     DebugInfoKind = codegenoptions::NoDebugInfo;
837     break;
838   case DebugDirectivesOnly:
839     DebugInfoKind = codegenoptions::DebugDirectivesOnly;
840     break;
841   case EmitSameDebugInfoAsHost:
842     // Use same debug info level as the host.
843     break;
844   }
845 }
846 
847 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
848                                        ArgStringList &CC1Args) const {
849   // Check our CUDA version if we're going to include the CUDA headers.
850   if (!DriverArgs.hasArg(options::OPT_nogpuinc) &&
851       !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
852     StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
853     assert(!Arch.empty() && "Must have an explicit GPU arch.");
854     CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch));
855   }
856   CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
857 }
858 
859 llvm::opt::DerivedArgList *
860 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
861                              StringRef BoundArch,
862                              Action::OffloadKind DeviceOffloadKind) const {
863   DerivedArgList *DAL =
864       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
865   if (!DAL)
866     DAL = new DerivedArgList(Args.getBaseArgs());
867 
868   const OptTable &Opts = getDriver().getOpts();
869 
870   // For OpenMP device offloading, append derived arguments. Make sure
871   // flags are not duplicated.
872   // Also append the compute capability.
873   if (DeviceOffloadKind == Action::OFK_OpenMP) {
874     for (Arg *A : Args) {
875       bool IsDuplicate = false;
876       for (Arg *DALArg : *DAL) {
877         if (A == DALArg) {
878           IsDuplicate = true;
879           break;
880         }
881       }
882       if (!IsDuplicate)
883         DAL->append(A);
884     }
885 
886     StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
887     if (Arch.empty())
888       DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
889                         CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
890 
891     return DAL;
892   }
893 
894   for (Arg *A : Args) {
895     DAL->append(A);
896   }
897 
898   if (!BoundArch.empty()) {
899     DAL->eraseArg(options::OPT_march_EQ);
900     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
901   }
902   return DAL;
903 }
904 
905 Tool *CudaToolChain::buildAssembler() const {
906   return new tools::NVPTX::Assembler(*this);
907 }
908 
909 Tool *CudaToolChain::buildLinker() const {
910   if (OK == Action::OFK_OpenMP)
911     return new tools::NVPTX::OpenMPLinker(*this);
912   return new tools::NVPTX::Linker(*this);
913 }
914 
915 void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
916   HostTC.addClangWarningOptions(CC1Args);
917 }
918 
919 ToolChain::CXXStdlibType
920 CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
921   return HostTC.GetCXXStdlibType(Args);
922 }
923 
924 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
925                                               ArgStringList &CC1Args) const {
926   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
927 }
928 
929 void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
930                                                  ArgStringList &CC1Args) const {
931   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
932 }
933 
934 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
935                                         ArgStringList &CC1Args) const {
936   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
937 }
938 
939 SanitizerMask CudaToolChain::getSupportedSanitizers() const {
940   // The CudaToolChain only supports sanitizers in the sense that it allows
941   // sanitizer arguments on the command line if they are supported by the host
942   // toolchain. The CudaToolChain will actually ignore any command line
943   // arguments for any of these "supported" sanitizers. That means that no
944   // sanitization of device code is actually supported at this time.
945   //
946   // This behavior is necessary because the host and device toolchains
947   // invocations often share the command line, so the device toolchain must
948   // tolerate flags meant only for the host toolchain.
949   return HostTC.getSupportedSanitizers();
950 }
951 
952 VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
953                                                const ArgList &Args) const {
954   return HostTC.computeMSVCVersion(D, Args);
955 }
956