Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/HIP.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- HIP.cpp - HIP 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 "HIP.h"
10
#include "CommonArgs.h"
11
#include "InputInfo.h"
12
#include "clang/Basic/Cuda.h"
13
#include "clang/Driver/Compilation.h"
14
#include "clang/Driver/Driver.h"
15
#include "clang/Driver/DriverDiagnostic.h"
16
#include "clang/Driver/Options.h"
17
#include "llvm/Support/FileSystem.h"
18
#include "llvm/Support/Path.h"
19
20
using namespace clang::driver;
21
using namespace clang::driver::toolchains;
22
using namespace clang::driver::tools;
23
using namespace clang;
24
using namespace llvm::opt;
25
26
#if _WIN32 || _WIN64
27
#define NULL_FILE "nul"
28
#else
29
6
#define NULL_FILE "/dev/null"
30
#endif
31
32
namespace {
33
34
static void addBCLib(const Driver &D, const ArgList &Args,
35
                     ArgStringList &CmdArgs, ArgStringList LibraryPaths,
36
196
                     StringRef BCName) {
37
196
  StringRef FullName;
38
196
  for (std::string LibraryPath : LibraryPaths) {
39
44
    SmallString<128> Path(LibraryPath);
40
44
    llvm::sys::path::append(Path, BCName);
41
44
    FullName = Path;
42
44
    if (llvm::sys::fs::exists(FullName)) {
43
34
      CmdArgs.push_back("-mlink-builtin-bitcode");
44
34
      CmdArgs.push_back(Args.MakeArgString(FullName));
45
34
      return;
46
34
    }
47
44
  }
48
196
  D.Diag(diag::err_drv_no_such_file) << BCName;
49
162
}
50
51
} // namespace
52
53
const char *AMDGCN::Linker::constructLLVMLinkCommand(
54
    Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
55
    const ArgList &Args, StringRef SubArchName,
56
10
    StringRef OutputFilePrefix) const {
57
10
  ArgStringList CmdArgs;
58
10
  // Add the input bc's created by compile step.
59
10
  for (const auto &II : Inputs)
60
12
    CmdArgs.push_back(II.getFilename());
61
10
62
10
  // Add an intermediate output file.
63
10
  CmdArgs.push_back("-o");
64
10
  std::string TmpName =
65
10
      C.getDriver().GetTemporaryPath(OutputFilePrefix.str() + "-linked", "bc");
66
10
  const char *OutputFileName =
67
10
      C.addTempFile(C.getArgs().MakeArgString(TmpName));
68
10
  CmdArgs.push_back(OutputFileName);
69
10
  SmallString<128> ExecPath(C.getDriver().Dir);
70
10
  llvm::sys::path::append(ExecPath, "llvm-link");
71
10
  const char *Exec = Args.MakeArgString(ExecPath);
72
10
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
73
10
  return OutputFileName;
74
10
}
75
76
const char *AMDGCN::Linker::constructOptCommand(
77
    Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
78
    const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
79
10
    llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
80
10
  // Construct opt command.
81
10
  ArgStringList OptArgs;
82
10
  // The input to opt is the output from llvm-link.
83
10
  OptArgs.push_back(InputFileName);
84
10
  // Pass optimization arg to opt.
85
10
  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
86
0
    StringRef OOpt = "3";
87
0
    if (A->getOption().matches(options::OPT_O4) ||
88
0
        A->getOption().matches(options::OPT_Ofast))
89
0
      OOpt = "3";
90
0
    else if (A->getOption().matches(options::OPT_O0))
91
0
      OOpt = "0";
92
0
    else if (A->getOption().matches(options::OPT_O)) {
93
0
      // -Os, -Oz, and -O(anything else) map to -O2
94
0
      OOpt = llvm::StringSwitch<const char *>(A->getValue())
95
0
                 .Case("1", "1")
96
0
                 .Case("2", "2")
97
0
                 .Case("3", "3")
98
0
                 .Case("s", "2")
99
0
                 .Case("z", "2")
100
0
                 .Default("2");
101
0
    }
102
0
    OptArgs.push_back(Args.MakeArgString("-O" + OOpt));
103
0
  }
