Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/ToolChains/HIPAMD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- HIPAMD.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 "HIPAMD.h"
10
#include "AMDGPU.h"
11
#include "CommonArgs.h"
12
#include "HIPUtility.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/InputInfo.h"
19
#include "clang/Driver/Options.h"
20
#include "clang/Driver/SanitizerArgs.h"
21
#include "llvm/Support/Alignment.h"
22
#include "llvm/Support/FileSystem.h"
23
#include "llvm/Support/Path.h"
24
#include "llvm/TargetParser/TargetParser.h"
25
26
using namespace clang::driver;
27
using namespace clang::driver::toolchains;
28
using namespace clang::driver::tools;
29
using namespace clang;
30
using namespace llvm::opt;
31
32
#if defined(_WIN32) || defined(_WIN64)
33
#define NULL_FILE "nul"
34
#else
35
#define NULL_FILE "/dev/null"
36
#endif
37
38
static bool shouldSkipSanitizeOption(const ToolChain &TC,
39
                                     const llvm::opt::ArgList &DriverArgs,
40
                                     StringRef TargetID,
41
0
                                     const llvm::opt::Arg *A) {
42
  // For actions without targetID, do nothing.
43
0
  if (TargetID.empty())
44
0
    return false;
45
0
  Option O = A->getOption();
46
0
  if (!O.matches(options::OPT_fsanitize_EQ))
47
0
    return false;
48
49
0
  if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
50
0
                          options::OPT_fno_gpu_sanitize, true))
51
0
    return true;
52
53
0
  auto &Diags = TC.getDriver().getDiags();
54
55
  // For simplicity, we only allow -fsanitize=address
56
0
  SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
57
0
  if (K != SanitizerKind::Address)
58
0
    return true;
59
60
0
  llvm::StringMap<bool> FeatureMap;
61
0
  auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
62
63
0
  assert(OptionalGpuArch && "Invalid Target ID");
64
0
  (void)OptionalGpuArch;
65
0
  auto Loc = FeatureMap.find("xnack");
66
0
  if (Loc == FeatureMap.end() || !Loc->second) {
67
0
    Diags.Report(
68
0
        clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
69
0
        << A->getAsString(DriverArgs) << TargetID << "xnack+";
70
0
    return true;
71
0
  }
72
0
  return false;
73
0
}
74
75
void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C,
76
                                         const JobAction &JA,
77
                                         const InputInfoList &Inputs,
78
                                         const InputInfo &Output,
79
0
                                         const llvm::opt::ArgList &Args) const {
80
  // Construct llvm-link command.
81
  // The output from llvm-link is a bitcode file.
82
0
  ArgStringList LlvmLinkArgs;
83
84
0
  assert(!Inputs.empty() && "Must have at least one input.");
85
86
0
  LlvmLinkArgs.append({"-o", Output.getFilename()});
87
0
  for (auto Input : Inputs)
88
0
    LlvmLinkArgs.push_back(Input.getFilename());
89
90
  // Look for archive of bundled bitcode in arguments, and add temporary files
91
  // for the extracted archive of bitcode to inputs.
92
0
  auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
93
0
  AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn",
94
0
                             TargetID, /*IsBitCodeSDL=*/true);
95
96
0
  const char *LlvmLink =
97
0
    Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
98
0
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
99
0
                                         LlvmLink, LlvmLinkArgs, Inputs,
100
0
                                         Output));
101
0
}
102
103
void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
104
                                         const InputInfoList &Inputs,
105
                                         const InputInfo &Output,
