LLVM 24.0.0git
AArch64TargetParser.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/Format.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
38 // Note: this now takes cpu aliases into account
39 std::optional<CpuInfo> Cpu = parseCpu(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(Feature);
61 if (!FMV && Feature.starts_with('+'))
62 if (std::optional<AArch64::ExtensionInfo> Ext =
64 FMV = lookupFMVByID(Ext->ID);
65 return FMV;
66}
67
69 // Transitively enable the Arch Extensions which correspond to each feature.
70 ExtensionSet FeatureBits;
71 APInt PriorityMask = APInt::getZero(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(*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(*Info.ID))
85 PriorityMask.setBit(Info.PriorityBit);
86
87 return PriorityMask;
88}
89
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(*FMV->ID);
97
98 // Construct a bitmask for all the transitively enabled Arch Extensions.
99 APInt FeaturesMask = APInt::getZero(128);
100 for (const FMVInfo &Info : getFMVInfo())
101 if (Info.ID && FeatureBits.Enabled.test(*Info.ID))
102 FeaturesMask.setBit(*Info.FeatureBit);
103
104 return FeaturesMask;
105}
106
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(E.ID))
113 Features.push_back(StrTab[E.PosTargetFeature]);
114
115 return true;
116}
117
119 for (const auto &A : CpuAliases)
120 if (StrTab[A.AltName] == Name)
121 return StrTab[A.Name];
122 return Name;
123}
124
126 bool IsNegated = ArchExt.starts_with("no");
127 StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(2) : ArchExt;
128
129 if (auto AE = parseArchExtension(ArchExtBase))
130 return StrTab[IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature];
131 return StringRef();
132}
133
135 for (const auto &C : CpuInfos)
136 Values.push_back(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(StrTab[Alias.AltName]);
142
144}
145
147 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
148 TT.isOSWindows() || TT.isOHOSFamily();
149}
150
151// Allows partial match, ex. "v8a" matches "armv8a".
154 if (checkArchVersion(Arch) < 8)
155 return {};
156
158 for (const auto &A : ArchInfos) {
159 if (StrTab[A.Name].ends_with(Syn))
160 return &A;
161 }
162 return {};
163}
164
165std::optional<AArch64::ExtensionInfo>
167 if (ArchExt.empty())
168 return {};
169 static const DenseMap<StringRef, const ExtensionInfo *> NameToExt = [] {
171 for (const auto &A : Extensions)
172 for (StringRef Name : {StrTab[A.UserVisibleName], StrTab[A.Alias]})
173 Map.try_emplace(Name, &A);
174 return Map;
175 }();
176 if (const ExtensionInfo *A = NameToExt.lookup(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>
195 static const DenseMap<StringRef, const ExtensionInfo *> FeatureToExt = [] {
197 for (const auto &E : Extensions)
198 for (StringRef Feature :
199 {StrTab[E.PosTargetFeature], StrTab[E.NegTargetFeature]})
200 Map.try_emplace(Feature, &E);
201 return Map;
202 }();
203 if (const ExtensionInfo *E = FeatureToExt.lookup(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
221 outs() << "All available -march extensions for AArch64\n\n"
222 << " " << left_justify("Name", 20)
223 << left_justify("Architecture Feature(s)", 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(!Ext.Description.value() ? "%-20s%s\n"
232 : "%-20s%-55s%s\n",
233 StrTab[Ext.UserVisibleName].data(),
234 StrTab[Ext.ArchFeatureName].data(),
235 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("Architecture Feature(s)", 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(PosFeatureName))
249 EnabledExtensionsInfo.push_back(*ExtInfo);
250 }
251
252 std::sort(EnabledExtensionsInfo.begin(), EnabledExtensionsInfo.end(),
253 [](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("%-55s%s\n", StrTab[Ext.ArchFeatureName].data(),
262 StrTab[Ext.Description].data());
263 }
264}
265
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(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(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(ARMV8_4A) &&
297 !BaseArch->is_superset(ARMV9A))
298 enable(AEK_FP16FML);
299
300 // For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
301 if (E == AEK_CRYPTO && BaseArch->is_superset(ARMV8_4A)) {
302 enable(AEK_SHA3);
303 enable(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(AEK_AES);
313 disable(AEK_SHA2);
314 disable(AEK_SHA3);
315 disable(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(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(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(AEK_SVESHA3);
338
339 if (E == AEK_SVE2BITPERM){
340 disable(AEK_SVEBITPERM);
341 disable(AEK_SVE2);
342 }
343
344 if (!Enabled.test(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(E);
353
354 // Recursively disable all features that depends on this one.
355 for (auto Dep : ExtensionDependencies)
356 if (E == Dep.Earlier)
357 disable(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(E.ID))
366 enable(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(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(E.ID))
384 enable(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("no-"))
394 NChars = 3;
395 else if (Modifier.starts_with("no"))
396 NChars = 2;
397 bool IsNegated = NChars != 0;
398 StringRef ArchExt = Modifier.drop_front(NChars);
399
400 if (auto AE = parseArchExtension(ArchExt)) {
401 if (IsNegated)
402 disable(AE->ID);
403 else
404 enable(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(F)) {
417 Touched.set(AE->ID);
418 if (IsNegated)
419 Enabled.reset(AE->ID);
420 else
421 Enabled.set(AE->ID);
422 continue;
423 }
424 NonExtensions.push_back(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
437AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
438 return lookupExtensionByID(ExtID);
439}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::optional< AArch64::FMVInfo > getFMVInfoFrom(StringRef Feature)
static unsigned checkArchVersion(llvm::StringRef Arch)
const llvm::AArch64::ExtensionInfo & lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID)
std::optional< AArch64::FMVInfo > lookupFMVByID(AArch64::ArchExtKind ExtID)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< ExtensionSet, false, SPIRVExtensionsParser > Extensions("spirv-ext", cl::desc("Specify list of enabled SPIR-V extensions"))
#define LLVM_DEBUG(...)
Definition Debug.h:119
Class for arbitrary precision integers.
Definition APInt.h:78
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1350
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
constexpr bool test(unsigned I) const
Definition Bitset.h:109
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:809
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:635
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool isX18ReservedByDefault(const Triple &TT)
LLVM_ABI StringRef getArchExtFeature(StringRef ArchExt)
LLVM_ABI std::optional< ExtensionInfo > parseArchExtension(StringRef Extension)
LLVM_ABI std::optional< CpuInfo > parseCpu(StringRef Name)
LLVM_ABI const std::vector< FMVInfo > & getFMVInfo()
LLVM_ABI const ArchInfo * parseArch(StringRef Arch)
LLVM_ABI const ArchInfo * getArchForCpu(StringRef CPU)
LLVM_ABI const ExtensionInfo & getExtensionByID(ArchExtKind(ExtID))
LLVM_ABI void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values)
LLVM_ABI APInt getCpuSupportsMask(ArrayRef< StringRef > Features)
LLVM_ABI void printEnabledExtensions(const std::set< StringRef > &EnabledFeatureNames)
LLVM_ABI std::optional< FMVInfo > parseFMVExtension(StringRef Extension)
Bitset< AEK_NUM_EXTENSIONS > ExtensionBitset
LLVM_ABI APInt getFMVPriority(ArrayRef< StringRef > Features)
LLVM_ABI void PrintSupportedExtensions()
LLVM_ABI std::optional< ExtensionInfo > targetFeatureToExtension(StringRef TargetFeature)
LLVM_ABI StringRef resolveCPUAlias(StringRef CPU)
LLVM_ABI bool getExtensionFeatures(const AArch64::ExtensionBitset &Extensions, std::vector< StringRef > &Features)
LLVM_ABI StringRef getCanonicalArchName(StringRef Arch)
MArch is expected to be of the form (arm|thumb)?(eb)?(v.
LLVM_ABI StringRef getArchSynonym(StringRef Arch)
Converts e.g. "armv8" -> "armv8-a".
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:102
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition Format.h:123
StringTable::Offset AltName
AArch64::ExtensionBitset DefaultExts
static LLVM_ABI std::optional< ArchInfo > findBySubArch(StringRef SubArch)
LLVM_ABI void enable(ArchExtKind E)
LLVM_ABI void disable(ArchExtKind E)
void toLLVMFeatureList(std::vector< T > &Features) const