104
10
  OptArgs.push_back("-mtriple=amdgcn-amd-amdhsa");
105
10
  OptArgs.push_back(Args.MakeArgString("-mcpu=" + SubArchName));
106
10
107
10
  for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
108
2
    OptArgs.push_back(A->getValue(0));
109
2
  }
110
10
111
10
  OptArgs.push_back("-o");
112
10
  std::string TmpFileName = C.getDriver().GetTemporaryPath(
113
10
      OutputFilePrefix.str() + "-optimized", "bc");
114
10
  const char *OutputFileName =
115
10
      C.addTempFile(C.getArgs().MakeArgString(TmpFileName));
116
10
  OptArgs.push_back(OutputFileName);
117
10
  SmallString<128> OptPath(C.getDriver().Dir);
118
10
  llvm::sys::path::append(OptPath, "opt");
119
10
  const char *OptExec = Args.MakeArgString(OptPath);
120
10
  C.addCommand(llvm::make_unique<Command>(JA, *this, OptExec, OptArgs, Inputs));
121
10
  return OutputFileName;
122
10
}
123
124
const char *AMDGCN::Linker::constructLlcCommand(
125
    Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
126
    const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
127
10
    llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
128
10
  // Construct llc command.
129
10
  ArgStringList LlcArgs{InputFileName, "-mtriple=amdgcn-amd-amdhsa",
130
10
                        "-filetype=obj",
131
10
                        Args.MakeArgString("-mcpu=" + SubArchName)};
132
10
133
10
  // Extract all the -m options
134
10
  std::vector<llvm::StringRef> Features;
135
10
  handleTargetFeaturesGroup(
136
10
    Args, Features, options::OPT_m_amdgpu_Features_Group);
137
10
138
10
  // Add features to mattr such as xnack
139
10
  std::string MAttrString = "-mattr=";
140
10
  for(auto OneFeature : Features) {
141
0
    MAttrString.append(Args.MakeArgString(OneFeature));
142
0
    if (OneFeature != Features.back())
143
0
      MAttrString.append(",");
144
0
  }
145
10
  if(!Features.empty())
146
0
    LlcArgs.push_back(Args.MakeArgString(MAttrString));
147
10
148
10
  for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
149
2
    LlcArgs.push_back(A->getValue(0));
150
2
  }
151
10
152
10
  // Add output filename
153
10
  LlcArgs.push_back("-o");
154
10
  std::string LlcOutputFileName =
155
10
      C.getDriver().GetTemporaryPath(OutputFilePrefix, "o");
156
10
  const char *LlcOutputFile =
157
10
      C.addTempFile(C.getArgs().MakeArgString(LlcOutputFileName));
158
10
  LlcArgs.push_back(LlcOutputFile);
159
10
  SmallString<128> LlcPath(C.getDriver().Dir);
160
10
  llvm::sys::path::append(LlcPath, "llc");
161
10
  const char *Llc = Args.MakeArgString(LlcPath);
162
10
  C.addCommand(llvm::make_unique<Command>(JA, *this, Llc, LlcArgs, Inputs));
163
10
  return LlcOutputFile;
164
10
}
165
166
void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
167
                                          const InputInfoList &Inputs,
168
                                          const InputInfo &Output,
169
                                          const llvm::opt::ArgList &Args,
170
10
                                          const char *InputFileName) const {
171
10
  // Construct lld command.
172
10
  // The output from ld.lld is an HSA code object file.
173
10
  ArgStringList LldArgs{
174
10
      "-flavor", "gnu", "-shared", "-o", Output.getFilename(), InputFileName};
175
10
  SmallString<128> LldPath(C.getDriver().Dir);
176
10
  llvm::sys::path::append(LldPath, "lld");
177
10
  const char *Lld = Args.MakeArgString(LldPath);
178
10
  C.addCommand(llvm::make_unique<Command>(JA, *this, Lld, LldArgs, Inputs));
179
10
}
180
181
// Construct a clang-offload-bundler command to bundle code objects for
182
// different GPU's into a HIP fat binary.
183
void AMDGCN::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
184
                  StringRef OutputFileName, const InputInfoList &Inputs,
