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