Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/Cuda.cpp
Line
Count
Source (jump to first uncovered line)
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".
43
41
CudaVersionInfo parseCudaVersionFile(llvm::StringRef V) {
44
41
  V = V.trim();
45
41
  if (!V.startswith("CUDA Version "))
46
0
    return {V.str(), CudaVersion::UNKNOWN};
47
41
  V = V.substr(strlen("CUDA Version "));
48
41
  SmallVector<StringRef,4> VersionParts;
49
41
  V.split(VersionParts, '.');
50
41
  return {"version.txt: " + V.str() + ".",
51
41
          VersionParts.size() < 2
52
0
              ? CudaVersion::UNKNOWN
53
41
              : CudaStringToVersion(
54
41
                    join_items(".", VersionParts[0], VersionParts[1]))};
55
41
}
56
57
2
CudaVersion getCudaVersion(uint32_t raw_version) {
58
2
  if (raw_version < 7050)
59
0
    return CudaVersion::CUDA_70;
60
2
  if (raw_version < 8000)
61
0
    return CudaVersion::CUDA_75;
62
2
  if (raw_version < 9000)
63
0
    return CudaVersion::CUDA_80;
64
2
  if (raw_version < 9010)
65
0
    return CudaVersion::CUDA_90;
66
2
  if (raw_version < 9020)
67
0
    return CudaVersion::CUDA_91;
68
2
  if (raw_version < 10000)
69
0
    return CudaVersion::CUDA_92;
70
2
  if (raw_version < 10010)
71
0
    return CudaVersion::CUDA_100;
72
2
  if (raw_version < 10020)
73
0
    return CudaVersion::CUDA_101;
74
2
  if (raw_version < 11000)
75
0
    return CudaVersion::CUDA_102;
76
2
  if (raw_version < 11010)
77
0
    return CudaVersion::CUDA_110;
78
2
  return CudaVersion::LATEST;
79
2
}
80
81
2
CudaVersionInfo parseCudaHFile(llvm::StringRef Input) {
82
  // Helper lambda which skips the words if the line starts with them or returns
83
  // None otherwise.
84
2
  auto StartsWithWords =
85
2
      [](llvm::StringRef Line,
86
8
         const SmallVector<StringRef, 3> words) -> llvm::Optional<StringRef> {
87
12
    for (StringRef word : words) {
88
12
      if (!Line.consume_front(word))
89
6
        return {};
90
6
      Line = Line.ltrim();
91
6
    }
92
2
    return Line;
93
8
  };
94
95
2
  Input = Input.ltrim();
96
8
  while (!Input.empty()) {
97
8
    if (auto Line =
98
2
            StartsWithWords(Input.ltrim(), {"#", "define", "CUDA_VERSION"})) {
99
2
      uint32_t RawVersion;
100
2
      Line->consumeInteger(10, RawVersion);
101
2
      return {"cuda.h: CUDA_VERSION=" + Twine(RawVersion).str() + ".",
102
2
              getCudaVersion(RawVersion)};
103
2
    }
104
    // Find next non-empty line.
105
6
    Input = Input.drop_front(Input.find_first_of("\n\r")).ltrim();
106
6
  }
107
0
  return {"cuda.h: CUDA_VERSION not found.", CudaVersion::UNKNOWN};
108
2
}
109
} // namespace
110
111
44
void CudaInstallationDetector::WarnIfUnsupportedVersion() {
112
44
  if (DetectedVersionIsNotSupported)
113
3
    D.Diag(diag::warn_drv_unknown_cuda_version)
114
3
        << DetectedVersion
115
3
        << CudaVersionToString(CudaVersion::LATEST_SUPPORTED);
116
44
}
117
118
CudaInstallationDetector::CudaInstallationDetector(
119
    const Driver &D, const llvm::Triple &HostTriple,
120
    const llvm::opt::ArgList &Args)
