Cuda.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //===--- Cuda.h - Cuda ToolChain Implementations ----------------*- 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_CUDA_H
  9. #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H
  10. #include "clang/Basic/Cuda.h"
  11. #include "clang/Driver/Action.h"
  12. #include "clang/Driver/Multilib.h"
  13. #include "clang/Driver/Tool.h"
  14. #include "clang/Driver/ToolChain.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/VersionTuple.h"
  17. #include <bitset>
  18. #include <set>
  19. #include <vector>
  20. namespace clang {
  21. namespace driver {
  22. /// A class to find a viable CUDA installation
  23. class CudaInstallationDetector {
  24. private:
  25. const Driver &D;
  26. bool IsValid = false;
  27. CudaVersion Version = CudaVersion::UNKNOWN;
  28. std::string InstallPath;
  29. std::string BinPath;
  30. std::string LibDevicePath;
  31. std::string IncludePath;
  32. llvm::StringMap<std::string> LibDeviceMap;
  33. // CUDA architectures for which we have raised an error in
  34. // CheckCudaVersionSupportsArch.
  35. mutable std::bitset<(int)CudaArch::LAST> ArchsWithBadVersion;
  36. public:
  37. CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
  38. const llvm::opt::ArgList &Args);
  39. void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
  40. llvm::opt::ArgStringList &CC1Args) const;
  41. /// Emit an error if Version does not support the given Arch.
  42. ///
  43. /// If either Version or Arch is unknown, does not emit an error. Emits at
  44. /// most one error per Arch.
  45. void CheckCudaVersionSupportsArch(CudaArch Arch) const;
  46. /// Check whether we detected a valid Cuda install.
  47. bool isValid() const { return IsValid; }
  48. /// Print information about the detected CUDA installation.
  49. void print(raw_ostream &OS) const;
  50. /// Get the detected Cuda install's version.
  51. CudaVersion version() const {
  52. return Version == CudaVersion::NEW ? CudaVersion::PARTIALLY_SUPPORTED
  53. : Version;
  54. }
  55. /// Get the detected Cuda installation path.
  56. StringRef getInstallPath() const { return InstallPath; }
  57. /// Get the detected path to Cuda's bin directory.
  58. StringRef getBinPath() const { return BinPath; }
  59. /// Get the detected Cuda Include path.
  60. StringRef getIncludePath() const { return IncludePath; }
  61. /// Get the detected Cuda device library path.
  62. StringRef getLibDevicePath() const { return LibDevicePath; }
  63. /// Get libdevice file for given architecture
  64. std::string getLibDeviceFile(StringRef Gpu) const {
  65. return LibDeviceMap.lookup(Gpu);
  66. }
  67. void WarnIfUnsupportedVersion();
  68. };
  69. namespace tools {
  70. namespace NVPTX {
  71. // Run ptxas, the NVPTX assembler.
  72. class LLVM_LIBRARY_VISIBILITY Assembler : public Tool {
  73. public:
  74. Assembler(const ToolChain &TC) : Tool("NVPTX::Assembler", "ptxas", TC) {}
  75. bool hasIntegratedCPP() const override { return false; }
  76. void ConstructJob(Compilation &C, const JobAction &JA,
  77. const InputInfo &Output, const InputInfoList &Inputs,
  78. const llvm::opt::ArgList &TCArgs,
  79. const char *LinkingOutput) const override;
  80. };
  81. // Runs fatbinary, which combines GPU object files ("cubin" files) and/or PTX
  82. // assembly into a single output file.
  83. class LLVM_LIBRARY_VISIBILITY FatBinary : public Tool {
  84. public:
  85. FatBinary(const ToolChain &TC) : Tool("NVPTX::Linker", "fatbinary", TC) {}
  86. bool hasIntegratedCPP() const override { return false; }
  87. void ConstructJob(Compilation &C, const JobAction &JA,
  88. const InputInfo &Output, const InputInfoList &Inputs,
  89. const llvm::opt::ArgList &TCArgs,
  90. const char *LinkingOutput) const override;
  91. };
  92. // Runs nvlink, which links GPU object files ("cubin" files) into a single file.
  93. class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
  94. public:
  95. Linker(const ToolChain &TC) : Tool("NVPTX::Linker", "fatbinary", TC) {}
  96. bool hasIntegratedCPP() const override { return false; }
  97. void ConstructJob(Compilation &C, const JobAction &JA,
  98. const InputInfo &Output, const InputInfoList &Inputs,
  99. const llvm::opt::ArgList &TCArgs,
  100. const char *LinkingOutput) const override;
  101. };
  102. void getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  103. const llvm::opt::ArgList &Args,
  104. std::vector<StringRef> &Features);
  105. } // end namespace NVPTX
  106. } // end namespace tools
  107. namespace toolchains {
  108. class LLVM_LIBRARY_VISIBILITY NVPTXToolChain : public ToolChain {
  109. public:
  110. NVPTXToolChain(const Driver &D, const llvm::Triple &Triple,
  111. const llvm::Triple &HostTriple,
  112. const llvm::opt::ArgList &Args);
  113. NVPTXToolChain(const Driver &D, const llvm::Triple &Triple,
  114. const llvm::opt::ArgList &Args);
  115. llvm::opt::DerivedArgList *
  116. TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
  117. Action::OffloadKind DeviceOffloadKind) const override;
  118. // Never try to use the integrated assembler with CUDA; always fork out to
  119. // ptxas.
  120. bool useIntegratedAs() const override { return false; }
  121. bool isCrossCompiling() const override { return true; }
  122. bool isPICDefault() const override { return false; }
  123. bool isPIEDefault(const llvm::opt::ArgList &Args) const override {
  124. return false;
  125. }
  126. bool isPICDefaultForced() const override { return false; }
  127. bool SupportsProfiling() const override { return false; }
  128. bool IsMathErrnoDefault() const override { return false; }
  129. bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override;
  130. void adjustDebugInfoKind(codegenoptions::DebugInfoKind &DebugInfoKind,
  131. const llvm::opt::ArgList &Args) const override;
  132. // NVPTX supports only DWARF2.
  133. unsigned GetDefaultDwarfVersion() const override { return 2; }
  134. unsigned getMaxDwarfVersion() const override { return 2; }
  135. CudaInstallationDetector CudaInstallation;
  136. protected:
  137. Tool *buildAssembler() const override; // ptxas.
  138. Tool *buildLinker() const override; // nvlink.
  139. };
  140. class LLVM_LIBRARY_VISIBILITY CudaToolChain : public NVPTXToolChain {
  141. public:
  142. CudaToolChain(const Driver &D, const llvm::Triple &Triple,
  143. const ToolChain &HostTC, const llvm::opt::ArgList &Args);
  144. const llvm::Triple *getAuxTriple() const override {
  145. return &HostTC.getTriple();
  146. }
  147. std::string getInputFilename(const InputInfo &Input) const override;
  148. llvm::opt::DerivedArgList *
  149. TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
  150. Action::OffloadKind DeviceOffloadKind) const override;
  151. void
  152. addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
  153. llvm::opt::ArgStringList &CC1Args,
  154. Action::OffloadKind DeviceOffloadKind) const override;
  155. llvm::DenormalMode getDefaultDenormalModeForType(
  156. const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
  157. const llvm::fltSemantics *FPType = nullptr) const override;
  158. void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
  159. llvm::opt::ArgStringList &CC1Args) const override;
  160. void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override;
  161. CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override;
  162. void
  163. AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
  164. llvm::opt::ArgStringList &CC1Args) const override;
  165. void AddClangCXXStdlibIncludeArgs(
  166. const llvm::opt::ArgList &Args,
  167. llvm::opt::ArgStringList &CC1Args) const override;
  168. void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
  169. llvm::opt::ArgStringList &CC1Args) const override;
  170. SanitizerMask getSupportedSanitizers() const override;
  171. VersionTuple
  172. computeMSVCVersion(const Driver *D,
  173. const llvm::opt::ArgList &Args) const override;
  174. const ToolChain &HostTC;
  175. /// Uses nvptx-arch tool to get arch of the system GPU. Will return error
  176. /// if unable to find one.
  177. virtual Expected<SmallVector<std::string>>
  178. getSystemGPUArchs(const llvm::opt::ArgList &Args) const override;
  179. protected:
  180. Tool *buildAssembler() const override; // ptxas
  181. Tool *buildLinker() const override; // fatbinary (ok, not really a linker)
  182. };
  183. } // end namespace toolchains
  184. } // end namespace driver
  185. } // end namespace clang
  186. #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H