Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/AMDGPU.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- AMDGPU.cpp - AMDGPU 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 "AMDGPU.h"
10
#include "CommonArgs.h"
11
#include "clang/Basic/TargetID.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/DriverDiagnostic.h"
14
#include "clang/Driver/InputInfo.h"
15
#include "clang/Driver/Options.h"
16
#include "llvm/Option/ArgList.h"
17
#include "llvm/Support/Error.h"
18
#include "llvm/Support/FileUtilities.h"
19
#include "llvm/Support/LineIterator.h"
20
#include "llvm/Support/Path.h"
21
#include "llvm/Support/VirtualFileSystem.h"
22
#include <system_error>
23
24
0
#define AMDGPU_ARCH_PROGRAM_NAME "amdgpu-arch"
25
26
using namespace clang::driver;
27
using namespace clang::driver::tools;
28
using namespace clang::driver::toolchains;
29
using namespace clang;
30
using namespace llvm::opt;
31
32
// Look for sub-directory starts with PackageName under ROCm candidate path.
33
// If there is one and only one matching sub-directory found, append the
34
// sub-directory to Path. If there is no matching sub-directory or there are
35
// more than one matching sub-directories, diagnose them. Returns the full
36
// path of the package if there is only one matching sub-directory, otherwise
37
// returns an empty string.
38
llvm::SmallString<0>
39
RocmInstallationDetector::findSPACKPackage(const Candidate &Cand,
40
58.1k
                                           StringRef PackageName) {
41
58.1k
  if (!Cand.isSPACK())
42
58.1k
    return {};
43
11
  std::error_code EC;
44
11
  std::string Prefix = Twine(PackageName + "-" + Cand.SPACKReleaseStr).str();
45
11
  llvm::SmallVector<llvm::SmallString<0>> SubDirs;
46
11
  for (llvm::vfs::directory_iterator File = D.getVFS().dir_begin(Cand.Path, EC),
47
11
                                     FileEnd;
48
22
       File != FileEnd && 
!EC13
;
File.increment(EC)11
) {
49
13
    llvm::StringRef FileName = llvm::sys::path::filename(File->path());
50
13
    if (FileName.startswith(Prefix)) {
51
6
      SubDirs.push_back(FileName);
52
6
      if (SubDirs.size() > 1)
53
2
        break;
54
6
    }
55
13
  }
56
11
  if (SubDirs.size() == 1) {
57
2
    auto PackagePath = Cand.Path;
58
2
    llvm::sys::path::append(PackagePath, SubDirs[0]);
59
2
    return PackagePath;
60
2
  }
61
9
  if (SubDirs.size() == 0 && 
Verbose3
) {
62
2
    llvm::errs() << "SPACK package " << Prefix << " not found at " << Cand.Path
63
2
                 << '\n';
64
2
    return {};
65
2
  }
66
67
7
  if (SubDirs.size() > 1 && 
Verbose2
) {
68
2
    llvm::errs() << "Cannot use SPACK package " << Prefix << " at " << Cand.Path
69
2
                 << " due to multiple installations for the same version\n";
70
2
  }
71
7
  return {};
72
9
}
73
74
655
void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
75
655
  assert(!Path.empty());
76
77
0
  const StringRef Suffix(".bc");
78
655
  const StringRef Suffix2(".amdgcn.bc");
79
80
655
  std::error_code EC;
81
655
  for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Path, EC), LE;
82
215k
       !EC && LI != LE; 
LI = LI.increment(EC)215k
) {
83
215k
    StringRef FilePath = LI->path();
84
215k
    StringRef FileName = llvm::sys::path::filename(FilePath);
85
215k
    if (!FileName.endswith(Suffix))
86
213k
      continue;
87
88
1.44k
    StringRef BaseName;
89
1.44k
    if (FileName.endswith(Suffix2))
90
0
      BaseName = FileName.drop_back(Suffix2.size());
91
1.44k
    else if (FileName.endswith(Suffix))
92
1.44k
      BaseName = FileName.drop_back(Suffix.size());
93
94
1.44k
    if (BaseName == "ocml") {
95
68
      OCML = FilePath;
96
1.37k
    } else if (BaseName == "ockl") {
97
68
      OCKL = FilePath;
98
1.30k
    } else if (BaseName == "opencl") {
99
68
      OpenCL = FilePath;
100
1.23k
    } else if (BaseName == "hip") {
101
68
      HIP = FilePath;
102
1.16k
    } else if (BaseName == "asanrtl") {
103
67
      AsanRTL = FilePath;
104
1.10k
    } else if (BaseName == "oclc_finite_only_off") {
105
68
      FiniteOnly.Off = FilePath;
106
1.03k
    } else if (BaseName == "oclc_finite_only_on") {
107
68
      FiniteOnly.On = FilePath;
108
966
    } else if (BaseName == "oclc_daz_opt_on") {
109
68
      DenormalsAreZero.On = FilePath;
110
898
    } else if (BaseName == "oclc_daz_opt_off") {
111
68
      DenormalsAreZero.Off = FilePath;
112
830
    } else if (BaseName == "oclc_correctly_rounded_sqrt_on") {
113
68
      CorrectlyRoundedSqrt.On = FilePath;
114
762
    } else if (BaseName == "oclc_correctly_rounded_sqrt_off") {
115
68
      CorrectlyRoundedSqrt.Off = FilePath;
116
694
    } else if (BaseName == "oclc_unsafe_math_on") {
117
68
      UnsafeMath.On = FilePath;
118
626
    } else if (BaseName == "oclc_unsafe_math_off") {
119
68
      UnsafeMath.Off = FilePath;
120
558
    } else if (BaseName == "oclc_wavefrontsize64_on") {
121
68
      WavefrontSize64.On = FilePath;
122
490
    } else if (BaseName == "oclc_wavefrontsize64_off") {
123
68
      WavefrontSize64.Off = FilePath;
124
422
    } else {
125
      // Process all bitcode filenames that look like
126
      // ocl_isa_version_XXX.amdgcn.bc
127
422
      const StringRef DeviceLibPrefix = "oclc_isa_version_";
128
422
      if (!BaseName.startswith(DeviceLibPrefix))
129
14
        continue;
130
131
408
      StringRef IsaVersionNumber =
132
408
        BaseName.drop_front(DeviceLibPrefix.size());
133
134
408
      llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
135
408
      SmallString<8> Tmp;
136
408
      LibDeviceMap.insert(
137
408
        std::make_pair(GfxName.toStringRef(Tmp), FilePath.str()));
138
408
    }
139
1.44k
  }