121
47.9k
    : D(D) {
122
47.9k
  struct Candidate {
123
47.9k
    std::string Path;
124
47.9k
    bool StrictChecking;
125
126
47.9k
    Candidate(std::string Path, bool StrictChecking = false)
127
183k
        : Path(Path), StrictChecking(StrictChecking) {}
128
47.9k
  };
129
47.9k
  SmallVector<Candidate, 4> Candidates;
130
131
  // In decreasing order so we prefer newer versions to older versions.
132
47.9k
  std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
133
47.9k
  auto &FS = D.getVFS();
134
135
47.9k
  if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) {
136
99
    Candidates.emplace_back(
137
99
        Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
138
47.8k
  } else if (HostTriple.isOSWindows()) {
139
7.90k
    for (const char *Ver : Versions)
140
23.7k
      Candidates.emplace_back(
141
23.7k
          D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
142
23.7k
          Ver);
143
39.9k
  } else {
144
39.9k
    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
39.9k
      if (llvm::ErrorOr<std::string> ptxas =
154
16
              llvm::sys::findProgramByName("ptxas")) {
155
16
        SmallString<256> ptxasAbsolutePath;
156
16
        llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath);
157
158
16
        StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath);
159
16
        if (llvm::sys::path::filename(ptxasDir) == "bin")
160
16
          Candidates.emplace_back(
161
16
              std::string(llvm::sys::path::parent_path(ptxasDir)),
162
16
              /*StrictChecking=*/true);
163
16
      }
164
39.9k
    }
165
166
39.9k
    Candidates.emplace_back(D.SysRoot + "/usr/local/cuda");
167
39.9k
    for (const char *Ver : Versions)
168
119k
      Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
169
170
39.9k
    Distro Dist(FS, llvm::Triple(llvm::sys::getProcessTriple()));
171
39.9k
    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
0
      Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
175
39.9k
  }
176
177
47.9k
  bool NoCudaLib = Args.hasArg(options::OPT_nogpulib);
