Coverage Report

Created: 2019-07-24 05:18

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