ROCm.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
  9. #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
  10. #include "clang/Basic/Cuda.h"
  11. #include "clang/Basic/LLVM.h"
  12. #include "clang/Driver/Driver.h"
  13. #include "clang/Driver/Options.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Support/VersionTuple.h"
  19. namespace clang {
  20. namespace driver {
  21. /// ABI version of device library.
  22. struct DeviceLibABIVersion {
  23. unsigned ABIVersion = 0;
  24. DeviceLibABIVersion(unsigned V) : ABIVersion(V) {}
  25. static DeviceLibABIVersion fromCodeObjectVersion(unsigned CodeObjectVersion) {
  26. if (CodeObjectVersion < 4)
  27. CodeObjectVersion = 4;
  28. return DeviceLibABIVersion(CodeObjectVersion * 100);
  29. }
  30. /// Whether ABI version bc file is requested.
  31. /// ABIVersion is code object version multiplied by 100. Code object v4
  32. /// and below works with ROCm 5.0 and below which does not have
  33. /// abi_version_*.bc. Code object v5 requires abi_version_500.bc.
  34. bool requiresLibrary() { return ABIVersion >= 500; }
  35. std::string toString() {
  36. assert(ABIVersion % 100 == 0 && "Not supported");
  37. return Twine(ABIVersion / 100).str();
  38. }
  39. };
  40. /// A class to find a viable ROCM installation
  41. /// TODO: Generalize to handle libclc.
  42. class RocmInstallationDetector {
  43. private:
  44. struct ConditionalLibrary {
  45. SmallString<0> On;
  46. SmallString<0> Off;
  47. bool isValid() const { return !On.empty() && !Off.empty(); }
  48. StringRef get(bool Enabled) const {
  49. assert(isValid());
  50. return Enabled ? On : Off;
  51. }
  52. };
  53. // Installation path candidate.
  54. struct Candidate {
  55. llvm::SmallString<0> Path;
  56. bool StrictChecking;
  57. // Release string for ROCm packages built with SPACK if not empty. The
  58. // installation directories of ROCm packages built with SPACK follow the
  59. // convention <package_name>-<rocm_release_string>-<hash>.
  60. std::string SPACKReleaseStr;
  61. bool isSPACK() const { return !SPACKReleaseStr.empty(); }
  62. Candidate(std::string Path, bool StrictChecking = false,
  63. StringRef SPACKReleaseStr = {})
  64. : Path(Path), StrictChecking(StrictChecking),
  65. SPACKReleaseStr(SPACKReleaseStr.str()) {}
  66. };
  67. const Driver &D;
  68. bool HasHIPRuntime = false;
  69. bool HasDeviceLibrary = false;
  70. // Default version if not detected or specified.
  71. const unsigned DefaultVersionMajor = 3;
  72. const unsigned DefaultVersionMinor = 5;
  73. const char *DefaultVersionPatch = "0";
  74. // The version string in Major.Minor.Patch format.
  75. std::string DetectedVersion;
  76. // Version containing major and minor.
  77. llvm::VersionTuple VersionMajorMinor;
  78. // Version containing patch.
  79. std::string VersionPatch;
  80. // ROCm path specified by --rocm-path.
  81. StringRef RocmPathArg;
  82. // ROCm device library paths specified by --rocm-device-lib-path.
  83. std::vector<std::string> RocmDeviceLibPathArg;
  84. // HIP runtime path specified by --hip-path.
  85. StringRef HIPPathArg;
  86. // HIP version specified by --hip-version.
  87. StringRef HIPVersionArg;
  88. // Wheter -nogpulib is specified.
  89. bool NoBuiltinLibs = false;
  90. // Paths
  91. SmallString<0> InstallPath;
  92. SmallString<0> BinPath;
  93. SmallString<0> LibPath;
  94. SmallString<0> LibDevicePath;
  95. SmallString<0> IncludePath;
  96. SmallString<0> SharePath;
  97. llvm::StringMap<std::string> LibDeviceMap;
  98. // Libraries that are always linked.
  99. SmallString<0> OCML;
  100. SmallString<0> OCKL;
  101. // Libraries that are always linked depending on the language
  102. SmallString<0> OpenCL;
  103. SmallString<0> HIP;
  104. // Asan runtime library
  105. SmallString<0> AsanRTL;
  106. // Libraries swapped based on compile flags.
  107. ConditionalLibrary WavefrontSize64;
  108. ConditionalLibrary FiniteOnly;
  109. ConditionalLibrary UnsafeMath;
  110. ConditionalLibrary DenormalsAreZero;
  111. ConditionalLibrary CorrectlyRoundedSqrt;
  112. // Maps ABI version to library path. The version number is in the format of
  113. // three digits as used in the ABI version library name.
  114. std::map<unsigned, std::string> ABIVersionMap;
  115. // Cache ROCm installation search paths.
  116. SmallVector<Candidate, 4> ROCmSearchDirs;
  117. bool PrintROCmSearchDirs;
  118. bool Verbose;
  119. bool allGenericLibsValid() const {
  120. return !OCML.empty() && !OCKL.empty() && !OpenCL.empty() && !HIP.empty() &&
  121. WavefrontSize64.isValid() && FiniteOnly.isValid() &&
  122. UnsafeMath.isValid() && DenormalsAreZero.isValid() &&
  123. CorrectlyRoundedSqrt.isValid();
  124. }
  125. void scanLibDevicePath(llvm::StringRef Path);
  126. bool parseHIPVersionFile(llvm::StringRef V);
  127. const SmallVectorImpl<Candidate> &getInstallationPathCandidates();
  128. /// Find the path to a SPACK package under the ROCm candidate installation
  129. /// directory if the candidate is a SPACK ROCm candidate. \returns empty
  130. /// string if the candidate is not SPACK ROCm candidate or the requested
  131. /// package is not found.
  132. llvm::SmallString<0> findSPACKPackage(const Candidate &Cand,
  133. StringRef PackageName);
  134. public:
  135. RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
  136. const llvm::opt::ArgList &Args,
  137. bool DetectHIPRuntime = true,
  138. bool DetectDeviceLib = false);
  139. /// Get file paths of default bitcode libraries common to AMDGPU based
  140. /// toolchains.
  141. llvm::SmallVector<std::string, 12>
  142. getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
  143. StringRef LibDeviceFile, bool Wave64, bool DAZ,
  144. bool FiniteOnly, bool UnsafeMathOpt,
  145. bool FastRelaxedMath, bool CorrectSqrt,
  146. DeviceLibABIVersion ABIVer, bool isOpenMP) const;
  147. /// Check file paths of default bitcode libraries common to AMDGPU based
  148. /// toolchains. \returns false if there are invalid or missing files.
  149. bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
  150. DeviceLibABIVersion ABIVer) const;
  151. /// Check whether we detected a valid HIP runtime.
  152. bool hasHIPRuntime() const { return HasHIPRuntime; }
  153. /// Check whether we detected a valid ROCm device library.
  154. bool hasDeviceLibrary() const { return HasDeviceLibrary; }
  155. /// Print information about the detected ROCm installation.
  156. void print(raw_ostream &OS) const;
  157. /// Get the detected Rocm install's version.
  158. // RocmVersion version() const { return Version; }
  159. /// Get the detected Rocm installation path.
  160. StringRef getInstallPath() const { return InstallPath; }
  161. /// Get the detected path to Rocm's bin directory.
  162. // StringRef getBinPath() const { return BinPath; }
  163. /// Get the detected Rocm Include path.
  164. StringRef getIncludePath() const { return IncludePath; }
  165. /// Get the detected Rocm library path.
  166. StringRef getLibPath() const { return LibPath; }
  167. /// Get the detected Rocm device library path.
  168. StringRef getLibDevicePath() const { return LibDevicePath; }
  169. StringRef getOCMLPath() const {
  170. assert(!OCML.empty());
  171. return OCML;
  172. }
  173. StringRef getOCKLPath() const {
  174. assert(!OCKL.empty());
  175. return OCKL;
  176. }
  177. StringRef getOpenCLPath() const {
  178. assert(!OpenCL.empty());
  179. return OpenCL;
  180. }
  181. StringRef getHIPPath() const {
  182. assert(!HIP.empty());
  183. return HIP;
  184. }
  185. /// Returns empty string of Asan runtime library is not available.
  186. StringRef getAsanRTLPath() const { return AsanRTL; }
  187. StringRef getWavefrontSize64Path(bool Enabled) const {
  188. return WavefrontSize64.get(Enabled);
  189. }
  190. StringRef getFiniteOnlyPath(bool Enabled) const {
  191. return FiniteOnly.get(Enabled);
  192. }
  193. StringRef getUnsafeMathPath(bool Enabled) const {
  194. return UnsafeMath.get(Enabled);
  195. }
  196. StringRef getDenormalsAreZeroPath(bool Enabled) const {
  197. return DenormalsAreZero.get(Enabled);
  198. }
  199. StringRef getCorrectlyRoundedSqrtPath(bool Enabled) const {
  200. return CorrectlyRoundedSqrt.get(Enabled);
  201. }
  202. StringRef getABIVersionPath(DeviceLibABIVersion ABIVer) const {
  203. auto Loc = ABIVersionMap.find(ABIVer.ABIVersion);
  204. if (Loc == ABIVersionMap.end())
  205. return StringRef();
  206. return Loc->second;
  207. }
  208. /// Get libdevice file for given architecture
  209. StringRef getLibDeviceFile(StringRef Gpu) const {
  210. auto Loc = LibDeviceMap.find(Gpu);
  211. if (Loc == LibDeviceMap.end())
  212. return "";
  213. return Loc->second;
  214. }
  215. void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
  216. llvm::opt::ArgStringList &CC1Args) const;
  217. void detectDeviceLibrary();
  218. void detectHIPRuntime();
  219. /// Get the values for --rocm-device-lib-path arguments
  220. ArrayRef<std::string> getRocmDeviceLibPathArg() const {
  221. return RocmDeviceLibPathArg;
  222. }
  223. /// Get the value for --rocm-path argument
  224. StringRef getRocmPathArg() const { return RocmPathArg; }
  225. /// Get the value for --hip-version argument
  226. StringRef getHIPVersionArg() const { return HIPVersionArg; }
  227. StringRef getHIPVersion() const { return DetectedVersion; }
  228. };
  229. } // end namespace driver
  230. } // end namespace clang
  231. #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H