185
6
                  const llvm::opt::ArgList &Args, const Tool& T) {
186
6
  // Construct clang-offload-bundler command to bundle object files for
187
6
  // for different GPU archs.
188
6
  ArgStringList BundlerArgs;
189
6
  BundlerArgs.push_back(Args.MakeArgString("-type=o"));
190
6
191
6
  // ToDo: Remove the dummy host binary entry which is required by
192
6
  // clang-offload-bundler.
193
6
  std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux";
194
6
  std::string BundlerInputArg = "-inputs=" NULL_FILE;
195
6
196
10
  for (const auto &II : Inputs) {
197
10
    const auto* A = II.getAction();
198
10
    BundlerTargetArg = BundlerTargetArg + ",hip-amdgcn-amd-amdhsa-" +
199
10
                       StringRef(A->getOffloadingArch()).str();
200
10
    BundlerInputArg = BundlerInputArg + "," + II.getFilename();
201
10
  }
202
6
  BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
203
6
  BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
204
6
205
6
  auto BundlerOutputArg =
206
6
      Args.MakeArgString(std::string("-outputs=").append(OutputFileName));
207
6
  BundlerArgs.push_back(BundlerOutputArg);
208
6
209
6
  SmallString<128> BundlerPath(C.getDriver().Dir);
210
6
  llvm::sys::path::append(BundlerPath, "clang-offload-bundler");
211
6
  const char *Bundler = Args.MakeArgString(BundlerPath);
212
6
  C.addCommand(llvm::make_unique<Command>(JA, T, Bundler, BundlerArgs, Inputs));
213
6
}
214
215
// For amdgcn the inputs of the linker job are device bitcode and output is
216
// object file. It calls llvm-link, opt, llc, then lld steps.
217
void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
218
                                   const InputInfo &Output,
219
                                   const InputInfoList &Inputs,
220
                                   const ArgList &Args,
221
15
                                   const char *LinkingOutput) const {
222
15
223
15
  if (JA.getType() == types::TY_HIP_FATBIN)
224
5
    return constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs, Args, *this);
225
10
226
10
  assert(getToolChain().getTriple().getArch() == llvm::Triple::amdgcn &&
227
10
         "Unsupported target");
228
10
229
10
  std::string SubArchName = JA.getOffloadingArch();
230
10
  assert(StringRef(SubArchName).startswith("gfx") && "Unsupported sub arch");
231
10
232
10
  // Prefix for temporary file name.
233
10
  std::string Prefix =
234
10
      llvm::sys::path::stem(Inputs[0].getFilename()).str() + "-" + SubArchName;
235
10
236
10
  // Each command outputs different files.
237
10
  const char *LLVMLinkCommand =
238
10
      constructLLVMLinkCommand(C, JA, Inputs, Args, SubArchName, Prefix);
239
10
  const char *OptCommand = constructOptCommand(C, JA, Inputs, Args, SubArchName,
240
10
                                               Prefix, LLVMLinkCommand);
241
10
  const char *LlcCommand =
242
10
      constructLlcCommand(C, JA, Inputs, Args, SubArchName, Prefix, OptCommand);
243
10
  constructLldCommand(C, JA, Inputs, Output, Args, LlcCommand);
244
10
}
245
246
HIPToolChain::HIPToolChain(const Driver &D, const llvm::Triple &Triple,
247
                             const ToolChain &HostTC, const ArgList &Args)
