TargetParser.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //===-- TargetParser - Parser for target features ---------------*- 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 implements a target parser to recognise hardware features such as
  10. // FPU/CPU/ARCH names as well as specific support such as HDIV, etc.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/TargetParser.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "llvm/ADT/Triple.h"
  17. using namespace llvm;
  18. using namespace AMDGPU;
  19. namespace {
  20. struct GPUInfo {
  21. StringLiteral Name;
  22. StringLiteral CanonicalName;
  23. AMDGPU::GPUKind Kind;
  24. unsigned Features;
  25. };
  26. constexpr GPUInfo R600GPUs[] = {
  27. // Name Canonical Kind Features
  28. // Name
  29. {{"r600"}, {"r600"}, GK_R600, FEATURE_NONE },
  30. {{"rv630"}, {"r600"}, GK_R600, FEATURE_NONE },
  31. {{"rv635"}, {"r600"}, GK_R600, FEATURE_NONE },
  32. {{"r630"}, {"r630"}, GK_R630, FEATURE_NONE },
  33. {{"rs780"}, {"rs880"}, GK_RS880, FEATURE_NONE },
  34. {{"rs880"}, {"rs880"}, GK_RS880, FEATURE_NONE },
  35. {{"rv610"}, {"rs880"}, GK_RS880, FEATURE_NONE },
  36. {{"rv620"}, {"rs880"}, GK_RS880, FEATURE_NONE },
  37. {{"rv670"}, {"rv670"}, GK_RV670, FEATURE_NONE },
  38. {{"rv710"}, {"rv710"}, GK_RV710, FEATURE_NONE },
  39. {{"rv730"}, {"rv730"}, GK_RV730, FEATURE_NONE },
  40. {{"rv740"}, {"rv770"}, GK_RV770, FEATURE_NONE },
  41. {{"rv770"}, {"rv770"}, GK_RV770, FEATURE_NONE },
  42. {{"cedar"}, {"cedar"}, GK_CEDAR, FEATURE_NONE },
  43. {{"palm"}, {"cedar"}, GK_CEDAR, FEATURE_NONE },
  44. {{"cypress"}, {"cypress"}, GK_CYPRESS, FEATURE_FMA },
  45. {{"hemlock"}, {"cypress"}, GK_CYPRESS, FEATURE_FMA },
  46. {{"juniper"}, {"juniper"}, GK_JUNIPER, FEATURE_NONE },
  47. {{"redwood"}, {"redwood"}, GK_REDWOOD, FEATURE_NONE },
  48. {{"sumo"}, {"sumo"}, GK_SUMO, FEATURE_NONE },
  49. {{"sumo2"}, {"sumo"}, GK_SUMO, FEATURE_NONE },
  50. {{"barts"}, {"barts"}, GK_BARTS, FEATURE_NONE },
  51. {{"caicos"}, {"caicos"}, GK_CAICOS, FEATURE_NONE },
  52. {{"aruba"}, {"cayman"}, GK_CAYMAN, FEATURE_FMA },
  53. {{"cayman"}, {"cayman"}, GK_CAYMAN, FEATURE_FMA },
  54. {{"turks"}, {"turks"}, GK_TURKS, FEATURE_NONE }
  55. };
  56. // This table should be sorted by the value of GPUKind
  57. // Don't bother listing the implicitly true features
  58. constexpr GPUInfo AMDGCNGPUs[] = {
  59. // Name Canonical Kind Features
  60. // Name
  61. {{"gfx600"}, {"gfx600"}, GK_GFX600, FEATURE_FAST_FMA_F32},
  62. {{"tahiti"}, {"gfx600"}, GK_GFX600, FEATURE_FAST_FMA_F32},
  63. {{"gfx601"}, {"gfx601"}, GK_GFX601, FEATURE_NONE},
  64. {{"pitcairn"}, {"gfx601"}, GK_GFX601, FEATURE_NONE},
  65. {{"verde"}, {"gfx601"}, GK_GFX601, FEATURE_NONE},
  66. {{"gfx602"}, {"gfx602"}, GK_GFX602, FEATURE_NONE},
  67. {{"hainan"}, {"gfx602"}, GK_GFX602, FEATURE_NONE},
  68. {{"oland"}, {"gfx602"}, GK_GFX602, FEATURE_NONE},
  69. {{"gfx700"}, {"gfx700"}, GK_GFX700, FEATURE_NONE},
  70. {{"kaveri"}, {"gfx700"}, GK_GFX700, FEATURE_NONE},
  71. {{"gfx701"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32},
  72. {{"hawaii"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32},
  73. {{"gfx702"}, {"gfx702"}, GK_GFX702, FEATURE_FAST_FMA_F32},
  74. {{"gfx703"}, {"gfx703"}, GK_GFX703, FEATURE_NONE},
  75. {{"kabini"}, {"gfx703"}, GK_GFX703, FEATURE_NONE},
  76. {{"mullins"}, {"gfx703"}, GK_GFX703, FEATURE_NONE},
  77. {{"gfx704"}, {"gfx704"}, GK_GFX704, FEATURE_NONE},
  78. {{"bonaire"}, {"gfx704"}, GK_GFX704, FEATURE_NONE},
  79. {{"gfx705"}, {"gfx705"}, GK_GFX705, FEATURE_NONE},
  80. {{"gfx801"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  81. {{"carrizo"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  82. {{"gfx802"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32},
  83. {{"iceland"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32},
  84. {{"tonga"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32},
  85. {{"gfx803"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32},
  86. {{"fiji"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32},
  87. {{"polaris10"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32},
  88. {{"polaris11"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32},
  89. {{"gfx805"}, {"gfx805"}, GK_GFX805, FEATURE_FAST_DENORMAL_F32},
  90. {{"tongapro"}, {"gfx805"}, GK_GFX805, FEATURE_FAST_DENORMAL_F32},
  91. {{"gfx810"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  92. {{"stoney"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  93. {{"gfx900"}, {"gfx900"}, GK_GFX900, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  94. {{"gfx902"}, {"gfx902"}, GK_GFX902, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  95. {{"gfx904"}, {"gfx904"}, GK_GFX904, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  96. {{"gfx906"}, {"gfx906"}, GK_GFX906, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
  97. {{"gfx908"}, {"gfx908"}, GK_GFX908, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
  98. {{"gfx909"}, {"gfx909"}, GK_GFX909, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  99. {{"gfx90a"}, {"gfx90a"}, GK_GFX90A, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
  100. {{"gfx90c"}, {"gfx90c"}, GK_GFX90C, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
  101. {{"gfx1010"}, {"gfx1010"}, GK_GFX1010, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK},
  102. {{"gfx1011"}, {"gfx1011"}, GK_GFX1011, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK},
  103. {{"gfx1012"}, {"gfx1012"}, GK_GFX1012, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK},
  104. {{"gfx1013"}, {"gfx1013"}, GK_GFX1013, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK},
  105. {{"gfx1030"}, {"gfx1030"}, GK_GFX1030, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  106. {{"gfx1031"}, {"gfx1031"}, GK_GFX1031, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  107. {{"gfx1032"}, {"gfx1032"}, GK_GFX1032, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  108. {{"gfx1033"}, {"gfx1033"}, GK_GFX1033, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  109. {{"gfx1034"}, {"gfx1034"}, GK_GFX1034, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  110. {{"gfx1035"}, {"gfx1035"}, GK_GFX1035, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32},
  111. };
  112. const GPUInfo *getArchEntry(AMDGPU::GPUKind AK, ArrayRef<GPUInfo> Table) {
  113. GPUInfo Search = { {""}, {""}, AK, AMDGPU::FEATURE_NONE };
  114. auto I =
  115. llvm::lower_bound(Table, Search, [](const GPUInfo &A, const GPUInfo &B) {
  116. return A.Kind < B.Kind;
  117. });
  118. if (I == Table.end())
  119. return nullptr;
  120. return I;
  121. }
  122. } // namespace
  123. StringRef llvm::AMDGPU::getArchNameAMDGCN(GPUKind AK) {
  124. if (const auto *Entry = getArchEntry(AK, AMDGCNGPUs))
  125. return Entry->CanonicalName;
  126. return "";
  127. }
  128. StringRef llvm::AMDGPU::getArchNameR600(GPUKind AK) {
  129. if (const auto *Entry = getArchEntry(AK, R600GPUs))
  130. return Entry->CanonicalName;
  131. return "";
  132. }
  133. AMDGPU::GPUKind llvm::AMDGPU::parseArchAMDGCN(StringRef CPU) {
  134. for (const auto &C : AMDGCNGPUs) {
  135. if (CPU == C.Name)
  136. return C.Kind;
  137. }
  138. return AMDGPU::GPUKind::GK_NONE;
  139. }
  140. AMDGPU::GPUKind llvm::AMDGPU::parseArchR600(StringRef CPU) {
  141. for (const auto &C : R600GPUs) {
  142. if (CPU == C.Name)
  143. return C.Kind;
  144. }
  145. return AMDGPU::GPUKind::GK_NONE;
  146. }
  147. unsigned AMDGPU::getArchAttrAMDGCN(GPUKind AK) {
  148. if (const auto *Entry = getArchEntry(AK, AMDGCNGPUs))
  149. return Entry->Features;
  150. return FEATURE_NONE;
  151. }
  152. unsigned AMDGPU::getArchAttrR600(GPUKind AK) {
  153. if (const auto *Entry = getArchEntry(AK, R600GPUs))
  154. return Entry->Features;
  155. return FEATURE_NONE;
  156. }
  157. void AMDGPU::fillValidArchListAMDGCN(SmallVectorImpl<StringRef> &Values) {
  158. // XXX: Should this only report unique canonical names?
  159. for (const auto &C : AMDGCNGPUs)
  160. Values.push_back(C.Name);
  161. }
  162. void AMDGPU::fillValidArchListR600(SmallVectorImpl<StringRef> &Values) {
  163. for (const auto &C : R600GPUs)
  164. Values.push_back(C.Name);
  165. }
  166. AMDGPU::IsaVersion AMDGPU::getIsaVersion(StringRef GPU) {
  167. AMDGPU::GPUKind AK = parseArchAMDGCN(GPU);
  168. if (AK == AMDGPU::GPUKind::GK_NONE) {
  169. if (GPU == "generic-hsa")
  170. return {7, 0, 0};
  171. if (GPU == "generic")
  172. return {6, 0, 0};
  173. return {0, 0, 0};
  174. }
  175. switch (AK) {
  176. case GK_GFX600: return {6, 0, 0};
  177. case GK_GFX601: return {6, 0, 1};
  178. case GK_GFX602: return {6, 0, 2};
  179. case GK_GFX700: return {7, 0, 0};
  180. case GK_GFX701: return {7, 0, 1};
  181. case GK_GFX702: return {7, 0, 2};
  182. case GK_GFX703: return {7, 0, 3};
  183. case GK_GFX704: return {7, 0, 4};
  184. case GK_GFX705: return {7, 0, 5};
  185. case GK_GFX801: return {8, 0, 1};
  186. case GK_GFX802: return {8, 0, 2};
  187. case GK_GFX803: return {8, 0, 3};
  188. case GK_GFX805: return {8, 0, 5};
  189. case GK_GFX810: return {8, 1, 0};
  190. case GK_GFX900: return {9, 0, 0};
  191. case GK_GFX902: return {9, 0, 2};
  192. case GK_GFX904: return {9, 0, 4};
  193. case GK_GFX906: return {9, 0, 6};
  194. case GK_GFX908: return {9, 0, 8};
  195. case GK_GFX909: return {9, 0, 9};
  196. case GK_GFX90A: return {9, 0, 10};
  197. case GK_GFX90C: return {9, 0, 12};
  198. case GK_GFX1010: return {10, 1, 0};
  199. case GK_GFX1011: return {10, 1, 1};
  200. case GK_GFX1012: return {10, 1, 2};
  201. case GK_GFX1013: return {10, 1, 3};
  202. case GK_GFX1030: return {10, 3, 0};
  203. case GK_GFX1031: return {10, 3, 1};
  204. case GK_GFX1032: return {10, 3, 2};
  205. case GK_GFX1033: return {10, 3, 3};
  206. case GK_GFX1034: return {10, 3, 4};
  207. case GK_GFX1035: return {10, 3, 5};
  208. default: return {0, 0, 0};
  209. }
  210. }
  211. StringRef AMDGPU::getCanonicalArchName(const Triple &T, StringRef Arch) {
  212. assert(T.isAMDGPU());
  213. auto ProcKind = T.isAMDGCN() ? parseArchAMDGCN(Arch) : parseArchR600(Arch);
  214. if (ProcKind == GK_NONE)
  215. return StringRef();
  216. return T.isAMDGCN() ? getArchNameAMDGCN(ProcKind) : getArchNameR600(ProcKind);
  217. }
  218. namespace llvm {
  219. namespace RISCV {
  220. struct CPUInfo {
  221. StringLiteral Name;
  222. CPUKind Kind;
  223. unsigned Features;
  224. StringLiteral DefaultMarch;
  225. bool is64Bit() const { return (Features & FK_64BIT); }
  226. };
  227. constexpr CPUInfo RISCVCPUInfo[] = {
  228. #define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH) \
  229. {NAME, CK_##ENUM, FEATURES, DEFAULT_MARCH},
  230. #include "llvm/Support/RISCVTargetParser.def"
  231. };
  232. bool checkCPUKind(CPUKind Kind, bool IsRV64) {
  233. if (Kind == CK_INVALID)
  234. return false;
  235. return RISCVCPUInfo[static_cast<unsigned>(Kind)].is64Bit() == IsRV64;
  236. }
  237. bool checkTuneCPUKind(CPUKind Kind, bool IsRV64) {
  238. if (Kind == CK_INVALID)
  239. return false;
  240. return RISCVCPUInfo[static_cast<unsigned>(Kind)].is64Bit() == IsRV64;
  241. }
  242. CPUKind parseCPUKind(StringRef CPU) {
  243. return llvm::StringSwitch<CPUKind>(CPU)
  244. #define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH) .Case(NAME, CK_##ENUM)
  245. #include "llvm/Support/RISCVTargetParser.def"
  246. .Default(CK_INVALID);
  247. }
  248. StringRef resolveTuneCPUAlias(StringRef TuneCPU, bool IsRV64) {
  249. return llvm::StringSwitch<StringRef>(TuneCPU)
  250. #define PROC_ALIAS(NAME, RV32, RV64) .Case(NAME, IsRV64 ? StringRef(RV64) : StringRef(RV32))
  251. #include "llvm/Support/RISCVTargetParser.def"
  252. .Default(TuneCPU);
  253. }
  254. CPUKind parseTuneCPUKind(StringRef TuneCPU, bool IsRV64) {
  255. TuneCPU = resolveTuneCPUAlias(TuneCPU, IsRV64);
  256. return llvm::StringSwitch<CPUKind>(TuneCPU)
  257. #define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH) .Case(NAME, CK_##ENUM)
  258. #include "llvm/Support/RISCVTargetParser.def"
  259. .Default(CK_INVALID);
  260. }
  261. StringRef getMArchFromMcpu(StringRef CPU) {
  262. CPUKind Kind = parseCPUKind(CPU);
  263. return RISCVCPUInfo[static_cast<unsigned>(Kind)].DefaultMarch;
  264. }
  265. void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64) {
  266. for (const auto &C : RISCVCPUInfo) {
  267. if (C.Kind != CK_INVALID && IsRV64 == C.is64Bit())
  268. Values.emplace_back(C.Name);
  269. }
  270. }
  271. void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64) {
  272. for (const auto &C : RISCVCPUInfo) {
  273. if (C.Kind != CK_INVALID && IsRV64 == C.is64Bit())
  274. Values.emplace_back(C.Name);
  275. }
  276. #define PROC_ALIAS(NAME, RV32, RV64) Values.emplace_back(StringRef(NAME));
  277. #include "llvm/Support/RISCVTargetParser.def"
  278. }
  279. // Get all features except standard extension feature
  280. bool getCPUFeaturesExceptStdExt(CPUKind Kind,
  281. std::vector<StringRef> &Features) {
  282. unsigned CPUFeatures = RISCVCPUInfo[static_cast<unsigned>(Kind)].Features;
  283. if (CPUFeatures == FK_INVALID)
  284. return false;
  285. if (CPUFeatures & FK_64BIT)
  286. Features.push_back("+64bit");
  287. else
  288. Features.push_back("-64bit");
  289. return true;
  290. }
  291. StringRef computeDefaultABIFromArch(const llvm::RISCVISAInfo &ISAInfo) {
  292. if (ISAInfo.getXLen() == 32) {
  293. if (ISAInfo.hasExtension("d"))
  294. return "ilp32d";
  295. if (ISAInfo.hasExtension("e"))
  296. return "ilp32e";
  297. return "ilp32";
  298. } else if (ISAInfo.getXLen() == 64) {
  299. if (ISAInfo.hasExtension("d"))
  300. return "lp64d";
  301. return "lp64";
  302. }
  303. llvm_unreachable("Invalid XLEN");
  304. }
  305. } // namespace RISCV
  306. } // namespace llvm
  307. // Parse a branch protection specification, which has the form
  308. // standard | none | [bti,pac-ret[+b-key,+leaf]*]
  309. // Returns true on success, with individual elements of the specification
  310. // returned in `PBP`. Returns false in error, with `Err` containing
  311. // an erroneous part of the spec.
  312. bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
  313. StringRef &Err) {
  314. PBP = {"none", "a_key", false};
  315. if (Spec == "none")
  316. return true; // defaults are ok
  317. if (Spec == "standard") {
  318. PBP.Scope = "non-leaf";
  319. PBP.BranchTargetEnforcement = true;
  320. return true;
  321. }
  322. SmallVector<StringRef, 4> Opts;
  323. Spec.split(Opts, "+");
  324. for (int I = 0, E = Opts.size(); I != E; ++I) {
  325. StringRef Opt = Opts[I].trim();
  326. if (Opt == "bti") {
  327. PBP.BranchTargetEnforcement = true;
  328. continue;
  329. }
  330. if (Opt == "pac-ret") {
  331. PBP.Scope = "non-leaf";
  332. for (; I + 1 != E; ++I) {
  333. StringRef PACOpt = Opts[I + 1].trim();
  334. if (PACOpt == "leaf")
  335. PBP.Scope = "all";
  336. else if (PACOpt == "b-key")
  337. PBP.Key = "b_key";
  338. else
  339. break;
  340. }
  341. continue;
  342. }
  343. if (Opt == "")
  344. Err = "<empty>";
  345. else
  346. Err = Opt;
  347. return false;
  348. }
  349. return true;
  350. }