1//===-- AArch64TargetParser - Parser for AArch64 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 AArch64 hardware features
10// such as FPU/CPU/ARCH and extension names.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/TargetParser/AArch64TargetParser.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
19#include "llvm/TargetParser/ARMTargetParserCommon.h"
20#include "llvm/TargetParser/Triple.h"
21#include <cctype>
22#include <vector>
23
24#define DEBUG_TYPE "target-parser"
25
26using namespace llvm;
27
28#define EMIT_FMV_INFO
29#include "llvm/TargetParser/AArch64TargetParserDef.inc"
30
31static unsigned checkArchVersion(llvm::StringRef Arch) {
32 if (Arch.size() >= 2 && Arch[0] == 'v' && std::isdigit(Arch[1]))
33 return (Arch[1] - 48);
34 return 0;
35}
36
37const AArch64::ArchInfo *AArch64::getArchForCpu(StringRef CPU) {
38 // Note: this now takes cpu aliases into account
39 std::optional<CpuInfo> Cpu = parseCpu(Name: CPU);
40 if (!Cpu)
41 return nullptr;
42 return &ArchInfos[Cpu->ArchIdx];
43}
44
45std::optional<AArch64::ArchInfo> AArch64::ArchInfo::findBySubArch(StringRef SubArch) {
46 for (const auto &A : AArch64::ArchInfos)
47 if (A.getSubArch() == SubArch)
48 return A;
49 return {};
50}
51
52std::optional<AArch64::FMVInfo> lookupFMVByID(AArch64::ArchExtKind ExtID) {
53 for (const AArch64::FMVInfo &Info : AArch64::getFMVInfo())
54 if (Info.ID == ExtID)
55 return Info;
56 return {};
57}
58
59std::optional<AArch64::FMVInfo> getFMVInfoFrom(StringRef Feature) {
60 std::optional<AArch64::FMVInfo> FMV = AArch64::parseFMVExtension(Extension: Feature);
61 if (!FMV && Feature.starts_with(Prefix: '+'))
62 if (std::optional<AArch64::ExtensionInfo> Ext =
63 AArch64::targetFeatureToExtension(TargetFeature: Feature))
64 FMV = lookupFMVByID(ExtID: Ext->ID);
65 return FMV;
66}
67
68APInt AArch64::getFMVPriority(ArrayRef<StringRef> Features) {
69 // Transitively enable the Arch Extensions which correspond to each feature.
70 ExtensionSet FeatureBits;
71 APInt PriorityMask = APInt::getZero(numBits: 128);
72 for (const StringRef Feature : Features) {
73 if (std::optional<FMVInfo> FMV = getFMVInfoFrom(Feature)) {
74 // FMV feature without a corresponding Arch Extension may affect priority
75 if (FMV->ID)
76 FeatureBits.enable(E: *FMV->ID);
77 else
78 PriorityMask.setBit(FMV->PriorityBit);
79 }
80 }
81
82 // Construct a bitmask for all the transitively enabled Arch Extensions.
83 for (const FMVInfo &Info : getFMVInfo())
84 if (Info.ID && FeatureBits.Enabled.test(I: *Info.ID))
85 PriorityMask.setBit(Info.PriorityBit);
86
87 return PriorityMask;
88}
89
90APInt AArch64::getCpuSupportsMask(ArrayRef<StringRef> Features) {
91 // Transitively enable the Arch Extensions which correspond to each feature.
92 ExtensionSet FeatureBits;
93 for (const StringRef Feature : Features)
94 if (std::optional<FMVInfo> FMV = getFMVInfoFrom(Feature))
95 if (FMV->ID)
96 FeatureBits.enable(E: *FMV->ID);
97
98 // Construct a bitmask for all the transitively enabled Arch Extensions.
99 APInt FeaturesMask = APInt::getZero(numBits: 128);
100 for (const FMVInfo &Info : getFMVInfo())
101 if (Info.ID && FeatureBits.Enabled.test(I: *Info.ID))
102 FeaturesMask.setBit(*Info.FeatureBit);
103
104 return FeaturesMask;
105}
106
107bool AArch64::getExtensionFeatures(
108 const AArch64::ExtensionBitset &InputExts,
109 std::vector<StringRef> &Features) {
110 for (const auto &E : Extensions)
111 /* INVALID and NONE have no feature name. */
112 if (InputExts.test(I: E.ID))
113 Features.push_back(x: StrTab[E.PosTargetFeature]);
114
115 return true;
116}
117
118StringRef AArch64::resolveCPUAlias(StringRef Name) {
119 for (const auto &A : CpuAliases)
120 if (StrTab[A.AltName] == Name)
121 return StrTab[A.Name];
122 return Name;
123}
124
125StringRef AArch64::getArchExtFeature(StringRef ArchExt) {
126 bool IsNegated = ArchExt.starts_with(Prefix: "no");
127 StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(N: 2) : ArchExt;
128
129 if (auto AE = parseArchExtension(Extension: ArchExtBase))
130 return StrTab[IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature];
131 return StringRef();
132}
133
134void AArch64::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
135 for (const auto &C : CpuInfos)
136 Values.push_back(Elt: StrTab[C.Name]);
137
138 for (const auto &Alias : CpuAliases)
139 // The apple-latest alias is backend only, do not expose it to clang's -mcpu.
140 if (StrTab[Alias.AltName] != "apple-latest")
141 Values.push_back(Elt: StrTab[Alias.AltName]);
142
143 llvm::sort(C&: Values);
144}
145
146bool AArch64::isX18ReservedByDefault(const Triple &TT) {
147 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
148 TT.isOSWindows() || TT.isOHOSFamily();
149}
150
151// Allows partial match, ex. "v8a" matches "armv8a".
152const AArch64::ArchInfo *AArch64::parseArch(StringRef Arch) {
153 Arch = llvm::ARM::getCanonicalArchName(Arch);
154 if (checkArchVersion(Arch) < 8)
155 return {};
156
157 StringRef Syn = llvm::ARM::getArchSynonym(Arch);
158 for (const auto &A : ArchInfos) {
159 if (StrTab[A.Name].ends_with(Suffix: Syn))
160 return &A;
161 }
162 return {};
163}
164
165std::optional<AArch64::ExtensionInfo>
166AArch64::parseArchExtension(StringRef ArchExt) {
167 if (ArchExt.empty())
168 return {};
169 static const DenseMap<StringRef, const ExtensionInfo *> NameToExt = [] {
170 DenseMap<StringRef, const ExtensionInfo *> Map;
171 for (const auto &A : Extensions)
172 for (StringRef Name : {StrTab[A.UserVisibleName], StrTab[A.Alias]})
173 Map.try_emplace(Key: Name, Args: &A);
174 return Map;
175 }();
176 if (const ExtensionInfo *A = NameToExt.lookup(Val: ArchExt))
177 return *A;
178 return {};
179}
180
181std::optional<AArch64::FMVInfo> AArch64::parseFMVExtension(StringRef FMVExt) {
182 // FIXME introduce general alias functionality, or remove this exception.
183 if (FMVExt == "rdma")
184 FMVExt = "rdm";
185
186 for (const auto &I : getFMVInfo()) {
187 if (FMVExt == I.Name)
188 return I;
189 }
190 return {};
191}
192
193std::optional<AArch64::ExtensionInfo>
194AArch64::targetFeatureToExtension(StringRef TargetFeature) {
195 static const DenseMap<StringRef, const ExtensionInfo *> FeatureToExt = [] {
196 DenseMap<StringRef, const ExtensionInfo *> Map;
197 for (const auto &E : Extensions)
198 for (StringRef Feature :
199 {StrTab[E.PosTargetFeature], StrTab[E.NegTargetFeature]})
200 Map.try_emplace(Key: Feature, Args: &E);
201 return Map;
202 }();
203 if (const ExtensionInfo *E = FeatureToExt.lookup(Val: TargetFeature))
204 return *E;
205 return {};
206}
207
208std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
209 // Resolve aliases first.
210 Name = resolveCPUAlias(Name);
211
212 // Then find the CPU name.
213 for (const auto &C : CpuInfos)
214 if (Name == StrTab[C.Name])
215 return C;
216
217 return {};
218}
219
220void AArch64::PrintSupportedExtensions() {
221 outs() << "All available -march extensions for AArch64\n\n"
222 << " " << left_justify(Str: "Name", Width: 20)
223 << left_justify(Str: "Architecture Feature(s)", Width: 55)
224 << "Description\n";
225 for (const auto &Ext : Extensions) {
226 // Extensions without a feature cannot be used with -march.
227 if (Ext.UserVisibleName.value() && Ext.PosTargetFeature.value()) {
228 // NB: StringTable strings are null-terminated, so the StringRef can be
229 // used as C string without further conversion.
230 outs() << " "
231 << format(Fmt: !Ext.Description.value() ? "%-20s%s\n"
232 : "%-20s%-55s%s\n",
233 Vals: StrTab[Ext.UserVisibleName].data(),
234 Vals: StrTab[Ext.ArchFeatureName].data(),
235 Vals: StrTab[Ext.Description].data());
236 }
237 }
238}
239
240void
241AArch64::printEnabledExtensions(const std::set<StringRef> &EnabledFeatureNames) {
242 outs() << "Extensions enabled for the given AArch64 target\n\n"
243 << " " << left_justify(Str: "Architecture Feature(s)", Width: 55)
244 << "Description\n";
245 std::vector<ExtensionInfo> EnabledExtensionsInfo;
246 for (const auto &FeatureName : EnabledFeatureNames) {
247 std::string PosFeatureName = '+' + FeatureName.str();
248 if (auto ExtInfo = targetFeatureToExtension(TargetFeature: PosFeatureName))
249 EnabledExtensionsInfo.push_back(x: *ExtInfo);
250 }
251
252 std::sort(first: EnabledExtensionsInfo.begin(), last: EnabledExtensionsInfo.end(),
253 comp: [](const ExtensionInfo &Lhs, const ExtensionInfo &Rhs) {
254 return StrTab[Lhs.ArchFeatureName] < StrTab[Rhs.ArchFeatureName];
255 });
256
257 for (const auto &Ext : EnabledExtensionsInfo) {
258 // NB: StringTable strings are null-terminated, so the StringRef can be used
259 // as C string without further conversion.
260 outs() << " "
261 << format(Fmt: "%-55s%s\n", Vals: StrTab[Ext.ArchFeatureName].data(),
262 Vals: StrTab[Ext.Description].data());
263 }
264}
265
266const llvm::AArch64::ExtensionInfo &
267lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID) {
268 for (const auto &E : llvm::AArch64::Extensions)
269 if (E.ID == ExtID)
270 return E;
271 llvm_unreachable("Invalid extension ID");
272}
273
274void AArch64::ExtensionSet::enable(ArchExtKind E) {
275 if (Enabled.test(I: E))
276 return;
277
278 LLVM_DEBUG(llvm::dbgs() << "Enable "
279 << StrTab[lookupExtensionByID(E).UserVisibleName]
280 << "\n");
281
282 Touched.set(E);
283 Enabled.set(E);
284
285 // Recursively enable all features that this one depends on. This handles all
286 // of the simple cases, where the behaviour doesn't depend on the base
287 // architecture version.
288 for (auto Dep : ExtensionDependencies)
289 if (E == Dep.Later)
290 enable(E: Dep.Earlier);
291
292 // Special cases for dependencies which vary depending on the base
293 // architecture version.
294 if (BaseArch) {
295 // +fp16 implies +fp16fml for v8.4A+, but not v9.0-A+
296 if (E == AEK_FP16 && BaseArch->is_superset(Other: ARMV8_4A) &&
297 !BaseArch->is_superset(Other: ARMV9A))
298 enable(E: AEK_FP16FML);
299
300 // For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
301 if (E == AEK_CRYPTO && BaseArch->is_superset(Other: ARMV8_4A)) {
302 enable(E: AEK_SHA3);
303 enable(E: AEK_SM4);
304 }
305 }
306}
307
308void AArch64::ExtensionSet::disable(ArchExtKind E) {
309 // -crypto always disables aes, sha2, sha3 and sm4, even for architectures
310 // where the latter two would not be enabled by +crypto.
311 if (E == AEK_CRYPTO) {
312 disable(E: AEK_AES);
313 disable(E: AEK_SHA2);
314 disable(E: AEK_SHA3);
315 disable(E: AEK_SM4);
316 }
317
318 // sve2-aes was historically associated with both FEAT_SVE2 and FEAT_SVE_AES,
319 // the latter is now associated with sve-aes and sve2-aes has become shorthand
320 // for +sve2+sve-aes. For backwards compatibility, when we disable sve2-aes we
321 // must also disable sve-aes.
322 if (E == AEK_SVE2AES)
323 disable(E: AEK_SVEAES);
324
325 // sve2-sm4 was historically associated with both FEAT_SVE2 and
326 // FEAT_SVE_SM4, the latter is now associated with sve-sm4 and sve2-sm4 has
327 // become shorthand for +sve2+sve-sm4. For backwards compatibility, when we
328 // disable sve2-sm4 we must also disable sve-sm4.
329 if (E == AEK_SVE2SM4)
330 disable(E: AEK_SVESM4);
331
332 // sve2-sha3 was historically associated with both FEAT_SVE2 and
333 // FEAT_SVE_SHA3, the latter is now associated with sve-sha3 and sve2-sha3 has
334 // become shorthand for +sve2+sve-sha3. For backwards compatibility, when we
335 // disable sve2-sha3 we must also disable sve-sha3.
336 if (E == AEK_SVE2SHA3)
337 disable(E: AEK_SVESHA3);
338
339 if (E == AEK_SVE2BITPERM){
340 disable(E: AEK_SVEBITPERM);
341 disable(E: AEK_SVE2);
342 }
343
344 if (!Enabled.test(I: E))
345 return;
346
347 LLVM_DEBUG(llvm::dbgs() << "Disable "
348 << StrTab[lookupExtensionByID(E).UserVisibleName]
349 << "\n");
350
351 Touched.set(E);
352 Enabled.reset(I: E);
353
354 // Recursively disable all features that depends on this one.
355 for (auto Dep : ExtensionDependencies)
356 if (E == Dep.Earlier)
357 disable(E: Dep.Later);
358}
359
360void AArch64::ExtensionSet::addCPUDefaults(const CpuInfo &CPU) {
361 LLVM_DEBUG(llvm::dbgs() << "addCPUDefaults(" << StrTab[CPU.Name] << ")\n");
362 BaseArch = &ArchInfos[CPU.ArchIdx];
363
364 for (const auto &E : Extensions)
365 if (CPU.DefaultExtensions.test(I: E.ID))
366 enable(E: E.ID);
367
368 // Workaround: mark extensions implied by the base architecture as Enabled
369 // so that an explicit "+nofeat" is not silently ignored. We set Enabled
370 // directly instead of calling enable() to keep them out of Touched,
371 // preserving the previous output as much as possible and avoiding a flood
372 // of redundant "+feat" entries.
373 for (const auto &E : Extensions)
374 if (BaseArch->DefaultExts.test(I: E.ID))
375 Enabled.set(E.ID);
376}
377
378void AArch64::ExtensionSet::addArchDefaults(const ArchInfo &Arch) {
379 LLVM_DEBUG(llvm::dbgs() << "addArchDefaults(" << StrTab[Arch.Name] << ")\n");
380 BaseArch = &Arch;
381
382 for (const auto &E : Extensions)
383 if (Arch.DefaultExts.test(I: E.ID))
384 enable(E: E.ID);
385}
386
387bool AArch64::ExtensionSet::parseModifier(StringRef Modifier,
388 const bool AllowNoDashForm) {
389 LLVM_DEBUG(llvm::dbgs() << "parseModifier(" << Modifier << ")\n");
390
391 size_t NChars = 0;
392 // The "no-feat" form is allowed in the target attribute but nowhere else.
393 if (AllowNoDashForm && Modifier.starts_with(Prefix: "no-"))
394 NChars = 3;
395 else if (Modifier.starts_with(Prefix: "no"))
396 NChars = 2;
397 bool IsNegated = NChars != 0;
398 StringRef ArchExt = Modifier.drop_front(N: NChars);
399
400 if (auto AE = parseArchExtension(ArchExt)) {
401 if (IsNegated)
402 disable(E: AE->ID);
403 else
404 enable(E: AE->ID);
405 return true;
406 }
407 return false;
408}
409
410void AArch64::ExtensionSet::reconstructFromParsedFeatures(
411 const std::vector<std::string> &Features,
412 std::vector<std::string> &NonExtensions) {
413 assert(Touched.none() && "Bitset already initialized");
414 for (auto &F : Features) {
415 bool IsNegated = F[0] == '-';
416 if (auto AE = targetFeatureToExtension(TargetFeature: F)) {
417 Touched.set(AE->ID);
418 if (IsNegated)
419 Enabled.reset(I: AE->ID);
420 else
421 Enabled.set(AE->ID);
422 continue;
423 }
424 NonExtensions.push_back(x: F);
425 }
426}
427
428void AArch64::ExtensionSet::dump() const {
429 std::vector<StringRef> Features;
430 toLLVMFeatureList(Features);
431 for (StringRef F : Features)
432 llvm::outs() << F << " ";
433 llvm::outs() << "\n";
434}
435
436const AArch64::ExtensionInfo &
437AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
438 return lookupExtensionByID(ExtID);
439}
440