140
655
}
141
142
// Parse and extract version numbers from `.hipVersion`. Return `true` if
143
// the parsing fails.
144
107
bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) {
145
107
  SmallVector<StringRef, 4> VersionParts;
146
107
  V.split(VersionParts, '\n');
147
107
  unsigned Major = ~0U;
148
107
  unsigned Minor = ~0U;
149
745
  for (auto Part : VersionParts) {
150
745
    auto Splits = Part.rtrim().split('=');
151
745
    if (Splits.first == "HIP_VERSION_MAJOR") {
152
107
      if (Splits.second.getAsInteger(0, Major))
153
0
        return true;
154
638
    } else if (Splits.first == "HIP_VERSION_MINOR") {
155
107
      if (Splits.second.getAsInteger(0, Minor))
156
0
        return true;
157
531
    } else if (Splits.first == "HIP_VERSION_PATCH")
158
107
      VersionPatch = Splits.second.str();
159
745
  }
160
107
  if (Major == ~0U || Minor == ~0U)
161
0
    return true;
162
107
  VersionMajorMinor = llvm::VersionTuple(Major, Minor);
163
107
  DetectedVersion =
164
107
      (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
165
107
  return false;
166
107
}
167
168
/// \returns a list of candidate directories for ROCm installation, which is
169
/// cached and populated only once.
170
const SmallVectorImpl<RocmInstallationDetector::Candidate> &
171
46.6k
RocmInstallationDetector::getInstallationPathCandidates() {
172
173
  // Return the cached candidate list if it has already been populated.
174
46.6k
  if (!ROCmSearchDirs.empty())
175
352
    return ROCmSearchDirs;
176
177
46.3k
  auto DoPrintROCmSearchDirs = [&]() {
178
46.3k
    if (PrintROCmSearchDirs)
179
24
      
for (auto Cand : ROCmSearchDirs)6
{
180
24
        llvm::errs() << "ROCm installation search path";
181
24
        if (Cand.isSPACK())
182
2
          llvm::errs() << " (Spack " << Cand.SPACKReleaseStr << ")";
183
24
        llvm::errs() << ": " << Cand.Path << '\n';
184
24
      }
185
46.3k
  };
186
187
  // For candidate specified by --rocm-path we do not do strict check, i.e.,
188
  // checking existence of HIP version file and device library files.
189
46.3k
  if (!RocmPathArg.empty()) {
190
110
    ROCmSearchDirs.emplace_back(RocmPathArg.str());
191
110
    DoPrintROCmSearchDirs();
192
110
    return ROCmSearchDirs;
193
46.2k
  } else if (const char *RocmPathEnv = ::getenv("ROCM_PATH")) {
194
2
    if (!StringRef(RocmPathEnv).empty()) {
195
2
      ROCmSearchDirs.emplace_back(RocmPathEnv);
196
2
      DoPrintROCmSearchDirs();
197
2
      return ROCmSearchDirs;
198
2
    }
199
2
  }
200
201
  // Try to find relative to the compiler binary.
202
46.2k
  const char *InstallDir = D.getInstalledDir();
203
204
  // Check both a normal Unix prefix position of the clang binary, as well as
205
  // the Windows-esque layout the ROCm packages use with the host architecture
206
  // subdirectory of bin.
207
47.2k
  auto DeduceROCmPath = [](StringRef ClangPath) {
208
    // Strip off directory (usually bin)
209
47.2k
    StringRef ParentDir = llvm::sys::path::parent_path(ClangPath);
210
47.2k
    StringRef ParentName = llvm::sys::path::filename(ParentDir);
211
212
    // Some builds use bin/{host arch}, so go up again.
213
47.2k
    if (ParentName == "bin") {
214
6
      ParentDir = llvm::sys::path::parent_path(ParentDir);
215
6
      ParentName = llvm::sys::path::filename(ParentDir);
216
6
    }
217
218
    // Detect ROCm packages built with SPACK.
219
    // clang is installed at
220
    // <rocm_root>/llvm-amdgpu-<rocm_release_string>-<hash>/bin directory.
221
    // We only consider the parent directory of llvm-amdgpu package as ROCm
222
    // installation candidate for SPACK.
223
47.2k
    if (ParentName.startswith("llvm-amdgpu-")) {
224
8
      auto SPACKPostfix =
225
8
          ParentName.drop_front(strlen("llvm-amdgpu-")).split('-');
226
8
      auto SPACKReleaseStr = SPACKPostfix.first;
227
8
      if (!SPACKReleaseStr.empty()) {
228
8
        ParentDir = llvm::sys::path::parent_path(ParentDir);
229
8
        return Candidate(ParentDir.str(), /*StrictChecking=*/true,
230
8
                         SPACKReleaseStr);
231
8
      }
232
8
    }
233
234
    // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin
235
    // Some versions of the aomp package install to /opt/rocm/aomp/bin
236
47.2k
    if (ParentName == "llvm" || 
ParentName.startswith("aomp")47.2k
)
237
0
      ParentDir = llvm::sys::path::parent_path(ParentDir);
238
239
47.2k
    return Candidate(ParentDir.str(), /*StrictChecking=*/true);
240
47.2k
  };
241
242
  // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic
243
  // link of clang itself.
244
46.2k
  ROCmSearchDirs.emplace_back(DeduceROCmPath(InstallDir));
245
246
  // Deduce ROCm path by the real path of the invoked clang, resolving symbolic
247
  // link of clang itself.
248
46.2k
  llvm::SmallString<256> RealClangPath;
249
46.2k
  llvm::sys::fs::real_path(D.getClangProgramPath(), RealClangPath);
250
46.2k
  auto ParentPath = llvm::sys::path::parent_path(RealClangPath);
251
46.2k
  if (ParentPath != InstallDir)
252
1.03k
    ROCmSearchDirs.emplace_back(DeduceROCmPath(ParentPath));
253
254
  // Device library may be installed in clang or resource directory.
255
46.2k
  auto ClangRoot = llvm::sys::path::parent_path(InstallDir);
256
46.2k
  auto RealClangRoot = llvm::sys::path::parent_path(ParentPath);
257
46.2k
  ROCmSearchDirs.emplace_back(ClangRoot.str(), /*StrictChecking=*/true);
258
46.2k
  if (RealClangRoot != ClangRoot)
259
1.03k
    ROCmSearchDirs.emplace_back(RealClangRoot.str(), /*StrictChecking=*/true);
260
46.2k
  ROCmSearchDirs.emplace_back(D.ResourceDir,
261
46.2k
                              /*StrictChecking=*/true);
262
263
46.2k
  ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/rocm",
264
46.2k
                              /*StrictChecking=*/true);
265
266
  // Find the latest /opt/rocm-{release} directory.
267
46.2k
  std::error_code EC;
268
46.2k
  std::string LatestROCm;
269
46.2k
  llvm::VersionTuple LatestVer;
270
  // Get ROCm version from ROCm directory name.
271
46.2k
  auto GetROCmVersion = [](StringRef DirName) {
272
4
    llvm::VersionTuple V;
273
4
    std::string VerStr = DirName.drop_front(strlen("rocm-")).str();
274
    // The ROCm directory name follows the format of
275
    // rocm-{major}.{minor}.{subMinor}[-{build}]
276
4
    std::replace(VerStr.begin(), VerStr.end(), '-', '.');
277
4
    V.tryParse(VerStr);
278
4
    return V;
279
4
  };
280
46.2k
  for (llvm::vfs::directory_iterator
281
46.2k
           File = D.getVFS().dir_begin(D.SysRoot + "/opt", EC),
282
46.2k
           FileEnd;
283
132k
       File != FileEnd && 
!EC86.6k
;
File.increment(EC)86.6k
) {
284
86.6k
    llvm::StringRef FileName = llvm::sys::path::filename(File->path());
285
86.6k
    if (!FileName.startswith("rocm-"))
286
86.6k
      continue;
287
8
    if (LatestROCm.empty()) {
288
2
      LatestROCm = FileName.str();
289
2
      LatestVer = GetROCmVersion(LatestROCm);
290
2
      continue;
291
2
    }
292
6
    auto Ver = GetROCmVersion(FileName);
293
6
    if (LatestVer < Ver) {
294
2
      LatestROCm = FileName.str();
295
2
      LatestVer = Ver;
296
2
    }
297
6
  }
298
46.2k
  if (!LatestROCm.empty())
299
2
    ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
300
2
                                /*StrictChecking=*/true);
