Coverage Report

Created: 2021-04-13 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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 "AMDGPU.h"
11
#include "CommonArgs.h"
12
#include "InputInfo.h"
13
#include "clang/Basic/Cuda.h"
14
#include "clang/Basic/TargetID.h"
15
#include "clang/Driver/Compilation.h"
16
#include "clang/Driver/Driver.h"
17
#include "clang/Driver/DriverDiagnostic.h"
18
#include "clang/Driver/Options.h"
19
#include "llvm/Support/Alignment.h"
20
#include "llvm/Support/FileSystem.h"
21
#include "llvm/Support/Path.h"
22
#include "llvm/Support/TargetParser.h"
23
24
using namespace clang::driver;
25
using namespace clang::driver::toolchains;
26
using namespace clang::driver::tools;
27
using namespace clang;
28
using namespace llvm::opt;
29
30
#if defined(_WIN32) || defined(_WIN64)
31
#define NULL_FILE "nul"
32
#else
33
147
#define NULL_FILE "/dev/null"
34
#endif
35
36
namespace {
37
const unsigned HIPCodeObjectAlign = 4096;
38
} // namespace
39
40
void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
41
                                          const InputInfoList &Inputs,
42
                                          const InputInfo &Output,
43
189
                                          const llvm::opt::ArgList &Args) const {
44
  // Construct lld command.
45
  // The output from ld.lld is an HSA code object file.
46
189
  ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined", "-shared",
47
189
                        "-plugin-opt=-amdgpu-internalize-symbols"};
48
49
189
  auto &TC = getToolChain();
50
189
  auto &D = TC.getDriver();
51
189
  assert(!Inputs.empty() && "Must have at least one input.");
52
0
  addLTOOptions(TC, Args, LldArgs, Output, Inputs[0],
53
189
                D.getLTOMode() == LTOK_Thin);
54
55
  // Extract all the -m options
56
189
  std::vector<llvm::StringRef> Features;
57
189
  amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
58
59
  // Add features to mattr such as cumode
60
189
  std::string MAttrString = "-plugin-opt=-mattr=";
61
189
  for (auto OneFeature : unifyTargetFeatures(Features)) {
62
35
    MAttrString.append(Args.MakeArgString(OneFeature));
63
35
    if (OneFeature != Features.back())
64
8
      MAttrString.append(",");
65
35
  }
66
189
  if (!Features.empty())
67
27
    LldArgs.push_back(Args.MakeArgString(MAttrString));
68
69
189
  for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
70
5
    LldArgs.push_back(
71
5
        Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
72
5
  }
73
74
189
  if (C.getDriver().isSaveTempsEnabled())
75
14
    LldArgs.push_back("-save-temps");
76
77
189
  addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
78
79
189
  LldArgs.append({"-o", Output.getFilename()});
80
189
  for (auto Input : Inputs)
81
203
    LldArgs.push_back(Input.getFilename());
82
83
189
  if (Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
84
189
                   false))
85
20
    
llvm::for_each(TC.getHIPDeviceLibs(Args), [&](StringRef BCFile) 3
{
86
20
      LldArgs.push_back(Args.MakeArgString(BCFile));
87
20
    });
88
89
189
  const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
90
189
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
91
189
                                         Lld, LldArgs, Inputs, Output));
92
189
}
93
94
// Construct a clang-offload-bundler command to bundle code objects for
95
// different GPU's into a HIP fat binary.
96
void AMDGCN::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
97
                  StringRef OutputFileName, const InputInfoList &Inputs,
98
147
                  const llvm::opt::ArgList &Args, const Tool& T) {
99
  // Construct clang-offload-bundler command to bundle object files for
100
  // for different GPU archs.
101
147
  ArgStringList BundlerArgs;
102
147
  BundlerArgs.push_back(Args.MakeArgString("-type=o"));
103
147
  BundlerArgs.push_back(
104
147
      Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign)));
105
106
  // ToDo: Remove the dummy host binary entry which is required by
107
  // clang-offload-bundler.
108
147
  std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux";
109
147
  std::string BundlerInputArg = "-inputs=" NULL_FILE;
110
111
  // For code object version 2 and 3, the offload kind in bundle ID is 'hip'
112
  // for backward compatibility. For code object version 4 and greater, the
113
  // offload kind in bundle ID is 'hipv4'.
114
147
  std::string OffloadKind = "hip";
115
147
  if (getOrCheckAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4)
116
141
    OffloadKind = OffloadKind + "v4";
