Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/ROCm.h
Line
Count
Source (jump to first uncovered line)
1
//===--- ROCm.h - ROCm installation detector --------------------*- 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
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
10
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
11
12
#include "clang/Basic/Cuda.h"
13
#include "clang/Basic/LLVM.h"
14
#include "clang/Driver/Driver.h"
15
#include "clang/Driver/Options.h"
16
#include "llvm/ADT/SmallString.h"
17
#include "llvm/ADT/StringMap.h"
18
#include "llvm/ADT/Triple.h"
19
#include "llvm/Option/ArgList.h"
20
#include "llvm/Support/VersionTuple.h"
21
22
namespace clang {
23
namespace driver {
24
25
/// ABI version of device library.
26
struct DeviceLibABIVersion {
27
  unsigned ABIVersion = 0;
28
128
  DeviceLibABIVersion(unsigned V) : ABIVersion(V) {}
29
128
  static DeviceLibABIVersion fromCodeObjectVersion(unsigned CodeObjectVersion) {
30
128
    if (CodeObjectVersion < 4)
31
4
      CodeObjectVersion = 4;
32
128
    return DeviceLibABIVersion(CodeObjectVersion * 100);
33
128
  }
34
  /// Whether ABI version bc file is requested.
35
  /// ABIVersion is code object version multiplied by 100. Code object v4
36
  /// and below works with ROCm 5.0 and below which does not have
37
  /// abi_version_*.bc. Code object v5 requires abi_version_500.bc.
38
79
  bool requiresLibrary() { return ABIVersion >= 500; }
39
1
  std::string toString() {
40
1
    assert(ABIVersion % 100 == 0 && "Not supported");
41
0
    return Twine(ABIVersion / 100).str();
42
1
  }
43
};
44
45
/// A class to find a viable ROCM installation
46
/// TODO: Generalize to handle libclc.
47
class RocmInstallationDetector {
48
private:
49
  struct ConditionalLibrary {
50
    SmallString<0> On;
51
    SmallString<0> Off;
52
53
815
    bool isValid() const { return !On.empty() && !Off.empty(); }
54
55
390
    StringRef get(bool Enabled) const {
56
390
      assert(isValid());
57
390
      return Enabled ? 
On161
:
Off229
;
58
390
    }
59
  };
60
61
  // Installation path candidate.
62
  struct Candidate {
63
    llvm::SmallString<0> Path;
64
    bool StrictChecking;
65
    // Release string for ROCm packages built with SPACK if not empty. The
66
    // installation directories of ROCm packages built with SPACK follow the
67
    // convention <package_name>-<rocm_release_string>-<hash>.
68
    std::string SPACKReleaseStr;
69
70
60.3k
    bool isSPACK() const { return !SPACKReleaseStr.empty(); }
71
    Candidate(std::string Path, bool StrictChecking = false,
72
              StringRef SPACKReleaseStr = {})
73
        : Path(Path), StrictChecking(StrictChecking),
74
198k
          SPACKReleaseStr(SPACKReleaseStr.str()) {}
75
  };
76
77
  const Driver &D;
78
  bool HasHIPRuntime = false;
79
  bool HasDeviceLibrary = false;
80
81
  // Default version if not detected or specified.
82
  const unsigned DefaultVersionMajor = 3;
83
  const unsigned DefaultVersionMinor = 5;
84
  const char *DefaultVersionPatch = "0";
85
86
  // The version string in Major.Minor.Patch format.
87
  std::string DetectedVersion;
88
  // Version containing major and minor.
89
  llvm::VersionTuple VersionMajorMinor;
90
  // Version containing patch.
91
  std::string VersionPatch;
92
93
  // ROCm path specified by --rocm-path.
94
  StringRef RocmPathArg;
95
  // ROCm device library paths specified by --rocm-device-lib-path.
96
  std::vector<std::string> RocmDeviceLibPathArg;
97
  // HIP runtime path specified by --hip-path.
98
  StringRef HIPPathArg;
99
  // HIP version specified by --hip-version.
100
  StringRef HIPVersionArg;
101
  // Wheter -nogpulib is specified.
102
  bool NoBuiltinLibs = false;
103
104
  // Paths
105
  SmallString<0> InstallPath;
106
  SmallString<0> BinPath;
107
  SmallString<0> LibPath;
108
  SmallString<0> LibDevicePath;
109
  SmallString<0> IncludePath;
110
  llvm::StringMap<std::string> LibDeviceMap;
111
112
  // Libraries that are always linked.
113
  SmallString<0> OCML;
114
  SmallString<0> OCKL;
115
116
  // Libraries that are always linked depending on the language
117
  SmallString<0> OpenCL;
118
  SmallString<0> HIP;
119
120
  // Asan runtime library
121
  SmallString<0> AsanRTL;
122
123
  // Libraries swapped based on compile flags.
124
  ConditionalLibrary WavefrontSize64;
125
  ConditionalLibrary FiniteOnly;
126
  ConditionalLibrary UnsafeMath;
127
  ConditionalLibrary DenormalsAreZero;
128
  ConditionalLibrary CorrectlyRoundedSqrt;
129
130
  // Maps ABI version to library path. The version number is in the format of
131
  // three digits as used in the ABI version library name.
132
  std::map<unsigned, std::string> ABIVersionMap;
133
134
  // Cache ROCm installation search paths.
135
  SmallVector<Candidate, 4> ROCmSearchDirs;
136
  bool PrintROCmSearchDirs;
137
  bool Verbose;
138
139
701
  bool allGenericLibsValid() const {
140
701
    return !OCML.empty() && 
!OCKL.empty()85
&&
!OpenCL.empty()85
&&
!HIP.empty()85
&&
141
701
           
WavefrontSize64.isValid()85
&&
FiniteOnly.isValid()85
&&
142
701
           
UnsafeMath.isValid()85
&&
DenormalsAreZero.isValid()85
&&
143
701
           
CorrectlyRoundedSqrt.isValid()85
;
144
701
  }
145
146
  void scanLibDevicePath(llvm::StringRef Path);
147
  bool parseHIPVersionFile(llvm::StringRef V);
148
  const SmallVectorImpl<Candidate> &getInstallationPathCandidates();
149
150
  /// Find the path to a SPACK package under the ROCm candidate installation
151
  /// directory if the candidate is a SPACK ROCm candidate. \returns empty
152
  /// string if the candidate is not SPACK ROCm candidate or the requested
153
  /// package is not found.
154
  llvm::SmallString<0> findSPACKPackage(const Candidate &Cand,
155
                                        StringRef PackageName);
156
157
public:
158
  RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
159
                           const llvm::opt::ArgList &Args,
160
                           bool DetectHIPRuntime = true,
161
                           bool DetectDeviceLib = false);
162
163
  /// Get file paths of default bitcode libraries common to AMDGPU based
164
  /// toolchains.
165
  llvm::SmallVector<std::string, 12>
166
  getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
167
                       StringRef LibDeviceFile, bool Wave64, bool DAZ,
168
                       bool FiniteOnly, bool UnsafeMathOpt,
169
                       bool FastRelaxedMath, bool CorrectSqrt,
170
                       DeviceLibABIVersion ABIVer, bool isOpenMP) const;
171
  /// Check file paths of default bitcode libraries common to AMDGPU based
172
  /// toolchains. \returns false if there are invalid or missing files.
173
  bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
174
                              DeviceLibABIVersion ABIVer) const;