301
302
46.2k
  DoPrintROCmSearchDirs();
303
46.2k
  return ROCmSearchDirs;
304
46.3k
}
305
306
RocmInstallationDetector::RocmInstallationDetector(
307
    const Driver &D, const llvm::Triple &HostTriple,
308
    const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib)
309
46.3k
    : D(D) {
310
46.3k
  Verbose = Args.hasArg(options::OPT_v);
311
46.3k
  RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ);
312
46.3k
  PrintROCmSearchDirs =
313
46.3k
      Args.hasArg(clang::driver::options::OPT_print_rocm_search_dirs);
314
46.3k
  RocmDeviceLibPathArg =
315
46.3k
      Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ);
316
46.3k
  HIPPathArg = Args.getLastArgValue(clang::driver::options::OPT_hip_path_EQ);
317
46.3k
  if (auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) {
318
6
    HIPVersionArg = A->getValue();
319
6
    unsigned Major = ~0U;
320
6
    unsigned Minor = ~0U;
321
6
    SmallVector<StringRef, 3> Parts;
322
6
    HIPVersionArg.split(Parts, '.');
323
6
    if (Parts.size())
324
6
      Parts[0].getAsInteger(0, Major);
325
6
    if (Parts.size() > 1)
326
5
      Parts[1].getAsInteger(0, Minor);
327
6
    if (Parts.size() > 2)
328
3
      VersionPatch = Parts[2].str();
329
6
    if (VersionPatch.empty())
330
3
      VersionPatch = "0";
331
6
    if (Major != ~0U && 
Minor == ~0U5
)
332
1
      Minor = 0;
333
6
    if (Major == ~0U || 
Minor == ~0U5
)
334
1
      D.Diag(diag::err_drv_invalid_value)
335
1
          << A->getAsString(Args) << HIPVersionArg;
336
337
6
    VersionMajorMinor = llvm::VersionTuple(Major, Minor);
338
6
    DetectedVersion =
339
6
        (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
340
46.3k
  } else {
341
46.3k
    VersionPatch = DefaultVersionPatch;
342
46.3k
    VersionMajorMinor =
343
46.3k
        llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
344
46.3k
    DetectedVersion = (Twine(DefaultVersionMajor) + "." +
345
46.3k
                       Twine(DefaultVersionMinor) + "." + VersionPatch)
346
46.3k
                          .str();
347
46.3k
  }
348
349
46.3k
  if (DetectHIPRuntime)
350
46.3k
    detectHIPRuntime();
351
46.3k
  if (DetectDeviceLib)
352
0
    detectDeviceLibrary();
353
46.3k
}
354
355
372
void RocmInstallationDetector::detectDeviceLibrary() {
356
372
  assert(LibDevicePath.empty());
357
358
372
  if (!RocmDeviceLibPathArg.empty())
359
17
    LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
360
355
  else if (const char *LibPathEnv = ::getenv("HIP_DEVICE_LIB_PATH"))
361
2
    LibDevicePath = LibPathEnv;
362
363
372
  auto &FS = D.getVFS();
364
372
  if (!LibDevicePath.empty()) {
365
    // Maintain compatability with HIP flag/envvar pointing directly at the
366
    // bitcode library directory. This points directly at the library path instead
367
    // of the rocm root installation.
368
19
    if (!FS.exists(LibDevicePath))
369
0
      return;
370
371
19
    scanLibDevicePath(LibDevicePath);
372
19
    HasDeviceLibrary = allGenericLibsValid() && 
!LibDeviceMap.empty()5
;
373
19
    return;
374
19
  }
375
376
  // The install path situation in old versions of ROCm is a real mess, and
377
  // use a different install layout. Multiple copies of the device libraries
378
  // exist for each frontend project, and differ depending on which build
379
  // system produced the packages. Standalone OpenCL builds also have a
380
  // different directory structure from the ROCm OpenCL package.
381
353
  auto &ROCmDirs = getInstallationPathCandidates();
382
1.22k
  for (const auto &Candidate : ROCmDirs) {
383
1.22k
    auto CandidatePath = Candidate.Path;
384
385
    // Check device library exists at the given path.
386
3.55k
    auto CheckDeviceLib = [&](StringRef Path) {
387
3.55k
      bool CheckLibDevice = (!NoBuiltinLibs || 
Candidate.StrictChecking0
);
388
3.55k
      if (CheckLibDevice && !FS.exists(Path))
389
2.91k
        return false;
390
391
636
      scanLibDevicePath(Path);
392
393
636
      if (!NoBuiltinLibs) {
394
        // Check that the required non-target libraries are all available.
395
636
        if (!allGenericLibsValid())
396
573
          return false;
397
398
        // Check that we have found at least one libdevice that we can link in
399
        // if -nobuiltinlib hasn't been specified.
400
63
        if (LibDeviceMap.empty())
401
0
          return false;
402
63
      }
403
63
      return true;
404
636
    };
405
406
    // The possible structures are:
407
    // - ${ROCM_ROOT}/amdgcn/bitcode/*
408
    // - ${ROCM_ROOT}/lib/*
409
    // - ${ROCM_ROOT}/lib/bitcode/*
410
    // so try to detect these layouts.
411
1.22k
    static constexpr std::array<const char *, 2> SubDirsList[] = {
412
1.22k
        {"amdgcn", "bitcode"},
413
1.22k
        {"lib", ""},
414
1.22k
        {"lib", "bitcode"},
415
1.22k
    };
416
417
    // Make a path by appending sub-directories to InstallPath.
418
3.55k
    auto MakePath = [&](const llvm::ArrayRef<const char *> &SubDirs) {
419
3.55k
      auto Path = CandidatePath;
420
3.55k
      for (auto SubDir : SubDirs)
421
7.10k
        llvm::sys::path::append(Path, SubDir);
422
3.55k
      return Path;
423
3.55k
    };
424
425
3.55k
    for (auto SubDirs : SubDirsList) {
426
3.55k
      LibDevicePath = MakePath(SubDirs);
427
3.55k
      HasDeviceLibrary = CheckDeviceLib(LibDevicePath);
428
3.55k
      if (HasDeviceLibrary)
429
63
        return;
430
3.55k
    }
431
1.22k
  }
432
353
}
433
434
46.3k
void RocmInstallationDetector::detectHIPRuntime() {
435
46.3k
  SmallVector<Candidate, 4> HIPSearchDirs;
436
46.3k
  if (!HIPPathArg.empty())
437
2
    HIPSearchDirs.emplace_back(HIPPathArg.str(), /*StrictChecking=*/true);
438
46.3k
  else
439
46.3k
    HIPSearchDirs.append(getInstallationPathCandidates());
440
46.3k
  auto &FS = D.getVFS();
441
442
187k
  for (const auto &Candidate : HIPSearchDirs) {
443
187k
    InstallPath = Candidate.Path;
444
187k
    if (InstallPath.empty() || 
!FS.exists(InstallPath)130k
)
445
128k
      continue;
446
    // HIP runtime built by SPACK is installed to
447
    // <rocm_root>/hip-<rocm_release_string>-<hash> directory.
448
58.1k
    auto SPACKPath = findSPACKPackage(Candidate, "hip");
449
18.4E
    InstallPath = 
SPACKPath.empty()58.1k
?
InstallPath58.1k
: SPACKPath;
450
451
58.1k
    BinPath = InstallPath;
452
58.1k
    llvm::sys::path::append(BinPath, "bin");
453
58.1k
    IncludePath = InstallPath;
454
58.1k
    llvm::sys::path::append(IncludePath, "include");
455
58.1k
    LibPath = InstallPath;
456
58.1k
    llvm::sys::path::append(LibPath, "lib");
457
458
58.1k
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
459
58.1k
        FS.getBufferForFile(BinPath + "/.hipVersion");
460
58.1k
    if (!VersionFile && 
Candidate.StrictChecking58.0k
)
461
58.0k
      continue;
462
463
109
    
if (98
HIPVersionArg.empty()98
&& VersionFile)
464
107
      if (parseHIPVersionFile((*VersionFile)->getBuffer()))
465
0
        continue;
466
467
98
    HasHIPRuntime = true;
468
98
    return;
469
98
  }
470
46.2k
  HasHIPRuntime = false;
471
46.2k
}
472
473
208
void RocmInstallationDetector::print(raw_ostream &OS) const {
474
208
  if (hasHIPRuntime())
475
17
    OS << "Found HIP installation: " << InstallPath << ", version "
476
17
       << DetectedVersion << '\n';
477
208
}
478
479
void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
480
457
                                                 ArgStringList &CC1Args) const {
481
457
  bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5);
