NVPTX.h 5.7 KB

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