1 //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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/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>
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;
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 void CudaInstallationDetector::ParseCudaVersionFile(llvm::StringRef V) {
36 Version = CudaVersion::UNKNOWN;
37 if (!V.startswith("CUDA Version "))
39 V = V.substr(strlen("CUDA Version "));
40 SmallVector<StringRef,4> VersionParts;
41 V.split(VersionParts, '.');
42 if (VersionParts.size() < 2)
44 DetectedVersion = join_items(".", VersionParts[0], VersionParts[1]);
45 Version = CudaStringToVersion(DetectedVersion);
46 if (Version != CudaVersion::UNKNOWN)
49 Version = CudaVersion::LATEST;
50 DetectedVersionIsNotSupported = true;
53 void CudaInstallationDetector::WarnIfUnsupportedVersion() {
54 if (DetectedVersionIsNotSupported)
55 D.Diag(diag::warn_drv_unknown_cuda_version)
56 << DetectedVersion << CudaVersionToString(Version);
59 CudaInstallationDetector::CudaInstallationDetector(
60 const Driver &D, const llvm::Triple &HostTriple,
61 const llvm::opt::ArgList &Args)
67 Candidate(std::string Path, bool StrictChecking = false)
68 : Path(Path), StrictChecking(StrictChecking) {}
70 SmallVector<Candidate, 4> Candidates;
72 // In decreasing order so we prefer newer versions to older versions.
73 std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
75 if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) {
76 Candidates.emplace_back(
77 Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
78 } else if (HostTriple.isOSWindows()) {
79 for (const char *Ver : Versions)
80 Candidates.emplace_back(
81 D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
84 if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) {
85 // Try to find ptxas binary. If the executable is located in a directory
86 // called 'bin/', its parent directory might be a good guess for a valid
88 // However, some distributions might installs 'ptxas' to /usr/bin. In that
89 // case the candidate would be '/usr' which passes the following checks
90 // because '/usr/include' exists as well. To avoid this case, we always
91 // check for the directory potentially containing files for libdevice,
92 // even if the user passes -nocudalib.
93 if (llvm::ErrorOr<std::string> ptxas =
94 llvm::sys::findProgramByName("ptxas")) {
95 SmallString<256> ptxasAbsolutePath;
96 llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath);
98 StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath);
99 if (llvm::sys::path::filename(ptxasDir) == "bin")
100 Candidates.emplace_back(llvm::sys::path::parent_path(ptxasDir),
101 /*StrictChecking=*/true);
105 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda");
106 for (const char *Ver : Versions)
107 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
109 Distro Dist(D.getVFS(), llvm::Triple(llvm::sys::getProcessTriple()));
110 if (Dist.IsDebian() || Dist.IsUbuntu())
111 // Special case for Debian to have nvidia-cuda-toolkit work
112 // out of the box. More info on http://bugs.debian.org/882505
113 Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
116 bool NoCudaLib = Args.hasArg(options::OPT_nogpulib);
118 for (const auto &Candidate : Candidates) {
119 InstallPath = Candidate.Path;
120 if (InstallPath.empty() || !D.getVFS().exists(InstallPath))
123 BinPath = InstallPath + "/bin";
124 IncludePath = InstallPath + "/include";
125 LibDevicePath = InstallPath + "/nvvm/libdevice";
127 auto &FS = D.getVFS();
128 if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
130 bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
131 if (CheckLibDevice && !FS.exists(LibDevicePath))
134 // On Linux, we have both lib and lib64 directories, and we need to choose
135 // based on our triple. On MacOS, we have only a lib directory.
137 // It's sufficient for our purposes to be flexible: If both lib and lib64
138 // exist, we choose whichever one matches our triple. Otherwise, if only
139 // lib exists, we use it.
140 if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64"))
141 LibPath = InstallPath + "/lib64";
142 else if (FS.exists(InstallPath + "/lib"))
143 LibPath = InstallPath + "/lib";
147 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
148 FS.getBufferForFile(InstallPath + "/version.txt");
150 // CUDA 7.0 doesn't have a version.txt, so guess that's our version if
151 // version.txt isn't present.
152 Version = CudaVersion::CUDA_70;
154 ParseCudaVersionFile((*VersionFile)->getBuffer());
157 if (Version >= CudaVersion::CUDA_90) {
158 // CUDA-9+ uses single libdevice file for all GPU variants.
159 std::string FilePath = LibDevicePath + "/libdevice.10.bc";
160 if (FS.exists(FilePath)) {
161 for (const char *GpuArchName :
162 {"sm_30", "sm_32", "sm_35", "sm_37", "sm_50", "sm_52", "sm_53",
163 "sm_60", "sm_61", "sm_62", "sm_70", "sm_72", "sm_75"}) {
164 const CudaArch GpuArch = StringToCudaArch(GpuArchName);
165 if (Version >= MinVersionForCudaArch(GpuArch) &&
166 Version <= MaxVersionForCudaArch(GpuArch))
167 LibDeviceMap[GpuArchName] = FilePath;
172 for (llvm::sys::fs::directory_iterator LI(LibDevicePath, EC), LE;
173 !EC && LI != LE; LI = LI.increment(EC)) {
174 StringRef FilePath = LI->path();
175 StringRef FileName = llvm::sys::path::filename(FilePath);
176 // Process all bitcode filenames that look like
177 // libdevice.compute_XX.YY.bc
178 const StringRef LibDeviceName = "libdevice.";
179 if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
181 StringRef GpuArch = FileName.slice(
182 LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
183 LibDeviceMap[GpuArch] = FilePath.str();
184 // Insert map entries for specific devices with this compute
185 // capability. NVCC's choice of the libdevice library version is
186 // rather peculiar and depends on the CUDA version.
187 if (GpuArch == "compute_20") {
188 LibDeviceMap["sm_20"] = FilePath;
189 LibDeviceMap["sm_21"] = FilePath;
190 LibDeviceMap["sm_32"] = FilePath;
191 } else if (GpuArch == "compute_30") {
192 LibDeviceMap["sm_30"] = FilePath;
193 if (Version < CudaVersion::CUDA_80) {
194 LibDeviceMap["sm_50"] = FilePath;
195 LibDeviceMap["sm_52"] = FilePath;
196 LibDeviceMap["sm_53"] = FilePath;
198 LibDeviceMap["sm_60"] = FilePath;
199 LibDeviceMap["sm_61"] = FilePath;
200 LibDeviceMap["sm_62"] = FilePath;
201 } else if (GpuArch == "compute_35") {
202 LibDeviceMap["sm_35"] = FilePath;
203 LibDeviceMap["sm_37"] = FilePath;
204 } else if (GpuArch == "compute_50") {
205 if (Version >= CudaVersion::CUDA_80) {
206 LibDeviceMap["sm_50"] = FilePath;
207 LibDeviceMap["sm_52"] = FilePath;
208 LibDeviceMap["sm_53"] = FilePath;
214 // Check that we have found at least one libdevice that we can link in if
215 // -nocudalib hasn't been specified.
216 if (LibDeviceMap.empty() && !NoCudaLib)
224 void CudaInstallationDetector::AddCudaIncludeArgs(
225 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
226 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
227 // Add cuda_wrappers/* to our system include path. This lets us wrap
228 // standard library headers.
229 SmallString<128> P(D.ResourceDir);
230 llvm::sys::path::append(P, "include");
231 llvm::sys::path::append(P, "cuda_wrappers");
232 CC1Args.push_back("-internal-isystem");
233 CC1Args.push_back(DriverArgs.MakeArgString(P));
236 if (DriverArgs.hasArg(options::OPT_nocudainc))
240 D.Diag(diag::err_drv_no_cuda_installation);
244 CC1Args.push_back("-internal-isystem");
245 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
246 CC1Args.push_back("-include");
247 CC1Args.push_back("__clang_cuda_runtime_wrapper.h");
250 void CudaInstallationDetector::CheckCudaVersionSupportsArch(
251 CudaArch Arch) const {
252 if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
253 ArchsWithBadVersion.count(Arch) > 0)
256 auto MinVersion = MinVersionForCudaArch(Arch);
257 auto MaxVersion = MaxVersionForCudaArch(Arch);
258 if (Version < MinVersion || Version > MaxVersion) {
259 ArchsWithBadVersion.insert(Arch);
260 D.Diag(diag::err_drv_cuda_version_unsupported)
261 << CudaArchToString(Arch) << CudaVersionToString(MinVersion)
262 << CudaVersionToString(MaxVersion) << InstallPath
263 << CudaVersionToString(Version);
267 void CudaInstallationDetector::print(raw_ostream &OS) const {
269 OS << "Found CUDA installation: " << InstallPath << ", version "
270 << CudaVersionToString(Version) << "\n";
274 /// Debug info level for the NVPTX devices. We may need to emit different debug
275 /// info level for the host and for the device itselfi. This type controls
276 /// emission of the debug info for the devices. It either prohibits disable info
277 /// emission completely, or emits debug directives only, or emits same debug
278 /// info as for the host.
279 enum DeviceDebugInfoLevel {
280 DisableDebugInfo, /// Do not emit debug info for the devices.
281 DebugDirectivesOnly, /// Emit only debug directives.
282 EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
285 } // anonymous namespace
287 /// Define debug info level for the NVPTX devices. If the debug info for both
288 /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
289 /// only debug directives are requested for the both host and device
290 /// (-gline-directvies-only), or the debug info only for the device is disabled
291 /// (optimization is on and --cuda-noopt-device-debug was not specified), the
292 /// debug directves only must be emitted for the device. Otherwise, use the same
293 /// debug info level just like for the host (with the limitations of only
294 /// supported DWARF2 standard).
295 static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
296 const Arg *A = Args.getLastArg(options::OPT_O_Group);
297 bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) ||
298 Args.hasFlag(options::OPT_cuda_noopt_device_debug,
299 options::OPT_no_cuda_noopt_device_debug,
301 if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
302 const Option &Opt = A->getOption();
303 if (Opt.matches(options::OPT_gN_Group)) {
304 if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0))
305 return DisableDebugInfo;
306 if (Opt.matches(options::OPT_gline_directives_only))
307 return DebugDirectivesOnly;
309 return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
311 return DisableDebugInfo;
314 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
315 const InputInfo &Output,
316 const InputInfoList &Inputs,
318 const char *LinkingOutput) const {
320 static_cast<const toolchains::CudaToolChain &>(getToolChain());
321 assert(TC.getTriple().isNVPTX() && "Wrong platform");
323 StringRef GPUArchName;
324 // If this is an OpenMP action we need to extract the device architecture
325 // from the -march=arch option. This option may come from -Xopenmp-target
326 // flag or the default value.
327 if (JA.isDeviceOffloading(Action::OFK_OpenMP)) {
328 GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
329 assert(!GPUArchName.empty() && "Must have an architecture passed in.");
331 GPUArchName = JA.getOffloadingArch();
333 // Obtain architecture from the action.
334 CudaArch gpu_arch = StringToCudaArch(GPUArchName);
335 assert(gpu_arch != CudaArch::UNKNOWN &&
336 "Device action expected to have an architecture.");
338 // Check that our installation's ptxas supports gpu_arch.
339 if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
340 TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
343 ArgStringList CmdArgs;
344 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
345 DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
346 if (DIKind == EmitSameDebugInfoAsHost) {
347 // ptxas does not accept -g option if optimization is enabled, so
348 // we ignore the compiler's -O* options if we want debug info.
349 CmdArgs.push_back("-g");
350 CmdArgs.push_back("--dont-merge-basicblocks");
351 CmdArgs.push_back("--return-at-end");
352 } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
353 // Map the -O we received to -O{0,1,2,3}.
355 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
356 // default, so it may correspond more closely to the spirit of clang -O2.
358 // -O3 seems like the least-bad option when -Osomething is specified to
359 // clang but it isn't handled below.
360 StringRef OOpt = "3";
361 if (A->getOption().matches(options::OPT_O4) ||
362 A->getOption().matches(options::OPT_Ofast))
364 else if (A->getOption().matches(options::OPT_O0))
366 else if (A->getOption().matches(options::OPT_O)) {
367 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
368 OOpt = llvm::StringSwitch<const char *>(A->getValue())
376 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
378 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
379 // to no optimizations, but ptxas's default is -O3.
380 CmdArgs.push_back("-O0");
382 if (DIKind == DebugDirectivesOnly)
383 CmdArgs.push_back("-lineinfo");
385 // Pass -v to ptxas if it was passed to the driver.
386 if (Args.hasArg(options::OPT_v))
387 CmdArgs.push_back("-v");
389 CmdArgs.push_back("--gpu-name");
390 CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
391 CmdArgs.push_back("--output-file");
392 CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
393 for (const auto& II : Inputs)
394 CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
396 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
397 CmdArgs.push_back(Args.MakeArgString(A));
399 bool Relocatable = false;
400 if (JA.isOffloading(Action::OFK_OpenMP))
401 // In OpenMP we need to generate relocatable code.
402 Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
403 options::OPT_fnoopenmp_relocatable_target,
405 else if (JA.isOffloading(Action::OFK_Cuda))
406 Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
407 options::OPT_fno_gpu_rdc, /*Default=*/false);
410 CmdArgs.push_back("-c");
413 if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
414 Exec = A->getValue();
416 Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
417 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
420 static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
421 bool includePTX = true;
422 for (Arg *A : Args) {
423 if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
424 A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
427 const StringRef ArchStr = A->getValue();
428 if (ArchStr == "all" || ArchStr == gpu_arch) {
429 includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
436 // All inputs to this linker must be from CudaDeviceActions, as we need to look
437 // at the Inputs' Actions in order to figure out which GPU architecture they
439 void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
440 const InputInfo &Output,
441 const InputInfoList &Inputs,
443 const char *LinkingOutput) const {
445 static_cast<const toolchains::CudaToolChain &>(getToolChain());
446 assert(TC.getTriple().isNVPTX() && "Wrong platform");
448 ArgStringList CmdArgs;
449 if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
450 CmdArgs.push_back("--cuda");
451 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32");
452 CmdArgs.push_back(Args.MakeArgString("--create"));
453 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
454 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
455 CmdArgs.push_back("-g");
457 for (const auto& II : Inputs) {
458 auto *A = II.getAction();
459 assert(A->getInputs().size() == 1 &&
460 "Device offload action is expected to have a single input");
461 const char *gpu_arch_str = A->getOffloadingArch();
462 assert(gpu_arch_str &&
463 "Device action expected to have associated a GPU architecture!");
464 CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
466 if (II.getType() == types::TY_PP_Asm &&
467 !shouldIncludePTX(Args, gpu_arch_str))
469 // We need to pass an Arch of the form "sm_XX" for cubin files and
470 // "compute_XX" for ptx.
472 (II.getType() == types::TY_PP_Asm)
473 ? CudaVirtualArchToString(VirtualArchForCudaArch(gpu_arch))
475 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
476 Arch + ",file=" + II.getFilename()));
479 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
480 CmdArgs.push_back(Args.MakeArgString(A));
482 const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
483 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
486 void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
487 const InputInfo &Output,
488 const InputInfoList &Inputs,
490 const char *LinkingOutput) const {
492 static_cast<const toolchains::CudaToolChain &>(getToolChain());
493 assert(TC.getTriple().isNVPTX() && "Wrong platform");
495 ArgStringList CmdArgs;
497 // OpenMP uses nvlink to link cubin files. The result will be embedded in the
498 // host binary by the host linker.
499 assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
500 "CUDA toolchain not expected for an OpenMP host device.");
502 if (Output.isFilename()) {
503 CmdArgs.push_back("-o");
504 CmdArgs.push_back(Output.getFilename());
506 assert(Output.isNothing() && "Invalid output.");
507 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
508 CmdArgs.push_back("-g");
510 if (Args.hasArg(options::OPT_v))
511 CmdArgs.push_back("-v");
514 Args.getLastArgValue(options::OPT_march_EQ);
515 assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
517 CmdArgs.push_back("-arch");
518 CmdArgs.push_back(Args.MakeArgString(GPUArch));
520 // Assume that the directory specified with --libomptarget_nvptx_path
521 // contains the static library libomptarget-nvptx.a.
522 if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
523 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue()));
525 // Add paths specified in LIBRARY_PATH environment variable as -L options.
526 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
528 // Add paths for the default clang library path.
529 SmallString<256> DefaultLibPath =
530 llvm::sys::path::parent_path(TC.getDriver().Dir);
531 llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
532 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
534 // Add linking against library implementing OpenMP calls on NVPTX target.
535 CmdArgs.push_back("-lomptarget-nvptx");
537 for (const auto &II : Inputs) {
538 if (II.getType() == types::TY_LLVM_IR ||
539 II.getType() == types::TY_LTO_IR ||
540 II.getType() == types::TY_LTO_BC ||
541 II.getType() == types::TY_LLVM_BC) {
542 C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
543 << getToolChain().getTripleString();
547 // Currently, we only pass the input files to the linker, we do not pass
548 // any libraries that may be valid only for the host.
549 if (!II.isFilename())
552 const char *CubinF = C.addTempFile(
553 C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
555 CmdArgs.push_back(CubinF);
559 Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
560 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
563 /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
564 /// which isn't properly a linker but nonetheless performs the step of stitching
565 /// together object files from the assembler into a single blob.
567 CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
568 const ToolChain &HostTC, const ArgList &Args,
569 const Action::OffloadKind OK)
570 : ToolChain(D, Triple, Args), HostTC(HostTC),
571 CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
572 if (CudaInstallation.isValid()) {
573 CudaInstallation.WarnIfUnsupportedVersion();
574 getProgramPaths().push_back(CudaInstallation.getBinPath());
576 // Lookup binaries into the driver directory, this is used to
577 // discover the clang-offload-bundler executable.
578 getProgramPaths().push_back(getDriver().Dir);
581 std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
582 // Only object files are changed, for example assembly files keep their .s
583 // extensions. CUDA also continues to use .o as they don't use nvlink but
585 if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object))
586 return ToolChain::getInputFilename(Input);
588 // Replace extension for object files with cubin because nvlink relies on
589 // these particular file names.
590 SmallString<256> Filename(ToolChain::getInputFilename(Input));
591 llvm::sys::path::replace_extension(Filename, "cubin");
592 return Filename.str();
595 void CudaToolChain::addClangTargetOptions(
596 const llvm::opt::ArgList &DriverArgs,
597 llvm::opt::ArgStringList &CC1Args,
598 Action::OffloadKind DeviceOffloadingKind) const {
599 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
601 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
602 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
603 assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
604 DeviceOffloadingKind == Action::OFK_Cuda) &&
605 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
607 if (DeviceOffloadingKind == Action::OFK_Cuda) {
608 CC1Args.push_back("-fcuda-is-device");
610 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
611 options::OPT_fno_cuda_flush_denormals_to_zero, false))
612 CC1Args.push_back("-fcuda-flush-denormals-to-zero");
614 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
615 options::OPT_fno_cuda_approx_transcendentals, false))
616 CC1Args.push_back("-fcuda-approx-transcendentals");
618 if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
620 CC1Args.push_back("-fgpu-rdc");
623 if (DriverArgs.hasArg(options::OPT_nogpulib))
626 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
628 if (LibDeviceFile.empty()) {
629 if (DeviceOffloadingKind == Action::OFK_OpenMP &&
630 DriverArgs.hasArg(options::OPT_S))
633 getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
637 CC1Args.push_back("-mlink-builtin-bitcode");
638 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
640 // New CUDA versions often introduce new instructions that are only supported
641 // by new PTX version, so we need to raise PTX level to enable them in NVPTX
643 const char *PtxFeature = nullptr;
644 switch(CudaInstallation.version()) {
645 case CudaVersion::CUDA_101:
646 PtxFeature = "+ptx64";
648 case CudaVersion::CUDA_100:
649 PtxFeature = "+ptx63";
651 case CudaVersion::CUDA_92:
652 PtxFeature = "+ptx61";
654 case CudaVersion::CUDA_91:
655 PtxFeature = "+ptx61";
657 case CudaVersion::CUDA_90:
658 PtxFeature = "+ptx60";
661 PtxFeature = "+ptx42";
663 CC1Args.append({"-target-feature", PtxFeature});
664 if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr,
665 options::OPT_fno_cuda_short_ptr, false))
666 CC1Args.append({"-mllvm", "--nvptx-short-ptr"});
668 if (CudaInstallation.version() >= CudaVersion::UNKNOWN)
669 CC1Args.push_back(DriverArgs.MakeArgString(
670 Twine("-target-sdk-version=") +
671 CudaVersionToString(CudaInstallation.version())));
673 if (DeviceOffloadingKind == Action::OFK_OpenMP) {
674 SmallVector<StringRef, 8> LibraryPaths;
675 if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
676 LibraryPaths.push_back(A->getValue());
678 // Add user defined library paths from LIBRARY_PATH.
679 llvm::Optional<std::string> LibPath =
680 llvm::sys::Process::GetEnv("LIBRARY_PATH");
682 SmallVector<StringRef, 8> Frags;
683 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
684 llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
685 for (StringRef Path : Frags)
686 LibraryPaths.emplace_back(Path.trim());
689 // Add path to lib / lib64 folder.
690 SmallString<256> DefaultLibPath =
691 llvm::sys::path::parent_path(getDriver().Dir);
692 llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
693 LibraryPaths.emplace_back(DefaultLibPath.c_str());
695 std::string LibOmpTargetName =
696 "libomptarget-nvptx-" + GpuArch.str() + ".bc";
697 bool FoundBCLibrary = false;
698 for (StringRef LibraryPath : LibraryPaths) {
699 SmallString<128> LibOmpTargetFile(LibraryPath);
700 llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
701 if (llvm::sys::fs::exists(LibOmpTargetFile)) {
702 CC1Args.push_back("-mlink-builtin-bitcode");
703 CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
704 FoundBCLibrary = true;
709 getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
714 bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
715 const Option &O = A->getOption();
716 return (O.matches(options::OPT_gN_Group) &&
717 !O.matches(options::OPT_gmodules)) ||
718 O.matches(options::OPT_g_Flag) ||
719 O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) ||
720 O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) ||
721 O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) ||
722 O.matches(options::OPT_gdwarf_5) ||
723 O.matches(options::OPT_gcolumn_info);
726 void CudaToolChain::adjustDebugInfoKind(
727 codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
728 switch (mustEmitDebugInfo(Args)) {
729 case DisableDebugInfo:
730 DebugInfoKind = codegenoptions::NoDebugInfo;
732 case DebugDirectivesOnly:
733 DebugInfoKind = codegenoptions::DebugDirectivesOnly;
735 case EmitSameDebugInfoAsHost:
736 // Use same debug info level as the host.
741 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
742 ArgStringList &CC1Args) const {
743 // Check our CUDA version if we're going to include the CUDA headers.
744 if (!DriverArgs.hasArg(options::OPT_nocudainc) &&
745 !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
746 StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
747 assert(!Arch.empty() && "Must have an explicit GPU arch.");
748 CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch));
750 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
753 llvm::opt::DerivedArgList *
754 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
756 Action::OffloadKind DeviceOffloadKind) const {
757 DerivedArgList *DAL =
758 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
760 DAL = new DerivedArgList(Args.getBaseArgs());
762 const OptTable &Opts = getDriver().getOpts();
764 // For OpenMP device offloading, append derived arguments. Make sure
765 // flags are not duplicated.
766 // Also append the compute capability.
767 if (DeviceOffloadKind == Action::OFK_OpenMP) {
768 for (Arg *A : Args) {
769 bool IsDuplicate = false;
770 for (Arg *DALArg : *DAL) {
780 StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
782 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
783 CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
788 for (Arg *A : Args) {
789 if (A->getOption().matches(options::OPT_Xarch__)) {
790 // Skip this argument unless the architecture matches BoundArch
791 if (BoundArch.empty() || A->getValue(0) != BoundArch)
794 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
795 unsigned Prev = Index;
796 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
798 // If the argument parsing failed or more than one argument was
799 // consumed, the -Xarch_ argument's parameter tried to consume
800 // extra arguments. Emit an error and ignore.
802 // We also want to disallow any options which would alter the
803 // driver behavior; that isn't going to work in our model. We
804 // use isDriverOption() as an approximation, although things
805 // like -O4 are going to slip through.
806 if (!XarchArg || Index > Prev + 1) {
807 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
808 << A->getAsString(Args);
810 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
811 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
812 << A->getAsString(Args);
815 XarchArg->setBaseArg(A);
816 A = XarchArg.release();
817 DAL->AddSynthesizedArg(A);
822 if (!BoundArch.empty()) {
823 DAL->eraseArg(options::OPT_march_EQ);
824 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
829 Tool *CudaToolChain::buildAssembler() const {
830 return new tools::NVPTX::Assembler(*this);
833 Tool *CudaToolChain::buildLinker() const {
834 if (OK == Action::OFK_OpenMP)
835 return new tools::NVPTX::OpenMPLinker(*this);
836 return new tools::NVPTX::Linker(*this);
839 void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
840 HostTC.addClangWarningOptions(CC1Args);
843 ToolChain::CXXStdlibType
844 CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
845 return HostTC.GetCXXStdlibType(Args);
848 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
849 ArgStringList &CC1Args) const {
850 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
853 void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
854 ArgStringList &CC1Args) const {
855 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
858 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
859 ArgStringList &CC1Args) const {
860 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
863 SanitizerMask CudaToolChain::getSupportedSanitizers() const {
864 // The CudaToolChain only supports sanitizers in the sense that it allows
865 // sanitizer arguments on the command line if they are supported by the host
866 // toolchain. The CudaToolChain will actually ignore any command line
867 // arguments for any of these "supported" sanitizers. That means that no
868 // sanitization of device code is actually supported at this time.
870 // This behavior is necessary because the host and device toolchains
871 // invocations often share the command line, so the device toolchain must
872 // tolerate flags meant only for the host toolchain.
873 return HostTC.getSupportedSanitizers();
876 VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
877 const ArgList &Args) const {
878 return HostTC.computeMSVCVersion(D, Args);