482
483
457
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
484
    // HIP header includes standard library wrapper headers under clang
485
    // cuda_wrappers directory. Since these wrapper headers include_next
486
    // standard C++ headers, whereas libc++ headers include_next other clang
487
    // headers. The include paths have to follow this order:
488
    // - wrapper include path
489
    // - standard C++ include path
490
    // - other clang include path
491
    // Since standard C++ and other clang include paths are added in other
492
    // places after this function, here we only need to make sure wrapper
493
    // include path is added.
494
    //
495
    // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
496
    // a workaround.
497
455
    SmallString<128> P(D.ResourceDir);
498
455
    if (UsesRuntimeWrapper)
499
77
      llvm::sys::path::append(P, "include", "cuda_wrappers");
500
455
    CC1Args.push_back("-internal-isystem");
501
455
    CC1Args.push_back(DriverArgs.MakeArgString(P));
502
455
  }
503
504
457
  if (DriverArgs.hasArg(options::OPT_nogpuinc))
505
161
    return;
506
507
296
  if (!hasHIPRuntime()) {
508
231
    D.Diag(diag::err_drv_no_hip_runtime);
509
231
    return;
510
231
  }
511
512
65
  CC1Args.push_back("-internal-isystem");
513
65
  CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
514
65
  if (UsesRuntimeWrapper)