178
179
183k
  for (const auto &Candidate : Candidates) {
180
183k
    InstallPath = Candidate.Path;
181
183k
    if (
InstallPath.empty()183k
|| !FS.exists(InstallPath))
182
183k
      continue;
183
184
118
    BinPath = InstallPath + "/bin";
185
118
    IncludePath = InstallPath + "/include";
186
118
    LibDevicePath = InstallPath + "/nvvm/libdevice";
187
188
118
    if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
189
0
      continue;
190
118
    bool CheckLibDevice = (!NoCudaLib || 
Candidate.StrictChecking16
);
191
118
    if (CheckLibDevice && 
!FS.exists(LibDevicePath)106
)
192
12
      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
106
    if (HostTriple.isArch64Bit() && 
FS.exists(InstallPath + "/lib64")61
)
201
59
      LibPath = InstallPath + "/lib64";
202
47
    else if (FS.exists(InstallPath + "/lib"))
203
47
      LibPath = InstallPath + "/lib";
204
0
    else
205
0
      continue;
206
207
106
    CudaVersionInfo VersionInfo = {"", CudaVersion::UNKNOWN};
208
106
    if (auto VersionFile = FS.getBufferForFile(InstallPath + "/version.txt"))
209
41
      VersionInfo = parseCudaVersionFile((*VersionFile)->getBuffer());
210
    // If version file didn't give us the version, try to find it in cuda.h
211
106
    if (VersionInfo.Version == CudaVersion::UNKNOWN)
212
68
      if (auto CudaHFile = FS.getBufferForFile(InstallPath + "/include/cuda.h"))
213
2
        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
106
    if (VersionInfo.Version == CudaVersion::UNKNOWN) {
218
66
      VersionInfo.Version = (FS.exists(LibDevicePath + "/libdevice.10.bc"))
219
3
                                ? Version = CudaVersion::LATEST
220
63
                                : Version = CudaVersion::CUDA_70;
221
66
      VersionInfo.DetectedVersion =
222
66
          "No version found in version.txt or cuda.h.";
223
66
    }
224
225
106
    Version = VersionInfo.Version;
226
106
    DetectedVersion = VersionInfo.DetectedVersion;
227
228
    // TODO(tra): remove the warning once we have all features of 10.2
229
    // and 11.0 implemented.
230
106
    DetectedVersionIsNotSupported = Version > CudaVersion::LATEST_SUPPORTED;
231
232
106
    if (Version >= CudaVersion::CUDA_90) {
233
      // CUDA-9+ uses single libdevice file for all GPU variants.
234
15
      std::string FilePath = LibDevicePath + "/libdevice.10.bc";
235
15
      if (FS.exists(FilePath)) {
236
645
        for (int Arch = (int)CudaArch::SM_30, E = (int)CudaArch::LAST; Arch < E;
237
630
             ++Arch) {
238
630
          CudaArch GpuArch = static_cast<CudaArch>(Arch);
239
630
          if (!IsNVIDIAGpuArch(GpuArch))
240
420
            continue;
241
210
          std::string GpuArchName(CudaArchToString(GpuArch));
242
210
          LibDeviceMap[GpuArchName] = FilePath;
243
210
        }
244
15
      }
245
91
    } else {
246
91
      std::error_code EC;
247
91
      for (llvm::vfs::directory_iterator LI = FS.dir_begin(LibDevicePath, EC),
248
91
                                         LE;
249
321
           !EC && 
LI != LE317
;
LI = LI.increment(EC)230
) {
250
230
        StringRef FilePath = LI->path();
251
230
        StringRef FileName = llvm::sys::path::filename(FilePath);
252
        // Process all bitcode filenames that look like
253
        // libdevice.compute_XX.YY.bc
254
230
        const StringRef LibDeviceName = "libdevice.";
255
230
        if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
256
0
          continue;
257
230
        StringRef GpuArch = FileName.slice(
258
230
            LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
259
230
        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
230
        if (GpuArch == "compute_20") {
264
28
          LibDeviceMap["sm_20"] = std::string(FilePath);
265
28
          LibDeviceMap["sm_21"] = std::string(FilePath);
266
28
          LibDeviceMap["sm_32"] = std::string(FilePath);
267
202
        } else if (GpuArch == "compute_30") {
268
87
          LibDeviceMap["sm_30"] = std::string(FilePath);
269
87
          if (Version < CudaVersion::CUDA_80) {
270
59
            LibDeviceMap["sm_50"] = std::string(FilePath);
271
59
            LibDeviceMap["sm_52"] = std::string(FilePath);
272
59
            LibDeviceMap["sm_53"] = std::string(FilePath);
273
59
          }
274
87
          LibDeviceMap["sm_60"] = std::string(FilePath);
275
87
          LibDeviceMap["sm_61"] = std::string(FilePath);
276
87
          LibDeviceMap["sm_62"] = std::string(FilePath);
277
115
        } else if (GpuArch == "compute_35") {
278
87
          LibDeviceMap["sm_35"] = std::string(FilePath);
279
87
          LibDeviceMap["sm_37"] = std::string(FilePath);
280
28
        } else if (GpuArch == "compute_50") {
281
28
          if (Version >= CudaVersion::CUDA_80) {
282
28
            LibDeviceMap["sm_50"] = std::string(FilePath);
283
28
            LibDeviceMap["sm_52"] = std::string(FilePath);
284
28
            LibDeviceMap["sm_53"] = std::string(FilePath);
285
28
          }
286
28
        }
287
230
      }
288
91
    }
289
290
    // Check that we have found at least one libdevice that we can link in if
291
    // -nocudalib hasn't been specified.
292
106
    if (LibDeviceMap.empty() && 
!NoCudaLib4
)
293
0
      continue;
294
295
106
    IsValid = true;
296
106
    break;
297
106
  }
298
47.9k
}
299
300
void CudaInstallationDetector::AddCudaIncludeArgs(
301
485
    const ArgList &DriverArgs, ArgStringList &CC1Args) const {
302
485
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
303
    // Add cuda_wrappers/* to our system include path.  This lets us wrap
304
    // standard library headers.
305
485
    SmallString<128> P(D.ResourceDir);
306
485
    llvm::sys::path::append(P, "include");
307
485
    llvm::sys::path::append(P, "cuda_wrappers");
308
485
    CC1Args.push_back("-internal-isystem");
309
485
    CC1Args.push_back(DriverArgs.MakeArgString(P));
310
485
  }
311
312
485
  if (DriverArgs.hasArg(options::OPT_nogpuinc))
313
93
    return;
314
315
392
  if (!isValid()) {
316
324
    D.Diag(diag::err_drv_no_cuda_installation);
317
324
    return;
318
324
  }
319
320
68
  CC1Args.push_back("-internal-isystem");
321
68
  CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
322
68
  CC1Args.push_back("-include");
323
68
  CC1Args.push_back("__clang_cuda_runtime_wrapper.h");
324
68
}
325
326
void CudaInstallationDetector::CheckCudaVersionSupportsArch(
327
475
    CudaArch Arch) const {
328
475
  if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
329
74
      ArchsWithBadVersion[(int)Arch])
