clang 23.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 GPUSan;
86 };
87
88 const Driver &D;
89 bool HasHIPRuntime = false;
90 bool HasDeviceLibrary = false;
91 bool HasHIPStdParLibrary = false;
92 bool HasRocThrustLibrary = false;
93 bool HasRocPrimLibrary = false;
94
95 // Default version if not detected or specified.
96 const unsigned DefaultVersionMajor = 3;
97 const unsigned DefaultVersionMinor = 5;
98 const char *DefaultVersionPatch = "0";
99
100 // The version string in Major.Minor.Patch format.
101 std::string DetectedVersion;
102 // Version containing major and minor.
103 llvm::VersionTuple VersionMajorMinor;
104 // Version containing patch.
105 std::string VersionPatch;
106
107 // ROCm path specified by --rocm-path.
108 StringRef RocmPathArg;
109 // ROCm device library paths specified by --rocm-device-lib-path.
110 std::vector<std::string> RocmDeviceLibPathArg;
111 // HIP runtime path specified by --hip-path.
112 StringRef HIPPathArg;
113 // HIP Standard Parallel Algorithm acceleration library specified by
114 // --hipstdpar-path
115 StringRef HIPStdParPathArg;
116 // rocThrust algorithm library specified by --hipstdpar-thrust-path
117 StringRef HIPRocThrustPathArg;
118 // rocPrim algorithm library specified by --hipstdpar-prim-path
119 StringRef HIPRocPrimPathArg;
120 // HIP version specified by --hip-version.
121 StringRef HIPVersionArg;
122 // Wheter -nogpulib is specified.
123 bool NoBuiltinLibs = false;
124
125 // Paths
126 SmallString<0> InstallPath;
127 SmallString<0> BinPath;
128 SmallString<0> LibPath;
129 SmallString<0> LibDevicePath;
130 SmallString<0> IncludePath;
131 SmallString<0> SharePath;
132 llvm::StringMap<std::string> LibDeviceMap;
133
134 // Libraries that are always linked.
135 SmallString<0> OCML;
136 SmallString<0> OCKL;
137
138 // Libraries that are always linked depending on the language
139 SmallString<0> OpenCL;
140
141 // Asan runtime library
142 SmallString<0> AsanRTL;
143
144 // Libraries swapped based on compile flags.
145 ConditionalLibrary WavefrontSize64;
146 ConditionalLibrary FiniteOnly;
147 ConditionalLibrary UnsafeMath;
148
149 // Maps ABI version to library path. The version number is in the format of
150 // three digits as used in the ABI version library name.
151 std::map<unsigned, std::string> ABIVersionMap;
152
153 // Cache ROCm installation search paths.
154 SmallVector<Candidate, 4> ROCmSearchDirs;
155 bool PrintROCmSearchDirs;
156 bool Verbose;
157
158 bool allGenericLibsValid() const {
159 return !OCML.empty() && !OCKL.empty() && !OpenCL.empty() &&
160 WavefrontSize64.isValid();
161 }
162
163 void scanLibDevicePath(llvm::StringRef Path);
164 bool parseHIPVersionFile(llvm::StringRef V);
165 const SmallVectorImpl<Candidate> &getInstallationPathCandidates();
166
167 /// Find the path to a SPACK package under the ROCm candidate installation
168 /// directory if the candidate is a SPACK ROCm candidate. \returns empty
169 /// string if the candidate is not SPACK ROCm candidate or the requested
170 /// package is not found.
171 llvm::SmallString<0> findSPACKPackage(const Candidate &Cand,
172 StringRef PackageName);
173
174public:
175 RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
176 const llvm::opt::ArgList &Args,
177 bool DetectHIPRuntime = true);
178
179 /// Get file paths of default bitcode libraries common to AMDGPU based
180 /// toolchains.
182 getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
183 StringRef LibDeviceFile, StringRef GPUArch,
184 const Action::OffloadKind DeviceOffloadingKind,
185 const bool NeedsASanRT) const;
186 /// Check file paths of default bitcode libraries common to AMDGPU based
187 /// toolchains. \returns false if there are invalid or missing files.
188 bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
189 DeviceLibABIVersion ABIVer) const;
190
191 /// Check whether we detected a valid HIP runtime.
192 bool hasHIPRuntime() const { return HasHIPRuntime; }
193
194 /// Check whether we detected a valid ROCm device library.
195 bool hasDeviceLibrary() const { return HasDeviceLibrary; }
196
197 /// Check whether we detected a valid HIP STDPAR Acceleration library.
198 bool hasHIPStdParLibrary() const { return HasHIPStdParLibrary; }
199
200 /// Print information about the detected ROCm installation.
201 void print(raw_ostream &OS) const;
202
203 /// Get the detected Rocm install's version.
204 // RocmVersion version() const { return Version; }
205
206 /// Get the detected Rocm installation path.
207 StringRef getInstallPath() const { return InstallPath; }
208
209 /// Get the detected path to Rocm's bin directory.
210 // StringRef getBinPath() const { return BinPath; }
211
212 /// Get the detected Rocm Include path.
213 StringRef getIncludePath() const { return IncludePath; }
214
215 /// Get the detected Rocm library path.
216 StringRef getLibPath() const { return LibPath; }
217
218 /// Get the detected Rocm device library path.
219 StringRef getLibDevicePath() const { return LibDevicePath; }
220
221 StringRef getOCMLPath() const {
222 assert(!OCML.empty());
223 return OCML;
224 }
225
226 StringRef getOCKLPath() const {
227 assert(!OCKL.empty());
228 return OCKL;
229 }
230
231 StringRef getOpenCLPath() const {
232 assert(!OpenCL.empty());
233 return OpenCL;
234 }
235
236 /// Returns empty string of Asan runtime library is not available.
237 StringRef getAsanRTLPath() const { return AsanRTL; }
238
239 StringRef getWavefrontSize64Path(bool Enabled) const {
240 return WavefrontSize64.get(Enabled);
241 }
242
243 StringRef getFiniteOnlyPath(bool Enabled) const {
244 return FiniteOnly.isValid() ? FiniteOnly.get(Enabled) : "";
245 }
246
247 StringRef getUnsafeMathPath(bool Enabled) const {
248 return UnsafeMath.isValid() ? UnsafeMath.get(Enabled) : "";
249 }
250
251 StringRef getABIVersionPath(DeviceLibABIVersion ABIVer) const {
252 auto Loc = ABIVersionMap.find(ABIVer.ABIVersion);
253 if (Loc == ABIVersionMap.end())
254 return StringRef();
255 return Loc->second;
256 }
257
258 /// Get libdevice file for given architecture
259 StringRef getLibDeviceFile(StringRef Gpu) const {
260 auto Loc = LibDeviceMap.find(Gpu);
261 if (Loc == LibDeviceMap.end())
262 return "";
263 return Loc->second;
264 }
265
266 void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
267 llvm::opt::ArgStringList &CC1Args) const;
268
269 void detectDeviceLibrary();
270 void detectHIPRuntime();
271
272 /// Get the values for --rocm-device-lib-path arguments
274 return RocmDeviceLibPathArg;
275 }
276
277 /// Get the value for --rocm-path argument
278 StringRef getRocmPathArg() const { return RocmPathArg; }
279
280 /// Get the value for --hip-version argument
281 StringRef getHIPVersionArg() const { return HIPVersionArg; }
282
283 StringRef getHIPVersion() const { return DetectedVersion; }
284};
285
286} // namespace driver
287} // namespace clang
288
289#endif // LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
#define V(N, I)
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
StringRef getIncludePath() const
Get the detected path to Rocm's bin directory.
StringRef getAsanRTLPath() const
Returns empty string of Asan runtime library is not available.
RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args, bool DetectHIPRuntime=true)
Definition AMDGPU.cpp:314
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 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:995
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:1020
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:510
StringRef getWavefrontSize64Path(bool Enabled) const
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
Definition AMDGPU.cpp:504
The JSON file list parser is used to communicate input to InstallAPI.
static DeviceLibABIVersion fromCodeObjectVersion(unsigned CodeObjectVersion)
bool requiresLibrary()
Whether ABI version bc file is requested.