248
30
    : ToolChain(D, Triple, Args), HostTC(HostTC) {
249
30
  // Lookup binaries into the driver directory, this is used to
250
30
  // discover the clang-offload-bundler executable.
251
30
  getProgramPaths().push_back(getDriver().Dir);
252
30
}
253
254
void HIPToolChain::addClangTargetOptions(
255
    const llvm::opt::ArgList &DriverArgs,
256
    llvm::opt::ArgStringList &CC1Args,
257
26
    Action::OffloadKind DeviceOffloadingKind) const {
258
26
  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
259
26
260
26
  StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
261
26
  assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
262
26
  (void) GpuArch;
263
26
  assert(DeviceOffloadingKind == Action::OFK_HIP &&
264
26
         "Only HIP offloading kinds are supported for GPUs.");
265
26
266
26
  CC1Args.push_back("-target-cpu");
267
26
  CC1Args.push_back(DriverArgs.MakeArgStringRef(GpuArch));
268
26
  CC1Args.push_back("-fcuda-is-device");
269
26
270
26
  if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
271
26
                         options::OPT_fno_cuda_flush_denormals_to_zero, false))
272
1
    CC1Args.push_back("-fcuda-flush-denormals-to-zero");
273
26
274
26
  if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
275
26
                         options::OPT_fno_cuda_approx_transcendentals, false))
276
0
    CC1Args.push_back("-fcuda-approx-transcendentals");
277
26
278
26
  if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
279
26
                         false))
280
18
    CC1Args.push_back("-fgpu-rdc");
281
26
282
26
  // Default to "hidden" visibility, as object level linking will not be
283
26
  // supported for the foreseeable future.
284
26
  if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
285
26
                         options::OPT_fvisibility_ms_compat)) {
286
26
    CC1Args.append({"-fvisibility", "hidden"});
287
26
    CC1Args.push_back("-fapply-global-visibility-to-externs");
288
26
  }
289
26
  ArgStringList LibraryPaths;
290
26
291
26
  // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
292
26
  for (auto Path :
293
26
       DriverArgs.getAllArgValues(options::OPT_hip_device_lib_path_EQ))
294
18
    LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
295
26
296
26
  addDirectoryList(DriverArgs, LibraryPaths, "-L", "HIP_DEVICE_LIB_PATH");
297
26
298
26
  llvm::SmallVector<std::string, 10> BCLibs;
299
26
300
26
  // Add bitcode library in --hip-device-lib.
301
26
  for (auto Lib : DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ)) {
302
16
    BCLibs.push_back(DriverArgs.MakeArgString(Lib));
303
16
  }
304
26
305
26
  // If --hip-device-lib is not set, add the default bitcode libraries.
306
26
  if (BCLibs.empty()) {
307
18
    // Get the bc lib file name for ISA version. For example,
308
18
    // gfx803 => oclc_isa_version_803.amdgcn.bc.
309
18
    std::string GFXVersion = GpuArch.drop_front(3).str();
310
18
    std::string ISAVerBC = "oclc_isa_version_" + GFXVersion + ".amdgcn.bc";
311
18
312
18
    llvm::StringRef FlushDenormalControlBC;
313
18
    if (DriverArgs.hasArg(options::OPT_fcuda_flush_denormals_to_zero))
314
1
      FlushDenormalControlBC = "oclc_daz_opt_on.amdgcn.bc";
315
17
    else
316
17
      FlushDenormalControlBC = "oclc_daz_opt_off.amdgcn.bc";
317
18
318
18
    llvm::StringRef WaveFrontSizeBC;
319
18
    if (stoi(GFXVersion) < 1000)
320
18
      WaveFrontSizeBC = "oclc_wavefrontsize64_on.amdgcn.bc";
321
0
    else
322
0
      WaveFrontSizeBC = "oclc_wavefrontsize64_off.amdgcn.bc";
323
18
324
18
    BCLibs.append({"hip.amdgcn.bc", "opencl.amdgcn.bc", "ocml.amdgcn.bc",
325
18
                   "ockl.amdgcn.bc", "oclc_finite_only_off.amdgcn.bc",
326
18
                   FlushDenormalControlBC,
327
18
                   "oclc_correctly_rounded_sqrt_on.amdgcn.bc",
328
18
                   "oclc_unsafe_math_off.amdgcn.bc", ISAVerBC,
329
18
                   WaveFrontSizeBC});
330
18
  }