106
0
                                         const llvm::opt::ArgList &Args) const {
107
  // Construct lld command.
108
  // The output from ld.lld is an HSA code object file.
109
0
  ArgStringList LldArgs{"-flavor",
110
0
                        "gnu",
111
0
                        "-m",
112
0
                        "elf64_amdgpu",
113
0
                        "--no-undefined",
114
0
                        "-shared",
115
0
                        "-plugin-opt=-amdgpu-internalize-symbols"};
116
0
  if (Args.hasArg(options::OPT_hipstdpar))
117
0
    LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar");
118
119
0
  auto &TC = getToolChain();
120
0
  auto &D = TC.getDriver();
121
0
  assert(!Inputs.empty() && "Must have at least one input.");
122
0
  bool IsThinLTO = D.getLTOMode(/*IsOffload=*/true) == LTOK_Thin;
123
0
  addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
124
125
  // Extract all the -m options
126
0
  std::vector<llvm::StringRef> Features;
127
0
  amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
128
129
  // Add features to mattr such as cumode
130
0
  std::string MAttrString = "-plugin-opt=-mattr=";
131
0
  for (auto OneFeature : unifyTargetFeatures(Features)) {
132
0
    MAttrString.append(Args.MakeArgString(OneFeature));
133
0
    if (OneFeature != Features.back())
134
0
      MAttrString.append(",");
135
0
  }
136
0
  if (!Features.empty())
137
0
    LldArgs.push_back(Args.MakeArgString(MAttrString));
138
139
  // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
140
  // Since AMDGPU backend currently does not support ISA-level linking, all
141
  // called functions need to be imported.
142
0
  if (IsThinLTO)
143
0
    LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
144
145
0
  if (C.getDriver().isSaveTempsEnabled())
146
0
    LldArgs.push_back("-save-temps");
147
148
0
  addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
149
150
  // Given that host and device linking happen in separate processes, the device
151
  // linker doesn't always have the visibility as to which device symbols are
152
  // needed by a program, especially for the device symbol dependencies that are
153
  // introduced through the host symbol resolution.
154
  // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B()
155
  // (B.obj) In this case, the device linker doesn't know that A.obj actually
156
  // depends on the kernel functions in B.obj.  When linking to static device
157
  // library, the device linker may drop some of the device global symbols if
158
  // they aren't referenced.  As a workaround, we are adding to the
159
  // --whole-archive flag such that all global symbols would be linked in.
160
0
  LldArgs.push_back("--whole-archive");
161
162
0
  for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
163
0
    StringRef ArgVal = Arg->getValue(1);
164
0
    auto SplitArg = ArgVal.split("-mllvm=");
165
0
    if (!SplitArg.second.empty()) {
166
0
      LldArgs.push_back(
167
0
          Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second));
168
0
    } else {
169
0
      LldArgs.push_back(Args.MakeArgString(ArgVal));
170
0
    }
171
0
    Arg->claim();
172
0
  }
173
174
0
  LldArgs.append({"-o", Output.getFilename()});
175
0
  for (auto Input : Inputs)
176
0
    LldArgs.push_back(Input.getFilename());
177
178
  // Look for archive of bundled bitcode in arguments, and add temporary files
179
  // for the extracted archive of bitcode to inputs.
180
0
  auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
181
0
  AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
182
0
                             TargetID, /*IsBitCodeSDL=*/true);
183
184
0
  LldArgs.push_back("--no-whole-archive");
185
186
0
  const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
187
0
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
188
0
                                         Lld, LldArgs, Inputs, Output));
189
0
}
190
191
// For amdgcn the inputs of the linker job are device bitcode and output is
192
// either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
193
// llc, then lld steps.
194
void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
195
                                  const InputInfo &Output,
196
                                  const InputInfoList &Inputs,
197
                                  const ArgList &Args,
198
0
                                  const char *LinkingOutput) const {
199
0
  if (Inputs.size() > 0 &&
200
0
      Inputs[0].getType() == types::TY_Image &&
201
0
      JA.getType() == types::TY_Object)
202
0
    return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
203
0
                                                         Args, JA, *this);
204
205
0
  if (JA.getType() == types::TY_HIP_FATBIN)
206
0
    return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
207
0
                                          Args, *this);
208
209
0
  if (JA.getType() == types::TY_LLVM_BC)
210
0
    return constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