330
406
    return;
331
332
69
  auto MinVersion = MinVersionForCudaArch(Arch);
333
69
  auto MaxVersion = MaxVersionForCudaArch(Arch);
334
69
  if (Version < MinVersion || 
Version > MaxVersion63
) {
335
7
    ArchsWithBadVersion[(int)Arch] = true;
336
7
    D.Diag(diag::err_drv_cuda_version_unsupported)
337
7
        << CudaArchToString(Arch) << CudaVersionToString(MinVersion)
338
7
        << CudaVersionToString(MaxVersion) << InstallPath
339
7
        << CudaVersionToString(Version);
340
7
  }
341
69
}
342
343
197
void CudaInstallationDetector::print(raw_ostream &OS) const {
344
197
  if (isValid())
345
54
    OS << "Found CUDA installation: " << InstallPath << ", version "
346
54
       << CudaVersionToString(Version) << "\n";
347
197
}
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).
371
828
static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
372
828
  const Arg *A = Args.getLastArg(options::OPT_O_Group);
373
828
  bool IsDebugEnabled = !A || 
A->getOption().matches(options::OPT_O0)105
||
374
78
                        Args.hasFlag(options::OPT_cuda_noopt_device_debug,
375
78
                                     options::OPT_no_cuda_noopt_device_debug,
376
78
                                     /*Default=*/false);
377
828
  if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
378
114
    const Option &Opt = A->getOption();
379
114
    if (Opt.matches(options::OPT_gN_Group)) {
380
54
      if (Opt.matches(options::OPT_g0) || 
Opt.matches(options::OPT_ggdb0)48
)
381
12
        return DisableDebugInfo;
382
42
      if (Opt.matches(options::OPT_gline_directives_only))
383
6
        return DebugDirectivesOnly;
384
96
    }
385
96
    return IsDebugEnabled ? 
EmitSameDebugInfoAsHost78
:
DebugDirectivesOnly18
;
386
96
  }
387
714
  return willEmitRemarks(Args) ? 
DebugDirectivesOnly4
:
DisableDebugInfo710
;
388
714
}
389
390
void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
391
                                    const InputInfo &Output,
392
                                    const InputInfoList &Inputs,
393
                                    const ArgList &Args,
394
263
                                    const char *LinkingOutput) const {
395
263
  const auto &TC =
396
263
      static_cast<const toolchains::CudaToolChain &>(getToolChain());
397
263
  assert(TC.getTriple().isNVPTX() && "Wrong platform");
398
399
263
  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
263
  if (JA.isDeviceOffloading(Action::OFK_OpenMP)) {
404
42
    GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
405
42
    assert(!GPUArchName.empty() && "Must have an architecture passed in.");
406
42
  } else
407
221
    GPUArchName = JA.getOffloadingArch();
408
409
  // Obtain architecture from the action.
410
263
  CudaArch gpu_arch = StringToCudaArch(GPUArchName);
411
263
  assert(gpu_arch != CudaArch::UNKNOWN &&
412
263
         "Device action expected to have an architecture.");
413
414
  // Check that our installation's ptxas supports gpu_arch.
415
263
  if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
416
262
    TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
417
262
  }
418
419
263
  ArgStringList CmdArgs;
420
238
  CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : 
"-m32"25
);
421
263
  DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
422
263
  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
26
    CmdArgs.push_back("-g");
426
26
    CmdArgs.push_back("--dont-merge-basicblocks");
427
26
    CmdArgs.push_back("--return-at-end");
428
237
  } 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
20
    StringRef OOpt = "3";
437
20
    if (A->getOption().matches(options::OPT_O4) ||
438
19
        A->getOption().matches(options::OPT_Ofast))
439
2
      OOpt = "3";
440
18
    else if (A->getOption().matches(options::OPT_O0))
441
3
      OOpt = "0";
442
15
    else if (A->getOption().matches(options::OPT_O)) {
443
      // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
444
15
      OOpt = llvm::StringSwitch<const char *>(A->getValue())
445
15
                 .Case("1", "1")
446
15
                 .Case("2", "2")
447
15
                 .Case("3", "3")
448
15
                 .Case("s", "2")
449
15
                 .Case("z", "2")
450
15
                 .Default("2");
451
15
    }
452
20
    CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
453
217
  } 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
