ROCm.h 7.6 KB

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