LLVM 22.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/Support/Debug.h"
16#include "llvm/Support/Format.h"
20#include <cctype>
21#include <vector>
22
23#define DEBUG_TYPE "target-parser"
24
25using namespace llvm;
26
27#define EMIT_FMV_INFO
28#include "llvm/TargetParser/AArch64TargetParserDef.inc"
29
30static unsigned checkArchVersion(llvm::StringRef Arch) {
31 if (Arch.size() >= 2 && Arch[0] == 'v' && std::isdigit(Arch[1]))
32 return (Arch[1] - 48);
33 return 0;
34}
35
37 // Note: this now takes cpu aliases into account
38 std::optional<CpuInfo> Cpu = parseCpu(CPU);
39 if (!Cpu)
40 return nullptr;
41 return &Cpu->Arch;
42}
43
44std::optional<AArch64::ArchInfo> AArch64::ArchInfo::findBySubArch(StringRef SubArch) {
45 for (const auto *A : AArch64::ArchInfos)
46 if (A->getSubArch() == SubArch)
47 return *A;
48 return {};
49}
50
51std::optional<AArch64::FMVInfo> lookupFMVByID(AArch64::ArchExtKind ExtID) {
53 if (Info.ID == ExtID)
54 return Info;
55 return {};
56}
57
58std::optional<AArch64::FMVInfo> getFMVInfoFrom(StringRef Feature) {
59 std::optional<AArch64::FMVInfo> FMV = AArch64::parseFMVExtension(Feature);
60 if (!FMV && Feature.starts_with('+'))
61 if (std::optional<AArch64::ExtensionInfo> Ext =
63 FMV = lookupFMVByID(Ext->ID);
64 return FMV;
65}
66
68 // Transitively enable the Arch Extensions which correspond to each feature.
69 ExtensionSet FeatureBits;
70 APInt PriorityMask = APInt::getZero(128);
71 for (const StringRef Feature : Features) {
72 if (std::optional<FMVInfo> FMV = getFMVInfoFrom(Feature)) {
73 // FMV feature without a corresponding Arch Extension may affect priority
74 if (FMV->ID)
75 FeatureBits.enable(*FMV->ID);
76 else
77 PriorityMask.setBit(FMV->PriorityBit);
78 }
79 }
80
81 // Construct a bitmask for all the transitively enabled Arch Extensions.
82 for (const FMVInfo &Info : getFMVInfo())
83 if (Info.ID && FeatureBits.Enabled.test(*Info.ID))
84 PriorityMask.setBit(Info.PriorityBit);
85
86 return PriorityMask;
87}
88
90 // Transitively enable the Arch Extensions which correspond to each feature.
91 ExtensionSet FeatureBits;
92 for (const StringRef Feature : Features)
93 if (std::optional<FMVInfo> FMV = getFMVInfoFrom(Feature))
94 if (FMV->ID)
95 FeatureBits.enable(*FMV->ID);
96
97 // Construct a bitmask for all the transitively enabled Arch Extensions.
98 APInt FeaturesMask = APInt::getZero(128);
99 for (const FMVInfo &Info : getFMVInfo())
100 if (Info.ID && FeatureBits.Enabled.test(*Info.ID))
101 FeaturesMask.setBit(*Info.FeatureBit);
102
103 return FeaturesMask;
104}
105
107 const AArch64::ExtensionBitset &InputExts,
108 std::vector<StringRef> &Features) {
109 for (const auto &E : Extensions)
110 /* INVALID and NONE have no feature name. */
111 if (InputExts.test(E.ID) && !E.PosTargetFeature.empty())
112 Features.push_back(E.PosTargetFeature);
113
114 return true;
115}
116
118 for (const auto &A : CpuAliases)
119 if (A.AltName == Name)
120 return A.Name;
121 return Name;
122}
123
125 bool IsNegated = ArchExt.starts_with("no");
126 StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(2) : ArchExt;
127
128 if (auto AE = parseArchExtension(ArchExtBase)) {
129 assert(!(AE.has_value() && AE->NegTargetFeature.empty()));
130 return IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature;
131 }
132
133 return StringRef();
134}
135
137 for (const auto &C : CpuInfos)
138 Values.push_back(C.Name);
139
140 for (const auto &Alias : CpuAliases)
141 // The apple-latest alias is backend only, do not expose it to clang's -mcpu.
142 if (Alias.AltName != "apple-latest")
143 Values.push_back(Alias.AltName);
144
145 llvm::sort(Values);
146}
147
149 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
150 TT.isOSWindows() || TT.isOHOSFamily();
151}
152
153// Allows partial match, ex. "v8a" matches "armv8a".
156 if (checkArchVersion(Arch) < 8)
157 return {};
158
160 for (const auto *A : ArchInfos) {
161 if (A->Name.ends_with(Syn))
162 return A;
163 }
164 return {};
165}
166
167std::optional<AArch64::ExtensionInfo>
169 if (ArchExt.empty())
170 return {};
171 for (const auto &A : Extensions) {
172 if (ArchExt == A.UserVisibleName || ArchExt == A.Alias)
173 return A;
174 }
175 return {};
176}
177
178std::optional<AArch64::FMVInfo> AArch64::parseFMVExtension(StringRef FMVExt) {
179 // FIXME introduce general alias functionality, or remove this exception.
180 if (FMVExt == "rdma")
181 FMVExt = "rdm";
182
183 for (const auto &I : getFMVInfo()) {
184 if (FMVExt == I.Name)
185 return I;
186 }
187 return {};
188}
189
190std::optional<AArch64::ExtensionInfo>
192 for (const auto &E : Extensions)
193 if (TargetFeature == E.PosTargetFeature ||
194 TargetFeature == E.NegTargetFeature)
195 return E;
196 return {};
197}
198
199std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
200 // Resolve aliases first.
201 Name = resolveCPUAlias(Name);
202
203 // Then find the CPU name.
204 for (const auto &C : CpuInfos)
205 if (Name == C.Name)
206 return C;
207
208 return {};
209}
210
212 outs() << "All available -march extensions for AArch64\n\n"
213 << " " << left_justify("Name", 20)
214 << left_justify("Architecture Feature(s)", 55)
215 << "Description\n";
216 for (const auto &Ext : Extensions) {
217 // Extensions without a feature cannot be used with -march.
218 if (!Ext.UserVisibleName.empty() && !Ext.PosTargetFeature.empty()) {
219 outs() << " "
220 << format(Ext.Description.empty() ? "%-20s%s\n" : "%-20s%-55s%s\n",
221 Ext.UserVisibleName.str().c_str(),
222 Ext.ArchFeatureName.str().c_str(),
223 Ext.Description.str().c_str());
224 }
225 }
226}
227
228void
229AArch64::printEnabledExtensions(const std::set<StringRef> &EnabledFeatureNames) {
230 outs() << "Extensions enabled for the given AArch64 target\n\n"
231 << " " << left_justify("Architecture Feature(s)", 55)
232 << "Description\n";
233 std::vector<ExtensionInfo> EnabledExtensionsInfo;
234 for (const auto &FeatureName : EnabledFeatureNames) {
235 std::string PosFeatureName = '+' + FeatureName.str();
236 if (auto ExtInfo = targetFeatureToExtension(PosFeatureName))
237 EnabledExtensionsInfo.push_back(*ExtInfo);
238 }
239
240 std::sort(EnabledExtensionsInfo.begin(), EnabledExtensionsInfo.end(),
241 [](const ExtensionInfo &Lhs, const ExtensionInfo &Rhs) {
242 return Lhs.ArchFeatureName < Rhs.ArchFeatureName;
243 });
244
245 for (const auto &Ext : EnabledExtensionsInfo) {
246 outs() << " "
247 << format("%-55s%s\n",
248 Ext.ArchFeatureName.str().c_str(),
249 Ext.Description.str().c_str());
250 }
251}
252
254lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID) {
255 for (const auto &E : llvm::AArch64::Extensions)
256 if (E.ID == ExtID)
257 return E;
258 llvm_unreachable("Invalid extension ID");
259}
260
261void AArch64::ExtensionSet::enable(ArchExtKind E) {
262 if (Enabled.test(E))
263 return;
264
265 LLVM_DEBUG(llvm::dbgs() << "Enable " << lookupExtensionByID(E).UserVisibleName << "\n");
266
267 Touched.set(E);
268 Enabled.set(E);
269
270 // Recursively enable all features that this one depends on. This handles all
271 // of the simple cases, where the behaviour doesn't depend on the base
272 // architecture version.
273 for (auto Dep : ExtensionDependencies)
274 if (E == Dep.Later)
275 enable(Dep.Earlier);
276
277 // Special cases for dependencies which vary depending on the base
278 // architecture version.
279 if (BaseArch) {
280 // +fp16 implies +fp16fml for v8.4A+, but not v9.0-A+
281 if (E == AEK_FP16 && BaseArch->is_superset(ARMV8_4A) &&
282 !BaseArch->is_superset(ARMV9A))
283 enable(AEK_FP16FML);
284
285 // For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
286 if (E == AEK_CRYPTO && BaseArch->is_superset(ARMV8_4A)) {
287 enable(AEK_SHA3);
288 enable(AEK_SM4);
289 }
290 }
291}
292
294 // -crypto always disables aes, sha2, sha3 and sm4, even for architectures
295 // where the latter two would not be enabled by +crypto.
296 if (E == AEK_CRYPTO) {
297 disable(AEK_AES);
298 disable(AEK_SHA2);
299 disable(AEK_SHA3);
300 disable(AEK_SM4);
301 }
302
303 // sve2-aes was historically associated with both FEAT_SVE2 and FEAT_SVE_AES,
304 // the latter is now associated with sve-aes and sve2-aes has become shorthand
305 // for +sve2+sve-aes. For backwards compatibility, when we disable sve2-aes we
306 // must also disable sve-aes.
307 if (E == AEK_SVE2AES)
308 disable(AEK_SVEAES);
309
310 // sve2-sm4 was historically associated with both FEAT_SVE2 and
311 // FEAT_SVE_SM4, the latter is now associated with sve-sm4 and sve2-sm4 has
312 // become shorthand for +sve2+sve-sm4. For backwards compatibility, when we
313 // disable sve2-sm4 we must also disable sve-sm4.
314 if (E == AEK_SVE2SM4)
315 disable(AEK_SVESM4);
316
317 // sve2-sha3 was historically associated with both FEAT_SVE2 and
318 // FEAT_SVE_SHA3, the latter is now associated with sve-sha3 and sve2-sha3 has
319 // become shorthand for +sve2+sve-sha3. For backwards compatibility, when we
320 // disable sve2-sha3 we must also disable sve-sha3.
321 if (E == AEK_SVE2SHA3)
322 disable(AEK_SVESHA3);
323
324 if (E == AEK_SVE2BITPERM){
325 disable(AEK_SVEBITPERM);
326 disable(AEK_SVE2);
327 }
328
329 if (!Enabled.test(E))
330 return;
331
332 LLVM_DEBUG(llvm::dbgs() << "Disable " << lookupExtensionByID(E).UserVisibleName << "\n");
333
334 Touched.set(E);
335 Enabled.reset(E);
336
337 // Recursively disable all features that depends on this one.
338 for (auto Dep : ExtensionDependencies)
339 if (E == Dep.Earlier)
340 disable(Dep.Later);
341}
342
344 LLVM_DEBUG(llvm::dbgs() << "addCPUDefaults(" << CPU.Name << ")\n");
345 BaseArch = &CPU.Arch;
346
347 AArch64::ExtensionBitset CPUExtensions = CPU.getImpliedExtensions();
348 for (const auto &E : Extensions)
349 if (CPUExtensions.test(E.ID))
350 enable(E.ID);
351}
352
354 LLVM_DEBUG(llvm::dbgs() << "addArchDefaults(" << Arch.Name << ")\n");
355 BaseArch = &Arch;
356
357 for (const auto &E : Extensions)
358 if (Arch.DefaultExts.test(E.ID))
359 enable(E.ID);
360}
361
363 const bool AllowNoDashForm) {
364 LLVM_DEBUG(llvm::dbgs() << "parseModifier(" << Modifier << ")\n");
365
366 size_t NChars = 0;
367 // The "no-feat" form is allowed in the target attribute but nowhere else.
368 if (AllowNoDashForm && Modifier.starts_with("no-"))
369 NChars = 3;
370 else if (Modifier.starts_with("no"))
371 NChars = 2;
372 bool IsNegated = NChars != 0;
373 StringRef ArchExt = Modifier.drop_front(NChars);
374
375 if (auto AE = parseArchExtension(ArchExt)) {
376 if (AE->PosTargetFeature.empty() || AE->NegTargetFeature.empty())
377 return false;
378 if (IsNegated)
379 disable(AE->ID);
380 else
381 enable(AE->ID);
382 return true;
383 }
384 return false;
385}
386
388 const std::vector<std::string> &Features,
389 std::vector<std::string> &NonExtensions) {
390 assert(Touched.none() && "Bitset already initialized");
391 for (auto &F : Features) {
392 bool IsNegated = F[0] == '-';
393 if (auto AE = targetFeatureToExtension(F)) {
394 Touched.set(AE->ID);
395 if (IsNegated)
396 Enabled.reset(AE->ID);
397 else
398 Enabled.set(AE->ID);
399 continue;
400 }
401 NonExtensions.push_back(F);
402 }
403}
404
406 std::vector<StringRef> Features;
407 toLLVMFeatureList(Features);
408 for (StringRef F : Features)
409 llvm::outs() << F << " ";
410 llvm::outs() << "\n";
411}
412
414AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
415 return lookupExtensionByID(ExtID);
416}
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< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< std::set< SPIRV::Extension::Extension >, false, SPIRVExtensionsParser > Extensions("spirv-ext", cl::desc("Specify list of enabled SPIR-V extensions"))
#define LLVM_DEBUG(...)
Definition Debug.h:114
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:1331
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
ArrayRef - 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:95
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:611
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
#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".
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
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:1634
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition Format.h:150
AArch64::ExtensionBitset DefaultExts
static LLVM_ABI std::optional< ArchInfo > findBySubArch(StringRef SubArch)
LLVM_ABI bool parseModifier(StringRef Modifier, const bool AllowNoDashForm=false)
LLVM_ABI void addCPUDefaults(const CpuInfo &CPU)
LLVM_ABI void enable(ArchExtKind E)
LLVM_ABI void disable(ArchExtKind E)
void toLLVMFeatureList(std::vector< T > &Features) const
LLVM_ABI void addArchDefaults(const ArchInfo &Arch)
LLVM_ABI void reconstructFromParsedFeatures(const std::vector< std::string > &Features, std::vector< std::string > &NonExtensions)