515
65
    CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"});
516
65
}
517
518
void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
519
                                  const InputInfo &Output,
520
                                  const InputInfoList &Inputs,
521
                                  const ArgList &Args,
522
69
                                  const char *LinkingOutput) const {
523
524
69
  std::string Linker = getToolChain().GetProgramPath(getShortName());
525
69
  ArgStringList CmdArgs;
526
69
  addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
527
69
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
528
69
  CmdArgs.push_back("-shared");
529
69
  CmdArgs.push_back("-o");
530
69
  CmdArgs.push_back(Output.getFilename());
531
69
  C.addCommand(std::make_unique<Command>(
532
69
      JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
533
69
      CmdArgs, Inputs, Output));
534
69
}
535
536
void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
537
                                     const llvm::Triple &Triple,
538
                                     const llvm::opt::ArgList &Args,
539
828
                                     std::vector<StringRef> &Features) {
540
  // Add target ID features to -target-feature options. No diagnostics should
541
  // be emitted here since invalid target ID is diagnosed at other places.
542
828
  StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
543
828
  if (!TargetID.empty()) {
544
782
    llvm::StringMap<bool> FeatureMap;
545
782
    auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap);
546
782
    if (OptionalGpuArch) {
547
766
      StringRef GpuArch = OptionalGpuArch.getValue();
548
      // Iterate through all possible target ID features for the given GPU.
549
      // If it is mapped to true, add +feature.
550
      // If it is mapped to false, add -feature.
551
      // If it is not in the map (default), do not add it
552
766
      for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) {
553
599
        auto Pos = FeatureMap.find(Feature);
554
599
        if (Pos == FeatureMap.end())
555
504
          continue;
556
95
        Features.push_back(Args.MakeArgStringRef(
557
95
            (Twine(Pos->second ? 
"+"59
:
"-"36
) + Feature).str()));
558
95
      }
559
766
    }