217
    CmdArgs.push_back("-O0");
457
217
  }
458
263
  if (DIKind == DebugDirectivesOnly)
459
9
    CmdArgs.push_back("-lineinfo");
460
461
  // Pass -v to ptxas if it was passed to the driver.
462
263
  if (Args.hasArg(options::OPT_v))
463
40
    CmdArgs.push_back("-v");
464
465
263
  CmdArgs.push_back("--gpu-name");
466
263
  CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
467
263
  CmdArgs.push_back("--output-file");
468
263
  CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
469
263
  for (const auto& II : Inputs)
470
263
    CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
471
472
263
  for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
473
2
    CmdArgs.push_back(Args.MakeArgString(A));
474
475
263
  bool Relocatable = false;
476
263
  if (JA.isOffloading(Action::OFK_OpenMP))
477
    // In OpenMP we need to generate relocatable code.
478
42
    Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
479
42
                               options::OPT_fnoopenmp_relocatable_target,
480
42
                               /*Default=*/true);
481
221
  else if (JA.isOffloading(Action::OFK_Cuda))
482
221
    Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
483
221
                               options::OPT_fno_gpu_rdc, /*Default=*/false);
484
485
263
  if (Relocatable)
486
48
    CmdArgs.push_back("-c");
487
488
263
  const char *Exec;
489
263
  if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
490
1
    Exec = A->getValue();
491
262
  else
492
262
    Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
493
263
  C.addCommand(std::make_unique<Command>(
494
263
      JA, *this,
495
263
      ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
496
263
                          "--options-file"},
497
263
      Exec, CmdArgs, Inputs, Output));
498
263
}
499
500
198
static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
501
198
  bool includePTX = true;
502
1.57k
  for (Arg *A : Args) {
503
1.57k
    if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
504
1.56k
          A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
505
1.54k
      continue;
506
28
    A->claim();
507
28
    const StringRef ArchStr = A->getValue();
508
28
    if (ArchStr == "all" || 
ArchStr == gpu_arch12
) {
509
22
      includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
510
22
      continue;
511
22
    }
512
28
  }
513
198
  return includePTX;
514
198
}
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.
519
void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
520
                                 const InputInfo &Output,
521
                                 const InputInfoList &Inputs,
522
                                 const ArgList &Args,
523
182
                                 const char *LinkingOutput) const {
524
182
  const auto &TC =
525
182
      static_cast<const toolchains::CudaToolChain &>(getToolChain());
526
182
  assert(TC.getTriple().isNVPTX() && "Wrong platform");
527
528
182
  ArgStringList CmdArgs;
529
182
  if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
530
179
    CmdArgs.push_back("--cuda");
531
157
  CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : 
"-32"25
);
532
182
  CmdArgs.push_back(Args.MakeArgString("--create"));
533
182
  CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
534
182
  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
535
16
    CmdArgs.push_back("-g");
536
537
396
  for (const auto& II : Inputs) {
538
396
    auto *A = II.getAction();
539
396
    assert(A->getInputs().size() == 1 &&
540
396
           "Device offload action is expected to have a single input");
541
396
    const char *gpu_arch_str = A->getOffloadingArch();
542
396
    assert(gpu_arch_str &&
543
396
           "Device action expected to have associated a GPU architecture!");
544
396
    CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
545
546
396
    if (II.getType() == types::TY_PP_Asm &&
547
198
        !shouldIncludePTX(Args, gpu_arch_str))
548
8
      continue;
549
    // We need to pass an Arch of the form "sm_XX" for cubin files and
550
    // "compute_XX" for ptx.
551
388
    const char *Arch = (II.getType() == types::TY_PP_Asm)
552
190
                           ? CudaArchToVirtualArchString(gpu_arch)
553
198
                           : gpu_arch_str;
554
388
    CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
555
388
                                         Arch + ",file=" + II.getFilename()));
556
388
  }
557
558
182
  for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
559
2
    CmdArgs.push_back(Args.MakeArgString(A));
560
561
182
  const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
562
182
  C.addCommand(std::make_unique<Command>(
563
182
      JA, *this,
564
182
      ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
565
182
                          "--options-file"},
566
182
      Exec, CmdArgs, Inputs, Output));
567
182
}
568
569
void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
570
                                       const InputInfo &Output,
571
                                       const InputInfoList &Inputs,