117
189
  for (const auto &II : Inputs) {
118
189
    const auto* A = II.getAction();
119
189
    BundlerTargetArg = BundlerTargetArg + "," + OffloadKind +
120
189
                       "-amdgcn-amd-amdhsa--" +
121
189
                       StringRef(A->getOffloadingArch()).str();
122
189
    BundlerInputArg = BundlerInputArg + "," + II.getFilename();
123
189
  }
124
147
  BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
125
147
  BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
126
127
147
  std::string Output = std::string(OutputFileName);
128
147
  auto BundlerOutputArg =
129
147
      Args.MakeArgString(std::string("-outputs=").append(Output));
130
147
  BundlerArgs.push_back(BundlerOutputArg);
131
132
147
  const char *Bundler = Args.MakeArgString(
133
147
      T.getToolChain().GetProgramPath("clang-offload-bundler"));
134
147
  C.addCommand(std::make_unique<Command>(
135
147
      JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
136
147
      InputInfo(&JA, Args.MakeArgString(Output))));
137
147
}
138
139
/// Add Generated HIP Object File which has device images embedded into the
140
/// host to the argument list for linking. Using MC directives, embed the
141
/// device code and also define symbols required by the code generation so that
142
/// the image can be retrieved at runtime.
143
void AMDGCN::Linker::constructGenerateObjFileFromHIPFatBinary(
144
    Compilation &C, const InputInfo &Output,
145
    const InputInfoList &Inputs, const ArgList &Args,
146
30
    const JobAction &JA) const {
147
30
  const ToolChain &TC = getToolChain();
148
30
  std::string Name =
149
30
      std::string(llvm::sys::path::stem(Output.getFilename()));
150
151
  // Create Temp Object File Generator,
152
  // Offload Bundled file and Bundled Object file.
153
  // Keep them if save-temps is enabled.
154
30
  const char *McinFile;
155
30
  const char *BundleFile;
156
30
  if (C.getDriver().isSaveTempsEnabled()) {
157
5
    McinFile = C.getArgs().MakeArgString(Name + ".mcin");
158
5
    BundleFile = C.getArgs().MakeArgString(Name + ".hipfb");
159
25
  } else {
160
25
    auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin");
161
25
    McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin));
162
25
    auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb");
163
25
    BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb));
164
25
  }
165
30
  constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, *this);
166
167
  // Create a buffer to write the contents of the temp obj generator.
168
30
  std::string ObjBuffer;
169
30
  llvm::raw_string_ostream ObjStream(ObjBuffer);
170
171
  // Add MC directives to embed target binaries. We ensure that each
172
  // section and image is 16-byte aligned. This is not mandatory, but
173
  // increases the likelihood of data to be aligned with a cache block
174
  // in several main host machines.
175
30
  ObjStream << "#       HIP Object Generator\n";
176
30
  ObjStream << "# *** Automatically generated by Clang ***\n";
177
30
  ObjStream << "  .type __hip_fatbin,@object\n";
178
30
  ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";
179
30
  ObjStream << "  .globl __hip_fatbin\n";
180
30
  ObjStream << "  .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign))
181
30
            << "\n";
182
30
  ObjStream << "__hip_fatbin:\n";
183
30
  ObjStream << "  .incbin \"" << BundleFile << "\"\n";
184
30
  ObjStream.flush();
185
186
  // Dump the contents of the temp object file gen if the user requested that.
187
  // We support this option to enable testing of behavior with -###.
188
30
  if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script))
189
1
    llvm::errs() << ObjBuffer;
190
191
  // Open script file and write the contents.
192
30
  std::error_code EC;
193
30
  llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None);
194
195
30
  if (EC) {
196
0
    C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message();
197
0
    return;
198
0
  }
199
200
30
  Objf << ObjBuffer;
201
202
30
  ArgStringList McArgs{"-o",      Output.getFilename(),
203
30
                       McinFile,  "--filetype=obj"};
204
30
  const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc"));
205
30
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
206
30
                                         Mc, McArgs, Inputs, Output));
207
30
}
208
209
// For amdgcn the inputs of the linker job are device bitcode and output is
210
// object file. It calls llvm-link, opt, llc, then lld steps.
211
void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
212
                                   const InputInfo &Output,
213
                                   const InputInfoList &Inputs,
214
                                   const ArgList &Args,
215
336
                                   const char *LinkingOutput) const {
216
336
  if (Inputs.size() > 0 &&
217
336
      Inputs[0].getType() == types::TY_Image &&
218
336
      
JA.getType() == types::TY_Object147
)
219
30
    return constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs, Args, JA);
