clang  10.0.0git
Cuda.cpp
Go to the documentation of this file.
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"
15 #include "clang/Driver/Distro.h"
16 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/Options.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/VirtualFileSystem.h"
25 #include <system_error>
26 
27 using namespace clang::driver;
28 using namespace clang::driver::toolchains;
29 using namespace clang::driver::tools;
30 using namespace clang;
31 using namespace llvm::opt;
32 
33 // Parses the contents of version.txt in an CUDA installation. It should
34 // contain one line of the from e.g. "CUDA Version 7.5.2".
35 static CudaVersion ParseCudaVersionFile(const Driver &D, llvm::StringRef V) {
36  if (!V.startswith("CUDA Version "))
37  return CudaVersion::UNKNOWN;
38  V = V.substr(strlen("CUDA Version "));
39  SmallVector<StringRef,4> VersionParts;
40  V.split(VersionParts, '.');
41  if (VersionParts.size() < 2)
42  return CudaVersion::UNKNOWN;
43  std::string MajorMinor = join_items(".", VersionParts[0], VersionParts[1]);
44  CudaVersion Version = CudaStringToVersion(MajorMinor);
45  if (Version != CudaVersion::UNKNOWN)
46  return Version;
47 
48  // Issue a warning and assume that the version we've found is compatible with
49  // the latest version we support.
50  D.Diag(diag::warn_drv_unknown_cuda_version)
51  << MajorMinor << CudaVersionToString(CudaVersion::LATEST);
52  return CudaVersion::LATEST;
53 }
54 
56  const Driver &D, const llvm::Triple &HostTriple,
57  const llvm::opt::ArgList &Args)
58  : D(D) {
59  struct Candidate {
60  std::string Path;
61  bool StrictChecking;
62 
63  Candidate(std::string Path, bool StrictChecking = false)
64  : Path(Path), StrictChecking(StrictChecking) {}
65  };
66  SmallVector<Candidate, 4> Candidates;
67 
68  // In decreasing order so we prefer newer versions to older versions.
69  std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
70 
71  if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) {
72  Candidates.emplace_back(
73  Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
74  } else if (HostTriple.isOSWindows()) {
75  for (const char *Ver : Versions)
76  Candidates.emplace_back(
77  D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
78  Ver);
79  } else {
80  if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) {
81  // Try to find ptxas binary. If the executable is located in a directory
82  // called 'bin/', its parent directory might be a good guess for a valid
83  // CUDA installation.
84  // However, some distributions might installs 'ptxas' to /usr/bin. In that
85  // case the candidate would be '/usr' which passes the following checks
86  // because '/usr/include' exists as well. To avoid this case, we always
87  // check for the directory potentially containing files for libdevice,
88  // even if the user passes -nocudalib.
89  if (llvm::ErrorOr<std::string> ptxas =
90  llvm::sys::findProgramByName("ptxas")) {
91  SmallString<256> ptxasAbsolutePath;
92  llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath);
93 
94  StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath);
95  if (llvm::sys::path::filename(ptxasDir) == "bin")
96  Candidates.emplace_back(llvm::sys::path::parent_path(ptxasDir),
97  /*StrictChecking=*/true);
98  }
99  }
100 
101  Candidates.emplace_back(D.SysRoot + "/usr/local/cuda");
102  for (const char *Ver : Versions)
103  Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
104 
105  Distro Dist(D.getVFS(), llvm::Triple(llvm::sys::getProcessTriple()));
106  if (Dist.IsDebian() || Dist.IsUbuntu())
107  // Special case for Debian to have nvidia-cuda-toolkit work
108  // out of the box. More info on http://bugs.debian.org/882505
109  Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
110  }
111 
112  bool NoCudaLib = Args.hasArg(options::OPT_nogpulib);
113 
114  for (const auto &Candidate : Candidates) {
115  InstallPath = Candidate.Path;
116  if (InstallPath.empty() || !D.getVFS().exists(InstallPath))
117  continue;
118 
119  BinPath = InstallPath + "/bin";
120  IncludePath = InstallPath + "/include";
121  LibDevicePath = InstallPath + "/nvvm/libdevice";
122 
123  auto &FS = D.getVFS();
124  if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
125  continue;
126  bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
127  if (CheckLibDevice && !FS.exists(LibDevicePath))
128  continue;
129 
130  // On Linux, we have both lib and lib64 directories, and we need to choose
131  // based on our triple. On MacOS, we have only a lib directory.
132  //
133  // It's sufficient for our purposes to be flexible: If both lib and lib64
134  // exist, we choose whichever one matches our triple. Otherwise, if only
135  // lib exists, we use it.
136  if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64"))
137  LibPath = InstallPath + "/lib64";
138  else if (FS.exists(InstallPath + "/lib"))
139  LibPath = InstallPath + "/lib";
140  else
141  continue;
142 
143  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
144  FS.getBufferForFile(InstallPath + "/version.txt");
145  if (!VersionFile) {
146  // CUDA 7.0 doesn't have a version.txt, so guess that's our version if
147  // version.txt isn't present.
148  Version = CudaVersion::CUDA_70;
149  } else {
150  Version = ParseCudaVersionFile(D, (*VersionFile)->getBuffer());
151  }
152 
153  if (Version >= CudaVersion::CUDA_90) {
154  // CUDA-9+ uses single libdevice file for all GPU variants.
155  std::string FilePath = LibDevicePath + "/libdevice.10.bc";
156  if (FS.exists(FilePath)) {
157  for (const char *GpuArchName :
158  {"sm_30", "sm_32", "sm_35", "sm_37", "sm_50", "sm_52", "sm_53",
159  "sm_60", "sm_61", "sm_62", "sm_70", "sm_72", "sm_75"}) {
160  const CudaArch GpuArch = StringToCudaArch(GpuArchName);
161  if (Version >= MinVersionForCudaArch(GpuArch) &&
162  Version <= MaxVersionForCudaArch(GpuArch))
163  LibDeviceMap[GpuArchName] = FilePath;
164  }
165  }
166  } else {
167  std::error_code EC;
168  for (llvm::sys::fs::directory_iterator LI(LibDevicePath, EC), LE;
169  !EC && LI != LE; LI = LI.increment(EC)) {
170  StringRef FilePath = LI->path();
171  StringRef FileName = llvm::sys::path::filename(FilePath);
172  // Process all bitcode filenames that look like
173  // libdevice.compute_XX.YY.bc
174  const StringRef LibDeviceName = "libdevice.";
175  if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
176  continue;
177  StringRef GpuArch = FileName.slice(
178  LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
179  LibDeviceMap[GpuArch] = FilePath.str();
180  // Insert map entries for specific devices with this compute
181  // capability. NVCC's choice of the libdevice library version is
182  // rather peculiar and depends on the CUDA version.
183  if (GpuArch == "compute_20") {
184  LibDeviceMap["sm_20"] = FilePath;
185  LibDeviceMap["sm_21"] = FilePath;
186  LibDeviceMap["sm_32"] = FilePath;
187  } else if (GpuArch == "compute_30") {
188  LibDeviceMap["sm_30"] = FilePath;
189  if (Version < CudaVersion::CUDA_80) {
190  LibDeviceMap["sm_50"] = FilePath;
191  LibDeviceMap["sm_52"] = FilePath;
192  LibDeviceMap["sm_53"] = FilePath;
193  }
194  LibDeviceMap["sm_60"] = FilePath;
195  LibDeviceMap["sm_61"] = FilePath;
196  LibDeviceMap["sm_62"] = FilePath;
197  } else if (GpuArch == "compute_35") {
198  LibDeviceMap["sm_35"] = FilePath;
199  LibDeviceMap["sm_37"] = FilePath;
200  } else if (GpuArch == "compute_50") {
201  if (Version >= CudaVersion::CUDA_80) {
202  LibDeviceMap["sm_50"] = FilePath;
203  LibDeviceMap["sm_52"] = FilePath;
204  LibDeviceMap["sm_53"] = FilePath;
205  }
206  }
207  }
208  }
209 
210  // Check that we have found at least one libdevice that we can link in if
211  // -nocudalib hasn't been specified.
212  if (LibDeviceMap.empty() && !NoCudaLib)
213  continue;
214 
215  IsValid = true;
216  break;
217  }
218 }
219 
221  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
222  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
223  // Add cuda_wrappers/* to our system include path. This lets us wrap
224  // standard library headers.
225  SmallString<128> P(D.ResourceDir);
226  llvm::sys::path::append(P, "include");
227  llvm::sys::path::append(P, "cuda_wrappers");
228  CC1Args.push_back("-internal-isystem");
229  CC1Args.push_back(DriverArgs.MakeArgString(P));
230  }
231 
232  if (DriverArgs.hasArg(options::OPT_nocudainc))
233  return;
234 
235  if (!isValid()) {
236  D.Diag(diag::err_drv_no_cuda_installation);
237  return;
238  }
239 
240  CC1Args.push_back("-internal-isystem");
241  CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
242  CC1Args.push_back("-include");
243  CC1Args.push_back("__clang_cuda_runtime_wrapper.h");
244 }
245 
247  CudaArch Arch) const {
248  if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
249  ArchsWithBadVersion.count(Arch) > 0)
250  return;
251 
252  auto MinVersion = MinVersionForCudaArch(Arch);
253  auto MaxVersion = MaxVersionForCudaArch(Arch);
254  if (Version < MinVersion || Version > MaxVersion) {
255  ArchsWithBadVersion.insert(Arch);
256  D.Diag(diag::err_drv_cuda_version_unsupported)
257  << CudaArchToString(Arch) << CudaVersionToString(MinVersion)
258  << CudaVersionToString(MaxVersion) << InstallPath
259  << CudaVersionToString(Version);
260  }
261 }
262 
263 void CudaInstallationDetector::print(raw_ostream &OS) const {
264  if (isValid())
265  OS << "Found CUDA installation: " << InstallPath << ", version "
266  << CudaVersionToString(Version) << "\n";
267 }
268 
269 namespace {
270 /// Debug info level for the NVPTX devices. We may need to emit different debug
271 /// info level for the host and for the device itselfi. This type controls
272 /// emission of the debug info for the devices. It either prohibits disable info
273 /// emission completely, or emits debug directives only, or emits same debug
274 /// info as for the host.
276  DisableDebugInfo, /// Do not emit debug info for the devices.
277  DebugDirectivesOnly, /// Emit only debug directives.
278  EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
279  /// host.
280 };
281 } // anonymous namespace
282 
283 /// Define debug info level for the NVPTX devices. If the debug info for both
284 /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
285 /// only debug directives are requested for the both host and device
286 /// (-gline-directvies-only), or the debug info only for the device is disabled
287 /// (optimization is on and --cuda-noopt-device-debug was not specified), the
288 /// debug directves only must be emitted for the device. Otherwise, use the same
289 /// debug info level just like for the host (with the limitations of only
290 /// supported DWARF2 standard).
291 static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
292  const Arg *A = Args.getLastArg(options::OPT_O_Group);
293  bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) ||
294  Args.hasFlag(options::OPT_cuda_noopt_device_debug,
295  options::OPT_no_cuda_noopt_device_debug,
296  /*Default=*/false);
297  if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
298  const Option &Opt = A->getOption();
299  if (Opt.matches(options::OPT_gN_Group)) {
300  if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0))
301  return DisableDebugInfo;
302  if (Opt.matches(options::OPT_gline_directives_only))
303  return DebugDirectivesOnly;
304  }
305  return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
306  }
307  return DisableDebugInfo;
308 }
309 
310 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
311  const InputInfo &Output,
312  const InputInfoList &Inputs,
313  const ArgList &Args,
314  const char *LinkingOutput) const {
315  const auto &TC =
316  static_cast<const toolchains::CudaToolChain &>(getToolChain());
317  assert(TC.getTriple().isNVPTX() && "Wrong platform");
318 
319  StringRef GPUArchName;
320  // If this is an OpenMP action we need to extract the device architecture
321  // from the -march=arch option. This option may come from -Xopenmp-target
322  // flag or the default value.
324  GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
325  assert(!GPUArchName.empty() && "Must have an architecture passed in.");
326  } else
327  GPUArchName = JA.getOffloadingArch();
328 
329  // Obtain architecture from the action.
330  CudaArch gpu_arch = StringToCudaArch(GPUArchName);
331  assert(gpu_arch != CudaArch::UNKNOWN &&
332  "Device action expected to have an architecture.");
333 
334  // Check that our installation's ptxas supports gpu_arch.
335  if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
336  TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
337  }
338 
339  ArgStringList CmdArgs;
340  CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
342  if (DIKind == EmitSameDebugInfoAsHost) {
343  // ptxas does not accept -g option if optimization is enabled, so
344  // we ignore the compiler's -O* options if we want debug info.
345  CmdArgs.push_back("-g");
346  CmdArgs.push_back("--dont-merge-basicblocks");
347  CmdArgs.push_back("--return-at-end");
348  } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
349  // Map the -O we received to -O{0,1,2,3}.
350  //
351  // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
352  // default, so it may correspond more closely to the spirit of clang -O2.
353 
354  // -O3 seems like the least-bad option when -Osomething is specified to
355  // clang but it isn't handled below.
356  StringRef OOpt = "3";
357  if (A->getOption().matches(options::OPT_O4) ||
358  A->getOption().matches(options::OPT_Ofast))
359  OOpt = "3";
360  else if (A->getOption().matches(options::OPT_O0))
361  OOpt = "0";
362  else if (A->getOption().matches(options::OPT_O)) {
363  // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
364  OOpt = llvm::StringSwitch<const char *>(A->getValue())
365  .Case("1", "1")
366  .Case("2", "2")
367  .Case("3", "3")
368  .Case("s", "2")
369  .Case("z", "2")
370  .Default("2");
371  }
372  CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
373  } else {
374  // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
375  // to no optimizations, but ptxas's default is -O3.
376  CmdArgs.push_back("-O0");
377  }
378  if (DIKind == DebugDirectivesOnly)
379  CmdArgs.push_back("-lineinfo");
380 
381  // Pass -v to ptxas if it was passed to the driver.
382  if (Args.hasArg(options::OPT_v))
383  CmdArgs.push_back("-v");
384 
385  CmdArgs.push_back("--gpu-name");
386  CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
387  CmdArgs.push_back("--output-file");
388  CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
389  for (const auto& II : Inputs)
390  CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
391 
392  for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
393  CmdArgs.push_back(Args.MakeArgString(A));
394 
395  bool Relocatable = false;
397  // In OpenMP we need to generate relocatable code.
398  Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
399  options::OPT_fnoopenmp_relocatable_target,
400  /*Default=*/true);
401  else if (JA.isOffloading(Action::OFK_Cuda))
402  Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
403  options::OPT_fno_gpu_rdc, /*Default=*/false);
404 
405  if (Relocatable)
406  CmdArgs.push_back("-c");
407 
408  const char *Exec;
409  if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
410  Exec = A->getValue();
411  else
412  Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
413  C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
414 }
415 
416 static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
417  bool includePTX = true;
418  for (Arg *A : Args) {
419  if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
420  A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
421  continue;
422  A->claim();
423  const StringRef ArchStr = A->getValue();
424  if (ArchStr == "all" || ArchStr == gpu_arch) {
425  includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
426  continue;
427  }
428  }
429  return includePTX;
430 }
431 
432 // All inputs to this linker must be from CudaDeviceActions, as we need to look
433 // at the Inputs' Actions in order to figure out which GPU architecture they
434 // correspond to.
435 void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
436  const InputInfo &Output,
437  const InputInfoList &Inputs,
438  const ArgList &Args,
439  const char *LinkingOutput) const {
440  const auto &TC =
441  static_cast<const toolchains::CudaToolChain &>(getToolChain());
442  assert(TC.getTriple().isNVPTX() && "Wrong platform");
443 
444  ArgStringList CmdArgs;
445  if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
446  CmdArgs.push_back("--cuda");
447  CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32");
448  CmdArgs.push_back(Args.MakeArgString("--create"));
449  CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
450  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
451  CmdArgs.push_back("-g");
452 
453  for (const auto& II : Inputs) {
454  auto *A = II.getAction();
455  assert(A->getInputs().size() == 1 &&
456  "Device offload action is expected to have a single input");
457  const char *gpu_arch_str = A->getOffloadingArch();
458  assert(gpu_arch_str &&
459  "Device action expected to have associated a GPU architecture!");
460  CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
461 
462  if (II.getType() == types::TY_PP_Asm &&
463  !shouldIncludePTX(Args, gpu_arch_str))
464  continue;
465  // We need to pass an Arch of the form "sm_XX" for cubin files and
466  // "compute_XX" for ptx.
467  const char *Arch =
468  (II.getType() == types::TY_PP_Asm)
470  : gpu_arch_str;
471  CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
472  Arch + ",file=" + II.getFilename()));
473  }
474 
475  for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
476  CmdArgs.push_back(Args.MakeArgString(A));
477 
478  const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
479  C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
480 }
481 
482 void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
483  const InputInfo &Output,
484  const InputInfoList &Inputs,
485  const ArgList &Args,
486  const char *LinkingOutput) const {
487  const auto &TC =
488  static_cast<const toolchains::CudaToolChain &>(getToolChain());
489  assert(TC.getTriple().isNVPTX() && "Wrong platform");
490 
491  ArgStringList CmdArgs;
492 
493  // OpenMP uses nvlink to link cubin files. The result will be embedded in the
494  // host binary by the host linker.
495  assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
496  "CUDA toolchain not expected for an OpenMP host device.");
497 
498  if (Output.isFilename()) {
499  CmdArgs.push_back("-o");
500  CmdArgs.push_back(Output.getFilename());
501  } else
502  assert(Output.isNothing() && "Invalid output.");
503  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
504  CmdArgs.push_back("-g");
505 
506  if (Args.hasArg(options::OPT_v))
507  CmdArgs.push_back("-v");
508 
509  StringRef GPUArch =
510  Args.getLastArgValue(options::OPT_march_EQ);
511  assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
512 
513  CmdArgs.push_back("-arch");
514  CmdArgs.push_back(Args.MakeArgString(GPUArch));
515 
516  // Assume that the directory specified with --libomptarget_nvptx_path
517  // contains the static library libomptarget-nvptx.a.
518  if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
519  CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue()));
520 
521  // Add paths specified in LIBRARY_PATH environment variable as -L options.
522  addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
523 
524  // Add paths for the default clang library path.
525  SmallString<256> DefaultLibPath =
526  llvm::sys::path::parent_path(TC.getDriver().Dir);
527  llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
528  CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
529 
530  // Add linking against library implementing OpenMP calls on NVPTX target.
531  CmdArgs.push_back("-lomptarget-nvptx");
532 
533  for (const auto &II : Inputs) {
534  if (II.getType() == types::TY_LLVM_IR ||
535  II.getType() == types::TY_LTO_IR ||
536  II.getType() == types::TY_LTO_BC ||
537  II.getType() == types::TY_LLVM_BC) {
538  C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
539  << getToolChain().getTripleString();
540  continue;
541  }
542 
543  // Currently, we only pass the input files to the linker, we do not pass
544  // any libraries that may be valid only for the host.
545  if (!II.isFilename())
546  continue;
547 
548  const char *CubinF = C.addTempFile(
549  C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
550 
551  CmdArgs.push_back(CubinF);
552  }
553 
554  const char *Exec =
555  Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
556  C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
557 }
558 
559 /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
560 /// which isn't properly a linker but nonetheless performs the step of stitching
561 /// together object files from the assembler into a single blob.
562 
563 CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
564  const ToolChain &HostTC, const ArgList &Args,
565  const Action::OffloadKind OK)
566  : ToolChain(D, Triple, Args), HostTC(HostTC),
567  CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
570  // Lookup binaries into the driver directory, this is used to
571  // discover the clang-offload-bundler executable.
572  getProgramPaths().push_back(getDriver().Dir);
573 }
574 
575 std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
576  // Only object files are changed, for example assembly files keep their .s
577  // extensions. CUDA also continues to use .o as they don't use nvlink but
578  // fatbinary.
579  if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object))
580  return ToolChain::getInputFilename(Input);
581 
582  // Replace extension for object files with cubin because nvlink relies on
583  // these particular file names.
585  llvm::sys::path::replace_extension(Filename, "cubin");
586  return Filename.str();
587 }
588 
590  const llvm::opt::ArgList &DriverArgs,
591  llvm::opt::ArgStringList &CC1Args,
592  Action::OffloadKind DeviceOffloadingKind) const {
593  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
594 
595  StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
596  assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
597  assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
598  DeviceOffloadingKind == Action::OFK_Cuda) &&
599  "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
600 
601  if (DeviceOffloadingKind == Action::OFK_Cuda) {
602  CC1Args.push_back("-fcuda-is-device");
603 
604  if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
605  options::OPT_fno_cuda_flush_denormals_to_zero, false))
606  CC1Args.push_back("-fcuda-flush-denormals-to-zero");
607 
608  if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
609  options::OPT_fno_cuda_approx_transcendentals, false))
610  CC1Args.push_back("-fcuda-approx-transcendentals");
611 
612  if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
613  false))
614  CC1Args.push_back("-fgpu-rdc");
615  }
616 
617  if (DriverArgs.hasArg(options::OPT_nogpulib))
618  return;
619 
620  std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
621 
622  if (LibDeviceFile.empty()) {
623  if (DeviceOffloadingKind == Action::OFK_OpenMP &&
624  DriverArgs.hasArg(options::OPT_S))
625  return;
626 
627  getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
628  return;
629  }
630 
631  CC1Args.push_back("-mlink-builtin-bitcode");
632  CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
633 
634  // New CUDA versions often introduce new instructions that are only supported
635  // by new PTX version, so we need to raise PTX level to enable them in NVPTX
636  // back-end.
637  const char *PtxFeature = nullptr;
638  switch(CudaInstallation.version()) {
640  PtxFeature = "+ptx64";
641  break;
643  PtxFeature = "+ptx63";
644  break;
646  PtxFeature = "+ptx61";
647  break;
649  PtxFeature = "+ptx61";
650  break;
652  PtxFeature = "+ptx60";
653  break;
654  default:
655  PtxFeature = "+ptx42";
656  }
657  CC1Args.append({"-target-feature", PtxFeature});
658  if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr,
659  options::OPT_fno_cuda_short_ptr, false))
660  CC1Args.append({"-mllvm", "--nvptx-short-ptr"});
661 
663  CC1Args.push_back(DriverArgs.MakeArgString(
664  Twine("-target-sdk-version=") +
666 
667  if (DeviceOffloadingKind == Action::OFK_OpenMP) {
668  SmallVector<StringRef, 8> LibraryPaths;
669  if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
670  LibraryPaths.push_back(A->getValue());
671 
672  // Add user defined library paths from LIBRARY_PATH.
674  llvm::sys::Process::GetEnv("LIBRARY_PATH");
675  if (LibPath) {
677  const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
678  llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
679  for (StringRef Path : Frags)
680  LibraryPaths.emplace_back(Path.trim());
681  }
682 
683  // Add path to lib / lib64 folder.
684  SmallString<256> DefaultLibPath =
685  llvm::sys::path::parent_path(getDriver().Dir);
686  llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
687  LibraryPaths.emplace_back(DefaultLibPath.c_str());
688 
689  std::string LibOmpTargetName =
690  "libomptarget-nvptx-" + GpuArch.str() + ".bc";
691  bool FoundBCLibrary = false;
692  for (StringRef LibraryPath : LibraryPaths) {
693  SmallString<128> LibOmpTargetFile(LibraryPath);
694  llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
695  if (llvm::sys::fs::exists(LibOmpTargetFile)) {
696  CC1Args.push_back("-mlink-builtin-bitcode");
697  CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
698  FoundBCLibrary = true;
699  break;
700  }
701  }
702  if (!FoundBCLibrary)
703  getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
704  << LibOmpTargetName;
705  }
706 }
707 
708 bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
709  const Option &O = A->getOption();
710  return (O.matches(options::OPT_gN_Group) &&
711  !O.matches(options::OPT_gmodules)) ||
712  O.matches(options::OPT_g_Flag) ||
713  O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) ||
714  O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) ||
715  O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) ||
716  O.matches(options::OPT_gdwarf_5) ||
717  O.matches(options::OPT_gcolumn_info);
718 }
719 
721  codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
722  switch (mustEmitDebugInfo(Args)) {
723  case DisableDebugInfo:
724  DebugInfoKind = codegenoptions::NoDebugInfo;
725  break;
726  case DebugDirectivesOnly:
727  DebugInfoKind = codegenoptions::DebugDirectivesOnly;
728  break;
729  case EmitSameDebugInfoAsHost:
730  // Use same debug info level as the host.
731  break;
732  }
733 }
734 
735 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
736  ArgStringList &CC1Args) const {
737  // Check our CUDA version if we're going to include the CUDA headers.
738  if (!DriverArgs.hasArg(options::OPT_nocudainc) &&
739  !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
740  StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
741  assert(!Arch.empty() && "Must have an explicit GPU arch.");
743  }
744  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
745 }
746 
747 llvm::opt::DerivedArgList *
748 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
749  StringRef BoundArch,
750  Action::OffloadKind DeviceOffloadKind) const {
751  DerivedArgList *DAL =
752  HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
753  if (!DAL)
754  DAL = new DerivedArgList(Args.getBaseArgs());
755 
756  const OptTable &Opts = getDriver().getOpts();
757 
758  // For OpenMP device offloading, append derived arguments. Make sure
759  // flags are not duplicated.
760  // Also append the compute capability.
761  if (DeviceOffloadKind == Action::OFK_OpenMP) {
762  for (Arg *A : Args) {
763  bool IsDuplicate = false;
764  for (Arg *DALArg : *DAL) {
765  if (A == DALArg) {
766  IsDuplicate = true;
767  break;
768  }
769  }
770  if (!IsDuplicate)
771  DAL->append(A);
772  }
773 
774  StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
775  if (Arch.empty())
776  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
777  CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
778 
779  return DAL;
780  }
781 
782  for (Arg *A : Args) {
783  if (A->getOption().matches(options::OPT_Xarch__)) {
784  // Skip this argument unless the architecture matches BoundArch
785  if (BoundArch.empty() || A->getValue(0) != BoundArch)
786  continue;
787 
788  unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
789  unsigned Prev = Index;
790  std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
791 
792  // If the argument parsing failed or more than one argument was
793  // consumed, the -Xarch_ argument's parameter tried to consume
794  // extra arguments. Emit an error and ignore.
795  //
796  // We also want to disallow any options which would alter the
797  // driver behavior; that isn't going to work in our model. We
798  // use isDriverOption() as an approximation, although things
799  // like -O4 are going to slip through.
800  if (!XarchArg || Index > Prev + 1) {
801  getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
802  << A->getAsString(Args);
803  continue;
804  } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
805  getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
806  << A->getAsString(Args);
807  continue;
808  }
809  XarchArg->setBaseArg(A);
810  A = XarchArg.release();
811  DAL->AddSynthesizedArg(A);
812  }
813  DAL->append(A);
814  }
815 
816  if (!BoundArch.empty()) {
817  DAL->eraseArg(options::OPT_march_EQ);
818  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
819  }
820  return DAL;
821 }
822 
824  return new tools::NVPTX::Assembler(*this);
825 }
826 
828  if (OK == Action::OFK_OpenMP)
829  return new tools::NVPTX::OpenMPLinker(*this);
830  return new tools::NVPTX::Linker(*this);
831 }
832 
833 void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
835 }
836 
838 CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
839  return HostTC.GetCXXStdlibType(Args);
840 }
841 
842 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
843  ArgStringList &CC1Args) const {
844  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
845 }
846 
848  ArgStringList &CC1Args) const {
849  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
850 }
851 
852 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
853  ArgStringList &CC1Args) const {
854  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
855 }
856 
858  // The CudaToolChain only supports sanitizers in the sense that it allows
859  // sanitizer arguments on the command line if they are supported by the host
860  // toolchain. The CudaToolChain will actually ignore any command line
861  // arguments for any of these "supported" sanitizers. That means that no
862  // sanitization of device code is actually supported at this time.
863  //
864  // This behavior is necessary because the host and device toolchains
865  // invocations often share the command line, so the device toolchain must
866  // tolerate flags meant only for the host toolchain.
868 }
869 
871  const ArgList &Args) const {
872  return HostTC.computeMSVCVersion(D, Args);
873 }
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const
Add warning options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:746
CudaVersion CudaStringToVersion(const llvm::Twine &S)
Definition: Cuda.cpp:35
CudaArch
Definition: Cuda.h:36
const char * CudaArchToString(CudaArch A)
Definition: Cuda.cpp:48
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: Cuda.cpp:748
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Cuda.cpp:857
StringRef P
virtual void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: ToolChain.cpp:859
VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override
On Windows, returns the MSVC compatibility version.
Definition: Cuda.cpp:870
bool isHostOffloading(OffloadKind OKind) const
Check if this action have any offload kinds.
Definition: Action.h:203
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
CudaArch StringToCudaArch(llvm::StringRef S)
Definition: Cuda.cpp:128
Don&#39;t generate debug info.
CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args)
Definition: Cuda.cpp:55
static CudaVersion ParseCudaVersionFile(const Driver &D, llvm::StringRef V)
Definition: Cuda.cpp:35
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Add warning options that need to be passed to cc1 for this target.
Definition: Cuda.cpp:833
void print(raw_ostream &OS) const
Print information about the detected CUDA installation.
Definition: Cuda.cpp:263
Tool * buildAssembler() const override
Definition: Cuda.cpp:823
const char * getFilename() const
Definition: InputInfo.h:83
path_list & getProgramPaths()
Definition: ToolChain.h:240
virtual void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add the clang cc1 arguments for system include paths.
Definition: ToolChain.cpp:737
Distro - Helper class for detecting and classifying Linux distributions.
Definition: Distro.h:23
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:209
void CheckCudaVersionSupportsArch(CudaArch Arch) const
Emit an error if Version does not support the given Arch.
Definition: Cuda.cpp:246
Emit only debug directives with the line numbers data.
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
bool isDeviceOffloading(OffloadKind OKind) const
Definition: Action.h:206
std::string getLibDeviceFile(StringRef Gpu) const
Get libdevice file for given architecture.
Definition: Cuda.h:75
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
types::ID getType() const
Definition: InputInfo.h:77
CudaInstallationDetector CudaInstallation
Definition: Cuda.h:188
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:319
void addDirectoryList(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const char *ArgName, const char *EnvVar)
const char * CudaVersionToString(CudaVersion V)
Definition: Cuda.cpp:11
StringRef Filename
Definition: Format.cpp:1825
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: Cuda.cpp:847
static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args)
Define debug info level for the NVPTX devices.
Definition: Cuda.cpp:291
virtual VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const
On Windows, returns the MSVC compatibility version.
Definition: ToolChain.cpp:990
#define V(N, I)
Definition: ASTContext.h:2941
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
const char * CudaVirtualArchToString(CudaVirtualArch A)
Definition: Cuda.cpp:168
bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override
Does this toolchain supports given debug info option or not.
Definition: Cuda.cpp:708
Tool * buildLinker() const override
Definition: Cuda.cpp:827
std::string getInputFilename(const InputInfo &Input) const override
Some toolchains need to modify the file name, for example to replace the extension for object files w...
Definition: Cuda.cpp:575
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Cuda.cpp:589
static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch)
Definition: Cuda.cpp:416
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Cuda.cpp:220
CudaVersion version() const
Get the detected Cuda install&#39;s version.
Definition: Cuda.h:63
virtual std::string getInputFilename(const InputInfo &Input) const
Some toolchains need to modify the file name, for example to replace the extension for object files w...
Definition: ToolChain.cpp:245
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:186
CudaVersion MaxVersionForCudaArch(CudaArch A)
Get the latest CudaVersion that supports the given CudaArch.
Definition: Cuda.cpp:337
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Cuda.cpp:842
const Driver & getDriver() const
Definition: ToolChain.h:199
CudaVersion
Definition: Cuda.h:20
bool isValid() const
Check whether we detected a valid Cuda install.
Definition: Cuda.h:58
StringRef getIncludePath() const
Get the detected Cuda Include path.
Definition: Cuda.h:69
CudaVersion MinVersionForCudaArch(CudaArch A)
Get the earliest CudaVersion that supports the given CudaArch.
Definition: Cuda.cpp:286
Dataflow Directional Tag Classes.
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: Cuda.cpp:735
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Cuda.cpp:838
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
CudaVirtualArch VirtualArchForCudaArch(CudaArch A)
Get the compute_xx corresponding to an sm_yy.
Definition: Cuda.cpp:226
DeviceDebugInfoLevel
Debug info level for the NVPTX devices.
Definition: Cuda.cpp:275
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
const Driver & getDriver() const
Definition: Compilation.h:133
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:942
bool isNothing() const
Definition: InputInfo.h:74
virtual llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: ToolChain.h:284
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:237
bool isFilename() const
Definition: InputInfo.h:75
void adjustDebugInfoKind(codegenoptions::DebugInfoKind &DebugInfoKind, const llvm::opt::ArgList &Args) const override
Adjust debug information kind considering all passed options.
Definition: Cuda.cpp:720
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:315
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:801
const char * addTempFile(const char *Name)
addTempFile - Add a file to remove on exit, and returns its argument.
Definition: Compilation.h:232
virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add arguments to use MCU GCC toolchain includes.
Definition: ToolChain.cpp:973
virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const
Add options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:742
StringRef getBinPath() const
Get the detected path to Cuda&#39;s bin directory.
Definition: Cuda.h:67
const char * getOffloadingArch() const
Definition: Action.h:199
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use MCU GCC toolchain includes.
Definition: Cuda.cpp:852
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88