560
782
  }
561
562
828
  if (Args.hasFlag(options::OPT_mwavefrontsize64,
563
828
                   options::OPT_mno_wavefrontsize64, false))
564
13
    Features.push_back("+wavefrontsize64");
565
566
828
  handleTargetFeaturesGroup(
567
828
    Args, Features, options::OPT_m_amdgpu_Features_Group);
568
828
}
569
570
/// AMDGPU Toolchain
571
AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
572
                                 const ArgList &Args)
573
    : Generic_ELF(D, Triple, Args),
574
      OptionsDefault(
575
395
          {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
576
  // Check code object version options. Emit warnings for legacy options
577
  // and errors for the last invalid code object version options.
578
  // It is done here to avoid repeated warning or error messages for
579
  // each tool invocation.
580
395
  checkAMDGPUCodeObjectVersion(D, Args);
581
395
}
582
583
69
Tool *AMDGPUToolChain::buildLinker() const {
584
69
  return new tools::amdgpu::Linker(*this);
585
69
}
586
587
DerivedArgList *
588
AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
589
115
                               Action::OffloadKind DeviceOffloadKind) const {
590
591
115
  DerivedArgList *DAL =
592
115
      Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
593
594
115
  const OptTable &Opts = getDriver().getOpts();
595
596
115
  if (!DAL)
597
115
    DAL = new DerivedArgList(Args.getBaseArgs());
598
599
792
  for (Arg *A : Args) {
600
792
    if (!shouldSkipArgument(A))
601
792
      DAL->append(A);
602
792
  }
603
604
115
  checkTargetID(*DAL);
605
606
115
  if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
607
81
    return DAL;
608
609
  // Phase 1 (.cl -> .bc)
610
34
  if (Args.hasArg(options::OPT_c) && 
Args.hasArg(options::OPT_emit_llvm)17
) {
611
17
    DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit()
612
17
                                                ? options::OPT_m64
613
17
                                                : 
options::OPT_m320
));
614
615
    // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
616
    // as they defined that way in Options.td
617
17
    if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4,
618
17
                     options::OPT_Ofast))
619
9
      DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O),
620
9
                        getOptionDefault(options::OPT_O));
621
17
  }
622
623
34
  return DAL;
624
115
}
625
626
bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
627
474
    llvm::AMDGPU::GPUKind Kind) {
628
629
  // Assume nothing without a specific target.
630
474
  if (Kind == llvm::AMDGPU::GK_NONE)
631
41
    return false;
632
633
433
  const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
634
635
  // Default to enabling f32 denormals by default on subtargets where fma is
636
  // fast with denormals
637
433
  const bool BothDenormAndFMAFast =
638
433
      (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
639
433
      
(ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32)290
;
640
433
  return !BothDenormAndFMAFast;
641
474
}
642
643
llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
644
    const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
645
832
    const llvm::fltSemantics *FPType) const {
646
  // Denormals should always be enabled for f16 and f64.
647
832
  if (!FPType || 
FPType != &llvm::APFloat::IEEEsingle()416
)
648
416
    return llvm::DenormalMode::getIEEE();
649
650
416
  if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
651
416
      
JA.getOffloadingDeviceKind() == Action::OFK_Cuda115
) {
652
301
    auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch());
653
301
    auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch);
654
301
    if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
655
301
        DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
656
301
                           options::OPT_fno_gpu_flush_denormals_to_zero,
657
301
                           getDefaultDenormsAreZeroForTarget(Kind)))
658
93
      return llvm::DenormalMode::getPreserveSign();
659
660
208
    return llvm::DenormalMode::getIEEE();
661
301
  }
662
663
115
  const StringRef GpuArch = getGPUArch(DriverArgs);
664
115
  auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
665
666
  // TODO: There are way too many flags that change this. Do we need to check
667
  // them all?
668
115
  bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
669
115
             
getDefaultDenormsAreZeroForTarget(Kind)108
;
670
671
  // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
672
  // also implicit treated as zero (DAZ).
673
115
  return DAZ ? 
llvm::DenormalMode::getPreserveSign()43
:
674
115
               
llvm::DenormalMode::getIEEE()72
;
675
416
}
676
677
bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
678
67
                               llvm::AMDGPU::GPUKind Kind) {
679
67
  const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
680
67
  bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
681
682
67
  return !HasWave32 || DriverArgs.hasFlag(
683
7
    options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false);
684
67
}
685
686
687
/// ROCM Toolchain
688
ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
689
                             const ArgList &Args)
