clang 24.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
167public:
168 RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
169 const llvm::opt::ArgList &Args,
170 bool DetectHIPRuntime = true);
171
172 /// Get file paths of default bitcode libraries common to AMDGPU based
173 /// toolchains.
175 getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
176 StringRef LibDeviceFile, StringRef GPUArch,
177 const Action::OffloadKind DeviceOffloadingKind,
178 const bool NeedsASanRT) const;
179 /// Check file paths of default bitcode libraries common to AMDGPU based
180 /// toolchains. \returns false if there are invalid or missing files.
181 bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
182 DeviceLibABIVersion ABIVer) const;
183
184 /// Check whether we detected a valid HIP runtime.
185 bool hasHIPRuntime() const { return HasHIPRuntime; }
186
187 /// Check whether we detected a valid ROCm device library.
188 bool hasDeviceLibrary() const { return HasDeviceLibrary; }
189
190 /// Check whether we detected a valid HIP STDPAR Acceleration library.
191 bool hasHIPStdParLibrary() const { return HasHIPStdParLibrary; }
192
193 /// Print information about the detected ROCm installation.
194 void print(raw_ostream &OS) const;
195
196 /// Get the detected Rocm install's version.
197 // RocmVersion version() const { return Version; }
198
199 /// Get the detected Rocm installation path.
200 StringRef getInstallPath() const { return InstallPath; }
201
202 /// Get the detected path to Rocm's bin directory.
203 // StringRef getBinPath() const { return BinPath; }
204
205 /// Get the detected Rocm Include path.
206 StringRef getIncludePath() const { return IncludePath; }
207
208 /// Get the detected Rocm library path.
209 StringRef getLibPath() const { return LibPath; }
210
211 /// Get the detected Rocm device library path.
212 StringRef getLibDevicePath() const { return LibDevicePath; }
213
214 StringRef getOCMLPath() const {
215 assert(!OCML.empty());
216 return OCML;
217 }
218
219 StringRef getOCKLPath() const {
220 assert(!OCKL.empty());
221 return OCKL;
222 }
223
224 StringRef getOpenCLPath() const {
225 assert(!OpenCL.empty());
226 return OpenCL;
227 }
228
229 /// Returns empty string of Asan runtime library is not available.
230 StringRef getAsanRTLPath() const { return AsanRTL; }
231
232 StringRef getWavefrontSize64Path(bool Enabled) const {
233 return WavefrontSize64.get(Enabled);
234 }
235
236 StringRef getFiniteOnlyPath(bool Enabled) const {
237 return FiniteOnly.isValid() ? FiniteOnly.get(Enabled) : "";
238 }
239
240 StringRef getUnsafeMathPath(bool Enabled) const {
241 return UnsafeMath.isValid() ? UnsafeMath.get(Enabled) : "";
242 }
243
244 StringRef getABIVersionPath(DeviceLibABIVersion ABIVer) const {
245 auto Loc = ABIVersionMap.find(ABIVer.ABIVersion);
246 if (Loc == ABIVersionMap.end())
247 return StringRef();
248 return Loc->second;
249 }
250
251 /// Get libdevice file for given architecture
252 StringRef getLibDeviceFile(StringRef Gpu) const {
253 auto Loc = LibDeviceMap.find(Gpu);
254 if (Loc == LibDeviceMap.end())
255 return "";
256 return Loc->second;
257 }
258
259 void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
260 llvm::opt::ArgStringList &CC1Args) const;
261
262 void detectDeviceLibrary();
263 void detectHIPRuntime(const llvm::Triple &HostTriple);
264
265 /// Get the values for --rocm-device-lib-path arguments
267 return RocmDeviceLibPathArg;
268 }
269
270 /// Get the value for --rocm-path argument
271 StringRef getRocmPathArg() const { return RocmPathArg; }
272
273 /// Get the value for --hip-version argument
274 StringRef getHIPVersionArg() const { return HIPVersionArg; }
275
276 StringRef getHIPVersion() const { return DetectedVersion; }
277};
278
279} // namespace driver
280} // namespace clang
281
282#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:93
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:317
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:1108
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:1133
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:531
void detectHIPRuntime(const llvm::Triple &HostTriple)
Definition AMDGPU.cpp:438
StringRef getWavefrontSize64Path(bool Enabled) const
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
Definition AMDGPU.cpp:525
Top level wrappers for InstallAPI frontend operations.
@ On
Always emit colors regardless of the output stream.
@ Off
Never emit colors regardless of the output stream.
static DeviceLibABIVersion fromCodeObjectVersion(unsigned CodeObjectVersion)
bool requiresLibrary()
Whether ABI version bc file is requested.