220
221
306
  if (JA.getType() == types::TY_HIP_FATBIN)
222
117
    return constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs, Args, *this);
223
224
189
  return constructLldCommand(C, JA, Inputs, Output, Args);
225
306
}
226
227
HIPToolChain::HIPToolChain(const Driver &D, const llvm::Triple &Triple,
228
                             const ToolChain &HostTC, const ArgList &Args)
229
207
    : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
230
  // Lookup binaries into the driver directory, this is used to
231
  // discover the clang-offload-bundler executable.
232
207
  getProgramPaths().push_back(getDriver().Dir);
233
207
}
234
235
void HIPToolChain::addClangTargetOptions(
236
    const llvm::opt::ArgList &DriverArgs,
237
    llvm::opt::ArgStringList &CC1Args,
238
258
    Action::OffloadKind DeviceOffloadingKind) const {
239
258
  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
240
241
258
  assert(DeviceOffloadingKind == Action::OFK_HIP &&
242
258
         "Only HIP offloading kinds are supported for GPUs.");
243
244
0
  CC1Args.push_back("-fcuda-is-device");
245
246
258
  if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
247
258
                         options::OPT_fno_cuda_approx_transcendentals, false))
248
0
    CC1Args.push_back("-fcuda-approx-transcendentals");
249
250
258
  if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
251
258
                          false))
252
181
    CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
253
254
258
  StringRef MaxThreadsPerBlock =
255
258
      DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
256
258
  if (!MaxThreadsPerBlock.empty()) {
257
1
    std::string ArgStr =
258
1
        std::string("--gpu-max-threads-per-block=") + MaxThreadsPerBlock.str();
259
1
    CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
260
1
  }
261
262
258
  CC1Args.push_back("-fcuda-allow-variadic-functions");
263
264
  // Default to "hidden" visibility, as object level linking will not be
265
  // supported for the foreseeable future.
266
258
  if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
267
258
                         options::OPT_fvisibility_ms_compat)) {
268
258
    CC1Args.append({"-fvisibility", "hidden"});
269
258
    CC1Args.push_back("-fapply-global-visibility-to-externs");
270
258
  }
271
272
400
  llvm::for_each(getHIPDeviceLibs(DriverArgs), [&](StringRef BCFile) {
273
400
    CC1Args.push_back("-mlink-builtin-bitcode");
274
400
    CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
275
400
  });
276
258
}
277
278
llvm::opt::DerivedArgList *
279
HIPToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
280
                             StringRef BoundArch,
281
525
                             Action::OffloadKind DeviceOffloadKind) const {
282
525
  DerivedArgList *DAL =
283
525
      HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
284
525
  if (!DAL)
285
480
    DAL = new DerivedArgList(Args.getBaseArgs());
286
287
525
  const OptTable &Opts = getDriver().getOpts();
288
289
4.63k
  for (Arg *A : Args) {
290
4.63k
    if (!shouldSkipArgument(A))
291
4.63k
      DAL->append(A);
292
4.63k
  }
293
294
525
  if (!BoundArch.empty()) {
295
239
    DAL->eraseArg(options::OPT_mcpu_EQ);
296
239
    DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
297
239
    checkTargetID(*DAL);
298
239
  }
299
300
525
  return DAL;
301
525
}
302
303
143
Tool *HIPToolChain::buildLinker() const {
304
143
  assert(getTriple().getArch() == llvm::Triple::amdgcn);
305
0
  return new tools::AMDGCN::Linker(*this);
306
143
}
307
308
258
void HIPToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
309
258
  HostTC.addClangWarningOptions(CC1Args);