690
372
    : AMDGPUToolChain(D, Triple, Args) {
691
372
  RocmInstallation.detectDeviceLibrary();
692
372
}
693
694
void AMDGPUToolChain::addClangTargetOptions(
695
    const llvm::opt::ArgList &DriverArgs,
696
    llvm::opt::ArgStringList &CC1Args,
697
100
    Action::OffloadKind DeviceOffloadingKind) const {
698
  // Default to "hidden" visibility, as object level linking will not be
699
  // supported for the foreseeable future.
700
100
  if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
701
100
                         options::OPT_fvisibility_ms_compat)) {
702
96
    CC1Args.push_back("-fvisibility");
703
96
    CC1Args.push_back("hidden");
704
96
    CC1Args.push_back("-fapply-global-visibility-to-externs");
705
96
  }
706
100
}
707
708
StringRef
709
187
AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
710
187
  return getProcessorFromTargetID(
711
187
      getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ));
712
187
}
713
714
AMDGPUToolChain::ParsedTargetIDType
715
399
AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
716
399
  StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
717
399
  if (TargetID.empty())
718
28
    return {None, None, None};
719
720
371
  llvm::StringMap<bool> FeatureMap;
721
371
  auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap);
722
371
  if (!OptionalGpuArch)
723
14
    return {TargetID.str(), None, None};
724
725
357
  return {TargetID.str(), OptionalGpuArch.getValue().str(), FeatureMap};
726
371
}
727
728
void AMDGPUToolChain::checkTargetID(
729
115
    const llvm::opt::ArgList &DriverArgs) const {
730
115
  auto PTID = getParsedTargetID(DriverArgs);
731
115
  if (PTID.OptionalTargetID && 
!PTID.OptionalGPUArch87
) {
732
14
    getDriver().Diag(clang::diag::err_drv_bad_target_id)
733
14
        << PTID.OptionalTargetID.getValue();
734
14
  }
735
115
}
736
737
llvm::Error
738
AMDGPUToolChain::detectSystemGPUs(const ArgList &Args,
739
0
                                  SmallVector<std::string, 1> &GPUArchs) const {
740
0
  std::string Program;
741
0
  if (Arg *A = Args.getLastArg(options::OPT_amdgpu_arch_tool_EQ))
742
0
    Program = A->getValue();
743
0
  else
744
0
    Program = GetProgramPath(AMDGPU_ARCH_PROGRAM_NAME);
745
0
  llvm::SmallString<64> OutputFile;
746
0
  llvm::sys::fs::createTemporaryFile("print-system-gpus", "" /* No Suffix */,
747
0
                                     OutputFile);
748
0
  llvm::FileRemover OutputRemover(OutputFile.c_str());
749
0
  llvm::Optional<llvm::StringRef> Redirects[] = {
750
0
      {""},
751
0
      OutputFile.str(),
752
0
      {""},
753
0
  };
754
755
0
  std::string ErrorMessage;
756
0
  if (int Result = llvm::sys::ExecuteAndWait(
757
0
          Program.c_str(), {}, {}, Redirects, /* SecondsToWait */ 0,
758
0
          /*MemoryLimit*/ 0, &ErrorMessage)) {
759
0
    if (Result > 0) {
760
0
      ErrorMessage = "Exited with error code " + std::to_string(Result);
761
0
    } else if (Result == -1) {
762
0
      ErrorMessage = "Execute failed: " + ErrorMessage;
763
0
    } else {
764
0
      ErrorMessage = "Crashed: " + ErrorMessage;
765
0
    }
766
767
0
    return llvm::createStringError(std::error_code(),
768
0
                                   Program + ": " + ErrorMessage);
769
0
  }
770
771
0
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
772
0
      llvm::MemoryBuffer::getFile(OutputFile.c_str());
773
0
  if (!OutputBuf) {
774
0
    return llvm::createStringError(OutputBuf.getError(),
775
0
                                   "Failed to read stdout of " + Program +
776
0
                                       ": " + OutputBuf.getError().message());
777
0
  }
778
779
0
  for (llvm::line_iterator LineIt(**OutputBuf); !LineIt.is_at_end(); ++LineIt) {
780
0
    GPUArchs.push_back(LineIt->str());
781
0
  }
782
0
  return llvm::Error::success();
783
0
}
784
785
llvm::Error AMDGPUToolChain::getSystemGPUArch(const ArgList &Args,
786
0
                                              std::string &GPUArch) const {
787
  // detect the AMDGPU installed in system
788
0
  SmallVector<std::string, 1> GPUArchs;
789
0
  auto Err = detectSystemGPUs(Args, GPUArchs);
790
0
  if (Err) {
791
0
    return Err;
792
0
  }
793
0
  if (GPUArchs.empty()) {
794
0
    return llvm::createStringError(std::error_code(),
795
0
                                   "No AMD GPU detected in the system");
796
0
  }
797
0
  GPUArch = GPUArchs[0];
798
0
  if (GPUArchs.size() > 1) {
799
0
    bool AllSame = std::all_of(
800
0
        GPUArchs.begin(), GPUArchs.end(),
801
0
        [&](const StringRef &GPUArch) { return GPUArch == GPUArchs.front(); });
802
0
    if (!AllSame)
803
0
      return llvm::createStringError(
804
0
          std::error_code(), "Multiple AMD GPUs found with different archs");
805
0
  }
806
0
  return llvm::Error::success();
807
0
}
808
809
void ROCMToolChain::addClangTargetOptions(
810
    const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
811
81
    Action::OffloadKind DeviceOffloadingKind) const {
812
81
  AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
813
81
                                         DeviceOffloadingKind);
