AMDGPU.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //===--- AMDGPU.h - Declare AMDGPU 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 AMDGPU TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
  13. #define LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
  14. #include "clang/Basic/TargetID.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/Basic/TargetOptions.h"
  17. #include "llvm/ADT/StringSet.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/Support/Compiler.h"
  20. #include "llvm/Support/TargetParser.h"
  21. namespace clang {
  22. namespace targets {
  23. class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
  24. static const Builtin::Info BuiltinInfo[];
  25. static const char *const GCCRegNames[];
  26. enum AddrSpace {
  27. Generic = 0,
  28. Global = 1,
  29. Local = 3,
  30. Constant = 4,
  31. Private = 5
  32. };
  33. static const LangASMap AMDGPUDefIsGenMap;
  34. static const LangASMap AMDGPUDefIsPrivMap;
  35. llvm::AMDGPU::GPUKind GPUKind;
  36. unsigned GPUFeatures;
  37. unsigned WavefrontSize;
  38. /// Target ID is device name followed by optional feature name postfixed
  39. /// by plus or minus sign delimitted by colon, e.g. gfx908:xnack+:sramecc-.
  40. /// If the target ID contains feature+, map it to true.
  41. /// If the target ID contains feature-, map it to false.
  42. /// If the target ID does not contain a feature (default), do not map it.
  43. llvm::StringMap<bool> OffloadArchFeatures;
  44. std::string TargetID;
  45. bool hasFP64() const {
  46. return getTriple().getArch() == llvm::Triple::amdgcn ||
  47. !!(GPUFeatures & llvm::AMDGPU::FEATURE_FP64);
  48. }
  49. /// Has fast fma f32
  50. bool hasFastFMAF() const {
  51. return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_FMA_F32);
  52. }
  53. /// Has fast fma f64
  54. bool hasFastFMA() const {
  55. return getTriple().getArch() == llvm::Triple::amdgcn;
  56. }
  57. bool hasFMAF() const {
  58. return getTriple().getArch() == llvm::Triple::amdgcn ||
  59. !!(GPUFeatures & llvm::AMDGPU::FEATURE_FMA);
  60. }
  61. bool hasFullRateDenormalsF32() const {
  62. return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
  63. }
  64. bool hasLDEXPF() const {
  65. return getTriple().getArch() == llvm::Triple::amdgcn ||
  66. !!(GPUFeatures & llvm::AMDGPU::FEATURE_LDEXP);
  67. }
  68. static bool isAMDGCN(const llvm::Triple &TT) {
  69. return TT.getArch() == llvm::Triple::amdgcn;
  70. }
  71. static bool isR600(const llvm::Triple &TT) {
  72. return TT.getArch() == llvm::Triple::r600;
  73. }
  74. public:
  75. AMDGPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
  76. void setAddressSpaceMap(bool DefaultIsPrivate);
  77. void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
  78. uint64_t getPointerWidthV(unsigned AddrSpace) const override {
  79. if (isR600(getTriple()))
  80. return 32;
  81. if (AddrSpace == Private || AddrSpace == Local)
  82. return 32;
  83. return 64;
  84. }
  85. uint64_t getPointerAlignV(unsigned AddrSpace) const override {
  86. return getPointerWidthV(AddrSpace);
  87. }
  88. uint64_t getMaxPointerWidth() const override {
  89. return getTriple().getArch() == llvm::Triple::amdgcn ? 64 : 32;
  90. }
  91. const char *getClobbers() const override { return ""; }
  92. ArrayRef<const char *> getGCCRegNames() const override;
  93. ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
  94. return None;
  95. }
  96. /// Accepted register names: (n, m is unsigned integer, n < m)
  97. /// v
  98. /// s
  99. /// a
  100. /// {vn}, {v[n]}
  101. /// {sn}, {s[n]}
  102. /// {an}, {a[n]}
  103. /// {S} , where S is a special register name
  104. ////{v[n:m]}
  105. /// {s[n:m]}
  106. /// {a[n:m]}
  107. bool validateAsmConstraint(const char *&Name,
  108. TargetInfo::ConstraintInfo &Info) const override {
  109. static const ::llvm::StringSet<> SpecialRegs({
  110. "exec", "vcc", "flat_scratch", "m0", "scc", "tba", "tma",
  111. "flat_scratch_lo", "flat_scratch_hi", "vcc_lo", "vcc_hi", "exec_lo",
  112. "exec_hi", "tma_lo", "tma_hi", "tba_lo", "tba_hi",
  113. });
  114. switch (*Name) {
  115. case 'I':
  116. Info.setRequiresImmediate(-16, 64);
  117. return true;
  118. case 'J':
  119. Info.setRequiresImmediate(-32768, 32767);
  120. return true;
  121. case 'A':
  122. case 'B':
  123. case 'C':
  124. Info.setRequiresImmediate();
  125. return true;
  126. default:
  127. break;
  128. }
  129. StringRef S(Name);
  130. if (S == "DA" || S == "DB") {
  131. Name++;
  132. Info.setRequiresImmediate();
  133. return true;
  134. }
  135. bool HasLeftParen = false;
  136. if (S.front() == '{') {
  137. HasLeftParen = true;
  138. S = S.drop_front();
  139. }
  140. if (S.empty())
  141. return false;
  142. if (S.front() != 'v' && S.front() != 's' && S.front() != 'a') {
  143. if (!HasLeftParen)
  144. return false;
  145. auto E = S.find('}');
  146. if (!SpecialRegs.count(S.substr(0, E)))
  147. return false;
  148. S = S.drop_front(E + 1);
  149. if (!S.empty())
  150. return false;
  151. // Found {S} where S is a special register.
  152. Info.setAllowsRegister();
  153. Name = S.data() - 1;
  154. return true;
  155. }
  156. S = S.drop_front();
  157. if (!HasLeftParen) {
  158. if (!S.empty())
  159. return false;
  160. // Found s, v or a.
  161. Info.setAllowsRegister();
  162. Name = S.data() - 1;
  163. return true;
  164. }
  165. bool HasLeftBracket = false;
  166. if (!S.empty() && S.front() == '[') {
  167. HasLeftBracket = true;
  168. S = S.drop_front();
  169. }
  170. unsigned long long N;
  171. if (S.empty() || consumeUnsignedInteger(S, 10, N))
  172. return false;
  173. if (!S.empty() && S.front() == ':') {
  174. if (!HasLeftBracket)
  175. return false;
  176. S = S.drop_front();
  177. unsigned long long M;
  178. if (consumeUnsignedInteger(S, 10, M) || N >= M)
  179. return false;
  180. }
  181. if (HasLeftBracket) {
  182. if (S.empty() || S.front() != ']')
  183. return false;
  184. S = S.drop_front();
  185. }
  186. if (S.empty() || S.front() != '}')
  187. return false;
  188. S = S.drop_front();
  189. if (!S.empty())
  190. return false;
  191. // Found {vn}, {sn}, {an}, {v[n]}, {s[n]}, {a[n]}, {v[n:m]}, {s[n:m]}
  192. // or {a[n:m]}.
  193. Info.setAllowsRegister();
  194. Name = S.data() - 1;
  195. return true;
  196. }
  197. // \p Constraint will be left pointing at the last character of
  198. // the constraint. In practice, it won't be changed unless the
  199. // constraint is longer than one character.
  200. std::string convertConstraint(const char *&Constraint) const override {
  201. StringRef S(Constraint);
  202. if (S == "DA" || S == "DB") {
  203. return std::string("^") + std::string(Constraint++, 2);
  204. }
  205. const char *Begin = Constraint;
  206. TargetInfo::ConstraintInfo Info("", "");
  207. if (validateAsmConstraint(Constraint, Info))
  208. return std::string(Begin).substr(0, Constraint - Begin + 1);
  209. Constraint = Begin;
  210. return std::string(1, *Constraint);
  211. }
  212. bool
  213. initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
  214. StringRef CPU,
  215. const std::vector<std::string> &FeatureVec) const override;
  216. ArrayRef<Builtin::Info> getTargetBuiltins() const override;
  217. bool useFP16ConversionIntrinsics() const override { return false; }
  218. void getTargetDefines(const LangOptions &Opts,
  219. MacroBuilder &Builder) const override;
  220. BuiltinVaListKind getBuiltinVaListKind() const override {
  221. return TargetInfo::CharPtrBuiltinVaList;
  222. }
  223. bool isValidCPUName(StringRef Name) const override {
  224. if (getTriple().getArch() == llvm::Triple::amdgcn)
  225. return llvm::AMDGPU::parseArchAMDGCN(Name) != llvm::AMDGPU::GK_NONE;
  226. return llvm::AMDGPU::parseArchR600(Name) != llvm::AMDGPU::GK_NONE;
  227. }
  228. void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
  229. bool setCPU(const std::string &Name) override {
  230. if (getTriple().getArch() == llvm::Triple::amdgcn) {
  231. GPUKind = llvm::AMDGPU::parseArchAMDGCN(Name);
  232. GPUFeatures = llvm::AMDGPU::getArchAttrAMDGCN(GPUKind);
  233. } else {
  234. GPUKind = llvm::AMDGPU::parseArchR600(Name);
  235. GPUFeatures = llvm::AMDGPU::getArchAttrR600(GPUKind);
  236. }
  237. return GPUKind != llvm::AMDGPU::GK_NONE;
  238. }
  239. void setSupportedOpenCLOpts() override {
  240. auto &Opts = getSupportedOpenCLOpts();
  241. Opts["cl_clang_storage_class_specifiers"] = true;
  242. Opts["__cl_clang_variadic_functions"] = true;
  243. Opts["__cl_clang_function_pointers"] = true;
  244. Opts["__cl_clang_non_portable_kernel_param_types"] = true;
  245. Opts["__cl_clang_bitfields"] = true;
  246. bool IsAMDGCN = isAMDGCN(getTriple());
  247. Opts["cl_khr_fp64"] = hasFP64();
  248. Opts["__opencl_c_fp64"] = hasFP64();
  249. if (IsAMDGCN || GPUKind >= llvm::AMDGPU::GK_CEDAR) {
  250. Opts["cl_khr_byte_addressable_store"] = true;
  251. Opts["cl_khr_global_int32_base_atomics"] = true;
  252. Opts["cl_khr_global_int32_extended_atomics"] = true;
  253. Opts["cl_khr_local_int32_base_atomics"] = true;
  254. Opts["cl_khr_local_int32_extended_atomics"] = true;
  255. }
  256. if (IsAMDGCN) {
  257. Opts["cl_khr_fp16"] = true;
  258. Opts["cl_khr_int64_base_atomics"] = true;
  259. Opts["cl_khr_int64_extended_atomics"] = true;
  260. Opts["cl_khr_mipmap_image"] = true;
  261. Opts["cl_khr_mipmap_image_writes"] = true;
  262. Opts["cl_khr_subgroups"] = true;
  263. Opts["cl_amd_media_ops"] = true;
  264. Opts["cl_amd_media_ops2"] = true;
  265. Opts["__opencl_c_images"] = true;
  266. Opts["__opencl_c_3d_image_writes"] = true;
  267. Opts["cl_khr_3d_image_writes"] = true;
  268. }
  269. }
  270. LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const override {
  271. switch (TK) {
  272. case OCLTK_Image:
  273. return LangAS::opencl_constant;
  274. case OCLTK_ClkEvent:
  275. case OCLTK_Queue:
  276. case OCLTK_ReserveID:
  277. return LangAS::opencl_global;
  278. default:
  279. return TargetInfo::getOpenCLTypeAddrSpace(TK);
  280. }
  281. }
  282. LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override {
  283. switch (AS) {
  284. case 0:
  285. return LangAS::opencl_generic;
  286. case 1:
  287. return LangAS::opencl_global;
  288. case 3:
  289. return LangAS::opencl_local;
  290. case 4:
  291. return LangAS::opencl_constant;
  292. case 5:
  293. return LangAS::opencl_private;
  294. default:
  295. return getLangASFromTargetAS(AS);
  296. }
  297. }
  298. LangAS getCUDABuiltinAddressSpace(unsigned AS) const override {
  299. switch (AS) {
  300. case 0:
  301. return LangAS::Default;
  302. case 1:
  303. return LangAS::cuda_device;
  304. case 3:
  305. return LangAS::cuda_shared;
  306. case 4:
  307. return LangAS::cuda_constant;
  308. default:
  309. return getLangASFromTargetAS(AS);
  310. }
  311. }
  312. llvm::Optional<LangAS> getConstantAddressSpace() const override {
  313. return getLangASFromTargetAS(Constant);
  314. }
  315. const llvm::omp::GV &getGridValue() const override {
  316. switch (WavefrontSize) {
  317. case 32:
  318. return llvm::omp::getAMDGPUGridValues<32>();
  319. case 64:
  320. return llvm::omp::getAMDGPUGridValues<64>();
  321. default:
  322. llvm_unreachable("getGridValue not implemented for this wavesize");
  323. }
  324. }
  325. /// \returns Target specific vtbl ptr address space.
  326. unsigned getVtblPtrAddressSpace() const override {
  327. return static_cast<unsigned>(Constant);
  328. }
  329. /// \returns If a target requires an address within a target specific address
  330. /// space \p AddressSpace to be converted in order to be used, then return the
  331. /// corresponding target specific DWARF address space.
  332. ///
  333. /// \returns Otherwise return None and no conversion will be emitted in the
  334. /// DWARF.
  335. Optional<unsigned>
  336. getDWARFAddressSpace(unsigned AddressSpace) const override {
  337. const unsigned DWARF_Private = 1;
  338. const unsigned DWARF_Local = 2;
  339. if (AddressSpace == Private) {
  340. return DWARF_Private;
  341. } else if (AddressSpace == Local) {
  342. return DWARF_Local;
  343. } else {
  344. return None;
  345. }
  346. }
  347. CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
  348. switch (CC) {
  349. default:
  350. return CCCR_Warning;
  351. case CC_C:
  352. case CC_OpenCLKernel:
  353. return CCCR_OK;
  354. }
  355. }
  356. // In amdgcn target the null pointer in global, constant, and generic
  357. // address space has value 0 but in private and local address space has
  358. // value ~0.
  359. uint64_t getNullPointerValue(LangAS AS) const override {
  360. // FIXME: Also should handle region.
  361. return (AS == LangAS::opencl_local || AS == LangAS::opencl_private)
  362. ? ~0 : 0;
  363. }
  364. void setAuxTarget(const TargetInfo *Aux) override;
  365. bool hasBitIntType() const override { return true; }
  366. // Record offload arch features since they are needed for defining the
  367. // pre-defined macros.
  368. bool handleTargetFeatures(std::vector<std::string> &Features,
  369. DiagnosticsEngine &Diags) override {
  370. auto TargetIDFeatures =
  371. getAllPossibleTargetIDFeatures(getTriple(), getArchNameAMDGCN(GPUKind));
  372. llvm::for_each(Features, [&](const auto &F) {
  373. assert(F.front() == '+' || F.front() == '-');
  374. if (F == "+wavefrontsize64")
  375. WavefrontSize = 64;
  376. bool IsOn = F.front() == '+';
  377. StringRef Name = StringRef(F).drop_front();
  378. if (!llvm::is_contained(TargetIDFeatures, Name))
  379. return;
  380. assert(OffloadArchFeatures.find(Name) == OffloadArchFeatures.end());
  381. OffloadArchFeatures[Name] = IsOn;
  382. });
  383. return true;
  384. }
  385. Optional<std::string> getTargetID() const override {
  386. if (!isAMDGCN(getTriple()))
  387. return llvm::None;
  388. // When -target-cpu is not set, we assume generic code that it is valid
  389. // for all GPU and use an empty string as target ID to represent that.
  390. if (GPUKind == llvm::AMDGPU::GK_NONE)
  391. return std::string("");
  392. return getCanonicalTargetID(getArchNameAMDGCN(GPUKind),
  393. OffloadArchFeatures);
  394. }
  395. };
  396. } // namespace targets
  397. } // namespace clang
  398. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H