211
212
0
  return constructLldCommand(C, JA, Inputs, Output, Args);
213
0
}
214
215
HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
216
                                 const ToolChain &HostTC, const ArgList &Args)
217
0
    : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
218
  // Lookup binaries into the driver directory, this is used to
219
  // discover the clang-offload-bundler executable.
220
0
  getProgramPaths().push_back(getDriver().Dir);
221
222
  // Diagnose unsupported sanitizer options only once.
223
0
  if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
224
0
                    true))
225
0
    return;
226
0
  for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
227
0
    SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
228
0
    if (K != SanitizerKind::Address)
229
0
      D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
230
0
          << A->getAsString(Args) << getTriple().str();
231
0
  }
232
0
}
233
234
void HIPAMDToolChain::addClangTargetOptions(
235
    const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
236
0
    Action::OffloadKind DeviceOffloadingKind) const {
237
0
  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
238
239
0
  assert(DeviceOffloadingKind == Action::OFK_HIP &&
240
0
         "Only HIP offloading kinds are supported for GPUs.");
241
242
0
  CC1Args.push_back("-fcuda-is-device");
243
244
0
  if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
245
0
                          false))
246
0
    CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
247
0
  if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar))
248
0
    CC1Args.append({"-mllvm", "-amdgpu-enable-hipstdpar"});
249
250
0
  StringRef MaxThreadsPerBlock =
251
0
      DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
252
0
  if (!MaxThreadsPerBlock.empty()) {
253
0
    std::string ArgStr =
254
0
        (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
255
0
    CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
256
0
  }
257
258
0
  CC1Args.push_back("-fcuda-allow-variadic-functions");
259
260
  // Default to "hidden" visibility, as object level linking will not be
261
  // supported for the foreseeable future.
262
0
  if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
263
0
                         options::OPT_fvisibility_ms_compat)) {
264
0
    CC1Args.append({"-fvisibility=hidden"});
265
0
    CC1Args.push_back("-fapply-global-visibility-to-externs");
266
0
  }
267
268
0
  for (auto BCFile : getDeviceLibs(DriverArgs)) {
269
0
    CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
270
0
                                               : "-mlink-bitcode-file");
271
0
    CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
272
0
  }
273
0
}
274
275
llvm::opt::DerivedArgList *
276
HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
277
                               StringRef BoundArch,
278
0
                               Action::OffloadKind DeviceOffloadKind) const {
279
0
  DerivedArgList *DAL =
280
0
      HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
281
0
  if (!DAL)
282
0
    DAL = new DerivedArgList(Args.getBaseArgs());
283
284
0
  const OptTable &Opts = getDriver().getOpts();
285
286
0
  for (Arg *A : Args) {
287
0
    if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A))
288
0
      DAL->append(A);
289
0
  }
290
291
0
  if (!BoundArch.empty()) {
292
0
    DAL->eraseArg(options::OPT_mcpu_EQ);
293
0
    DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
294
0
    checkTargetID(*DAL);
295
0
  }
296
297
0
  return DAL;
298
0
}
299
300
0
Tool *HIPAMDToolChain::buildLinker() const {
301
0
  assert(getTriple().getArch() == llvm::Triple::amdgcn);
302
0
  return new tools::AMDGCN::Linker(*this);
303
0
}
304
305
0
void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
306
0
  HostTC.addClangWarningOptions(CC1Args);
307
0
}
308
309
ToolChain::CXXStdlibType
310
0
HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
311
0
  return HostTC.GetCXXStdlibType(Args);
312
0
}
313
314
void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
315
0
                                                ArgStringList &CC1Args) const {
316
0
  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
317
0
}
318
319
void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
320
0
    const ArgList &Args, ArgStringList &CC1Args) const {
321
0
  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
322
0
}
323
324
void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
325
0
                                          ArgStringList &CC1Args) const {
326
0
  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
327
0
}
328
329
void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
330
0
                                        ArgStringList &CC1Args) const {
331
0
  RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
332
0
}
333
334
0
SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
335
  // The HIPAMDToolChain only supports sanitizers in the sense that it allows