814
815
  // For the OpenCL case where there is no offload target, accept -nostdlib to
816
  // disable bitcode linking.
817
81
  if (DeviceOffloadingKind == Action::OFK_None &&
818
81
      DriverArgs.hasArg(options::OPT_nostdlib))
819
8
    return;
820
821
73
  if (DriverArgs.hasArg(options::OPT_nogpulib))
822
16
    return;
823
824
57
  if (!RocmInstallation.hasDeviceLibrary()) {
825
39
    getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
826
39
    return;
827
39
  }
828
829
  // Get the device name and canonicalize it
830
18
  const StringRef GpuArch = getGPUArch(DriverArgs);
831
18
  auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
832
18
  const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
833
18
  std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
834
18
  if (LibDeviceFile.empty()) {
835
1
    getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
836
1
    return;
837
1
  }
838
839
17
  bool Wave64 = isWave64(DriverArgs, Kind);
840
841
  // TODO: There are way too many flags that change this. Do we need to check
842
  // them all?
843
17
  bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
844
17
             
getDefaultDenormsAreZeroForTarget(Kind)15
;
845
17
  bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only);
846
847
17
  bool UnsafeMathOpt =
848
17
      DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations);
849
17
  bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math);
850
17
  bool CorrectSqrt =
851
17
      DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
852
853
  // Add the OpenCL specific bitcode library.
854
17
  llvm::SmallVector<std::string, 12> BCLibs;
855
17
  BCLibs.push_back(RocmInstallation.getOpenCLPath().str());
856
857
  // Add the generic set of libraries.
858
17
  BCLibs.append(RocmInstallation.getCommonBitcodeLibs(
859
17
      DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
860
17
      FastRelaxedMath, CorrectSqrt));
861
862
153
  llvm::for_each(BCLibs, [&](StringRef BCFile) {
863
153
    CC1Args.push_back("-mlink-builtin-bitcode");
864
153
    CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
865
153
  });
866
17
}
867
868
llvm::SmallVector<std::string, 12>
869
RocmInstallationDetector::getCommonBitcodeLibs(
870
    const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
871
    bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
872
67
    bool CorrectSqrt) const {
873
874
67
  llvm::SmallVector<std::string, 12> BCLibs;
875
876
536
  auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); };
877
878
67
  AddBCLib(getOCMLPath());
879
67
  AddBCLib(getOCKLPath());
880
67
  AddBCLib(getDenormalsAreZeroPath(DAZ));
881
67
  AddBCLib(getUnsafeMathPath(UnsafeMathOpt || 
FastRelaxedMath65
));
882
67
  AddBCLib(getFiniteOnlyPath(FiniteOnly || 
FastRelaxedMath65
));
883
67
  AddBCLib(getCorrectlyRoundedSqrtPath(CorrectSqrt));
884
67
  AddBCLib(getWavefrontSize64Path(Wave64));
885
67
  AddBCLib(LibDeviceFile);
886
887
67
  return BCLibs;
888
67
}
889
890
6.56k
bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
891
6.56k
  Option O = A->getOption();
892
6.56k
  if (O.matches(options::OPT_fPIE) || 
O.matches(options::OPT_fpie)6.56k
)
893
6
    return true;
894
6.56k
  return false;
895
6.56k
}
896
897
llvm::SmallVector<std::string, 12>
898
ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
899
53
                                       const std::string &GPUArch) const {
900
53
  auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
901
53
  const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
902
903
53
  std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
904
53
  if (LibDeviceFile.empty()) {
905
3
    getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
906
3
    return {};
907
3
  }
908
909
  // If --hip-device-lib is not set, add the default bitcode libraries.
910
  // TODO: There are way too many flags that change this. Do we need to check
911
  // them all?
912
50
  bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
913
50
                                options::OPT_fno_gpu_flush_denormals_to_zero,
914
50
                                getDefaultDenormsAreZeroForTarget(Kind));
915
50
  bool FiniteOnly = DriverArgs.hasFlag(
916
50
      options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only, false);
917
50
  bool UnsafeMathOpt =
918
50
      DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
919
50
                         options::OPT_fno_unsafe_math_optimizations, false);
920
50
  bool FastRelaxedMath = DriverArgs.hasFlag(options::OPT_ffast_math,
921
50
                                            options::OPT_fno_fast_math, false);
922
50
  bool CorrectSqrt = DriverArgs.hasFlag(
923
50
      options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
924
50
      options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
925
50
  bool Wave64 = isWave64(DriverArgs, Kind);
926
927
50
  return RocmInstallation.getCommonBitcodeLibs(
928
50
      DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
929
50
      FastRelaxedMath, CorrectSqrt);
930
53
}