NVPTX.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===--- NVPTX.h - Declare NVPTX target feature support ---------*- 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. // This file declares NVPTX TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
  13. #define LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
  14. #include "clang/Basic/Cuda.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/Basic/TargetOptions.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include <optional>
  20. namespace clang {
  21. namespace targets {
  22. static const unsigned NVPTXAddrSpaceMap[] = {
  23. 0, // Default
  24. 1, // opencl_global
  25. 3, // opencl_local
  26. 4, // opencl_constant
  27. 0, // opencl_private
  28. // FIXME: generic has to be added to the target
  29. 0, // opencl_generic
  30. 1, // opencl_global_device
  31. 1, // opencl_global_host
  32. 1, // cuda_device
  33. 4, // cuda_constant
  34. 3, // cuda_shared
  35. 1, // sycl_global
  36. 1, // sycl_global_device
  37. 1, // sycl_global_host
  38. 3, // sycl_local
  39. 0, // sycl_private
  40. 0, // ptr32_sptr
  41. 0, // ptr32_uptr
  42. 0, // ptr64
  43. 0, // hlsl_groupshared
  44. };
  45. /// The DWARF address class. Taken from
  46. /// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
  47. static const int NVPTXDWARFAddrSpaceMap[] = {
  48. -1, // Default, opencl_private or opencl_generic - not defined
  49. 5, // opencl_global
  50. -1,
  51. 8, // opencl_local or cuda_shared
  52. 4, // opencl_constant or cuda_constant
  53. };
  54. class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
  55. static const char *const GCCRegNames[];
  56. CudaArch GPU;
  57. uint32_t PTXVersion;
  58. std::unique_ptr<TargetInfo> HostTarget;
  59. public:
  60. NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts,
  61. unsigned TargetPointerWidth);
  62. void getTargetDefines(const LangOptions &Opts,
  63. MacroBuilder &Builder) const override;
  64. ArrayRef<Builtin::Info> getTargetBuiltins() const override;
  65. bool
  66. initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
  67. StringRef CPU,
  68. const std::vector<std::string> &FeaturesVec) const override {
  69. Features[CudaArchToString(GPU)] = true;
  70. Features["ptx" + std::to_string(PTXVersion)] = true;
  71. return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
  72. }
  73. bool hasFeature(StringRef Feature) const override;
  74. ArrayRef<const char *> getGCCRegNames() const override;
  75. ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
  76. // No aliases.
  77. return std::nullopt;
  78. }
  79. bool validateAsmConstraint(const char *&Name,
  80. TargetInfo::ConstraintInfo &Info) const override {
  81. switch (*Name) {
  82. default:
  83. return false;
  84. case 'c':
  85. case 'h':
  86. case 'r':
  87. case 'l':
  88. case 'f':
  89. case 'd':
  90. Info.setAllowsRegister();
  91. return true;
  92. }
  93. }
  94. const char *getClobbers() const override {
  95. // FIXME: Is this really right?
  96. return "";
  97. }
  98. BuiltinVaListKind getBuiltinVaListKind() const override {
  99. // FIXME: implement
  100. return TargetInfo::CharPtrBuiltinVaList;
  101. }
  102. bool isValidCPUName(StringRef Name) const override {
  103. return StringToCudaArch(Name) != CudaArch::UNKNOWN;
  104. }
  105. void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override {
  106. for (int i = static_cast<int>(CudaArch::SM_20);
  107. i < static_cast<int>(CudaArch::Generic); ++i)
  108. Values.emplace_back(CudaArchToString(static_cast<CudaArch>(i)));
  109. }
  110. bool setCPU(const std::string &Name) override {
  111. GPU = StringToCudaArch(Name);
  112. return GPU != CudaArch::UNKNOWN;
  113. }
  114. void setSupportedOpenCLOpts() override {
  115. auto &Opts = getSupportedOpenCLOpts();
  116. Opts["cl_clang_storage_class_specifiers"] = true;
  117. Opts["__cl_clang_function_pointers"] = true;
  118. Opts["__cl_clang_variadic_functions"] = true;
  119. Opts["__cl_clang_non_portable_kernel_param_types"] = true;
  120. Opts["__cl_clang_bitfields"] = true;
  121. Opts["cl_khr_fp64"] = true;
  122. Opts["__opencl_c_fp64"] = true;
  123. Opts["cl_khr_byte_addressable_store"] = true;
  124. Opts["cl_khr_global_int32_base_atomics"] = true;
  125. Opts["cl_khr_global_int32_extended_atomics"] = true;
  126. Opts["cl_khr_local_int32_base_atomics"] = true;
  127. Opts["cl_khr_local_int32_extended_atomics"] = true;
  128. }
  129. const llvm::omp::GV &getGridValue() const override {
  130. return llvm::omp::NVPTXGridValues;
  131. }
  132. /// \returns If a target requires an address within a target specific address
  133. /// space \p AddressSpace to be converted in order to be used, then return the
  134. /// corresponding target specific DWARF address space.
  135. ///
  136. /// \returns Otherwise return std::nullopt and no conversion will be emitted
  137. /// in the DWARF.
  138. std::optional<unsigned>
  139. getDWARFAddressSpace(unsigned AddressSpace) const override {
  140. if (AddressSpace >= std::size(NVPTXDWARFAddrSpaceMap) ||
  141. NVPTXDWARFAddrSpaceMap[AddressSpace] < 0)
  142. return std::nullopt;
  143. return NVPTXDWARFAddrSpaceMap[AddressSpace];
  144. }
  145. CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
  146. // CUDA compilations support all of the host's calling conventions.
  147. //
  148. // TODO: We should warn if you apply a non-default CC to anything other than
  149. // a host function.
  150. if (HostTarget)
  151. return HostTarget->checkCallingConvention(CC);
  152. return CCCR_Warning;
  153. }
  154. bool hasBitIntType() const override { return true; }
  155. bool hasBFloat16Type() const override { return true; }
  156. const char *getBFloat16Mangling() const override { return "u6__bf16"; };
  157. };
  158. } // namespace targets
  159. } // namespace clang
  160. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H