clang  13.0.0git
ROCm.h
Go to the documentation of this file.
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 /// A class to find a viable ROCM installation
26 /// TODO: Generalize to handle libclc.
28 private:
29  struct ConditionalLibrary {
30  SmallString<0> On;
31  SmallString<0> Off;
32 
33  bool isValid() const { return !On.empty() && !Off.empty(); }
34 
35  StringRef get(bool Enabled) const {
36  assert(isValid());
37  return Enabled ? On : Off;
38  }
39  };
40 
41  // Installation path candidate.
42  struct Candidate {
44  bool StrictChecking;
45  // Release string for ROCm packages built with SPACK if not empty. The
46  // installation directories of ROCm packages built with SPACK follow the
47  // convention <package_name>-<rocm_release_string>-<hash>.
48  std::string SPACKReleaseStr;
49 
50  bool isSPACK() const { return !SPACKReleaseStr.empty(); }
51  Candidate(std::string Path, bool StrictChecking = false,
52  StringRef SPACKReleaseStr = {})
53  : Path(Path), StrictChecking(StrictChecking),
54  SPACKReleaseStr(SPACKReleaseStr.str()) {}
55  };
56 
57  const Driver &D;
58  bool HasHIPRuntime = false;
59  bool HasDeviceLibrary = false;
60 
61  // Default version if not detected or specified.
62  const unsigned DefaultVersionMajor = 3;
63  const unsigned DefaultVersionMinor = 5;
64  const char *DefaultVersionPatch = "0";
65 
66  // The version string in Major.Minor.Patch format.
67  std::string DetectedVersion;
68  // Version containing major and minor.
69  llvm::VersionTuple VersionMajorMinor;
70  // Version containing patch.
71  std::string VersionPatch;
72 
73  // ROCm path specified by --rocm-path.
74  StringRef RocmPathArg;
75  // ROCm device library paths specified by --rocm-device-lib-path.
76  std::vector<std::string> RocmDeviceLibPathArg;
77  // HIP runtime path specified by --hip-path.
78  StringRef HIPPathArg;
79  // HIP version specified by --hip-version.
80  StringRef HIPVersionArg;
81  // Wheter -nogpulib is specified.
82  bool NoBuiltinLibs = false;
83 
84  // Paths
85  SmallString<0> InstallPath;
86  SmallString<0> BinPath;
87  SmallString<0> LibPath;
88  SmallString<0> LibDevicePath;
89  SmallString<0> IncludePath;
90  llvm::StringMap<std::string> LibDeviceMap;
91 
92  // Libraries that are always linked.
93  SmallString<0> OCML;
94  SmallString<0> OCKL;
95 
96  // Libraries that are always linked depending on the language
97  SmallString<0> OpenCL;
98  SmallString<0> HIP;
99 
100  // Asan runtime library
101  SmallString<0> AsanRTL;
102 
103  // Libraries swapped based on compile flags.
104  ConditionalLibrary WavefrontSize64;
105  ConditionalLibrary FiniteOnly;
106  ConditionalLibrary UnsafeMath;
107  ConditionalLibrary DenormalsAreZero;
108  ConditionalLibrary CorrectlyRoundedSqrt;
109 
110  // Cache ROCm installation search paths.
111  SmallVector<Candidate, 4> ROCmSearchDirs;
112  bool PrintROCmSearchDirs;
113 
114  bool allGenericLibsValid() const {
115  return !OCML.empty() && !OCKL.empty() && !OpenCL.empty() && !HIP.empty() &&
116  WavefrontSize64.isValid() && FiniteOnly.isValid() &&
117  UnsafeMath.isValid() && DenormalsAreZero.isValid() &&
118  CorrectlyRoundedSqrt.isValid();
119  }
120 
121  void scanLibDevicePath(llvm::StringRef Path);
122  bool parseHIPVersionFile(llvm::StringRef V);
123  const SmallVectorImpl<Candidate> &getInstallationPathCandidates();
124 
125  /// Find the path to a SPACK package under the ROCm candidate installation
126  /// directory if the candidate is a SPACK ROCm candidate. \returns empty
127  /// string if the candidate is not SPACK ROCm candidate or the requested
128  /// package is not found.
129  llvm::SmallString<0> findSPACKPackage(const Candidate &Cand,
130  StringRef PackageName);
131 
132 public:
133  RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
134  const llvm::opt::ArgList &Args,
135  bool DetectHIPRuntime = true,
136  bool DetectDeviceLib = false);
137 
138  /// Get file paths of default bitcode libraries common to AMDGPU based
139  /// toolchains.
141  getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
142  StringRef LibDeviceFile, bool Wave64, bool DAZ,
143  bool FiniteOnly, bool UnsafeMathOpt,
144  bool FastRelaxedMath, bool CorrectSqrt) const;
145 
146  /// Check whether we detected a valid HIP runtime.
147  bool hasHIPRuntime() const { return HasHIPRuntime; }
148 
149  /// Check whether we detected a valid ROCm device library.
150  bool hasDeviceLibrary() const { return HasDeviceLibrary; }
151 
152  /// Print information about the detected ROCm installation.
153  void print(raw_ostream &OS) const;
154 
155  /// Get the detected Rocm install's version.
156  // RocmVersion version() const { return Version; }
157 
158  /// Get the detected Rocm installation path.
159  StringRef getInstallPath() const { return InstallPath; }
160 
161  /// Get the detected path to Rocm's bin directory.
162  // StringRef getBinPath() const { return BinPath; }
163 
164  /// Get the detected Rocm Include path.
165  StringRef getIncludePath() const { return IncludePath; }
166 
167  /// Get the detected Rocm library path.
168  StringRef getLibPath() const { return LibPath; }
169 
170  /// Get the detected Rocm device library path.
171  StringRef getLibDevicePath() const { return LibDevicePath; }
172 
173  StringRef getOCMLPath() const {
174  assert(!OCML.empty());
175  return OCML;
176  }
177 
178  StringRef getOCKLPath() const {
179  assert(!OCKL.empty());
180  return OCKL;
181  }
182 
183  StringRef getOpenCLPath() const {
184  assert(!OpenCL.empty());
185  return OpenCL;
186  }
187 
188  StringRef getHIPPath() const {
189  assert(!HIP.empty());
190  return HIP;
191  }
192 
193  /// Returns empty string of Asan runtime library is not available.
194  StringRef getAsanRTLPath() const { return AsanRTL; }
195 
196  StringRef getWavefrontSize64Path(bool Enabled) const {
197  return WavefrontSize64.get(Enabled);
198  }
199 
200  StringRef getFiniteOnlyPath(bool Enabled) const {
201  return FiniteOnly.get(Enabled);
202  }
203 
204  StringRef getUnsafeMathPath(bool Enabled) const {
205  return UnsafeMath.get(Enabled);
206  }
207 
208  StringRef getDenormalsAreZeroPath(bool Enabled) const {
209  return DenormalsAreZero.get(Enabled);
210  }
211 
212  StringRef getCorrectlyRoundedSqrtPath(bool Enabled) const {
213  return CorrectlyRoundedSqrt.get(Enabled);
214  }
215 
216  /// Get libdevice file for given architecture
217  std::string getLibDeviceFile(StringRef Gpu) const {
218  return LibDeviceMap.lookup(Gpu);
219  }
220 
221  void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
222  llvm::opt::ArgStringList &CC1Args) const;
223 
224  void detectDeviceLibrary();
225  void detectHIPRuntime();
226 
227  /// Get the values for --rocm-device-lib-path arguments
228  std::vector<std::string> getRocmDeviceLibPathArg() const {
229  return RocmDeviceLibPathArg;
230  }
231 
232  /// Get the value for --rocm-path argument
233  StringRef getRocmPathArg() const { return RocmPathArg; }
234 
235  /// Get the value for --hip-version argument
236  StringRef getHIPVersionArg() const { return HIPVersionArg; }
237 
238  std::string getHIPVersion() const { return DetectedVersion; }
239 };
240 
241 } // end namespace driver
242 } // end namespace clang
243 
244 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
clang::OpenCL
@ OpenCL
Definition: LangStandard.h:58
clang::driver::RocmInstallationDetector::detectHIPRuntime
void detectHIPRuntime()
Definition: AMDGPU.cpp:425
clang::driver::RocmInstallationDetector::getLibPath
StringRef getLibPath() const
Get the detected Rocm library path.
Definition: ROCm.h:168
clang::driver::RocmInstallationDetector::getHIPVersion
std::string getHIPVersion() const
Definition: ROCm.h:238
clang::driver::RocmInstallationDetector::getInstallPath
StringRef getInstallPath() const
Get the detected Rocm install's version.
Definition: ROCm.h:159
clang::driver::RocmInstallationDetector::hasHIPRuntime
bool hasHIPRuntime() const
Check whether we detected a valid HIP runtime.
Definition: ROCm.h:147
clang::driver::RocmInstallationDetector::AddHIPIncludeArgs
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: AMDGPU.cpp:470
clang::driver::RocmInstallationDetector::getDenormalsAreZeroPath
StringRef getDenormalsAreZeroPath(bool Enabled) const
Definition: ROCm.h:208
clang::driver::RocmInstallationDetector::getAsanRTLPath
StringRef getAsanRTLPath() const
Returns empty string of Asan runtime library is not available.
Definition: ROCm.h:194
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::RocmInstallationDetector::getLibDevicePath
StringRef getLibDevicePath() const
Get the detected Rocm device library path.
Definition: ROCm.h:171
llvm::SmallVector< Candidate, 4 >
clang::driver::RocmInstallationDetector::detectDeviceLibrary
void detectDeviceLibrary()
Definition: AMDGPU.cpp:343
clang::driver::RocmInstallationDetector::RocmInstallationDetector
RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args, bool DetectHIPRuntime=true, bool DetectDeviceLib=false)
Definition: AMDGPU.cpp:297
clang::driver::RocmInstallationDetector::getIncludePath
StringRef getIncludePath() const
Get the detected path to Rocm's bin directory.
Definition: ROCm.h:165
V
#define V(N, I)
Definition: ASTContext.h:3018
Options.h
clang::driver::RocmInstallationDetector::getOCKLPath
StringRef getOCKLPath() const
Definition: ROCm.h:178
clang::driver::RocmInstallationDetector::getCorrectlyRoundedSqrtPath
StringRef getCorrectlyRoundedSqrtPath(bool Enabled) const
Definition: ROCm.h:212
clang::driver::RocmInstallationDetector::getWavefrontSize64Path
StringRef getWavefrontSize64Path(bool Enabled) const
Definition: ROCm.h:196
clang::driver::RocmInstallationDetector::getOpenCLPath
StringRef getOpenCLPath() const
Definition: ROCm.h:183
llvm::SmallString< 0 >
clang::driver::RocmInstallationDetector::hasDeviceLibrary
bool hasDeviceLibrary() const
Check whether we detected a valid ROCm device library.
Definition: ROCm.h:150
clang::driver::RocmInstallationDetector::print
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
Definition: AMDGPU.cpp:464
clang::driver::RocmInstallationDetector::getHIPPath
StringRef getHIPPath() const
Definition: ROCm.h:188
clang::driver::RocmInstallationDetector::getHIPVersionArg
StringRef getHIPVersionArg() const
Get the value for –hip-version argument.
Definition: ROCm.h:236
clang::driver::RocmInstallationDetector::getFiniteOnlyPath
StringRef getFiniteOnlyPath(bool Enabled) const
Definition: ROCm.h:200
clang::driver::RocmInstallationDetector::getOCMLPath
StringRef getOCMLPath() const
Definition: ROCm.h:173
LLVM.h
clang::driver::RocmInstallationDetector::getCommonBitcodeLibs
llvm::SmallVector< std::string, 12 > getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64, bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath, bool CorrectSqrt) const
Get file paths of default bitcode libraries common to AMDGPU based toolchains.
Definition: AMDGPU.cpp:778
clang::driver::RocmInstallationDetector
A class to find a viable ROCM installation TODO: Generalize to handle libclc.
Definition: ROCm.h:27
Cuda.h
clang::driver::RocmInstallationDetector::getLibDeviceFile
std::string getLibDeviceFile(StringRef Gpu) const
Get libdevice file for given architecture.
Definition: ROCm.h:217
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::RocmInstallationDetector::getRocmDeviceLibPathArg
std::vector< std::string > getRocmDeviceLibPathArg() const
Get the values for –rocm-device-lib-path arguments.
Definition: ROCm.h:228
clang::driver::RocmInstallationDetector::getUnsafeMathPath
StringRef getUnsafeMathPath(bool Enabled) const
Definition: ROCm.h:204
clang::Language::HIP
@ HIP
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::driver::RocmInstallationDetector::getRocmPathArg
StringRef getRocmPathArg() const
Get the value for –rocm-path argument.
Definition: ROCm.h:233