Cuda.h 7.6 KB

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