572
                                       const ArgList &Args,
573
44
                                       const char *LinkingOutput) const {
574
44
  const auto &TC =
575
44
      static_cast<const toolchains::CudaToolChain &>(getToolChain());
576
44
  assert(TC.getTriple().isNVPTX() && "Wrong platform");
577
578
44
  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
44
  assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
583
44
         "CUDA toolchain not expected for an OpenMP host device.");
584
585
44
  if (Output.isFilename()) {
586
44
    CmdArgs.push_back("-o");
587
44
    CmdArgs.push_back(Output.getFilename());
588
44
  } else
589
44
    assert(Output.isNothing() && "Invalid output.");
590
44
  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
591
10
    CmdArgs.push_back("-g");
592
593
44
  if (Args.hasArg(options::OPT_v))
594
0
    CmdArgs.push_back("-v");
595
596
44
  StringRef GPUArch =
597
44
      Args.getLastArgValue(options::OPT_march_EQ);
598
44
  assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
599
600
44
  CmdArgs.push_back("-arch");
601
44
  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
44
  if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
606
2
    CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue()));
607
608
  // Add paths specified in LIBRARY_PATH environment variable as -L options.
609
44
  addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
610
611
  // Add paths for the default clang library path.
612
44
  SmallString<256> DefaultLibPath =
613
44
      llvm::sys::path::parent_path(TC.getDriver().Dir);
614
44
  llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
615
44
  CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
616
617
  // Add linking against library implementing OpenMP calls on NVPTX target.
618
44
  CmdArgs.push_back("-lomptarget-nvptx");
619
620
46
  for (const auto &II : Inputs) {
621
46
    if (II.getType() == types::TY_LLVM_IR ||
622
46
        II.getType() == types::TY_LTO_IR ||
623
46
        II.getType() == types::TY_LTO_BC ||
624
46
        II.getType() == types::TY_LLVM_BC) {
625
0
      C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
626
0
          << getToolChain().getTripleString();
627
0
      continue;
628
0
    }
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
46
    if (!II.isFilename())
633
0
      continue;
634
635
46
    const char *CubinF = C.addTempFile(
636
46
        C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
637
638
46
    CmdArgs.push_back(CubinF);
639
46
  }
640
641
44
  const char *Exec =
642
44
      Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
643
44
  C.addCommand(std::make_unique<Command>(
644
44
      JA, *this,
645
44
      ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
646
44
                          "--options-file"},
647
44
      Exec, CmdArgs, Inputs, Output));
648
44
}
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
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
343
      CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
659
343
  if (CudaInstallation.isValid()) {
660
44
    CudaInstallation.WarnIfUnsupportedVersion();
661
44
    getProgramPaths().push_back(std::string(CudaInstallation.getBinPath()));
662
44
  }
663
  // Lookup binaries into the driver directory, this is used to
664
  // discover the clang-offload-bundler executable.
665
343
  getProgramPaths().push_back(getDriver().Dir);
666
343
}
667
668
319
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
319
  if (!(OK == Action::OFK_OpenMP && 
Input.getType() == types::TY_Object98
))
673
223
    return ToolChain::getInputFilename(Input);
674
675
  // Replace extension for object files with cubin because nvlink relies on
676
  // these particular file names.
677
96
  SmallString<256> Filename(ToolChain::getInputFilename(Input));
678
96
  llvm::sys::path::replace_extension(Filename, "cubin");
679
96
  return std::string(Filename.str());
680
96
}
681
682
void CudaToolChain::addClangTargetOptions(
683
    const llvm::opt::ArgList &DriverArgs,
684
    llvm::opt::ArgStringList &CC1Args,
685
339
    Action::OffloadKind DeviceOffloadingKind) const {
686
339
  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
687
688
339
  StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
689
339
  assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
690
339
  assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
691
339
          DeviceOffloadingKind == Action::OFK_Cuda) &&
692
339
         "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
693
694
339
  if (DeviceOffloadingKind == Action::OFK_Cuda) {
695
279
    CC1Args.push_back("-fcuda-is-device");
696
697
279
    if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
698
279
                           options::OPT_fno_cuda_approx_transcendentals, false))
699
1
      CC1Args.push_back("-fcuda-approx-transcendentals");
700
701
279
    if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
702
279
                           false))
703
7
      CC1Args.push_back("-fgpu-rdc");
