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 "clang/Basic/Cuda.h"
11#include "clang/Config/config.h"
12#include "clang/Driver/CommonArgs.h"
13#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Distro.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Driver/InputInfo.h"
17#include "clang/Options/Options.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Config/llvm-config.h" // for LLVM_HOST_TRIPLE
21#include "llvm/Option/ArgList.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/Process.h"
25#include "llvm/Support/Program.h"
26#include "llvm/Support/VirtualFileSystem.h"
27#include "llvm/TargetParser/Host.h"
28#include "llvm/TargetParser/TargetParser.h"
29#include <system_error>
30
31using namespace clang::driver;
32using namespace clang::driver::toolchains;
33using namespace clang::driver::tools;
34using namespace clang;
35using namespace llvm::opt;
36
37namespace {
38
39CudaVersion getCudaVersion(uint32_t raw_version) {
40 if (raw_version < 7050)
41 return CudaVersion::CUDA_70;
42 if (raw_version < 8000)
43 return CudaVersion::CUDA_75;
44 if (raw_version < 9000)
45 return CudaVersion::CUDA_80;
46 if (raw_version < 9010)
47 return CudaVersion::CUDA_90;
48 if (raw_version < 9020)
49 return CudaVersion::CUDA_91;
50 if (raw_version < 10000)
51 return CudaVersion::CUDA_92;
52 if (raw_version < 10010)
53 return CudaVersion::CUDA_100;
54 if (raw_version < 10020)
55 return CudaVersion::CUDA_101;
56 if (raw_version < 11000)
57 return CudaVersion::CUDA_102;
58 if (raw_version < 11010)
59 return CudaVersion::CUDA_110;
60 if (raw_version < 11020)
61 return CudaVersion::CUDA_111;
62 if (raw_version < 11030)
63 return CudaVersion::CUDA_112;
64 if (raw_version < 11040)
65 return CudaVersion::CUDA_113;
66 if (raw_version < 11050)
67 return CudaVersion::CUDA_114;
68 if (raw_version < 11060)
69 return CudaVersion::CUDA_115;
70 if (raw_version < 11070)
71 return CudaVersion::CUDA_116;
72 if (raw_version < 11080)
73 return CudaVersion::CUDA_117;
74 if (raw_version < 11090)
75 return CudaVersion::CUDA_118;
76 if (raw_version < 12010)
77 return CudaVersion::CUDA_120;
78 if (raw_version < 12020)
79 return CudaVersion::CUDA_121;
80 if (raw_version < 12030)
81 return CudaVersion::CUDA_122;
82 if (raw_version < 12040)
83 return CudaVersion::CUDA_123;
84 if (raw_version < 12050)
85 return CudaVersion::CUDA_124;
86 if (raw_version < 12060)
87 return CudaVersion::CUDA_125;
88 if (raw_version < 12070)
89 return CudaVersion::CUDA_126;
90 if (raw_version < 12090)
91 return CudaVersion::CUDA_128;
92 if (raw_version < 13000)
93 return CudaVersion::CUDA_129;
94 if (raw_version < 13010)
95 return CudaVersion::CUDA_130;
96 if (raw_version < 13020)
97 return CudaVersion::CUDA_131;
98 if (raw_version < 13030)
99 return CudaVersion::CUDA_132;
100 if (raw_version < 13040)
101 return CudaVersion::CUDA_133;
102 if (raw_version < 13050)
103 return CudaVersion::CUDA_134;
104 return CudaVersion::NEW;
105}
106
107CudaVersion parseCudaHFile(llvm::StringRef Input) {
108 // Helper lambda which skips the words if the line starts with them or returns
109 // std::nullopt otherwise.
110 auto StartsWithWords =
111 [](llvm::StringRef Line,
112 const SmallVector<StringRef, 3> words) -> std::optional<StringRef> {
113 for (StringRef word : words) {
114 if (!Line.consume_front(Prefix: word))
115 return {};
116 Line = Line.ltrim();
117 }
118 return Line;
119 };
120
121 Input = Input.ltrim();
122 while (!Input.empty()) {
123 if (auto Line =
124 StartsWithWords(Input.ltrim(), {"#", "define", "CUDA_VERSION"})) {
125 uint32_t RawVersion;
126 Line->consumeInteger(Radix: 10, Result&: RawVersion);
127 return getCudaVersion(raw_version: RawVersion);
128 }
129 // Find next non-empty line.
130 Input = Input.drop_front(N: Input.find_first_of(Chars: "\n\r")).ltrim();
131 }
132 return CudaVersion::UNKNOWN;
133}
134} // namespace
135
136void CudaInstallationDetector::WarnIfUnsupportedVersion() const {
137 if (Version > CudaVersion::PARTIALLY_SUPPORTED) {
138 std::string VersionString = CudaVersionToString(V: Version);
139 if (!VersionString.empty())
140 VersionString.insert(pos: 0, s: " ");
141 D.Diag(DiagID: diag::warn_drv_new_cuda_version)
142 << VersionString
143 << (CudaVersion::PARTIALLY_SUPPORTED != CudaVersion::FULLY_SUPPORTED)
144 << CudaVersionToString(V: CudaVersion::PARTIALLY_SUPPORTED);
145 } else if (Version > CudaVersion::FULLY_SUPPORTED)
146 D.Diag(DiagID: diag::warn_drv_partially_supported_cuda_version)
147 << CudaVersionToString(V: Version);
148}
149
150CudaInstallationDetector::CudaInstallationDetector(
151 const Driver &D, const llvm::Triple &HostTriple,
152 const llvm::opt::ArgList &Args)
153 : D(D) {
154 struct Candidate {
155 std::string Path;
156 bool StrictChecking;
157
158 Candidate(std::string Path, bool StrictChecking = false)
159 : Path(Path), StrictChecking(StrictChecking) {}
160 };
161 SmallVector<Candidate, 4> Candidates;
162
163 // In decreasing order so we prefer newer versions to older versions.
164 std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
165 auto &FS = D.getVFS();
166
167 if (Args.hasArg(Ids: options::OPT_cuda_path_EQ)) {
168 Candidates.emplace_back(
169 Args: Args.getLastArgValue(Id: options::OPT_cuda_path_EQ).str());
170 } else if (HostTriple.isOSWindows()) {
171 for (const char *Ver : Versions)
172 Candidates.emplace_back(
173 Args: D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
174 Ver);
175 } else {
176 if (!Args.hasArg(Ids: options::OPT_cuda_path_ignore_env)) {
177 // Try to find ptxas binary. If the executable is located in a directory
178 // called 'bin/', its parent directory might be a good guess for a valid
179 // CUDA installation.
180 // However, some distributions might installs 'ptxas' to /usr/bin. In that
181 // case the candidate would be '/usr' which passes the following checks
182 // because '/usr/include' exists as well. To avoid this case, we always
183 // check for the directory potentially containing files for libdevice,
184 // even if the user passes -nocudalib.
185 if (llvm::ErrorOr<std::string> ptxas =
186 llvm::sys::findProgramByName(Name: "ptxas")) {
187 SmallString<256> ptxasAbsolutePath;
188 llvm::sys::fs::real_path(path: *ptxas, output&: ptxasAbsolutePath);
189
190 StringRef ptxasDir = llvm::sys::path::parent_path(path: ptxasAbsolutePath);
191 if (llvm::sys::path::filename(path: ptxasDir) == "bin")
192 Candidates.emplace_back(
193 Args: std::string(llvm::sys::path::parent_path(path: ptxasDir)),
194 /*StrictChecking=*/Args: true);
195 }
196 }
197
198 Candidates.emplace_back(Args: D.SysRoot + "/usr/local/cuda");
199 for (const char *Ver : Versions)
200 Candidates.emplace_back(Args: D.SysRoot + "/usr/local/cuda-" + Ver);
201
202 Distro Dist(FS, llvm::Triple(llvm::sys::getProcessTriple()));
203 if (Dist.IsDebian() || Dist.IsUbuntu())
204 // Special case for Debian to have nvidia-cuda-toolkit work
205 // out of the box. More info on http://bugs.debian.org/882505
206 Candidates.emplace_back(Args: D.SysRoot + "/usr/lib/cuda");
207 }
208
209 bool NoCudaLib =
210 !Args.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, Default: true);
211
212 for (const auto &Candidate : Candidates) {
213 InstallPath = Candidate.Path;
214 if (InstallPath.empty() || !FS.exists(Path: InstallPath))
215 continue;
216
217 BinPath = InstallPath + "/bin";
218 IncludePath = InstallPath + "/include";
219 LibDevicePath = InstallPath + "/nvvm/libdevice";
220
221 if (!(FS.exists(Path: IncludePath) && FS.exists(Path: BinPath)))
222 continue;
223 bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
224 if (CheckLibDevice && !FS.exists(Path: LibDevicePath))
225 continue;
226
227 Version = CudaVersion::UNKNOWN;
228 if (auto CudaHFile = FS.getBufferForFile(Name: InstallPath + "/include/cuda.h"))
229 Version = parseCudaHFile(Input: (*CudaHFile)->getBuffer());
230 // As the last resort, make an educated guess between CUDA-7.0, which had
231 // old-style libdevice bitcode, and an unknown recent CUDA version.
232 if (Version == CudaVersion::UNKNOWN) {
233 Version = FS.exists(Path: LibDevicePath + "/libdevice.10.bc")
234 ? CudaVersion::NEW
235 : CudaVersion::CUDA_70;
236 }
237
238 if (Version >= CudaVersion::CUDA_90) {
239 // CUDA-9+ uses single libdevice file for all GPU variants.
240 std::string FilePath = LibDevicePath + "/libdevice.10.bc";
241 if (FS.exists(Path: FilePath)) {
242 // CUDA-9+ uses a single libdevice file for every NVIDIA GPU variant
243 // (sm_30 and newer).
244#define NVPTX_GPU(NAME, KIND, VIRTUAL, SM_ID, MIN_VER, MAX_VER, SUFFIX) \
245 if ((SM_ID) >= 300) \
246 LibDeviceMap[NAME] = FilePath;
247#include "llvm/TargetParser/NVPTXTargetParser.def"
248 }
249 } else {
250 std::error_code EC;
251 for (llvm::vfs::directory_iterator LI = FS.dir_begin(Dir: LibDevicePath, EC),
252 LE;
253 !EC && LI != LE; LI = LI.increment(EC)) {
254 StringRef FilePath = LI->path();
255 StringRef FileName = llvm::sys::path::filename(path: FilePath);
256 // Process all bitcode filenames that look like
257 // libdevice.compute_XX.YY.bc
258 const StringRef LibDeviceName = "libdevice.";
259 if (!(FileName.starts_with(Prefix: LibDeviceName) && FileName.ends_with(Suffix: ".bc")))
260 continue;
261 StringRef GpuArch = FileName.slice(
262 Start: LibDeviceName.size(), End: FileName.find(C: '.', From: LibDeviceName.size()));
263 LibDeviceMap[GpuArch] = FilePath.str();
264 // Insert map entries for specific devices with this compute
265 // capability. NVCC's choice of the libdevice library version is
266 // rather peculiar and depends on the CUDA version.
267 if (GpuArch == "compute_20") {
268 LibDeviceMap["sm_20"] = std::string(FilePath);
269 LibDeviceMap["sm_21"] = std::string(FilePath);
270 LibDeviceMap["sm_32"] = std::string(FilePath);
271 } else if (GpuArch == "compute_30") {
272 LibDeviceMap["sm_30"] = std::string(FilePath);
273 if (Version < CudaVersion::CUDA_80) {
274 LibDeviceMap["sm_50"] = std::string(FilePath);
275 LibDeviceMap["sm_52"] = std::string(FilePath);
276 LibDeviceMap["sm_53"] = std::string(FilePath);
277 }
278 LibDeviceMap["sm_60"] = std::string(FilePath);
279 LibDeviceMap["sm_61"] = std::string(FilePath);
280 LibDeviceMap["sm_62"] = std::string(FilePath);
281 } else if (GpuArch == "compute_35") {
282 LibDeviceMap["sm_35"] = std::string(FilePath);
283 LibDeviceMap["sm_37"] = std::string(FilePath);
284 } else if (GpuArch == "compute_50") {
285 if (Version >= CudaVersion::CUDA_80) {
286 LibDeviceMap["sm_50"] = std::string(FilePath);
287 LibDeviceMap["sm_52"] = std::string(FilePath);
288 LibDeviceMap["sm_53"] = std::string(FilePath);
289 }
290 }
291 }
292 }
293
294 // Check that we have found at least one libdevice that we can link in if
295 // -nocudalib hasn't been specified.
296 if (LibDeviceMap.empty() && !NoCudaLib)
297 continue;
298
299 IsValid = true;
300 break;
301 }
302}
303
304void CudaInstallationDetector::AddCudaIncludeArgs(
305 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
306 if (DriverArgs.hasFlag(Pos: options::OPT_foffload_via_llvm,
307 Neg: options::OPT_fno_offload_via_llvm, Default: false))
308 return;
309
310 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
311 // Add cuda_wrappers/* to our system include path. This lets us wrap
312 // standard library headers.
313 SmallString<128> P(D.ResourceDir);
314 llvm::sys::path::append(path&: P, a: "include");
315 llvm::sys::path::append(path&: P, a: "cuda_wrappers");
316 CC1Args.push_back(Elt: "-internal-isystem");
317 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P));
318 }
319
320 if (!DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
321 Default: true))
322 return;
323
324 if (!isValid()) {
325 D.Diag(DiagID: diag::err_drv_no_cuda_installation);
326 return;
327 }
328
329 CC1Args.push_back(Elt: "-include");
330 CC1Args.push_back(Elt: "__clang_cuda_runtime_wrapper.h");
331}
332
333void CudaInstallationDetector::CheckCudaVersionSupportsArch(
334 OffloadArch Arch) const {
335 // Only NVIDIA architectures depend on the CUDA toolkit version.
336 if (!Arch.isNVPTX() || Version == CudaVersion::UNKNOWN ||
337 ArchsWithBadVersion[Arch.nvptxKind()])
338 return;
339
340 auto MinVersion = MinVersionForOffloadArch(A: Arch);
341 auto MaxVersion = MaxVersionForOffloadArch(A: Arch);
342 if (Version < MinVersion || Version > MaxVersion) {
343 ArchsWithBadVersion[Arch.nvptxKind()] = true;
344 D.Diag(DiagID: diag::err_drv_cuda_version_unsupported)
345 << OffloadArchToString(A: Arch) << CudaVersionToString(V: MinVersion)
346 << CudaVersionToString(V: MaxVersion) << InstallPath
347 << CudaVersionToString(V: Version);
348 }
349}
350
351void CudaInstallationDetector::print(raw_ostream &OS) const {
352 if (isValid())
353 OS << "Found CUDA installation: " << InstallPath << ", version "
354 << CudaVersionToString(V: Version) << "\n";
355}
356
357namespace {
358/// Debug info level for the NVPTX devices. We may need to emit different debug
359/// info level for the host and for the device itselfi. This type controls
360/// emission of the debug info for the devices. It either prohibits disable info
361/// emission completely, or emits debug directives only, or emits same debug
362/// info as for the host.
363enum DeviceDebugInfoLevel {
364 DisableDebugInfo, /// Do not emit debug info for the devices.
365 DebugDirectivesOnly, /// Emit only debug directives.
366 EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
367 /// host.
368};
369} // anonymous namespace
370
371/// Define debug info level for the NVPTX devices. If the debug info for both
372/// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
373/// only debug directives are requested for the both host and device
374/// (-gline-directvies-only), or the debug info only for the device is disabled
375/// (optimization is on and --cuda-noopt-device-debug was not specified), the
376/// debug directves only must be emitted for the device. Otherwise, use the same
377/// debug info level just like for the host (with the limitations of only
378/// supported DWARF2 standard).
379static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
380 const Arg *A = Args.getLastArg(Ids: options::OPT_O_Group);
381 bool IsDebugEnabled = !A || A->getOption().matches(ID: options::OPT_O0) ||
382 Args.hasFlag(Pos: options::OPT_cuda_noopt_device_debug,
383 Neg: options::OPT_no_cuda_noopt_device_debug,
384 /*Default=*/false);
385 if (const Arg *A = Args.getLastArg(Ids: options::OPT_g_Group)) {
386 const Option &Opt = A->getOption();
387 if (Opt.matches(ID: options::OPT_gN_Group)) {
388 if (Opt.matches(ID: options::OPT_g0) || Opt.matches(ID: options::OPT_ggdb0))
389 return DisableDebugInfo;
390 if (Opt.matches(ID: options::OPT_gline_directives_only))
391 return DebugDirectivesOnly;
392 }
393 return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
394 }
395 return willEmitRemarks(Args) ? DebugDirectivesOnly : DisableDebugInfo;
396}
397
398void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
399 const InputInfo &Output,
400 const InputInfoList &Inputs,
401 const ArgList &Args,
402 const char *LinkingOutput) const {
403 const auto &TC =
404 static_cast<const toolchains::NVPTXToolChain &>(getToolChain());
405
406 bool UsesLLVMOffloading = Args.hasFlag(
407 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
408 assert((TC.getTriple().isNVPTX() || UsesLLVMOffloading) && "Wrong platform");
409
410 BoundArch GPUArch;
411 // If this is a CUDA action we need to extract the device architecture
412 // from the Job's associated architecture, otherwise use the -march=arch
413 // option. This option may come from -Xopenmp-target flag or the default
414 // value.
415 if (JA.isDeviceOffloading(OKind: Action::OFK_Cuda)) {
416 GPUArch = JA.getOffloadingArch();
417 } else {
418 GPUArch = BoundArch(Args.getLastArgValue(Id: options::OPT_march_EQ));
419 if (GPUArch.empty()) {
420 C.getDriver().Diag(DiagID: diag::err_drv_offload_missing_gpu_arch)
421 << getToolChain().getArchName() << getShortName();
422 return;
423 }
424 }
425
426 // Obtain architecture from the action.
427 assert(!GPUArch.Arch.isUnknown() &&
428 "Device action expected to have an architecture.");
429
430 // Check that our installation's ptxas supports gpu_arch.
431 if (!UsesLLVMOffloading && !Args.hasArg(Ids: options::OPT_no_cuda_version_check)) {
432 TC.CudaInstallation.CheckCudaVersionSupportsArch(Arch: GPUArch.Arch);
433 }
434
435 ArgStringList CmdArgs;
436 CmdArgs.push_back(Elt: TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
437 DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
438 if (DIKind == EmitSameDebugInfoAsHost) {
439 // ptxas does not accept -g option if optimization is enabled, so
440 // we ignore the compiler's -O* options if we want debug info.
441 CmdArgs.push_back(Elt: "-g");
442 CmdArgs.push_back(Elt: "--dont-merge-basicblocks");
443 CmdArgs.push_back(Elt: "--return-at-end");
444 } else if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) {
445 // Map the -O we received to -O{0,1,2,3}.
446 //
447 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
448 // default, so it may correspond more closely to the spirit of clang -O2.
449
450 // -O3 seems like the least-bad option when -Osomething is specified to
451 // clang but it isn't handled below.
452 StringRef OOpt = "3";
453 if (A->getOption().matches(ID: options::OPT_O4) ||
454 A->getOption().matches(ID: options::OPT_Ofast))
455 OOpt = "3";
456 else if (A->getOption().matches(ID: options::OPT_O0))
457 OOpt = "0";
458 else if (A->getOption().matches(ID: options::OPT_O)) {
459 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
460 OOpt = llvm::StringSwitch<const char *>(A->getValue())
461 .Case(S: "1", Value: "1")
462 .Case(S: "2", Value: "2")
463 .Case(S: "3", Value: "3")
464 .Case(S: "s", Value: "2")
465 .Case(S: "z", Value: "2")
466 .Default(Value: "2");
467 }
468 CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::Twine("-O") + OOpt));
469 } else {
470 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
471 // to no optimizations, but ptxas's default is -O3.
472 CmdArgs.push_back(Elt: "-O0");
473 }
474 if (DIKind == DebugDirectivesOnly)
475 CmdArgs.push_back(Elt: "-lineinfo");
476
477 // Pass -v to ptxas if it was passed to the driver.
478 if (Args.hasArg(Ids: options::OPT_v))
479 CmdArgs.push_back(Elt: "-v");
480
481 CmdArgs.push_back(Elt: "--gpu-name");
482 CmdArgs.push_back(Elt: Args.MakeArgString(Str: GPUArch.ArchName));
483 CmdArgs.push_back(Elt: "--output-file");
484 std::string OutputFileName = TC.getInputFilename(Input: Output);
485
486 if (Output.isFilename() && OutputFileName != Output.getFilename())
487 C.addTempFile(Name: Args.MakeArgString(Str: OutputFileName));
488
489 CmdArgs.push_back(Elt: Args.MakeArgString(Str: OutputFileName));
490 for (const auto &II : Inputs)
491 CmdArgs.push_back(Elt: Args.MakeArgString(Str: II.getFilename()));
492
493 for (const auto &A : Args.getAllArgValues(Id: options::OPT_Xcuda_ptxas))
494 CmdArgs.push_back(Elt: Args.MakeArgString(Str: A));
495
496 bool Relocatable;
497 if (JA.isOffloading(OKind: Action::OFK_OpenMP))
498 // In OpenMP we need to generate relocatable code.
499 Relocatable = Args.hasFlag(Pos: options::OPT_fopenmp_relocatable_target,
500 Neg: options::OPT_fnoopenmp_relocatable_target,
501 /*Default=*/true);
502 else if (JA.isOffloading(OKind: Action::OFK_Cuda))
503 // In CUDA we generate relocatable code by default.
504 Relocatable = UsesLLVMOffloading ||
505 Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
506 /*Default=*/false);
507 else
508 // Otherwise, we are compiling directly and should create linkable output.
509 Relocatable = true;
510
511 if (Relocatable)
512 CmdArgs.push_back(Elt: "-c");
513
514 const char *Exec;
515 if (Arg *A = Args.getLastArg(Ids: options::OPT_ptxas_path_EQ))
516 Exec = A->getValue();
517 else
518 Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: "ptxas"));
519 C.addCommand(Cmd: std::make_unique<Command>(
520 args: JA, args: *this,
521 args: ResponseFileSupport{.ResponseKind: ResponseFileSupport::RF_Full, .ResponseEncoding: llvm::sys::WEM_UTF8,
522 .ResponseFlag: "--options-file="},
523 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
524}
525
526static bool shouldIncludePTX(const ArgList &Args, StringRef InputArch) {
527 bool includePTX = false;
528 for (Arg *A : Args.filtered(Ids: options::OPT_cuda_include_ptx_EQ,
529 Ids: options::OPT_no_cuda_include_ptx_EQ)) {
530 A->claim();
531 const StringRef ArchStr = A->getValue();
532 if (A->getOption().matches(ID: options::OPT_cuda_include_ptx_EQ) &&
533 (ArchStr == "all" || ArchStr == InputArch))
534 includePTX = true;
535 else if (A->getOption().matches(ID: options::OPT_no_cuda_include_ptx_EQ) &&
536 (ArchStr == "all" || ArchStr == InputArch))
537 includePTX = false;
538 }
539 return includePTX;
540}
541
542// All inputs to this linker must be from CudaDeviceActions, as we need to look
543// at the Inputs' Actions in order to figure out which GPU architecture they
544// correspond to.
545void NVPTX::FatBinary::ConstructJob(Compilation &C, const JobAction &JA,
546 const InputInfo &Output,
547 const InputInfoList &Inputs,
548 const ArgList &Args,
549 const char *LinkingOutput) const {
550 const auto &TC =
551 static_cast<const toolchains::CudaToolChain &>(getToolChain());
552 [[maybe_unused]] bool UsesLLVMOffloading = Args.hasFlag(
553 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
554 assert((UsesLLVMOffloading || TC.getTriple().isNVPTX()) && "Wrong platform");
555
556 ArgStringList CmdArgs;
557 if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
558 CmdArgs.push_back(Elt: "--cuda");
559 CmdArgs.push_back(Elt: TC.getTriple().isArch64Bit() ? "-64" : "-32");
560 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--create"));
561 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Output.getFilename()));
562 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
563 CmdArgs.push_back(Elt: "-g");
564
565 for (const auto &II : Inputs) {
566 auto *A = II.getAction();
567 assert(A->getInputs().size() == 1 &&
568 "Device offload action is expected to have a single input");
569 BoundArch GpuArch = A->getOffloadingArch();
570 assert(!GpuArch.empty() &&
571 "Device action expected to have associated a GPU architecture!");
572
573 if (II.getType() == types::TY_PP_Asm &&
574 !shouldIncludePTX(Args, InputArch: GpuArch.ArchName))
575 continue;
576 StringRef Kind = (II.getType() == types::TY_PP_Asm) ? "ptx" : "elf";
577 CmdArgs.push_back(Elt: Args.MakeArgString(
578 Str: "--image3=kind=" + Kind + ",sm=" + GpuArch.ArchName.drop_front(N: 3) +
579 ",file=" + getToolChain().getInputFilename(Input: II)));
580 }
581
582 for (const auto &A : Args.getAllArgValues(Id: options::OPT_Xcuda_fatbinary))
583 CmdArgs.push_back(Elt: Args.MakeArgString(Str: A));
584
585 const char *Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: "fatbinary"));
586 C.addCommand(Cmd: std::make_unique<Command>(
587 args: JA, args: *this,
588 args: ResponseFileSupport{.ResponseKind: ResponseFileSupport::RF_Full, .ResponseEncoding: llvm::sys::WEM_UTF8,
589 .ResponseFlag: "--options-file="},
590 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
591}
592
593void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
594 const InputInfo &Output,
595 const InputInfoList &Inputs,
596 const ArgList &Args,
597 const char *LinkingOutput) const {
598 const auto &TC =
599 static_cast<const toolchains::NVPTXToolChain &>(getToolChain());
600 ArgStringList CmdArgs;
601
602 [[maybe_unused]] bool UsesLLVMOffloading = Args.hasFlag(
603 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
604 assert((UsesLLVMOffloading || TC.getTriple().isNVPTX()) && "Wrong platform");
605
606 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
607 if (Output.isFilename()) {
608 CmdArgs.push_back(Elt: "-o");
609 CmdArgs.push_back(Elt: Output.getFilename());
610 }
611
612 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
613 CmdArgs.push_back(Elt: "-g");
614
615 if (Args.hasArg(Ids: options::OPT_v))
616 CmdArgs.push_back(Elt: "-v");
617
618 StringRef GPUArch = Args.getLastArgValue(Id: options::OPT_march_EQ);
619 if (GPUArch.empty() && !getToolChain().isUsingLTO(Args)) {
620 C.getDriver().Diag(DiagID: diag::err_drv_offload_missing_gpu_arch)
621 << getToolChain().getArchName() << getShortName();
622 return;
623 }
624
625 if (!GPUArch.empty()) {
626 CmdArgs.push_back(Elt: "-arch");
627 CmdArgs.push_back(Elt: Args.MakeArgString(Str: GPUArch));
628 }
629
630 if (Args.hasArg(Ids: options::OPT_ptxas_path_EQ))
631 CmdArgs.push_back(Elt: Args.MakeArgString(
632 Str: "--ptxas-path=" + Args.getLastArgValue(Id: options::OPT_ptxas_path_EQ)));
633
634 // The wrapper runs 'ptxas' itself when doing LTO, so it needs these.
635 for (const Arg *A : Args.filtered(Ids: options::OPT_Xcuda_ptxas)) {
636 A->claim();
637 CmdArgs.append(IL: {"-Xptxas", A->getValue()});
638 }
639
640 if (Args.hasArg(Ids: options::OPT_cuda_path_EQ) || TC.CudaInstallation.isValid()) {
641 StringRef CudaPath = Args.getLastArgValue(
642 Id: options::OPT_cuda_path_EQ,
643 Default: llvm::sys::path::parent_path(path: TC.CudaInstallation.getBinPath()));
644 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--cuda-path=" + CudaPath));
645 }
646
647 // Add paths specified in LIBRARY_PATH environment variable as -L options.
648 addDirectoryList(Args, CmdArgs, ArgName: "-L", EnvVar: "LIBRARY_PATH");
649
650 // Add standard library search paths passed on the command line.
651 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L);
652 getToolChain().AddFilePathLibArgs(Args, CmdArgs);
653 AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA);
654
655 if (auto LTO = getToolChain().getLTOMode(Args); LTO != LTOK_None)
656 addLTOOptions(ToolChain: getToolChain(), Args, CmdArgs, Output, Inputs,
657 IsThinLTO: LTO == LTOK_Thin);
658
659 // Forward the PTX features if the nvlink-wrapper needs it.
660 std::vector<StringRef> Features;
661 getNVPTXTargetFeatures(D: C.getDriver(), Triple: getToolChain().getTriple(), Args,
662 Features);
663 CmdArgs.push_back(
664 Elt: Args.MakeArgString(Str: "--plugin-opt=-mattr=" + llvm::join(R&: Features, Separator: ",")));
665
666 // Add paths for the default clang library path.
667 SmallString<256> DefaultLibPath =
668 llvm::sys::path::parent_path(path: TC.getDriver().Dir);
669 llvm::sys::path::append(path&: DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
670 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-L") + DefaultLibPath));
671
672 getToolChain().addProfileRTLibs(Args, CmdArgs);
673 addSanitizerRuntimes(TC: getToolChain(), Args, CmdArgs, C);
674
675 if (Args.hasArg(Ids: options::OPT_stdlib))
676 CmdArgs.append(IL: {"-lc", "-lm"});
677 if (Args.hasArg(Ids: options::OPT_startfiles)) {
678 std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
679 if (!IncludePath)
680 IncludePath = "/lib";
681 SmallString<128> P(*IncludePath);
682 llvm::sys::path::append(path&: P, a: "crt1.o");
683 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
684 }
685
686 C.addCommand(Cmd: std::make_unique<Command>(
687 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(),
688 args: Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "clang-nvlink-wrapper")),
689 args&: CmdArgs, args: Inputs, args: Output));
690}
691
692void NVPTX::getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple,
693 const llvm::opt::ArgList &Args,
694 std::vector<StringRef> &Features) {
695 if (Args.hasArg(Ids: options::OPT_cuda_feature_EQ)) {
696 StringRef PtxFeature = Args.getLastArgValue(Id: options::OPT_cuda_feature_EQ);
697 Features.push_back(x: Args.MakeArgString(Str: PtxFeature));
698 return;
699 }
700 CudaInstallationDetector CudaInstallation(D, Triple, Args);
701
702 // New CUDA versions often introduce new instructions that are only supported
703 // by new PTX version, so we need to raise PTX level to enable them in NVPTX
704 // back-end.
705 const char *PtxFeature = nullptr;
706 switch (CudaInstallation.version()) {
707#define CASE_CUDA_VERSION(CUDA_VER, PTX_VER) \
708 case CudaVersion::CUDA_##CUDA_VER: \
709 PtxFeature = "+ptx" #PTX_VER; \
710 break;
711 CASE_CUDA_VERSION(134, 94);
712 CASE_CUDA_VERSION(133, 93);
713 CASE_CUDA_VERSION(132, 92);
714 CASE_CUDA_VERSION(131, 91);
715 CASE_CUDA_VERSION(130, 90);
716 CASE_CUDA_VERSION(129, 88);
717 CASE_CUDA_VERSION(128, 87);
718 CASE_CUDA_VERSION(126, 85);
719 CASE_CUDA_VERSION(125, 85);
720 CASE_CUDA_VERSION(124, 84);
721 CASE_CUDA_VERSION(123, 83);
722 CASE_CUDA_VERSION(122, 82);
723 CASE_CUDA_VERSION(121, 81);
724 CASE_CUDA_VERSION(120, 80);
725 CASE_CUDA_VERSION(118, 78);
726 CASE_CUDA_VERSION(117, 77);
727 CASE_CUDA_VERSION(116, 76);
728 CASE_CUDA_VERSION(115, 75);
729 CASE_CUDA_VERSION(114, 74);
730 CASE_CUDA_VERSION(113, 73);
731 CASE_CUDA_VERSION(112, 72);
732 CASE_CUDA_VERSION(111, 71);
733 CASE_CUDA_VERSION(110, 70);
734 CASE_CUDA_VERSION(102, 65);
735 CASE_CUDA_VERSION(101, 64);
736 CASE_CUDA_VERSION(100, 63);
737 CASE_CUDA_VERSION(92, 61);
738 CASE_CUDA_VERSION(91, 61);
739 CASE_CUDA_VERSION(90, 60);
740 CASE_CUDA_VERSION(80, 50);
741 CASE_CUDA_VERSION(75, 43);
742 CASE_CUDA_VERSION(70, 42);
743#undef CASE_CUDA_VERSION
744 // TODO: Use specific CUDA version once it's public.
745 case clang::CudaVersion::NEW:
746 PtxFeature = "+ptx86";
747 break;
748 default:
749 // No PTX feature specified; let the backend choose based on the target SM.
750 break;
751 }
752 if (PtxFeature)
753 Features.push_back(x: PtxFeature);
754}
755
756/// NVPTX toolchain. Our assembler is ptxas, and our linker is nvlink. This
757/// operates as a stand-alone version of the NVPTX tools without the host
758/// toolchain.
759NVPTXToolChain::NVPTXToolChain(const Driver &D, const llvm::Triple &Triple,
760 const llvm::Triple &HostTriple,
761 const ArgList &Args)
762 : ToolChain(D, Triple, Args), CudaInstallation(D, HostTriple, Args) {
763 if (CudaInstallation.isValid())
764 getProgramPaths().push_back(Elt: std::string(CudaInstallation.getBinPath()));
765 // Lookup binaries into the driver directory, this is used to
766 // discover the 'nvptx-arch' executable.
767 getProgramPaths().push_back(Elt: getDriver().Dir);
768}
769
770/// We only need the host triple to locate the CUDA binary utilities, use the
771/// system's default triple if not provided.
772NVPTXToolChain::NVPTXToolChain(const Driver &D, const llvm::Triple &Triple,
773 const ArgList &Args)
774 : NVPTXToolChain(D, Triple, llvm::Triple(LLVM_HOST_TRIPLE), Args) {
775 loadMultilibsFromYAML(Args, D);
776}
777
778llvm::opt::DerivedArgList *
779NVPTXToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
780 BoundArch BA,
781 Action::OffloadKind OffloadKind) const {
782 DerivedArgList *DAL = ToolChain::TranslateArgs(Args, BA, DeviceOffloadKind: OffloadKind);
783 if (!DAL)
784 DAL = new DerivedArgList(Args.getBaseArgs());
785
786 const OptTable &Opts = getDriver().getOpts();
787
788 for (Arg *A : Args)
789 if (!llvm::is_contained(Range&: *DAL, Element: A))
790 DAL->append(A);
791
792 if (!DAL->hasArg(Ids: options::OPT_march_EQ) && OffloadKind != Action::OFK_None) {
793 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ),
794 Value: OffloadArchToString(A: OffloadArch::CudaDefault()));
795 } else if (DAL->getLastArgValue(Id: options::OPT_march_EQ) == "generic" &&
796 OffloadKind == Action::OFK_None) {
797 DAL->eraseArg(Id: options::OPT_march_EQ);
798 } else if (DAL->getLastArgValue(Id: options::OPT_march_EQ) == "native") {
799 auto GPUsOrErr = getSystemGPUArchs(Args);
800 if (!GPUsOrErr) {
801 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
802 << getArchName() << llvm::toString(E: GPUsOrErr.takeError()) << "-march";
803 } else {
804 auto &GPUs = *GPUsOrErr;
805 if (llvm::SmallSet<std::string, 1>(GPUs.begin(), GPUs.end()).size() > 1)
806 getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch)
807 << getArchName() << llvm::join(R&: GPUs, Separator: ", ") << "-march";
808 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ),
809 Value: Args.MakeArgString(Str: GPUs.front()));
810 }
811 }
812
813 return DAL;
814}
815
816void NVPTXToolChain::addClangTargetOptions(
817 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
818 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {}
819
820void NVPTXToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
821 ArgStringList &CC1Args) const {
822 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
823 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
824 return;
825
826 // Add multilib variant include paths in priority order.
827 for (const Multilib &M : getOrderedMultilibs()) {
828 if (M.isDefault())
829 continue;
830 if (std::optional<std::string> StdlibIncDir = getStdlibIncludePath()) {
831 SmallString<128> Dir(*StdlibIncDir);
832 llvm::sys::path::append(path&: Dir, a: M.includeSuffix());
833 if (getDriver().getVFS().exists(Path: Dir))
834 addSystemInclude(DriverArgs, CC1Args, Path: Dir);
835 }
836 }
837
838 if (std::optional<std::string> Path = getStdlibIncludePath())
839 addSystemInclude(DriverArgs, CC1Args, Path: *Path);
840}
841
842bool NVPTXToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
843 const Option &O = A->getOption();
844 return (O.matches(ID: options::OPT_gN_Group) &&
845 !O.matches(ID: options::OPT_gmodules)) ||
846 O.matches(ID: options::OPT_g_Flag) ||
847 O.matches(ID: options::OPT_ggdbN_Group) || O.matches(ID: options::OPT_ggdb) ||
848 O.matches(ID: options::OPT_gdwarf) || O.matches(ID: options::OPT_gdwarf_2) ||
849 O.matches(ID: options::OPT_gdwarf_3) || O.matches(ID: options::OPT_gdwarf_4) ||
850 O.matches(ID: options::OPT_gdwarf_5) ||
851 O.matches(ID: options::OPT_gcolumn_info);
852}
853
854void NVPTXToolChain::adjustDebugInfoKind(
855 llvm::codegenoptions::DebugInfoKind &DebugInfoKind,
856 const ArgList &Args) const {
857 switch (mustEmitDebugInfo(Args)) {
858 case DisableDebugInfo:
859 DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
860 break;
861 case DebugDirectivesOnly:
862 DebugInfoKind = llvm::codegenoptions::DebugDirectivesOnly;
863 break;
864 case EmitSameDebugInfoAsHost:
865 // Use same debug info level as the host.
866 break;
867 }
868}
869
870Expected<SmallVector<std::string>>
871NVPTXToolChain::getSystemGPUArchs(const ArgList &Args) const {
872 // Detect NVIDIA GPUs availible on the system.
873 std::string Program;
874 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ))
875 Program = A->getValue();
876 else
877 Program = GetProgramPath(Name: "nvptx-arch");
878
879 auto StdoutOrErr = getDriver().executeProgram(Args: {Program});
880 if (!StdoutOrErr)
881 return StdoutOrErr.takeError();
882
883 SmallVector<std::string, 1> GPUArchs;
884 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
885 if (!Arch.empty())
886 GPUArchs.push_back(Elt: Arch.str());
887
888 if (GPUArchs.empty())
889 return llvm::createStringError(EC: std::error_code(),
890 S: "No NVIDIA GPU detected in the system");
891
892 return std::move(GPUArchs);
893}
894
895/// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
896/// which isn't properly a linker but nonetheless performs the step of stitching
897/// together object files from the assembler into a single blob.
898
899CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
900 const ToolChain &HostTC, const ArgList &Args)
901 : NVPTXToolChain(D, Triple, HostTC.getTriple(), Args), HostTC(HostTC) {}
902
903void CudaToolChain::addClangTargetOptions(
904 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
905 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
906 HostTC.addClangTargetOptions(DriverArgs, CC1Args, BA, DeviceOffloadKind: DeviceOffloadingKind);
907
908 bool UsesLLVMOffloading = DriverArgs.hasFlag(
909 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
910
911 StringRef GpuArch = DriverArgs.getLastArgValue(Id: options::OPT_march_EQ);
912 assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
913 DeviceOffloadingKind == Action::OFK_Cuda || UsesLLVMOffloading) &&
914 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
915
916 CC1Args.append(IL: {"-fcuda-is-device", "-fno-threadsafe-statics"});
917
918 if (DriverArgs.hasFlag(Pos: options::OPT_fcuda_short_ptr,
919 Neg: options::OPT_fno_cuda_short_ptr, Default: false))
920 CC1Args.append(IL: {"-target-abi", "shortptr"});
921
922 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
923 Default: true))
924 return;
925
926 if (DeviceOffloadingKind == Action::OFK_OpenMP &&
927 DriverArgs.hasArg(Ids: options::OPT_S))
928 return;
929
930 if (UsesLLVMOffloading)
931 return;
932
933 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(Gpu: GpuArch);
934 if (LibDeviceFile.empty()) {
935 getDriver().Diag(DiagID: diag::err_drv_no_cuda_libdevice) << GpuArch;
936 return;
937 }
938
939 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
940 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibDeviceFile));
941
942 clang::CudaVersion CudaInstallationVersion = CudaInstallation.version();
943
944 if (CudaInstallationVersion >= CudaVersion::UNKNOWN)
945 CC1Args.push_back(
946 Elt: DriverArgs.MakeArgString(Str: Twine("-target-sdk-version=") +
947 CudaVersionToString(V: CudaInstallationVersion)));
948
949 if (DeviceOffloadingKind == Action::OFK_OpenMP) {
950 if (CudaInstallationVersion < CudaVersion::CUDA_92) {
951 getDriver().Diag(
952 DiagID: diag::err_drv_omp_offload_target_cuda_version_not_support)
953 << CudaVersionToString(V: CudaInstallationVersion);
954 return;
955 }
956
957 // Link the bitcode library late if we're using device LTO.
958 if (isUsingLTO(Args: DriverArgs, Kind: DeviceOffloadingKind))
959 return;
960
961 addOpenMPDeviceRTL(D: getDriver(), DriverArgs, CC1Args, BitcodeSuffix: GpuArch.str(),
962 Triple: getTriple(), HostTC);
963 }
964}
965
966llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType(
967 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
968 const llvm::fltSemantics *FPType) const {
969 if (JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
970 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
971 DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
972 Neg: options::OPT_fno_gpu_flush_denormals_to_zero, Default: false))
973 return llvm::DenormalMode::getPreserveSign();
974 }
975
976 assert(JA.getOffloadingDeviceKind() != Action::OFK_Host);
977 return llvm::DenormalMode::getIEEE();
978}
979
980void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
981 ArgStringList &CC1Args) const {
982 if (DriverArgs.hasFlag(Pos: options::OPT_foffload_via_llvm,
983 Neg: options::OPT_fno_offload_via_llvm, Default: false))
984 return;
985
986 // Check our CUDA version if we're going to include the CUDA headers.
987 if (DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
988 Default: true) &&
989 !DriverArgs.hasArg(Ids: options::OPT_no_cuda_version_check)) {
990 StringRef Arch = DriverArgs.getLastArgValue(Id: options::OPT_march_EQ);
991 assert(!Arch.empty() && "Must have an explicit GPU arch.");
992 CudaInstallation.CheckCudaVersionSupportsArch(Arch: StringToOffloadArch(S: Arch));
993 }
994 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
995}
996
997std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
998 // Only object files are changed, for example assembly files keep their .s
999 // extensions. If the user requested device-only compilation don't change it.
1000 if (Input.getType() != types::TY_Object || getDriver().offloadDeviceOnly())
1001 return ToolChain::getInputFilename(Input);
1002
1003 return ToolChain::getInputFilename(Input);
1004}
1005
1006llvm::opt::DerivedArgList *
1007CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
1008 BoundArch BA,
1009 Action::OffloadKind DeviceOffloadKind) const {
1010 DerivedArgList *DAL = HostTC.TranslateArgs(Args, BA, DeviceOffloadKind);
1011 if (!DAL)
1012 DAL = new DerivedArgList(Args.getBaseArgs());
1013
1014 const OptTable &Opts = getDriver().getOpts();
1015
1016 for (Arg *A : Args) {
1017 // Make sure flags are not duplicated.
1018 if (!llvm::is_contained(Range&: *DAL, Element: A)) {
1019 DAL->append(A);
1020 }
1021 }
1022
1023 if (BA) {
1024 DAL->eraseArg(Id: options::OPT_march_EQ);
1025 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ),
1026 Value: BA.ArchName);
1027 }
1028 return DAL;
1029}
1030
1031Tool *NVPTXToolChain::buildAssembler() const {
1032 return new tools::NVPTX::Assembler(*this);
1033}
1034
1035Tool *NVPTXToolChain::buildLinker() const {
1036 return new tools::NVPTX::Linker(*this);
1037}
1038
1039Tool *CudaToolChain::buildAssembler() const {
1040 return new tools::NVPTX::Assembler(*this);
1041}
1042
1043Tool *CudaToolChain::buildLinker() const {
1044 return new tools::NVPTX::FatBinary(*this);
1045}
1046
1047void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
1048 HostTC.addClangWarningOptions(CC1Args);
1049}
1050
1051ToolChain::CXXStdlibType
1052CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
1053 return HostTC.GetCXXStdlibType(Args);
1054}
1055
1056void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1057 ArgStringList &CC1Args) const {
1058 if (DriverArgs.hasFlag(Pos: options::OPT_foffload_via_llvm,
1059 Neg: options::OPT_fno_offload_via_llvm, Default: false))
1060 return;
1061
1062 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
1063
1064 if (DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
1065 Default: true) &&
1066 CudaInstallation.isValid())
1067 CC1Args.append(
1068 IL: {"-internal-isystem",
1069 DriverArgs.MakeArgString(Str: CudaInstallation.getIncludePath())});
1070}
1071
1072void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
1073 ArgStringList &CC1Args) const {
1074 HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
1075}
1076
1077void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
1078 ArgStringList &CC1Args) const {
1079 HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args);
1080}
1081
1082SanitizerMask CudaToolChain::getSupportedSanitizers(
1083 BoundArch BA, Action::OffloadKind DeviceOffloadKind) const {
1084 // The CudaToolChain only supports sanitizers in the sense that it allows
1085 // sanitizer arguments on the command line if they are supported by the host
1086 // toolchain. The CudaToolChain will actually ignore any command line
1087 // arguments for any of these "supported" sanitizers. That means that no
1088 // sanitization of device code is actually supported at this time.
1089 //
1090 // This behavior is necessary because the host and device toolchains
1091 // invocations often share the command line, so the device toolchain must
1092 // tolerate flags meant only for the host toolchain.
1093
1094 // FIXME: Be accurate and use DeviceOffloadKind.
1095 return HostTC.getSupportedSanitizers(BA, DeviceOffloadKind);
1096}
1097
1098VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
1099 const ArgList &Args) const {
1100 return HostTC.computeMSVCVersion(D, Args);
1101}
1102