xref: /freebsd/contrib/llvm-project/llvm/lib/TargetParser/TargetParser.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/TargetParser/TargetParser.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/TargetParser/Triple.h"
17 
18 using namespace llvm;
19 using namespace AMDGPU;
20 
21 namespace {
22 
23 struct GPUInfo {
24   StringLiteral Name;
25   StringLiteral CanonicalName;
26   AMDGPU::GPUKind Kind;
27   unsigned Features;
28 };
29 
30 constexpr GPUInfo R600GPUs[] = {
31   // Name       Canonical    Kind        Features
32   //            Name
33   {{"r600"},    {"r600"},    GK_R600,    FEATURE_NONE },
34   {{"rv630"},   {"r600"},    GK_R600,    FEATURE_NONE },
35   {{"rv635"},   {"r600"},    GK_R600,    FEATURE_NONE },
36   {{"r630"},    {"r630"},    GK_R630,    FEATURE_NONE },
37   {{"rs780"},   {"rs880"},   GK_RS880,   FEATURE_NONE },
38   {{"rs880"},   {"rs880"},   GK_RS880,   FEATURE_NONE },
39   {{"rv610"},   {"rs880"},   GK_RS880,   FEATURE_NONE },
40   {{"rv620"},   {"rs880"},   GK_RS880,   FEATURE_NONE },
41   {{"rv670"},   {"rv670"},   GK_RV670,   FEATURE_NONE },
42   {{"rv710"},   {"rv710"},   GK_RV710,   FEATURE_NONE },
43   {{"rv730"},   {"rv730"},   GK_RV730,   FEATURE_NONE },
44   {{"rv740"},   {"rv770"},   GK_RV770,   FEATURE_NONE },
45   {{"rv770"},   {"rv770"},   GK_RV770,   FEATURE_NONE },
46   {{"cedar"},   {"cedar"},   GK_CEDAR,   FEATURE_NONE },
47   {{"palm"},    {"cedar"},   GK_CEDAR,   FEATURE_NONE },
48   {{"cypress"}, {"cypress"}, GK_CYPRESS, FEATURE_FMA  },
49   {{"hemlock"}, {"cypress"}, GK_CYPRESS, FEATURE_FMA  },
50   {{"juniper"}, {"juniper"}, GK_JUNIPER, FEATURE_NONE },
51   {{"redwood"}, {"redwood"}, GK_REDWOOD, FEATURE_NONE },
52   {{"sumo"},    {"sumo"},    GK_SUMO,    FEATURE_NONE },
53   {{"sumo2"},   {"sumo"},    GK_SUMO,    FEATURE_NONE },
54   {{"barts"},   {"barts"},   GK_BARTS,   FEATURE_NONE },
55   {{"caicos"},  {"caicos"},  GK_CAICOS,  FEATURE_NONE },
56   {{"aruba"},   {"cayman"},  GK_CAYMAN,  FEATURE_FMA  },
57   {{"cayman"},  {"cayman"},  GK_CAYMAN,  FEATURE_FMA  },
58   {{"turks"},   {"turks"},   GK_TURKS,   FEATURE_NONE }
59 };
60 
61 // This table should be sorted by the value of GPUKind
62 // Don't bother listing the implicitly true features
63 constexpr GPUInfo AMDGCNGPUs[] = {
64     // clang-format off
65     // Name         Canonical    Kind        Features
66     //              Name
67     {{"gfx600"},    {"gfx600"},  GK_GFX600,  FEATURE_FAST_FMA_F32},
68     {{"tahiti"},    {"gfx600"},  GK_GFX600,  FEATURE_FAST_FMA_F32},
69     {{"gfx601"},    {"gfx601"},  GK_GFX601,  FEATURE_NONE},
70     {{"pitcairn"},  {"gfx601"},  GK_GFX601,  FEATURE_NONE},
71     {{"verde"},     {"gfx601"},  GK_GFX601,  FEATURE_NONE},
72     {{"gfx602"},    {"gfx602"},  GK_GFX602,  FEATURE_NONE},
73     {{"hainan"},    {"gfx602"},  GK_GFX602,  FEATURE_NONE},
74     {{"oland"},     {"gfx602"},  GK_GFX602,  FEATURE_NONE},
75     {{"gfx700"},    {"gfx700"},  GK_GFX700,  FEATURE_NONE},
76     {{"kaveri"},    {"gfx700"},  GK_GFX700,  FEATURE_NONE},
77     {{"gfx701"},    {"gfx701"},  GK_GFX701,  FEATURE_FAST_FMA_F32},
78     {{"hawaii"},    {"gfx701"},  GK_GFX701,  FEATURE_FAST_FMA_F32},
79     {{"gfx702"},    {"gfx702"},  GK_GFX702,  FEATURE_FAST_FMA_F32},
80     {{"gfx703"},    {"gfx703"},  GK_GFX703,  FEATURE_NONE},
81     {{"kabini"},    {"gfx703"},  GK_GFX703,  FEATURE_NONE},
82     {{"mullins"},   {"gfx703"},  GK_GFX703,  FEATURE_NONE},
83     {{"gfx704"},    {"gfx704"},  GK_GFX704,  FEATURE_NONE},
84     {{"bonaire"},   {"gfx704"},  GK_GFX704,  FEATURE_NONE},
85     {{"gfx705"},    {"gfx705"},  GK_GFX705,  FEATURE_NONE},
86     {{"gfx801"},    {"gfx801"},  GK_GFX801,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
87     {{"carrizo"},   {"gfx801"},  GK_GFX801,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
88     {{"gfx802"},    {"gfx802"},  GK_GFX802,  FEATURE_FAST_DENORMAL_F32},
89     {{"iceland"},   {"gfx802"},  GK_GFX802,  FEATURE_FAST_DENORMAL_F32},
90     {{"tonga"},     {"gfx802"},  GK_GFX802,  FEATURE_FAST_DENORMAL_F32},
91     {{"gfx803"},    {"gfx803"},  GK_GFX803,  FEATURE_FAST_DENORMAL_F32},
92     {{"fiji"},      {"gfx803"},  GK_GFX803,  FEATURE_FAST_DENORMAL_F32},
93     {{"polaris10"}, {"gfx803"},  GK_GFX803,  FEATURE_FAST_DENORMAL_F32},
94     {{"polaris11"}, {"gfx803"},  GK_GFX803,  FEATURE_FAST_DENORMAL_F32},
95     {{"gfx805"},    {"gfx805"},  GK_GFX805,  FEATURE_FAST_DENORMAL_F32},
96     {{"tongapro"},  {"gfx805"},  GK_GFX805,  FEATURE_FAST_DENORMAL_F32},
97     {{"gfx810"},    {"gfx810"},  GK_GFX810,  FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
98     {{"stoney"},    {"gfx810"},  GK_GFX810,  FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
99     {{"gfx900"},    {"gfx900"},  GK_GFX900,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
100     {{"gfx902"},    {"gfx902"},  GK_GFX902,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
101     {{"gfx904"},    {"gfx904"},  GK_GFX904,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
102     {{"gfx906"},    {"gfx906"},  GK_GFX906,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
103     {{"gfx908"},    {"gfx908"},  GK_GFX908,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
104     {{"gfx909"},    {"gfx909"},  GK_GFX909,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
105     {{"gfx90a"},    {"gfx90a"},  GK_GFX90A,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
106     {{"gfx90c"},    {"gfx90c"},  GK_GFX90C,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
107     {{"gfx940"},    {"gfx940"},  GK_GFX940,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
108     {{"gfx941"},    {"gfx941"},  GK_GFX941,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
109     {{"gfx942"},    {"gfx942"},  GK_GFX942,  FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK|FEATURE_SRAMECC},
110     {{"gfx1010"},   {"gfx1010"}, GK_GFX1010, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK|FEATURE_WGP},
111     {{"gfx1011"},   {"gfx1011"}, GK_GFX1011, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK|FEATURE_WGP},
112     {{"gfx1012"},   {"gfx1012"}, GK_GFX1012, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK|FEATURE_WGP},
113     {{"gfx1013"},   {"gfx1013"}, GK_GFX1013, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK|FEATURE_WGP},
114     {{"gfx1030"},   {"gfx1030"}, GK_GFX1030, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
115     {{"gfx1031"},   {"gfx1031"}, GK_GFX1031, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
116     {{"gfx1032"},   {"gfx1032"}, GK_GFX1032, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
117     {{"gfx1033"},   {"gfx1033"}, GK_GFX1033, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
118     {{"gfx1034"},   {"gfx1034"}, GK_GFX1034, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
119     {{"gfx1035"},   {"gfx1035"}, GK_GFX1035, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
120     {{"gfx1036"},   {"gfx1036"}, GK_GFX1036, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
121     {{"gfx1100"},   {"gfx1100"}, GK_GFX1100, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
122     {{"gfx1101"},   {"gfx1101"}, GK_GFX1101, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
123     {{"gfx1102"},   {"gfx1102"}, GK_GFX1102, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
124     {{"gfx1103"},   {"gfx1103"}, GK_GFX1103, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
125     {{"gfx1150"},   {"gfx1150"}, GK_GFX1150, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
126     {{"gfx1151"},   {"gfx1151"}, GK_GFX1151, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
127     {{"gfx1152"},   {"gfx1152"}, GK_GFX1152, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
128     {{"gfx1200"},   {"gfx1200"}, GK_GFX1200, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
129     {{"gfx1201"},   {"gfx1201"}, GK_GFX1201, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
130 
131     {{"gfx9-generic"},      {"gfx9-generic"},    GK_GFX9_GENERIC,    FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_XNACK},
132     {{"gfx10-1-generic"},   {"gfx10-1-generic"}, GK_GFX10_1_GENERIC, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_XNACK|FEATURE_WGP},
133     {{"gfx10-3-generic"},   {"gfx10-3-generic"}, GK_GFX10_3_GENERIC, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
134     {{"gfx11-generic"},     {"gfx11-generic"},   GK_GFX11_GENERIC,   FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
135     {{"gfx12-generic"},     {"gfx12-generic"},   GK_GFX12_GENERIC,   FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_WGP},
136     // clang-format on
137 };
138 
getArchEntry(AMDGPU::GPUKind AK,ArrayRef<GPUInfo> Table)139 const GPUInfo *getArchEntry(AMDGPU::GPUKind AK, ArrayRef<GPUInfo> Table) {
140   GPUInfo Search = { {""}, {""}, AK, AMDGPU::FEATURE_NONE };
141 
142   auto I =
143       llvm::lower_bound(Table, Search, [](const GPUInfo &A, const GPUInfo &B) {
144         return A.Kind < B.Kind;
145       });
146 
147   if (I == Table.end() || I->Kind != Search.Kind)
148     return nullptr;
149   return I;
150 }
151 
152 } // namespace
153 
getArchFamilyNameAMDGCN(GPUKind AK)154 StringRef llvm::AMDGPU::getArchFamilyNameAMDGCN(GPUKind AK) {
155   switch (AK) {
156   case AMDGPU::GK_GFX9_GENERIC:
157     return "gfx9";
158   case AMDGPU::GK_GFX10_1_GENERIC:
159   case AMDGPU::GK_GFX10_3_GENERIC:
160     return "gfx10";
161   case AMDGPU::GK_GFX11_GENERIC:
162     return "gfx11";
163   case AMDGPU::GK_GFX12_GENERIC:
164     return "gfx12";
165   default: {
166     StringRef ArchName = getArchNameAMDGCN(AK);
167     return ArchName.empty() ? "" : ArchName.drop_back(2);
168   }
169   }
170 }
171 
getArchNameAMDGCN(GPUKind AK)172 StringRef llvm::AMDGPU::getArchNameAMDGCN(GPUKind AK) {
173   if (const auto *Entry = getArchEntry(AK, AMDGCNGPUs))
174     return Entry->CanonicalName;
175   return "";
176 }
177 
getArchNameR600(GPUKind AK)178 StringRef llvm::AMDGPU::getArchNameR600(GPUKind AK) {
179   if (const auto *Entry = getArchEntry(AK, R600GPUs))
180     return Entry->CanonicalName;
181   return "";
182 }
183 
parseArchAMDGCN(StringRef CPU)184 AMDGPU::GPUKind llvm::AMDGPU::parseArchAMDGCN(StringRef CPU) {
185   for (const auto &C : AMDGCNGPUs) {
186     if (CPU == C.Name)
187       return C.Kind;
188   }
189 
190   return AMDGPU::GPUKind::GK_NONE;
191 }
192 
parseArchR600(StringRef CPU)193 AMDGPU::GPUKind llvm::AMDGPU::parseArchR600(StringRef CPU) {
194   for (const auto &C : R600GPUs) {
195     if (CPU == C.Name)
196       return C.Kind;
197   }
198 
199   return AMDGPU::GPUKind::GK_NONE;
200 }
201 
getArchAttrAMDGCN(GPUKind AK)202 unsigned AMDGPU::getArchAttrAMDGCN(GPUKind AK) {
203   if (const auto *Entry = getArchEntry(AK, AMDGCNGPUs))
204     return Entry->Features;
205   return FEATURE_NONE;
206 }
207 
getArchAttrR600(GPUKind AK)208 unsigned AMDGPU::getArchAttrR600(GPUKind AK) {
209   if (const auto *Entry = getArchEntry(AK, R600GPUs))
210     return Entry->Features;
211   return FEATURE_NONE;
212 }
213 
fillValidArchListAMDGCN(SmallVectorImpl<StringRef> & Values)214 void AMDGPU::fillValidArchListAMDGCN(SmallVectorImpl<StringRef> &Values) {
215   // XXX: Should this only report unique canonical names?
216   for (const auto &C : AMDGCNGPUs)
217     Values.push_back(C.Name);
218 }
219 
fillValidArchListR600(SmallVectorImpl<StringRef> & Values)220 void AMDGPU::fillValidArchListR600(SmallVectorImpl<StringRef> &Values) {
221   for (const auto &C : R600GPUs)
222     Values.push_back(C.Name);
223 }
224 
getIsaVersion(StringRef GPU)225 AMDGPU::IsaVersion AMDGPU::getIsaVersion(StringRef GPU) {
226   AMDGPU::GPUKind AK = parseArchAMDGCN(GPU);
227   if (AK == AMDGPU::GPUKind::GK_NONE) {
228     if (GPU == "generic-hsa")
229       return {7, 0, 0};
230     if (GPU == "generic")
231       return {6, 0, 0};
232     return {0, 0, 0};
233   }
234 
235   // clang-format off
236   switch (AK) {
237   case GK_GFX600:  return {6, 0, 0};
238   case GK_GFX601:  return {6, 0, 1};
239   case GK_GFX602:  return {6, 0, 2};
240   case GK_GFX700:  return {7, 0, 0};
241   case GK_GFX701:  return {7, 0, 1};
242   case GK_GFX702:  return {7, 0, 2};
243   case GK_GFX703:  return {7, 0, 3};
244   case GK_GFX704:  return {7, 0, 4};
245   case GK_GFX705:  return {7, 0, 5};
246   case GK_GFX801:  return {8, 0, 1};
247   case GK_GFX802:  return {8, 0, 2};
248   case GK_GFX803:  return {8, 0, 3};
249   case GK_GFX805:  return {8, 0, 5};
250   case GK_GFX810:  return {8, 1, 0};
251   case GK_GFX900:  return {9, 0, 0};
252   case GK_GFX902:  return {9, 0, 2};
253   case GK_GFX904:  return {9, 0, 4};
254   case GK_GFX906:  return {9, 0, 6};
255   case GK_GFX908:  return {9, 0, 8};
256   case GK_GFX909:  return {9, 0, 9};
257   case GK_GFX90A:  return {9, 0, 10};
258   case GK_GFX90C:  return {9, 0, 12};
259   case GK_GFX940:  return {9, 4, 0};
260   case GK_GFX941:  return {9, 4, 1};
261   case GK_GFX942:  return {9, 4, 2};
262   case GK_GFX1010: return {10, 1, 0};
263   case GK_GFX1011: return {10, 1, 1};
264   case GK_GFX1012: return {10, 1, 2};
265   case GK_GFX1013: return {10, 1, 3};
266   case GK_GFX1030: return {10, 3, 0};
267   case GK_GFX1031: return {10, 3, 1};
268   case GK_GFX1032: return {10, 3, 2};
269   case GK_GFX1033: return {10, 3, 3};
270   case GK_GFX1034: return {10, 3, 4};
271   case GK_GFX1035: return {10, 3, 5};
272   case GK_GFX1036: return {10, 3, 6};
273   case GK_GFX1100: return {11, 0, 0};
274   case GK_GFX1101: return {11, 0, 1};
275   case GK_GFX1102: return {11, 0, 2};
276   case GK_GFX1103: return {11, 0, 3};
277   case GK_GFX1150: return {11, 5, 0};
278   case GK_GFX1151: return {11, 5, 1};
279   case GK_GFX1152: return {11, 5, 2};
280   case GK_GFX1200: return {12, 0, 0};
281   case GK_GFX1201: return {12, 0, 1};
282 
283   // Generic targets return the lowest common denominator
284   // within their family. That is, the ISA that is the most
285   // restricted in terms of features.
286   //
287   // gfx9-generic is tricky because there is no lowest
288   // common denominator, so we return gfx900 which has mad-mix
289   // but this family doesn't have it.
290   //
291   // This API should never be used to check for a particular
292   // feature anyway.
293   //
294   // TODO: Split up this API depending on its caller so
295   // generic target handling is more obvious and less risky.
296   case GK_GFX9_GENERIC:    return {9, 0, 0};
297   case GK_GFX10_1_GENERIC: return {10, 1, 0};
298   case GK_GFX10_3_GENERIC: return {10, 3, 0};
299   case GK_GFX11_GENERIC:   return {11, 0, 3};
300   case GK_GFX12_GENERIC:   return {12, 0, 0};
301   default:         return {0, 0, 0};
302   }
303   // clang-format on
304 }
305 
getCanonicalArchName(const Triple & T,StringRef Arch)306 StringRef AMDGPU::getCanonicalArchName(const Triple &T, StringRef Arch) {
307   assert(T.isAMDGPU());
308   auto ProcKind = T.isAMDGCN() ? parseArchAMDGCN(Arch) : parseArchR600(Arch);
309   if (ProcKind == GK_NONE)
310     return StringRef();
311 
312   return T.isAMDGCN() ? getArchNameAMDGCN(ProcKind) : getArchNameR600(ProcKind);
313 }
314 
fillAMDGPUFeatureMap(StringRef GPU,const Triple & T,StringMap<bool> & Features)315 void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
316                                   StringMap<bool> &Features) {
317   // XXX - What does the member GPU mean if device name string passed here?
318   if (T.isSPIRV() && T.getOS() == Triple::OSType::AMDHSA) {
319     // AMDGCN SPIRV must support the union of all AMDGCN features.
320     Features["atomic-ds-pk-add-16-insts"] = true;
321     Features["atomic-flat-pk-add-16-insts"] = true;
322     Features["atomic-buffer-global-pk-add-f16-insts"] = true;
323     Features["atomic-global-pk-add-bf16-inst"] = true;
324     Features["atomic-fadd-rtn-insts"] = true;
325     Features["ci-insts"] = true;
326     Features["dot1-insts"] = true;
327     Features["dot2-insts"] = true;
328     Features["dot3-insts"] = true;
329     Features["dot4-insts"] = true;
330     Features["dot5-insts"] = true;
331     Features["dot7-insts"] = true;
332     Features["dot8-insts"] = true;
333     Features["dot9-insts"] = true;
334     Features["dot10-insts"] = true;
335     Features["dot11-insts"] = true;
336     Features["dl-insts"] = true;
337     Features["16-bit-insts"] = true;
338     Features["dpp"] = true;
339     Features["gfx8-insts"] = true;
340     Features["gfx9-insts"] = true;
341     Features["gfx90a-insts"] = true;
342     Features["gfx940-insts"] = true;
343     Features["gfx10-insts"] = true;
344     Features["gfx10-3-insts"] = true;
345     Features["gfx11-insts"] = true;
346     Features["gfx12-insts"] = true;
347     Features["image-insts"] = true;
348     Features["fp8-conversion-insts"] = true;
349     Features["s-memrealtime"] = true;
350     Features["s-memtime-inst"] = true;
351     Features["gws"] = true;
352     Features["fp8-insts"] = true;
353     Features["fp8-conversion-insts"] = true;
354     Features["atomic-ds-pk-add-16-insts"] = true;
355     Features["mai-insts"] = true;
356     Features["wavefrontsize32"] = true;
357     Features["wavefrontsize64"] = true;
358   } else if (T.isAMDGCN()) {
359     switch (parseArchAMDGCN(GPU)) {
360     case GK_GFX1201:
361     case GK_GFX1200:
362     case GK_GFX12_GENERIC:
363       Features["ci-insts"] = true;
364       Features["dot7-insts"] = true;
365       Features["dot8-insts"] = true;
366       Features["dot9-insts"] = true;
367       Features["dot10-insts"] = true;
368       Features["dot11-insts"] = true;
369       Features["dl-insts"] = true;
370       Features["atomic-ds-pk-add-16-insts"] = true;
371       Features["atomic-flat-pk-add-16-insts"] = true;
372       Features["atomic-buffer-global-pk-add-f16-insts"] = true;
373       Features["atomic-global-pk-add-bf16-inst"] = true;
374       Features["16-bit-insts"] = true;
375       Features["dpp"] = true;
376       Features["gfx8-insts"] = true;
377       Features["gfx9-insts"] = true;
378       Features["gfx10-insts"] = true;
379       Features["gfx10-3-insts"] = true;
380       Features["gfx11-insts"] = true;
381       Features["gfx12-insts"] = true;
382       Features["atomic-fadd-rtn-insts"] = true;
383       Features["image-insts"] = true;
384       Features["fp8-conversion-insts"] = true;
385       break;
386     case GK_GFX1152:
387     case GK_GFX1151:
388     case GK_GFX1150:
389     case GK_GFX1103:
390     case GK_GFX1102:
391     case GK_GFX1101:
392     case GK_GFX1100:
393     case GK_GFX11_GENERIC:
394       Features["ci-insts"] = true;
395       Features["dot5-insts"] = true;
396       Features["dot7-insts"] = true;
397       Features["dot8-insts"] = true;
398       Features["dot9-insts"] = true;
399       Features["dot10-insts"] = true;
400       Features["dl-insts"] = true;
401       Features["16-bit-insts"] = true;
402       Features["dpp"] = true;
403       Features["gfx8-insts"] = true;
404       Features["gfx9-insts"] = true;
405       Features["gfx10-insts"] = true;
406       Features["gfx10-3-insts"] = true;
407       Features["gfx11-insts"] = true;
408       Features["atomic-fadd-rtn-insts"] = true;
409       Features["image-insts"] = true;
410       Features["gws"] = true;
411       break;
412     case GK_GFX1036:
413     case GK_GFX1035:
414     case GK_GFX1034:
415     case GK_GFX1033:
416     case GK_GFX1032:
417     case GK_GFX1031:
418     case GK_GFX1030:
419     case GK_GFX10_3_GENERIC:
420       Features["ci-insts"] = true;
421       Features["dot1-insts"] = true;
422       Features["dot2-insts"] = true;
423       Features["dot5-insts"] = true;
424       Features["dot6-insts"] = true;
425       Features["dot7-insts"] = true;
426       Features["dot10-insts"] = true;
427       Features["dl-insts"] = true;
428       Features["16-bit-insts"] = true;
429       Features["dpp"] = true;
430       Features["gfx8-insts"] = true;
431       Features["gfx9-insts"] = true;
432       Features["gfx10-insts"] = true;
433       Features["gfx10-3-insts"] = true;
434       Features["image-insts"] = true;
435       Features["s-memrealtime"] = true;
436       Features["s-memtime-inst"] = true;
437       Features["gws"] = true;
438       break;
439     case GK_GFX1012:
440     case GK_GFX1011:
441       Features["dot1-insts"] = true;
442       Features["dot2-insts"] = true;
443       Features["dot5-insts"] = true;
444       Features["dot6-insts"] = true;
445       Features["dot7-insts"] = true;
446       Features["dot10-insts"] = true;
447       [[fallthrough]];
448     case GK_GFX1013:
449     case GK_GFX1010:
450     case GK_GFX10_1_GENERIC:
451       Features["dl-insts"] = true;
452       Features["ci-insts"] = true;
453       Features["16-bit-insts"] = true;
454       Features["dpp"] = true;
455       Features["gfx8-insts"] = true;
456       Features["gfx9-insts"] = true;
457       Features["gfx10-insts"] = true;
458       Features["image-insts"] = true;
459       Features["s-memrealtime"] = true;
460       Features["s-memtime-inst"] = true;
461       Features["gws"] = true;
462       break;
463     case GK_GFX942:
464     case GK_GFX941:
465     case GK_GFX940:
466       Features["gfx940-insts"] = true;
467       Features["fp8-insts"] = true;
468       Features["fp8-conversion-insts"] = true;
469       Features["atomic-ds-pk-add-16-insts"] = true;
470       Features["atomic-flat-pk-add-16-insts"] = true;
471       Features["atomic-global-pk-add-bf16-inst"] = true;
472       Features["gfx90a-insts"] = true;
473       Features["atomic-buffer-global-pk-add-f16-insts"] = true;
474       Features["atomic-fadd-rtn-insts"] = true;
475       Features["dot3-insts"] = true;
476       Features["dot4-insts"] = true;
477       Features["dot5-insts"] = true;
478       Features["dot6-insts"] = true;
479       Features["mai-insts"] = true;
480       Features["dl-insts"] = true;
481       Features["dot1-insts"] = true;
482       Features["dot2-insts"] = true;
483       Features["dot7-insts"] = true;
484       Features["dot10-insts"] = true;
485       Features["gfx9-insts"] = true;
486       Features["gfx8-insts"] = true;
487       Features["16-bit-insts"] = true;
488       Features["dpp"] = true;
489       Features["s-memrealtime"] = true;
490       Features["ci-insts"] = true;
491       Features["s-memtime-inst"] = true;
492       Features["gws"] = true;
493       break;
494     case GK_GFX90A:
495       Features["gfx90a-insts"] = true;
496       Features["atomic-buffer-global-pk-add-f16-insts"] = true;
497       Features["atomic-fadd-rtn-insts"] = true;
498       [[fallthrough]];
499     case GK_GFX908:
500       Features["dot3-insts"] = true;
501       Features["dot4-insts"] = true;
502       Features["dot5-insts"] = true;
503       Features["dot6-insts"] = true;
504       Features["mai-insts"] = true;
505       [[fallthrough]];
506     case GK_GFX906:
507       Features["dl-insts"] = true;
508       Features["dot1-insts"] = true;
509       Features["dot2-insts"] = true;
510       Features["dot7-insts"] = true;
511       Features["dot10-insts"] = true;
512       [[fallthrough]];
513     case GK_GFX90C:
514     case GK_GFX909:
515     case GK_GFX904:
516     case GK_GFX902:
517     case GK_GFX900:
518     case GK_GFX9_GENERIC:
519       Features["gfx9-insts"] = true;
520       [[fallthrough]];
521     case GK_GFX810:
522     case GK_GFX805:
523     case GK_GFX803:
524     case GK_GFX802:
525     case GK_GFX801:
526       Features["gfx8-insts"] = true;
527       Features["16-bit-insts"] = true;
528       Features["dpp"] = true;
529       Features["s-memrealtime"] = true;
530       [[fallthrough]];
531     case GK_GFX705:
532     case GK_GFX704:
533     case GK_GFX703:
534     case GK_GFX702:
535     case GK_GFX701:
536     case GK_GFX700:
537       Features["ci-insts"] = true;
538       [[fallthrough]];
539     case GK_GFX602:
540     case GK_GFX601:
541     case GK_GFX600:
542       Features["image-insts"] = true;
543       Features["s-memtime-inst"] = true;
544       Features["gws"] = true;
545       break;
546     case GK_NONE:
547       break;
548     default:
549       llvm_unreachable("Unhandled GPU!");
550     }
551   } else {
552     if (GPU.empty())
553       GPU = "r600";
554 
555     switch (llvm::AMDGPU::parseArchR600(GPU)) {
556     case GK_CAYMAN:
557     case GK_CYPRESS:
558     case GK_RV770:
559     case GK_RV670:
560       // TODO: Add fp64 when implemented.
561       break;
562     case GK_TURKS:
563     case GK_CAICOS:
564     case GK_BARTS:
565     case GK_SUMO:
566     case GK_REDWOOD:
567     case GK_JUNIPER:
568     case GK_CEDAR:
569     case GK_RV730:
570     case GK_RV710:
571     case GK_RS880:
572     case GK_R630:
573     case GK_R600:
574       break;
575     default:
576       llvm_unreachable("Unhandled GPU!");
577     }
578   }
579 }
580 
isWave32Capable(StringRef GPU,const Triple & T)581 static bool isWave32Capable(StringRef GPU, const Triple &T) {
582   bool IsWave32Capable = false;
583   // XXX - What does the member GPU mean if device name string passed here?
584   if (T.isAMDGCN()) {
585     switch (parseArchAMDGCN(GPU)) {
586     case GK_GFX1201:
587     case GK_GFX1200:
588     case GK_GFX1152:
589     case GK_GFX1151:
590     case GK_GFX1150:
591     case GK_GFX1103:
592     case GK_GFX1102:
593     case GK_GFX1101:
594     case GK_GFX1100:
595     case GK_GFX1036:
596     case GK_GFX1035:
597     case GK_GFX1034:
598     case GK_GFX1033:
599     case GK_GFX1032:
600     case GK_GFX1031:
601     case GK_GFX1030:
602     case GK_GFX1012:
603     case GK_GFX1011:
604     case GK_GFX1013:
605     case GK_GFX1010:
606     case GK_GFX12_GENERIC:
607     case GK_GFX11_GENERIC:
608     case GK_GFX10_3_GENERIC:
609     case GK_GFX10_1_GENERIC:
610       IsWave32Capable = true;
611       break;
612     default:
613       break;
614     }
615   }
616   return IsWave32Capable;
617 }
618 
619 std::pair<FeatureError, StringRef>
insertWaveSizeFeature(StringRef GPU,const Triple & T,StringMap<bool> & Features)620 AMDGPU::insertWaveSizeFeature(StringRef GPU, const Triple &T,
621                               StringMap<bool> &Features) {
622   bool IsWave32Capable = isWave32Capable(GPU, T);
623   const bool IsNullGPU = GPU.empty();
624   const bool HaveWave32 = Features.count("wavefrontsize32");
625   const bool HaveWave64 = Features.count("wavefrontsize64");
626   if (HaveWave32 && HaveWave64) {
627     return {AMDGPU::INVALID_FEATURE_COMBINATION,
628             "'wavefrontsize32' and 'wavefrontsize64' are mutually exclusive"};
629   }
630   if (HaveWave32 && !IsNullGPU && !IsWave32Capable) {
631     return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "wavefrontsize32"};
632   }
633   // Don't assume any wavesize with an unknown subtarget.
634   if (!IsNullGPU) {
635     // Default to wave32 if available, or wave64 if not
636     if (!HaveWave32 && !HaveWave64) {
637       StringRef DefaultWaveSizeFeature =
638           IsWave32Capable ? "wavefrontsize32" : "wavefrontsize64";
639       Features.insert(std::make_pair(DefaultWaveSizeFeature, true));
640     }
641   }
642   return {NO_ERROR, StringRef()};
643 }
644