704
279
  }
705
706
339
  if (DriverArgs.hasArg(options::OPT_nogpulib))
707
56
    return;
708
709
283
  std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
710
711
283
  if (LibDeviceFile.empty()) {
712
244
    if (DeviceOffloadingKind == Action::OFK_OpenMP &&
713
48
        DriverArgs.hasArg(options::OPT_S))
714
3
      return;
715
716
241
    getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
717
241
    return;
718
241
  }
719
720
39
  CC1Args.push_back("-mlink-builtin-bitcode");
721
39
  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
39
  const char *PtxFeature = nullptr;
727
39
  switch (CudaInstallation.version()) {
728
2
  case CudaVersion::CUDA_110:
729
2
    PtxFeature = "+ptx70";
730
2
    break;
731
1
  case CudaVersion::CUDA_102:
732
1
    PtxFeature = "+ptx65";
733
1
    break;
734
0
  case CudaVersion::CUDA_101:
735
0
    PtxFeature = "+ptx64";
736
0
    break;
737
0
  case CudaVersion::CUDA_100:
738
0
    PtxFeature = "+ptx63";
739
0
    break;
740
0
  case CudaVersion::CUDA_92:
741
0
    PtxFeature = "+ptx61";
742
0
    break;
743
0
  case CudaVersion::CUDA_91:
744
0
    PtxFeature = "+ptx61";
745
0
    break;
746
3
  case CudaVersion::CUDA_90:
747
3
    PtxFeature = "+ptx60";
748
3
    break;
749
33
  default:
750
33
    PtxFeature = "+ptx42";
751
39
  }
752
39
  CC1Args.append({"-target-feature", PtxFeature});
753
39
  if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr,
754
39
                         options::OPT_fno_cuda_short_ptr, false))
755
0
    CC1Args.append({"-mllvm", "--nvptx-short-ptr"});
756
757
39
  if (CudaInstallation.version() >= CudaVersion::UNKNOWN)
758
39
    CC1Args.push_back(DriverArgs.MakeArgString(
759
39
        Twine("-target-sdk-version=") +
760
39
        CudaVersionToString(CudaInstallation.version())));
761
762
39
  if (DeviceOffloadingKind == Action::OFK_OpenMP) {
763
9
    SmallVector<StringRef, 8> LibraryPaths;
764
9
    if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
765
3
      LibraryPaths.push_back(A->getValue());
766
767
    // Add user defined library paths from LIBRARY_PATH.
768
9
    llvm::Optional<std::string> LibPath =
769
9
        llvm::sys::Process::GetEnv("LIBRARY_PATH");
770
9
    if (LibPath) {
771
3
      SmallVector<StringRef, 8> Frags;
772
3
      const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
773
3
      llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
774
3
      for (StringRef Path : Frags)
775
3
        LibraryPaths.emplace_back(Path.trim());
776
3
    }
777
778
    // Add path to lib / lib64 folder.
779
9
    SmallString<256> DefaultLibPath =
780
9
        llvm::sys::path::parent_path(getDriver().Dir);
781
9
    llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
782
9
    LibraryPaths.emplace_back(DefaultLibPath.c_str());
783
784
9
    std::string LibOmpTargetName =
785
9
      "libomptarget-nvptx-" + GpuArch.str() + ".bc";
786
9
    bool FoundBCLibrary = false;
787
9
    for (StringRef LibraryPath : LibraryPaths) {
788
9
      SmallString<128> LibOmpTargetFile(LibraryPath);
789
9
      llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
790
9
      if (llvm::sys::fs::exists(LibOmpTargetFile)) {
791
6
        CC1Args.push_back("-mlink-builtin-bitcode");
792
6
        CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
793
6
        FoundBCLibrary = true;
794
6
        break;
795
6
      }
796
9
    }
797
9
    if (!FoundBCLibrary)
798
3
      getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
799
3
          << LibOmpTargetName;
800
9
  }
801
39
}
802
803
llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType(
804
    const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
805
678
    const llvm::fltSemantics *FPType) const {
806
678
  if (JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
807
558
    if (FPType && 
FPType == &llvm::APFloat::IEEEsingle()279
&&
808
279
        DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
809
279
                           options::OPT_fno_cuda_flush_denormals_to_zero,
810
279
                           false))
811
3
      return llvm::DenormalMode::getPreserveSign();
812
675
  }
