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 CorrectlyRoundedSqrt;
150
151 // Maps ABI version to library path. The version number is in the format of
152 // three digits as used in the ABI version library name.
153 std::map<unsigned, std::string> ABIVersionMap;
154
155 // Cache ROCm installation search paths.
156 SmallVector<Candidate, 4> ROCmSearchDirs;
157 bool PrintROCmSearchDirs;
158 bool Verbose;
159
160 bool allGenericLibsValid() const {
161 return !OCML.empty() && !OCKL.empty() && !OpenCL.empty() &&
162 WavefrontSize64.isValid() && FiniteOnly.isValid() &&
163 UnsafeMath.isValid() && CorrectlyRoundedSqrt.isValid();
164 }
165
166 void scanLibDevicePath(llvm::StringRef Path);
167 bool parseHIPVersionFile(llvm::StringRef V);
168 const SmallVectorImpl<Candidate> &getInstallationPathCandidates();
169
170 /// Find the path to a SPACK package under the ROCm candidate installation
171 /// directory if the candidate is a SPACK ROCm candidate. \returns empty
172 /// string if the candidate is not SPACK ROCm candidate or the requested
173 /// package is not found.
174 llvm::SmallString<0> findSPACKPackage(const Candidate &Cand,
175 StringRef PackageName);
176
177public:
178 RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
179 const llvm::opt::ArgList &Args,
180 bool DetectHIPRuntime = true,
181 bool DetectDeviceLib = false);
182
183 /// Get file paths of default bitcode libraries common to AMDGPU based
184 /// toolchains.
186 getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
187 StringRef LibDeviceFile, StringRef GPUArch,
188 const Action::OffloadKind DeviceOffloadingKind,
189 const bool NeedsASanRT) const;
190 /// Check file paths of default bitcode libraries common to AMDGPU based
191 /// toolchains. \returns false if there are invalid or missing files.
192 bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
193 DeviceLibABIVersion ABIVer) const;
194
195 /// Check whether we detected a valid HIP runtime.
196 bool hasHIPRuntime() const { return HasHIPRuntime; }
197
198 /// Check whether we detected a valid ROCm device library.
199 bool hasDeviceLibrary() const { return HasDeviceLibrary; }
200
201 /// Check whether we detected a valid HIP STDPAR Acceleration library.
202 bool hasHIPStdParLibrary() const { return HasHIPStdParLibrary; }
203
204 /// Print information about the detected ROCm installation.
205 void print(raw_ostream &OS) const;
206
207 /// Get the detected Rocm install's version.
208 // RocmVersion version() const { return Version; }
209
210 /// Get the detected Rocm installation path.
211 StringRef getInstallPath() const { return InstallPath; }
212
213 /// Get the detected path to Rocm's bin directory.
214 // StringRef getBinPath() const { return BinPath; }
215
216 /// Get the detected Rocm Include path.
217 StringRef getIncludePath() const { return IncludePath; }
218
219 /// Get the detected Rocm library path.
220 StringRef getLibPath() const { return LibPath; }
221
222 /// Get the detected Rocm device library path.
223 StringRef getLibDevicePath() const { return LibDevicePath; }
224
225 StringRef getOCMLPath() const {
226 assert(!OCML.empty());
227 return OCML;
228 }
229
230 StringRef getOCKLPath() const {
231 assert(!OCKL.empty());
232 return OCKL;
233 }
234
235 StringRef getOpenCLPath() const {
236 assert(!OpenCL.empty());
237 return OpenCL;
238 }
239
240 /// Returns empty string of Asan runtime library is not available.
241 StringRef getAsanRTLPath() const { return AsanRTL; }
242
243 StringRef getWavefrontSize64Path(bool Enabled) const {
244 return WavefrontSize64.get(Enabled);
245 }
246
247 StringRef getFiniteOnlyPath(bool Enabled) const {
248 return FiniteOnly.get(Enabled);
249 }
250
251 StringRef getUnsafeMathPath(bool Enabled) const {
252 return UnsafeMath.get(Enabled);
253 }
254
255 StringRef getCorrectlyRoundedSqrtPath(bool Enabled) const {
256 return CorrectlyRoundedSqrt.get(Enabled);
257 }
258
259 StringRef getABIVersionPath(DeviceLibABIVersion ABIVer) const {
260 auto Loc = ABIVersionMap.find(ABIVer.ABIVersion);
261 if (Loc == ABIVersionMap.end())
262 return StringRef();
263 return Loc->second;
264 }
265
266 /// Get libdevice file for given architecture
267 StringRef getLibDeviceFile(StringRef Gpu) const {
268 auto Loc = LibDeviceMap.find(Gpu);
269 if (Loc == LibDeviceMap.end())
270 return "";
271 return Loc->second;
272 }
273
274 void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
275 llvm::opt::ArgStringList &CC1Args) const;
276
277 void detectDeviceLibrary();
278 void detectHIPRuntime();
279
280 /// Get the values for --rocm-device-lib-path arguments
282 return RocmDeviceLibPathArg;
283 }
284
285 /// Get the value for --rocm-path argument
286 StringRef getRocmPathArg() const { return RocmPathArg; }
287
288 /// Get the value for --hip-version argument
289 StringRef getHIPVersionArg() const { return HIPVersionArg; }
290
291 StringRef getHIPVersion() const { return DetectedVersion; }
292};
293
294} // namespace driver
295} // namespace clang
296
297#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 getCorrectlyRoundedSqrtPath(bool Enabled) const
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, bool DetectDeviceLib=false)
Definition AMDGPU.cpp:324
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:999
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:1024
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:522
StringRef getWavefrontSize64Path(bool Enabled) const
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
Definition AMDGPU.cpp:516
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.