331
26
  for (auto Lib : BCLibs)
332
196
    addBCLib(getDriver(), DriverArgs, CC1Args, LibraryPaths, Lib);
333
26
}
334
335
llvm::opt::DerivedArgList *
336
HIPToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
337
                             StringRef BoundArch,
338
36
                             Action::OffloadKind DeviceOffloadKind) const {
339
36
  DerivedArgList *DAL =
340
36
      HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
341
36
  if (!DAL)
342
36
    DAL = new DerivedArgList(Args.getBaseArgs());
343
36
344
36
  const OptTable &Opts = getDriver().getOpts();
345
36
346
354
  for (Arg *A : Args) {
347
354
    if (A->getOption().matches(options::OPT_Xarch__)) {
348
0
      // Skip this argument unless the architecture matches BoundArch.
349
0
      if (BoundArch.empty() || A->getValue(0) != BoundArch)
350
0
        continue;
351
0
352
0
      unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
353
0
      unsigned Prev = Index;
354
0
      std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
355
0
356
0
      // If the argument parsing failed or more than one argument was
357
0
      // consumed, the -Xarch_ argument's parameter tried to consume
358
0
      // extra arguments. Emit an error and ignore.
359
0
      //
360
0
      // We also want to disallow any options which would alter the
361
0
      // driver behavior; that isn't going to work in our model. We
362
0
      // use isDriverOption() as an approximation, although things
363
0
      // like -O4 are going to slip through.
364
0
      if (!XarchArg || Index > Prev + 1) {
365
0
        getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
366
0
            << A->getAsString(Args);
367
0
        continue;
368
0
      } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
369
0
        getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
370
0
            << A->getAsString(Args);
371
0
        continue;
372
0
      }
373
0
      XarchArg->setBaseArg(A);
374
0
      A = XarchArg.release();
375
0
      DAL->AddSynthesizedArg(A);
376
0
    }
377
354
    DAL->append(A);
378
354
  }
379
36
380
36
  if (!BoundArch.empty()) {
381
28
    DAL->eraseArg(options::OPT_march_EQ);
382
28
    DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
383
28
  }
384
36
385
36
  return DAL;
386
36
}
387
388
7
Tool *HIPToolChain::buildLinker() const {
389
7
  assert(getTriple().getArch() == llvm::Triple::amdgcn);
390
7
  return new tools::AMDGCN::Linker(*this);
391
7
}
392
393
26
void HIPToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
394
26
  HostTC.addClangWarningOptions(CC1Args);
395
26
}
396
397
ToolChain::CXXStdlibType
398
0
HIPToolChain::GetCXXStdlibType(const ArgList &Args) const {
399
0
  return HostTC.GetCXXStdlibType(Args);
400
0
}
401
402
void HIPToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
403
40
                                              ArgStringList &CC1Args) const {
404
40
  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
405
40
}
406
407
void HIPToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
408
40
                                                 ArgStringList &CC1Args) const {
409
40
  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
410
40
}
411
412
void HIPToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
413
0
                                        ArgStringList &CC1Args) const {
414
0
  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
415
0
}
416
417
12
SanitizerMask HIPToolChain::getSupportedSanitizers() const {
418
12
  // The HIPToolChain only supports sanitizers in the sense that it allows
419
12
  // sanitizer arguments on the command line if they are supported by the host
420
12
  // toolchain. The HIPToolChain will actually ignore any command line
421
12
  // arguments for any of these "supported" sanitizers. That means that no
422
12
  // sanitization of device code is actually supported at this time.
423
12
  //
424
12
  // This behavior is necessary because the host and device toolchains
425
12
  // invocations often share the command line, so the device toolchain must
426
12
  // tolerate flags meant only for the host toolchain.
427
12
  return HostTC.getSupportedSanitizers();
428
12
}
429
430
VersionTuple HIPToolChain::computeMSVCVersion(const Driver *D,
431
26
                                               const ArgList &Args) const {
432
26
  return HostTC.computeMSVCVersion(D, Args);
433
26
}