175
176
  /// Check whether we detected a valid HIP runtime.
177
430
  bool hasHIPRuntime() const { return HasHIPRuntime; }
178
179
  /// Check whether we detected a valid ROCm device library.
180
235
  bool hasDeviceLibrary() const { return HasDeviceLibrary; }
181
182
  /// Print information about the detected ROCm installation.
183
  void print(raw_ostream &OS) const;
184
185
  /// Get the detected Rocm install's version.
186
  // RocmVersion version() const { return Version; }
187
188
  /// Get the detected Rocm installation path.
189
0
  StringRef getInstallPath() const { return InstallPath; }
190
191
  /// Get the detected path to Rocm's bin directory.
192
  // StringRef getBinPath() const { return BinPath; }
193
194
  /// Get the detected Rocm Include path.
195
85
  StringRef getIncludePath() const { return IncludePath; }
196
197
  /// Get the detected Rocm library path.
198
14
  StringRef getLibPath() const { return LibPath; }
199
200
  /// Get the detected Rocm device library path.
201
0
  StringRef getLibDevicePath() const { return LibDevicePath; }
202
203
78
  StringRef getOCMLPath() const {
204
78
    assert(!OCML.empty());
205
0
    return OCML;
206
78
  }
207
208
78
  StringRef getOCKLPath() const {
209
78
    assert(!OCKL.empty());
210
0
    return OCKL;
211
78
  }
212
213
17
  StringRef getOpenCLPath() const {
214
17
    assert(!OpenCL.empty());
215
0
    return OpenCL;
216
17
  }
217
218
67
  StringRef getHIPPath() const {
219
67
    assert(!HIP.empty());
220
0
    return HIP;
221
67
  }
222
223
  /// Returns empty string of Asan runtime library is not available.
224
6
  StringRef getAsanRTLPath() const { return AsanRTL; }
225
226
78
  StringRef getWavefrontSize64Path(bool Enabled) const {
227
78
    return WavefrontSize64.get(Enabled);
228
78
  }
229
230
78
  StringRef getFiniteOnlyPath(bool Enabled) const {
231
78
    return FiniteOnly.get(Enabled);
232
78
  }
233
234
78
  StringRef getUnsafeMathPath(bool Enabled) const {
235
78
    return UnsafeMath.get(Enabled);
236
78
  }
237
238
78
  StringRef getDenormalsAreZeroPath(bool Enabled) const {
239
78
    return DenormalsAreZero.get(Enabled);
240
78
  }
241
242
78
  StringRef getCorrectlyRoundedSqrtPath(bool Enabled) const {
243
78
    return CorrectlyRoundedSqrt.get(Enabled);
244
78
  }
245
246
80
  StringRef getABIVersionPath(DeviceLibABIVersion ABIVer) const {
247
80
    auto Loc = ABIVersionMap.find(ABIVer.ABIVersion);
248
80
    if (Loc == ABIVersionMap.end())
249
6
      return StringRef();
250
74
    return Loc->second;
251
80
  }
252
253
  /// Get libdevice file for given architecture
254
128
  std::string getLibDeviceFile(StringRef Gpu) const {
255
128
    return LibDeviceMap.lookup(Gpu);
256
128
  }
257
258
  void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
259
                         llvm::opt::ArgStringList &CC1Args) const;
260
261
  void detectDeviceLibrary();
262
  void detectHIPRuntime();
263
264
  /// Get the values for --rocm-device-lib-path arguments
265
137
  std::vector<std::string> getRocmDeviceLibPathArg() const {
266
137
    return RocmDeviceLibPathArg;
267
137
  }
268
269
  /// Get the value for --rocm-path argument
270
0
  StringRef getRocmPathArg() const { return RocmPathArg; }
271
272
  /// Get the value for --hip-version argument
273
0
  StringRef getHIPVersionArg() const { return HIPVersionArg; }
274
275
0
  std::string getHIPVersion() const { return DetectedVersion; }
276
};
277
278
} // end namespace driver
279
} // end namespace clang
280
281
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H