813
814
675
  assert(JA.getOffloadingDeviceKind() != Action::OFK_Host);
815
675
  return llvm::DenormalMode::getIEEE();
816
675
}
817
818
51
bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
819
51
  const Option &O = A->getOption();
820
51
  return (O.matches(options::OPT_gN_Group) &&
821
26
          !O.matches(options::OPT_gmodules)) ||
822
25
         O.matches(options::OPT_g_Flag) ||
823
8
         O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) ||
824
7
         O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) ||
825
7
         O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) ||
826
7
         O.matches(options::OPT_gdwarf_5) ||
827
3
         O.matches(options::OPT_gcolumn_info);
828
51
}
829
830
void CudaToolChain::adjustDebugInfoKind(
831
339
    codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
832
339
  switch (mustEmitDebugInfo(Args)) {
833
303
  case DisableDebugInfo:
834
303
    DebugInfoKind = codegenoptions::NoDebugInfo;
835
303
    break;
836
10
  case DebugDirectivesOnly:
837
10
    DebugInfoKind = codegenoptions::DebugDirectivesOnly;
838
10
    break;
839
26
  case EmitSameDebugInfoAsHost:
840
    // Use same debug info level as the host.
841
26
    break;
842
339
  }
843
339
}
844
845
void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
846
269
                                       ArgStringList &CC1Args) const {
847
  // Check our CUDA version if we're going to include the CUDA headers.
848
269
  if (!DriverArgs.hasArg(options::OPT_nogpuinc) &&
849
214
      !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
850
213
    StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
851
213
    assert(!Arch.empty() && "Must have an explicit GPU arch.");
852
213
    CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch));
853
213
  }
854
269
  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
855
269
}
856
857
llvm::opt::DerivedArgList *
858
CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
859
                             StringRef BoundArch,
860
705
                             Action::OffloadKind DeviceOffloadKind) const {
861
705
  DerivedArgList *DAL =
862
705
      HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
863
705
  if (!DAL)
864
566
    DAL = new DerivedArgList(Args.getBaseArgs());
865
866
705
  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
705
  if (DeviceOffloadKind == Action::OFK_OpenMP) {
872
396
    for (Arg *A : Args) {
873
396
      bool IsDuplicate = false;
874
2.03k
      for (Arg *DALArg : *DAL) {
875
2.03k
        if (A == DALArg) {
876
396
          IsDuplicate = true;
877
396
          break;
878
396
        }
879
2.03k
      }
880
396
      if (!IsDuplicate)
881
0
        DAL->append(A);
882
396
    }
883
884
46
    StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
885
46
    if (Arch.empty())
886
14
      DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
887
14
                        CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
888
889
46
    return DAL;
890
46
  }
891
892
4.78k
  
for (Arg *A : Args)659
{
893
4.78k
    DAL->append(A);
894
4.78k
  }
895
896
659
  if (!BoundArch.empty()) {
897
281
    DAL->eraseArg(options::OPT_march_EQ);
898
281
    DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
899
281
  }
900
659
  return DAL;
901
659
}
902
903
247
Tool *CudaToolChain::buildAssembler() const {
904
247
  return new tools::NVPTX::Assembler(*this);
905
247
}
906
907
228
Tool *CudaToolChain::buildLinker() const {
908
228
  if (OK == Action::OFK_OpenMP)
909
44
    return new tools::NVPTX::OpenMPLinker(*this);
910
184
  return new tools::NVPTX::Linker(*this);
911
184
}
912
913
339
void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
914
339
  HostTC.addClangWarningOptions(CC1Args);
915
339
}
916
917
ToolChain::CXXStdlibType
918
0
CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
919
0
  return HostTC.GetCXXStdlibType(Args);
920
0
}
921
922
void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
923
612
                                              ArgStringList &CC1Args) const {
924
612
  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
925
612
}
926
927
void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
928
489
                                                 ArgStringList &CC1Args) const {
929
489
  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
930
489
}
931
932
void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
933
0
                                        ArgStringList &CC1Args) const {
934
0
  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
935
0
}
936
937
280
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
280
  return HostTC.getSupportedSanitizers();
948
280
}
949
950
VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
951
339
                                               const ArgList &Args) const {
952
339
  return HostTC.computeMSVCVersion(D, Args);
953
339
}