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 "clang/Basic/Cuda.h"
12 #include "clang/Config/config.h"
13 #include "clang/Driver/Compilation.h"
14 #include "clang/Driver/Distro.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/DriverDiagnostic.h"
17 #include "clang/Driver/InputInfo.h"
18 #include "clang/Driver/Options.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/FormatAdapters.h"
24 #include "llvm/Support/FormatVariadic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/Process.h"
27 #include "llvm/Support/Program.h"
28 #include "llvm/Support/VirtualFileSystem.h"
29 #include "llvm/TargetParser/Host.h"
30 #include "llvm/TargetParser/TargetParser.h"
31 #include <system_error>
33 using namespace clang::driver
;
34 using namespace clang::driver::toolchains
;
35 using namespace clang::driver::tools
;
36 using namespace clang
;
37 using namespace llvm::opt
;
41 CudaVersion
getCudaVersion(uint32_t raw_version
) {
42 if (raw_version
< 7050)
43 return CudaVersion::CUDA_70
;
44 if (raw_version
< 8000)
45 return CudaVersion::CUDA_75
;
46 if (raw_version
< 9000)
47 return CudaVersion::CUDA_80
;
48 if (raw_version
< 9010)
49 return CudaVersion::CUDA_90
;
50 if (raw_version
< 9020)
51 return CudaVersion::CUDA_91
;
52 if (raw_version
< 10000)
53 return CudaVersion::CUDA_92
;
54 if (raw_version
< 10010)
55 return CudaVersion::CUDA_100
;
56 if (raw_version
< 10020)
57 return CudaVersion::CUDA_101
;
58 if (raw_version
< 11000)
59 return CudaVersion::CUDA_102
;
60 if (raw_version
< 11010)
61 return CudaVersion::CUDA_110
;
62 if (raw_version
< 11020)
63 return CudaVersion::CUDA_111
;
64 if (raw_version
< 11030)
65 return CudaVersion::CUDA_112
;
66 if (raw_version
< 11040)
67 return CudaVersion::CUDA_113
;
68 if (raw_version
< 11050)
69 return CudaVersion::CUDA_114
;
70 if (raw_version
< 11060)
71 return CudaVersion::CUDA_115
;
72 if (raw_version
< 11070)
73 return CudaVersion::CUDA_116
;
74 if (raw_version
< 11080)
75 return CudaVersion::CUDA_117
;
76 if (raw_version
< 11090)
77 return CudaVersion::CUDA_118
;
78 if (raw_version
< 12010)
79 return CudaVersion::CUDA_120
;
80 if (raw_version
< 12020)
81 return CudaVersion::CUDA_121
;
82 if (raw_version
< 12030)
83 return CudaVersion::CUDA_122
;
84 if (raw_version
< 12040)
85 return CudaVersion::CUDA_123
;
86 if (raw_version
< 12050)
87 return CudaVersion::CUDA_124
;
88 if (raw_version
< 12060)
89 return CudaVersion::CUDA_125
;
90 if (raw_version
< 12070)
91 return CudaVersion::CUDA_126
;
92 return CudaVersion::NEW
;
95 CudaVersion
parseCudaHFile(llvm::StringRef Input
) {
96 // Helper lambda which skips the words if the line starts with them or returns
97 // std::nullopt otherwise.
98 auto StartsWithWords
=
99 [](llvm::StringRef Line
,
100 const SmallVector
<StringRef
, 3> words
) -> std::optional
<StringRef
> {
101 for (StringRef word
: words
) {
102 if (!Line
.consume_front(word
))
109 Input
= Input
.ltrim();
110 while (!Input
.empty()) {
112 StartsWithWords(Input
.ltrim(), {"#", "define", "CUDA_VERSION"})) {
114 Line
->consumeInteger(10, RawVersion
);
115 return getCudaVersion(RawVersion
);
117 // Find next non-empty line.
118 Input
= Input
.drop_front(Input
.find_first_of("\n\r")).ltrim();
120 return CudaVersion::UNKNOWN
;
124 void CudaInstallationDetector::WarnIfUnsupportedVersion() {
125 if (Version
> CudaVersion::PARTIALLY_SUPPORTED
) {
126 std::string VersionString
= CudaVersionToString(Version
);
127 if (!VersionString
.empty())
128 VersionString
.insert(0, " ");
129 D
.Diag(diag::warn_drv_new_cuda_version
)
131 << (CudaVersion::PARTIALLY_SUPPORTED
!= CudaVersion::FULLY_SUPPORTED
)
132 << CudaVersionToString(CudaVersion::PARTIALLY_SUPPORTED
);
133 } else if (Version
> CudaVersion::FULLY_SUPPORTED
)
134 D
.Diag(diag::warn_drv_partially_supported_cuda_version
)
135 << CudaVersionToString(Version
);
138 CudaInstallationDetector::CudaInstallationDetector(
139 const Driver
&D
, const llvm::Triple
&HostTriple
,
140 const llvm::opt::ArgList
&Args
)
146 Candidate(std::string Path
, bool StrictChecking
= false)
147 : Path(Path
), StrictChecking(StrictChecking
) {}
149 SmallVector
<Candidate
, 4> Candidates
;
151 // In decreasing order so we prefer newer versions to older versions.
152 std::initializer_list
<const char *> Versions
= {"8.0", "7.5", "7.0"};
153 auto &FS
= D
.getVFS();
155 if (Args
.hasArg(clang::driver::options::OPT_cuda_path_EQ
)) {
156 Candidates
.emplace_back(
157 Args
.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ
).str());
158 } else if (HostTriple
.isOSWindows()) {
159 for (const char *Ver
: Versions
)
160 Candidates
.emplace_back(
161 D
.SysRoot
+ "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
164 if (!Args
.hasArg(clang::driver::options::OPT_cuda_path_ignore_env
)) {
165 // Try to find ptxas binary. If the executable is located in a directory
166 // called 'bin/', its parent directory might be a good guess for a valid
167 // CUDA installation.
168 // However, some distributions might installs 'ptxas' to /usr/bin. In that
169 // case the candidate would be '/usr' which passes the following checks
170 // because '/usr/include' exists as well. To avoid this case, we always
171 // check for the directory potentially containing files for libdevice,
172 // even if the user passes -nocudalib.
173 if (llvm::ErrorOr
<std::string
> ptxas
=
174 llvm::sys::findProgramByName("ptxas")) {
175 SmallString
<256> ptxasAbsolutePath
;
176 llvm::sys::fs::real_path(*ptxas
, ptxasAbsolutePath
);
178 StringRef ptxasDir
= llvm::sys::path::parent_path(ptxasAbsolutePath
);
179 if (llvm::sys::path::filename(ptxasDir
) == "bin")
180 Candidates
.emplace_back(
181 std::string(llvm::sys::path::parent_path(ptxasDir
)),
182 /*StrictChecking=*/true);
186 Candidates
.emplace_back(D
.SysRoot
+ "/usr/local/cuda");
187 for (const char *Ver
: Versions
)
188 Candidates
.emplace_back(D
.SysRoot
+ "/usr/local/cuda-" + Ver
);
190 Distro
Dist(FS
, llvm::Triple(llvm::sys::getProcessTriple()));
191 if (Dist
.IsDebian() || Dist
.IsUbuntu())
192 // Special case for Debian to have nvidia-cuda-toolkit work
193 // out of the box. More info on http://bugs.debian.org/882505
194 Candidates
.emplace_back(D
.SysRoot
+ "/usr/lib/cuda");
197 bool NoCudaLib
= Args
.hasArg(options::OPT_nogpulib
);
199 for (const auto &Candidate
: Candidates
) {
200 InstallPath
= Candidate
.Path
;
201 if (InstallPath
.empty() || !FS
.exists(InstallPath
))
204 BinPath
= InstallPath
+ "/bin";
205 IncludePath
= InstallPath
+ "/include";
206 LibDevicePath
= InstallPath
+ "/nvvm/libdevice";
208 if (!(FS
.exists(IncludePath
) && FS
.exists(BinPath
)))
210 bool CheckLibDevice
= (!NoCudaLib
|| Candidate
.StrictChecking
);
211 if (CheckLibDevice
&& !FS
.exists(LibDevicePath
))
214 Version
= CudaVersion::UNKNOWN
;
215 if (auto CudaHFile
= FS
.getBufferForFile(InstallPath
+ "/include/cuda.h"))
216 Version
= parseCudaHFile((*CudaHFile
)->getBuffer());
217 // As the last resort, make an educated guess between CUDA-7.0, which had
218 // old-style libdevice bitcode, and an unknown recent CUDA version.
219 if (Version
== CudaVersion::UNKNOWN
) {
220 Version
= FS
.exists(LibDevicePath
+ "/libdevice.10.bc")
222 : CudaVersion::CUDA_70
;
225 if (Version
>= CudaVersion::CUDA_90
) {
226 // CUDA-9+ uses single libdevice file for all GPU variants.
227 std::string FilePath
= LibDevicePath
+ "/libdevice.10.bc";
228 if (FS
.exists(FilePath
)) {
229 for (int Arch
= (int)OffloadArch::SM_30
, E
= (int)OffloadArch::LAST
;
231 OffloadArch OA
= static_cast<OffloadArch
>(Arch
);
232 if (!IsNVIDIAOffloadArch(OA
))
234 std::string
OffloadArchName(OffloadArchToString(OA
));
235 LibDeviceMap
[OffloadArchName
] = FilePath
;
240 for (llvm::vfs::directory_iterator LI
= FS
.dir_begin(LibDevicePath
, EC
),
242 !EC
&& LI
!= LE
; LI
= LI
.increment(EC
)) {
243 StringRef FilePath
= LI
->path();
244 StringRef FileName
= llvm::sys::path::filename(FilePath
);
245 // Process all bitcode filenames that look like
246 // libdevice.compute_XX.YY.bc
247 const StringRef LibDeviceName
= "libdevice.";
248 if (!(FileName
.starts_with(LibDeviceName
) && FileName
.ends_with(".bc")))
250 StringRef GpuArch
= FileName
.slice(
251 LibDeviceName
.size(), FileName
.find('.', LibDeviceName
.size()));
252 LibDeviceMap
[GpuArch
] = FilePath
.str();
253 // Insert map entries for specific devices with this compute
254 // capability. NVCC's choice of the libdevice library version is
255 // rather peculiar and depends on the CUDA version.
256 if (GpuArch
== "compute_20") {
257 LibDeviceMap
["sm_20"] = std::string(FilePath
);
258 LibDeviceMap
["sm_21"] = std::string(FilePath
);
259 LibDeviceMap
["sm_32"] = std::string(FilePath
);
260 } else if (GpuArch
== "compute_30") {
261 LibDeviceMap
["sm_30"] = std::string(FilePath
);
262 if (Version
< CudaVersion::CUDA_80
) {
263 LibDeviceMap
["sm_50"] = std::string(FilePath
);
264 LibDeviceMap
["sm_52"] = std::string(FilePath
);
265 LibDeviceMap
["sm_53"] = std::string(FilePath
);
267 LibDeviceMap
["sm_60"] = std::string(FilePath
);
268 LibDeviceMap
["sm_61"] = std::string(FilePath
);
269 LibDeviceMap
["sm_62"] = std::string(FilePath
);
270 } else if (GpuArch
== "compute_35") {
271 LibDeviceMap
["sm_35"] = std::string(FilePath
);
272 LibDeviceMap
["sm_37"] = std::string(FilePath
);
273 } else if (GpuArch
== "compute_50") {
274 if (Version
>= CudaVersion::CUDA_80
) {
275 LibDeviceMap
["sm_50"] = std::string(FilePath
);
276 LibDeviceMap
["sm_52"] = std::string(FilePath
);
277 LibDeviceMap
["sm_53"] = std::string(FilePath
);
283 // Check that we have found at least one libdevice that we can link in if
284 // -nocudalib hasn't been specified.
285 if (LibDeviceMap
.empty() && !NoCudaLib
)
293 void CudaInstallationDetector::AddCudaIncludeArgs(
294 const ArgList
&DriverArgs
, ArgStringList
&CC1Args
) const {
295 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
296 // Add cuda_wrappers/* to our system include path. This lets us wrap
297 // standard library headers.
298 SmallString
<128> P(D
.ResourceDir
);
299 llvm::sys::path::append(P
, "include");
300 llvm::sys::path::append(P
, "cuda_wrappers");
301 CC1Args
.push_back("-internal-isystem");
302 CC1Args
.push_back(DriverArgs
.MakeArgString(P
));
305 if (DriverArgs
.hasArg(options::OPT_nogpuinc
))
309 D
.Diag(diag::err_drv_no_cuda_installation
);
313 CC1Args
.push_back("-include");
314 CC1Args
.push_back("__clang_cuda_runtime_wrapper.h");
317 void CudaInstallationDetector::CheckCudaVersionSupportsArch(
318 OffloadArch Arch
) const {
319 if (Arch
== OffloadArch::UNKNOWN
|| Version
== CudaVersion::UNKNOWN
||
320 ArchsWithBadVersion
[(int)Arch
])
323 auto MinVersion
= MinVersionForOffloadArch(Arch
);
324 auto MaxVersion
= MaxVersionForOffloadArch(Arch
);
325 if (Version
< MinVersion
|| Version
> MaxVersion
) {
326 ArchsWithBadVersion
[(int)Arch
] = true;
327 D
.Diag(diag::err_drv_cuda_version_unsupported
)
328 << OffloadArchToString(Arch
) << CudaVersionToString(MinVersion
)
329 << CudaVersionToString(MaxVersion
) << InstallPath
330 << CudaVersionToString(Version
);
334 void CudaInstallationDetector::print(raw_ostream
&OS
) const {
336 OS
<< "Found CUDA installation: " << InstallPath
<< ", version "
337 << CudaVersionToString(Version
) << "\n";
341 /// Debug info level for the NVPTX devices. We may need to emit different debug
342 /// info level for the host and for the device itselfi. This type controls
343 /// emission of the debug info for the devices. It either prohibits disable info
344 /// emission completely, or emits debug directives only, or emits same debug
345 /// info as for the host.
346 enum DeviceDebugInfoLevel
{
347 DisableDebugInfo
, /// Do not emit debug info for the devices.
348 DebugDirectivesOnly
, /// Emit only debug directives.
349 EmitSameDebugInfoAsHost
, /// Use the same debug info level just like for the
352 } // anonymous namespace
354 /// Define debug info level for the NVPTX devices. If the debug info for both
355 /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
356 /// only debug directives are requested for the both host and device
357 /// (-gline-directvies-only), or the debug info only for the device is disabled
358 /// (optimization is on and --cuda-noopt-device-debug was not specified), the
359 /// debug directves only must be emitted for the device. Otherwise, use the same
360 /// debug info level just like for the host (with the limitations of only
361 /// supported DWARF2 standard).
362 static DeviceDebugInfoLevel
mustEmitDebugInfo(const ArgList
&Args
) {
363 const Arg
*A
= Args
.getLastArg(options::OPT_O_Group
);
364 bool IsDebugEnabled
= !A
|| A
->getOption().matches(options::OPT_O0
) ||
365 Args
.hasFlag(options::OPT_cuda_noopt_device_debug
,
366 options::OPT_no_cuda_noopt_device_debug
,
368 if (const Arg
*A
= Args
.getLastArg(options::OPT_g_Group
)) {
369 const Option
&Opt
= A
->getOption();
370 if (Opt
.matches(options::OPT_gN_Group
)) {
371 if (Opt
.matches(options::OPT_g0
) || Opt
.matches(options::OPT_ggdb0
))
372 return DisableDebugInfo
;
373 if (Opt
.matches(options::OPT_gline_directives_only
))
374 return DebugDirectivesOnly
;
376 return IsDebugEnabled
? EmitSameDebugInfoAsHost
: DebugDirectivesOnly
;
378 return willEmitRemarks(Args
) ? DebugDirectivesOnly
: DisableDebugInfo
;
381 void NVPTX::Assembler::ConstructJob(Compilation
&C
, const JobAction
&JA
,
382 const InputInfo
&Output
,
383 const InputInfoList
&Inputs
,
385 const char *LinkingOutput
) const {
387 static_cast<const toolchains::NVPTXToolChain
&>(getToolChain());
388 assert(TC
.getTriple().isNVPTX() && "Wrong platform");
390 StringRef GPUArchName
;
391 // If this is a CUDA action we need to extract the device architecture
392 // from the Job's associated architecture, otherwise use the -march=arch
393 // option. This option may come from -Xopenmp-target flag or the default
395 if (JA
.isDeviceOffloading(Action::OFK_Cuda
)) {
396 GPUArchName
= JA
.getOffloadingArch();
398 GPUArchName
= Args
.getLastArgValue(options::OPT_march_EQ
);
399 if (GPUArchName
.empty()) {
400 C
.getDriver().Diag(diag::err_drv_offload_missing_gpu_arch
)
401 << getToolChain().getArchName() << getShortName();
406 // Obtain architecture from the action.
407 OffloadArch gpu_arch
= StringToOffloadArch(GPUArchName
);
408 assert(gpu_arch
!= OffloadArch::UNKNOWN
&&
409 "Device action expected to have an architecture.");
411 // Check that our installation's ptxas supports gpu_arch.
412 if (!Args
.hasArg(options::OPT_no_cuda_version_check
)) {
413 TC
.CudaInstallation
.CheckCudaVersionSupportsArch(gpu_arch
);
416 ArgStringList CmdArgs
;
417 CmdArgs
.push_back(TC
.getTriple().isArch64Bit() ? "-m64" : "-m32");
418 DeviceDebugInfoLevel DIKind
= mustEmitDebugInfo(Args
);
419 if (DIKind
== EmitSameDebugInfoAsHost
) {
420 // ptxas does not accept -g option if optimization is enabled, so
421 // we ignore the compiler's -O* options if we want debug info.
422 CmdArgs
.push_back("-g");
423 CmdArgs
.push_back("--dont-merge-basicblocks");
424 CmdArgs
.push_back("--return-at-end");
425 } else if (Arg
*A
= Args
.getLastArg(options::OPT_O_Group
)) {
426 // Map the -O we received to -O{0,1,2,3}.
428 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
429 // default, so it may correspond more closely to the spirit of clang -O2.
431 // -O3 seems like the least-bad option when -Osomething is specified to
432 // clang but it isn't handled below.
433 StringRef OOpt
= "3";
434 if (A
->getOption().matches(options::OPT_O4
) ||
435 A
->getOption().matches(options::OPT_Ofast
))
437 else if (A
->getOption().matches(options::OPT_O0
))
439 else if (A
->getOption().matches(options::OPT_O
)) {
440 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
441 OOpt
= llvm::StringSwitch
<const char *>(A
->getValue())
449 CmdArgs
.push_back(Args
.MakeArgString(llvm::Twine("-O") + OOpt
));
451 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
452 // to no optimizations, but ptxas's default is -O3.
453 CmdArgs
.push_back("-O0");
455 if (DIKind
== DebugDirectivesOnly
)
456 CmdArgs
.push_back("-lineinfo");
458 // Pass -v to ptxas if it was passed to the driver.
459 if (Args
.hasArg(options::OPT_v
))
460 CmdArgs
.push_back("-v");
462 CmdArgs
.push_back("--gpu-name");
463 CmdArgs
.push_back(Args
.MakeArgString(OffloadArchToString(gpu_arch
)));
464 CmdArgs
.push_back("--output-file");
465 std::string OutputFileName
= TC
.getInputFilename(Output
);
467 if (Output
.isFilename() && OutputFileName
!= Output
.getFilename())
468 C
.addTempFile(Args
.MakeArgString(OutputFileName
));
470 CmdArgs
.push_back(Args
.MakeArgString(OutputFileName
));
471 for (const auto &II
: Inputs
)
472 CmdArgs
.push_back(Args
.MakeArgString(II
.getFilename()));
474 for (const auto &A
: Args
.getAllArgValues(options::OPT_Xcuda_ptxas
))
475 CmdArgs
.push_back(Args
.MakeArgString(A
));
478 if (JA
.isOffloading(Action::OFK_OpenMP
))
479 // In OpenMP we need to generate relocatable code.
480 Relocatable
= Args
.hasFlag(options::OPT_fopenmp_relocatable_target
,
481 options::OPT_fnoopenmp_relocatable_target
,
483 else if (JA
.isOffloading(Action::OFK_Cuda
))
484 // In CUDA we generate relocatable code by default.
485 Relocatable
= Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
,
488 // Otherwise, we are compiling directly and should create linkable output.
492 CmdArgs
.push_back("-c");
495 if (Arg
*A
= Args
.getLastArg(options::OPT_ptxas_path_EQ
))
496 Exec
= A
->getValue();
498 Exec
= Args
.MakeArgString(TC
.GetProgramPath("ptxas"));
499 C
.addCommand(std::make_unique
<Command
>(
501 ResponseFileSupport
{ResponseFileSupport::RF_Full
, llvm::sys::WEM_UTF8
,
503 Exec
, CmdArgs
, Inputs
, Output
));
506 static bool shouldIncludePTX(const ArgList
&Args
, StringRef InputArch
) {
507 // The new driver does not include PTX by default to avoid overhead.
508 bool includePTX
= !Args
.hasFlag(options::OPT_offload_new_driver
,
509 options::OPT_no_offload_new_driver
, false);
510 for (Arg
*A
: Args
.filtered(options::OPT_cuda_include_ptx_EQ
,
511 options::OPT_no_cuda_include_ptx_EQ
)) {
513 const StringRef ArchStr
= A
->getValue();
514 if (A
->getOption().matches(options::OPT_cuda_include_ptx_EQ
) &&
515 (ArchStr
== "all" || ArchStr
== InputArch
))
517 else if (A
->getOption().matches(options::OPT_no_cuda_include_ptx_EQ
) &&
518 (ArchStr
== "all" || ArchStr
== InputArch
))
524 // All inputs to this linker must be from CudaDeviceActions, as we need to look
525 // at the Inputs' Actions in order to figure out which GPU architecture they
527 void NVPTX::FatBinary::ConstructJob(Compilation
&C
, const JobAction
&JA
,
528 const InputInfo
&Output
,
529 const InputInfoList
&Inputs
,
531 const char *LinkingOutput
) const {
533 static_cast<const toolchains::CudaToolChain
&>(getToolChain());
534 assert(TC
.getTriple().isNVPTX() && "Wrong platform");
536 ArgStringList CmdArgs
;
537 if (TC
.CudaInstallation
.version() <= CudaVersion::CUDA_100
)
538 CmdArgs
.push_back("--cuda");
539 CmdArgs
.push_back(TC
.getTriple().isArch64Bit() ? "-64" : "-32");
540 CmdArgs
.push_back(Args
.MakeArgString("--create"));
541 CmdArgs
.push_back(Args
.MakeArgString(Output
.getFilename()));
542 if (mustEmitDebugInfo(Args
) == EmitSameDebugInfoAsHost
)
543 CmdArgs
.push_back("-g");
545 for (const auto &II
: Inputs
) {
546 auto *A
= II
.getAction();
547 assert(A
->getInputs().size() == 1 &&
548 "Device offload action is expected to have a single input");
549 const char *gpu_arch_str
= A
->getOffloadingArch();
550 assert(gpu_arch_str
&&
551 "Device action expected to have associated a GPU architecture!");
552 OffloadArch gpu_arch
= StringToOffloadArch(gpu_arch_str
);
554 if (II
.getType() == types::TY_PP_Asm
&&
555 !shouldIncludePTX(Args
, gpu_arch_str
))
557 // We need to pass an Arch of the form "sm_XX" for cubin files and
558 // "compute_XX" for ptx.
559 const char *Arch
= (II
.getType() == types::TY_PP_Asm
)
560 ? OffloadArchToVirtualArchString(gpu_arch
)
563 Args
.MakeArgString(llvm::Twine("--image=profile=") + Arch
+
564 ",file=" + getToolChain().getInputFilename(II
)));
567 for (const auto &A
: Args
.getAllArgValues(options::OPT_Xcuda_fatbinary
))
568 CmdArgs
.push_back(Args
.MakeArgString(A
));
570 const char *Exec
= Args
.MakeArgString(TC
.GetProgramPath("fatbinary"));
571 C
.addCommand(std::make_unique
<Command
>(
573 ResponseFileSupport
{ResponseFileSupport::RF_Full
, llvm::sys::WEM_UTF8
,
575 Exec
, CmdArgs
, Inputs
, Output
));
578 void NVPTX::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
579 const InputInfo
&Output
,
580 const InputInfoList
&Inputs
,
582 const char *LinkingOutput
) const {
584 static_cast<const toolchains::NVPTXToolChain
&>(getToolChain());
585 ArgStringList CmdArgs
;
587 assert(TC
.getTriple().isNVPTX() && "Wrong platform");
589 assert((Output
.isFilename() || Output
.isNothing()) && "Invalid output.");
590 if (Output
.isFilename()) {
591 CmdArgs
.push_back("-o");
592 CmdArgs
.push_back(Output
.getFilename());
595 if (mustEmitDebugInfo(Args
) == EmitSameDebugInfoAsHost
)
596 CmdArgs
.push_back("-g");
598 if (Args
.hasArg(options::OPT_v
))
599 CmdArgs
.push_back("-v");
601 StringRef GPUArch
= Args
.getLastArgValue(options::OPT_march_EQ
);
602 if (GPUArch
.empty() && !C
.getDriver().isUsingLTO()) {
603 C
.getDriver().Diag(diag::err_drv_offload_missing_gpu_arch
)
604 << getToolChain().getArchName() << getShortName();
608 if (!GPUArch
.empty()) {
609 CmdArgs
.push_back("-arch");
610 CmdArgs
.push_back(Args
.MakeArgString(GPUArch
));
613 if (Args
.hasArg(options::OPT_ptxas_path_EQ
))
614 CmdArgs
.push_back(Args
.MakeArgString(
615 "--pxtas-path=" + Args
.getLastArgValue(options::OPT_ptxas_path_EQ
)));
617 if (Args
.hasArg(options::OPT_cuda_path_EQ
))
618 CmdArgs
.push_back(Args
.MakeArgString(
619 "--cuda-path=" + Args
.getLastArgValue(options::OPT_cuda_path_EQ
)));
621 // Add paths specified in LIBRARY_PATH environment variable as -L options.
622 addDirectoryList(Args
, CmdArgs
, "-L", "LIBRARY_PATH");
624 // Add standard library search paths passed on the command line.
625 Args
.AddAllArgs(CmdArgs
, options::OPT_L
);
626 getToolChain().AddFilePathLibArgs(Args
, CmdArgs
);
627 AddLinkerInputs(getToolChain(), Inputs
, Args
, CmdArgs
, JA
);
629 if (C
.getDriver().isUsingLTO())
630 addLTOOptions(getToolChain(), Args
, CmdArgs
, Output
, Inputs
[0],
631 C
.getDriver().getLTOMode() == LTOK_Thin
);
633 // Forward the PTX features if the nvlink-wrapper needs it.
634 std::vector
<StringRef
> Features
;
635 getNVPTXTargetFeatures(C
.getDriver(), getToolChain().getTriple(), Args
,
638 Args
.MakeArgString("--plugin-opt=-mattr=" + llvm::join(Features
, ",")));
640 // Add paths for the default clang library path.
641 SmallString
<256> DefaultLibPath
=
642 llvm::sys::path::parent_path(TC
.getDriver().Dir
);
643 llvm::sys::path::append(DefaultLibPath
, CLANG_INSTALL_LIBDIR_BASENAME
);
644 CmdArgs
.push_back(Args
.MakeArgString(Twine("-L") + DefaultLibPath
));
646 if (Args
.hasArg(options::OPT_stdlib
))
647 CmdArgs
.append({"-lc", "-lm"});
648 if (Args
.hasArg(options::OPT_startfiles
)) {
649 std::optional
<std::string
> IncludePath
= getToolChain().getStdlibPath();
651 IncludePath
= "/lib";
652 SmallString
<128> P(*IncludePath
);
653 llvm::sys::path::append(P
, "crt1.o");
654 CmdArgs
.push_back(Args
.MakeArgString(P
));
657 C
.addCommand(std::make_unique
<Command
>(
659 ResponseFileSupport
{ResponseFileSupport::RF_Full
, llvm::sys::WEM_UTF8
,
661 Args
.MakeArgString(getToolChain().GetProgramPath("clang-nvlink-wrapper")),
662 CmdArgs
, Inputs
, Output
));
665 void NVPTX::getNVPTXTargetFeatures(const Driver
&D
, const llvm::Triple
&Triple
,
666 const llvm::opt::ArgList
&Args
,
667 std::vector
<StringRef
> &Features
) {
668 if (Args
.hasArg(options::OPT_cuda_feature_EQ
)) {
669 StringRef PtxFeature
=
670 Args
.getLastArgValue(options::OPT_cuda_feature_EQ
, "+ptx42");
671 Features
.push_back(Args
.MakeArgString(PtxFeature
));
674 CudaInstallationDetector
CudaInstallation(D
, Triple
, Args
);
676 // New CUDA versions often introduce new instructions that are only supported
677 // by new PTX version, so we need to raise PTX level to enable them in NVPTX
679 const char *PtxFeature
= nullptr;
680 switch (CudaInstallation
.version()) {
681 #define CASE_CUDA_VERSION(CUDA_VER, PTX_VER) \
682 case CudaVersion::CUDA_##CUDA_VER: \
683 PtxFeature = "+ptx" #PTX_VER; \
685 CASE_CUDA_VERSION(126, 85);
686 CASE_CUDA_VERSION(125, 85);
687 CASE_CUDA_VERSION(124, 84);
688 CASE_CUDA_VERSION(123, 83);
689 CASE_CUDA_VERSION(122, 82);
690 CASE_CUDA_VERSION(121, 81);
691 CASE_CUDA_VERSION(120, 80);
692 CASE_CUDA_VERSION(118, 78);
693 CASE_CUDA_VERSION(117, 77);
694 CASE_CUDA_VERSION(116, 76);
695 CASE_CUDA_VERSION(115, 75);
696 CASE_CUDA_VERSION(114, 74);
697 CASE_CUDA_VERSION(113, 73);
698 CASE_CUDA_VERSION(112, 72);
699 CASE_CUDA_VERSION(111, 71);
700 CASE_CUDA_VERSION(110, 70);
701 CASE_CUDA_VERSION(102, 65);
702 CASE_CUDA_VERSION(101, 64);
703 CASE_CUDA_VERSION(100, 63);
704 CASE_CUDA_VERSION(92, 61);
705 CASE_CUDA_VERSION(91, 61);
706 CASE_CUDA_VERSION(90, 60);
707 #undef CASE_CUDA_VERSION
708 // TODO: Use specific CUDA version once it's public.
709 case clang::CudaVersion::NEW
:
710 PtxFeature
= "+ptx86";
713 PtxFeature
= "+ptx42";
715 Features
.push_back(PtxFeature
);
718 /// NVPTX toolchain. Our assembler is ptxas, and our linker is nvlink. This
719 /// operates as a stand-alone version of the NVPTX tools without the host
721 NVPTXToolChain::NVPTXToolChain(const Driver
&D
, const llvm::Triple
&Triple
,
722 const llvm::Triple
&HostTriple
,
723 const ArgList
&Args
, bool Freestanding
= false)
724 : ToolChain(D
, Triple
, Args
), CudaInstallation(D
, HostTriple
, Args
),
725 Freestanding(Freestanding
) {
726 if (CudaInstallation
.isValid())
727 getProgramPaths().push_back(std::string(CudaInstallation
.getBinPath()));
728 // Lookup binaries into the driver directory, this is used to
729 // discover the 'nvptx-arch' executable.
730 getProgramPaths().push_back(getDriver().Dir
);
733 /// We only need the host triple to locate the CUDA binary utilities, use the
734 /// system's default triple if not provided.
735 NVPTXToolChain::NVPTXToolChain(const Driver
&D
, const llvm::Triple
&Triple
,
737 : NVPTXToolChain(D
, Triple
, llvm::Triple(LLVM_HOST_TRIPLE
), Args
,
738 /*Freestanding=*/true) {}
740 llvm::opt::DerivedArgList
*
741 NVPTXToolChain::TranslateArgs(const llvm::opt::DerivedArgList
&Args
,
743 Action::OffloadKind OffloadKind
) const {
744 DerivedArgList
*DAL
= ToolChain::TranslateArgs(Args
, BoundArch
, OffloadKind
);
746 DAL
= new DerivedArgList(Args
.getBaseArgs());
748 const OptTable
&Opts
= getDriver().getOpts();
751 if (!llvm::is_contained(*DAL
, A
))
754 if (!DAL
->hasArg(options::OPT_march_EQ
) && OffloadKind
!= Action::OFK_None
) {
755 DAL
->AddJoinedArg(nullptr, Opts
.getOption(options::OPT_march_EQ
),
756 OffloadArchToString(OffloadArch::CudaDefault
));
757 } else if (DAL
->getLastArgValue(options::OPT_march_EQ
) == "generic" &&
758 OffloadKind
== Action::OFK_None
) {
759 DAL
->eraseArg(options::OPT_march_EQ
);
760 } else if (DAL
->getLastArgValue(options::OPT_march_EQ
) == "native") {
761 auto GPUsOrErr
= getSystemGPUArchs(Args
);
763 getDriver().Diag(diag::err_drv_undetermined_gpu_arch
)
764 << getArchName() << llvm::toString(GPUsOrErr
.takeError()) << "-march";
766 if (GPUsOrErr
->size() > 1)
767 getDriver().Diag(diag::warn_drv_multi_gpu_arch
)
768 << getArchName() << llvm::join(*GPUsOrErr
, ", ") << "-march";
769 DAL
->AddJoinedArg(nullptr, Opts
.getOption(options::OPT_march_EQ
),
770 Args
.MakeArgString(GPUsOrErr
->front()));
777 void NVPTXToolChain::addClangTargetOptions(
778 const llvm::opt::ArgList
&DriverArgs
, llvm::opt::ArgStringList
&CC1Args
,
779 Action::OffloadKind DeviceOffloadingKind
) const {
780 // If we are compiling with a standalone NVPTX toolchain we want to try to
781 // mimic a standard environment as much as possible. So we enable lowering
782 // ctor / dtor functions to global symbols that can be registered.
784 CC1Args
.append({"-mllvm", "--nvptx-lower-global-ctor-dtor"});
787 bool NVPTXToolChain::supportsDebugInfoOption(const llvm::opt::Arg
*A
) const {
788 const Option
&O
= A
->getOption();
789 return (O
.matches(options::OPT_gN_Group
) &&
790 !O
.matches(options::OPT_gmodules
)) ||
791 O
.matches(options::OPT_g_Flag
) ||
792 O
.matches(options::OPT_ggdbN_Group
) || O
.matches(options::OPT_ggdb
) ||
793 O
.matches(options::OPT_gdwarf
) || O
.matches(options::OPT_gdwarf_2
) ||
794 O
.matches(options::OPT_gdwarf_3
) || O
.matches(options::OPT_gdwarf_4
) ||
795 O
.matches(options::OPT_gdwarf_5
) ||
796 O
.matches(options::OPT_gcolumn_info
);
799 void NVPTXToolChain::adjustDebugInfoKind(
800 llvm::codegenoptions::DebugInfoKind
&DebugInfoKind
,
801 const ArgList
&Args
) const {
802 switch (mustEmitDebugInfo(Args
)) {
803 case DisableDebugInfo
:
804 DebugInfoKind
= llvm::codegenoptions::NoDebugInfo
;
806 case DebugDirectivesOnly
:
807 DebugInfoKind
= llvm::codegenoptions::DebugDirectivesOnly
;
809 case EmitSameDebugInfoAsHost
:
810 // Use same debug info level as the host.
815 Expected
<SmallVector
<std::string
>>
816 NVPTXToolChain::getSystemGPUArchs(const ArgList
&Args
) const {
817 // Detect NVIDIA GPUs availible on the system.
819 if (Arg
*A
= Args
.getLastArg(options::OPT_nvptx_arch_tool_EQ
))
820 Program
= A
->getValue();
822 Program
= GetProgramPath("nvptx-arch");
824 auto StdoutOrErr
= executeToolChainProgram(Program
);
826 return StdoutOrErr
.takeError();
828 SmallVector
<std::string
, 1> GPUArchs
;
829 for (StringRef Arch
: llvm::split((*StdoutOrErr
)->getBuffer(), "\n"))
831 GPUArchs
.push_back(Arch
.str());
833 if (GPUArchs
.empty())
834 return llvm::createStringError(std::error_code(),
835 "No NVIDIA GPU detected in the system");
837 return std::move(GPUArchs
);
840 /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
841 /// which isn't properly a linker but nonetheless performs the step of stitching
842 /// together object files from the assembler into a single blob.
844 CudaToolChain::CudaToolChain(const Driver
&D
, const llvm::Triple
&Triple
,
845 const ToolChain
&HostTC
, const ArgList
&Args
)
846 : NVPTXToolChain(D
, Triple
, HostTC
.getTriple(), Args
), HostTC(HostTC
) {}
848 void CudaToolChain::addClangTargetOptions(
849 const llvm::opt::ArgList
&DriverArgs
, llvm::opt::ArgStringList
&CC1Args
,
850 Action::OffloadKind DeviceOffloadingKind
) const {
851 HostTC
.addClangTargetOptions(DriverArgs
, CC1Args
, DeviceOffloadingKind
);
853 StringRef GpuArch
= DriverArgs
.getLastArgValue(options::OPT_march_EQ
);
854 assert(!GpuArch
.empty() && "Must have an explicit GPU arch.");
855 assert((DeviceOffloadingKind
== Action::OFK_OpenMP
||
856 DeviceOffloadingKind
== Action::OFK_Cuda
) &&
857 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
859 CC1Args
.append({"-fcuda-is-device", "-mllvm",
860 "-enable-memcpyopt-without-libcalls",
861 "-fno-threadsafe-statics"});
863 // Unsized function arguments used for variadics were introduced in CUDA-9.0
864 // We still do not support generating code that actually uses variadic
865 // arguments yet, but we do need to allow parsing them as recent CUDA
866 // headers rely on that. https://github.com/llvm/llvm-project/issues/58410
867 if (CudaInstallation
.version() >= CudaVersion::CUDA_90
)
868 CC1Args
.push_back("-fcuda-allow-variadic-functions");
870 if (DriverArgs
.hasFlag(options::OPT_fcuda_short_ptr
,
871 options::OPT_fno_cuda_short_ptr
, false))
872 CC1Args
.append({"-mllvm", "--nvptx-short-ptr"});
874 if (DriverArgs
.hasArg(options::OPT_nogpulib
))
877 if (DeviceOffloadingKind
== Action::OFK_OpenMP
&&
878 DriverArgs
.hasArg(options::OPT_S
))
881 std::string LibDeviceFile
= CudaInstallation
.getLibDeviceFile(GpuArch
);
882 if (LibDeviceFile
.empty()) {
883 getDriver().Diag(diag::err_drv_no_cuda_libdevice
) << GpuArch
;
887 CC1Args
.push_back("-mlink-builtin-bitcode");
888 CC1Args
.push_back(DriverArgs
.MakeArgString(LibDeviceFile
));
890 // For now, we don't use any Offload/OpenMP device runtime when we offload
891 // CUDA via LLVM/Offload. We should split the Offload/OpenMP device runtime
892 // and include the "generic" (or CUDA-specific) parts.
893 if (DriverArgs
.hasFlag(options::OPT_foffload_via_llvm
,
894 options::OPT_fno_offload_via_llvm
, false))
897 clang::CudaVersion CudaInstallationVersion
= CudaInstallation
.version();
899 if (CudaInstallationVersion
>= CudaVersion::UNKNOWN
)
901 DriverArgs
.MakeArgString(Twine("-target-sdk-version=") +
902 CudaVersionToString(CudaInstallationVersion
)));
904 if (DeviceOffloadingKind
== Action::OFK_OpenMP
) {
905 if (CudaInstallationVersion
< CudaVersion::CUDA_92
) {
907 diag::err_drv_omp_offload_target_cuda_version_not_support
)
908 << CudaVersionToString(CudaInstallationVersion
);
912 // Link the bitcode library late if we're using device LTO.
913 if (getDriver().isUsingOffloadLTO())
916 addOpenMPDeviceRTL(getDriver(), DriverArgs
, CC1Args
, GpuArch
.str(),
917 getTriple(), HostTC
);
921 llvm::DenormalMode
CudaToolChain::getDefaultDenormalModeForType(
922 const llvm::opt::ArgList
&DriverArgs
, const JobAction
&JA
,
923 const llvm::fltSemantics
*FPType
) const {
924 if (JA
.getOffloadingDeviceKind() == Action::OFK_Cuda
) {
925 if (FPType
&& FPType
== &llvm::APFloat::IEEEsingle() &&
926 DriverArgs
.hasFlag(options::OPT_fgpu_flush_denormals_to_zero
,
927 options::OPT_fno_gpu_flush_denormals_to_zero
, false))
928 return llvm::DenormalMode::getPreserveSign();
931 assert(JA
.getOffloadingDeviceKind() != Action::OFK_Host
);
932 return llvm::DenormalMode::getIEEE();
935 void CudaToolChain::AddCudaIncludeArgs(const ArgList
&DriverArgs
,
936 ArgStringList
&CC1Args
) const {
937 // Check our CUDA version if we're going to include the CUDA headers.
938 if (!DriverArgs
.hasArg(options::OPT_nogpuinc
) &&
939 !DriverArgs
.hasArg(options::OPT_no_cuda_version_check
)) {
940 StringRef Arch
= DriverArgs
.getLastArgValue(options::OPT_march_EQ
);
941 assert(!Arch
.empty() && "Must have an explicit GPU arch.");
942 CudaInstallation
.CheckCudaVersionSupportsArch(StringToOffloadArch(Arch
));
944 CudaInstallation
.AddCudaIncludeArgs(DriverArgs
, CC1Args
);
947 std::string
CudaToolChain::getInputFilename(const InputInfo
&Input
) const {
948 // Only object files are changed, for example assembly files keep their .s
949 // extensions. If the user requested device-only compilation don't change it.
950 if (Input
.getType() != types::TY_Object
|| getDriver().offloadDeviceOnly())
951 return ToolChain::getInputFilename(Input
);
953 return ToolChain::getInputFilename(Input
);
956 llvm::opt::DerivedArgList
*
957 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList
&Args
,
959 Action::OffloadKind DeviceOffloadKind
) const {
960 DerivedArgList
*DAL
=
961 HostTC
.TranslateArgs(Args
, BoundArch
, DeviceOffloadKind
);
963 DAL
= new DerivedArgList(Args
.getBaseArgs());
965 const OptTable
&Opts
= getDriver().getOpts();
967 // For OpenMP device offloading, append derived arguments. Make sure
968 // flags are not duplicated.
969 // Also append the compute capability.
970 if (DeviceOffloadKind
== Action::OFK_OpenMP
) {
972 if (!llvm::is_contained(*DAL
, A
))
975 if (!DAL
->hasArg(options::OPT_march_EQ
)) {
976 StringRef Arch
= BoundArch
;
978 auto ArchsOrErr
= getSystemGPUArchs(Args
);
981 llvm::formatv("{0}", llvm::fmt_consume(ArchsOrErr
.takeError()));
982 getDriver().Diag(diag::err_drv_undetermined_gpu_arch
)
983 << llvm::Triple::getArchTypeName(getArch()) << ErrMsg
<< "-march";
984 Arch
= OffloadArchToString(OffloadArch::CudaDefault
);
986 Arch
= Args
.MakeArgString(ArchsOrErr
->front());
989 DAL
->AddJoinedArg(nullptr, Opts
.getOption(options::OPT_march_EQ
), Arch
);
995 for (Arg
*A
: Args
) {
996 // Make sure flags are not duplicated.
997 if (!llvm::is_contained(*DAL
, A
)) {
1002 if (!BoundArch
.empty()) {
1003 DAL
->eraseArg(options::OPT_march_EQ
);
1004 DAL
->AddJoinedArg(nullptr, Opts
.getOption(options::OPT_march_EQ
),
1010 Tool
*NVPTXToolChain::buildAssembler() const {
1011 return new tools::NVPTX::Assembler(*this);
1014 Tool
*NVPTXToolChain::buildLinker() const {
1015 return new tools::NVPTX::Linker(*this);
1018 Tool
*CudaToolChain::buildAssembler() const {
1019 return new tools::NVPTX::Assembler(*this);
1022 Tool
*CudaToolChain::buildLinker() const {
1023 return new tools::NVPTX::FatBinary(*this);
1026 void CudaToolChain::addClangWarningOptions(ArgStringList
&CC1Args
) const {
1027 HostTC
.addClangWarningOptions(CC1Args
);
1030 ToolChain::CXXStdlibType
1031 CudaToolChain::GetCXXStdlibType(const ArgList
&Args
) const {
1032 return HostTC
.GetCXXStdlibType(Args
);
1035 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList
&DriverArgs
,
1036 ArgStringList
&CC1Args
) const {
1037 HostTC
.AddClangSystemIncludeArgs(DriverArgs
, CC1Args
);
1039 if (!DriverArgs
.hasArg(options::OPT_nogpuinc
) && CudaInstallation
.isValid())
1041 {"-internal-isystem",
1042 DriverArgs
.MakeArgString(CudaInstallation
.getIncludePath())});
1045 void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList
&Args
,
1046 ArgStringList
&CC1Args
) const {
1047 HostTC
.AddClangCXXStdlibIncludeArgs(Args
, CC1Args
);
1050 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList
&Args
,
1051 ArgStringList
&CC1Args
) const {
1052 HostTC
.AddIAMCUIncludeArgs(Args
, CC1Args
);
1055 SanitizerMask
CudaToolChain::getSupportedSanitizers() const {
1056 // The CudaToolChain only supports sanitizers in the sense that it allows
1057 // sanitizer arguments on the command line if they are supported by the host
1058 // toolchain. The CudaToolChain will actually ignore any command line
1059 // arguments for any of these "supported" sanitizers. That means that no
1060 // sanitization of device code is actually supported at this time.
1062 // This behavior is necessary because the host and device toolchains
1063 // invocations often share the command line, so the device toolchain must
1064 // tolerate flags meant only for the host toolchain.
1065 return HostTC
.getSupportedSanitizers();
1068 VersionTuple
CudaToolChain::computeMSVCVersion(const Driver
*D
,
1069 const ArgList
&Args
) const {
1070 return HostTC
.computeMSVCVersion(D
, Args
);