336
  // sanitizer arguments on the command line if they are supported by the host
337
  // toolchain. The HIPAMDToolChain will actually ignore any command line
338
  // arguments for any of these "supported" sanitizers. That means that no
339
  // sanitization of device code is actually supported at this time.
340
  //
341
  // This behavior is necessary because the host and device toolchains
342
  // invocations often share the command line, so the device toolchain must
343
  // tolerate flags meant only for the host toolchain.
344
0
  return HostTC.getSupportedSanitizers();
345
0
}
346
347
VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
348
0
                                                 const ArgList &Args) const {
349
0
  return HostTC.computeMSVCVersion(D, Args);
350
0
}
351
352
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
353
0
HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
354
0
  llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
355
0
  if (DriverArgs.hasArg(options::OPT_nogpulib))
356
0
    return {};
357
0
  ArgStringList LibraryPaths;
358
359
  // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
360
0
  for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
361
0
    LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
362
363
0
  addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
364
365
  // Maintain compatability with --hip-device-lib.
366
0
  auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
367
0
  if (!BCLibArgs.empty()) {
368
0
    llvm::for_each(BCLibArgs, [&](StringRef BCName) {
369
0
      StringRef FullName;
370
0
      for (StringRef LibraryPath : LibraryPaths) {
371
0
        SmallString<128> Path(LibraryPath);
372
0
        llvm::sys::path::append(Path, BCName);
373
0
        FullName = Path;
374
0
        if (llvm::sys::fs::exists(FullName)) {
375
0
          BCLibs.push_back(FullName);
376
0
          return;
377
0
        }
378
0
      }
379
0
      getDriver().Diag(diag::err_drv_no_such_file) << BCName;
380
0
    });
381
0
  } else {
382
0
    if (!RocmInstallation->hasDeviceLibrary()) {
383
0
      getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
384
0
      return {};
385
0
    }
386
0
    StringRef GpuArch = getGPUArch(DriverArgs);
387
0
    assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
388
389
    // If --hip-device-lib is not set, add the default bitcode libraries.
390
0
    if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
391
0
                           options::OPT_fno_gpu_sanitize, true) &&
392
0
        getSanitizerArgs(DriverArgs).needsAsanRt()) {
393
0
      auto AsanRTL = RocmInstallation->getAsanRTLPath();
394
0
      if (AsanRTL.empty()) {
395
0
        unsigned DiagID = getDriver().getDiags().getCustomDiagID(
396
0
            DiagnosticsEngine::Error,
397
0
            "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
398
0
            "Please install ROCm device library which supports address "
399
0
            "sanitizer");
400
0
        getDriver().Diag(DiagID);
401
0
        return {};
402
0
      } else
403
0
        BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
404
0
    }
405
406
    // Add the HIP specific bitcode library.
407
0
    BCLibs.push_back(RocmInstallation->getHIPPath());
408
409
    // Add common device libraries like ocml etc.
410
0
    for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
411
0
      BCLibs.emplace_back(N);
412
413
    // Add instrument lib.
414
0
    auto InstLib =
415
0
        DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
416
0
    if (InstLib.empty())
417
0
      return BCLibs;
418
0
    if (llvm::sys::fs::exists(InstLib))
419
0
      BCLibs.push_back(InstLib);
420
0
    else
421
0
      getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
422
0
  }
423
424
0
  return BCLibs;
425
0
}
426
427
void HIPAMDToolChain::checkTargetID(
428
0
    const llvm::opt::ArgList &DriverArgs) const {
429
0
  auto PTID = getParsedTargetID(DriverArgs);
430
0
  if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
431
0
    getDriver().Diag(clang::diag::err_drv_bad_target_id)
432
0
        << *PTID.OptionalTargetID;
433
0
  }
434
0
}