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