310
258
}
311
312
ToolChain::CXXStdlibType
313
0
HIPToolChain::GetCXXStdlibType(const ArgList &Args) const {
314
0
  return HostTC.GetCXXStdlibType(Args);
315
0
}
316
317
void HIPToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
318
384
                                              ArgStringList &CC1Args) const {
319
384
  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
320
384
}
321
322
void HIPToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
323
384
                                                 ArgStringList &CC1Args) const {
324
384
  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
325
384
}
326
327
void HIPToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
328
0
                                        ArgStringList &CC1Args) const {
329
0
  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
330
0
}
331
332
void HIPToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
333
234
                                     ArgStringList &CC1Args) const {
334
234
  RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
335
234
}
336
337
162
SanitizerMask HIPToolChain::getSupportedSanitizers() const {
338
  // The HIPToolChain only supports sanitizers in the sense that it allows
339
  // sanitizer arguments on the command line if they are supported by the host
340
  // toolchain. The HIPToolChain will actually ignore any command line
341
  // arguments for any of these "supported" sanitizers. That means that no
342
  // sanitization of device code is actually supported at this time.
343
  //
344
  // This behavior is necessary because the host and device toolchains
345
  // invocations often share the command line, so the device toolchain must
346
  // tolerate flags meant only for the host toolchain.
347
162
  return HostTC.getSupportedSanitizers();
348
162
}
349
350
VersionTuple HIPToolChain::computeMSVCVersion(const Driver *D,
351
258
                                               const ArgList &Args) const {
352
258
  return HostTC.computeMSVCVersion(D, Args);
353
258
}
354
355
llvm::SmallVector<std::string, 12>
356
261
HIPToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
357
261
  llvm::SmallVector<std::string, 12> BCLibs;
358
261
  if (DriverArgs.hasArg(options::OPT_nogpulib))
359
159
    return {};
360
102
  ArgStringList LibraryPaths;
361
362
  // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
363
102
  for (auto Path : RocmInstallation.getRocmDeviceLibPathArg())
364
39
    LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
365
366
102
  addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
367
368
  // Maintain compatability with --hip-device-lib.
369
102
  auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
370
102
  if (!BCLibArgs.empty()) {
371
37
    llvm::for_each(BCLibArgs, [&](StringRef BCName) {
372
37
      StringRef FullName;
373
53
      for (std::string LibraryPath : LibraryPaths) {
374
53
        SmallString<128> Path(LibraryPath);
375
53
        llvm::sys::path::append(Path, BCName);
376
53
        FullName = Path;
377
53
        if (llvm::sys::fs::exists(FullName)) {
378
37
          BCLibs.push_back(FullName.str());
379
37
          return;
380
37
        }
381
53
      }
382
0
      getDriver().Diag(diag::err_drv_no_such_file) << BCName;
383
0
    });
384
81
  } else {
385
81
    if (!RocmInstallation.hasDeviceLibrary()) {
386
34
      getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
387
34
      return {};
388
34
    }
389
47
    StringRef GpuArch = getGPUArch(DriverArgs);
390
47
    assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
391
0
    (void)GpuArch;
392
47
    auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
393
47
    const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
394
395
47
    std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
396
47
    if (LibDeviceFile.empty()) {
397
3
      getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
398
3
      return {};
399
3
    }
400
401
    // If --hip-device-lib is not set, add the default bitcode libraries.
402
    // TODO: There are way too many flags that change this. Do we need to check
403
    // them all?
404
44
    bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
405
44
                                  options::OPT_fno_gpu_flush_denormals_to_zero,
406
44
                                  getDefaultDenormsAreZeroForTarget(Kind));
407
    // TODO: Check standard C++ flags?
408
44
    bool FiniteOnly = false;
409
44
    bool UnsafeMathOpt = false;
410
44
    bool FastRelaxedMath = false;
411
44
    bool CorrectSqrt = true;
412
44
    bool Wave64 = isWave64(DriverArgs, Kind);
413
414
44
    if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
415
44
                           options::OPT_fno_gpu_sanitize, false)) {
416
6
      auto AsanRTL = RocmInstallation.getAsanRTLPath();
417
6
      if (AsanRTL.empty()) {
418
2
        unsigned DiagID = getDriver().getDiags().getCustomDiagID(
419
2
            DiagnosticsEngine::Error,
420
2
            "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
421
2
            "Please install ROCm device library which supports address "
422
2
            "sanitizer");
423
2
        getDriver().Diag(DiagID);
424
2
        return {};
425
2
      } else
426
4
        BCLibs.push_back(AsanRTL.str());
427
6
    }
428
429
    // Add the HIP specific bitcode library.
430
42
    BCLibs.push_back(RocmInstallation.getHIPPath().str());
431
432
    // Add the generic set of libraries.
433
42
    BCLibs.append(RocmInstallation.getCommonBitcodeLibs(
434
42
        DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
435
42
        FastRelaxedMath, CorrectSqrt));
436
437
    // Add instrument lib.
438
42
    auto InstLib =
439
42
        DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
440
42
    if (InstLib.empty())
441
41
      return BCLibs;
442
1
    if (llvm::sys::fs::exists(InstLib))
443
1
      BCLibs.push_back(InstLib.str());
444
0
    else
445
0
      getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
446
1
  }
447
448
22
  return BCLibs;
449
102
}