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