LLVM 20.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
52 constexpr unsigned MaxFMVPriority = 1000;
53 unsigned Priority = 0;
54 unsigned NumFeatures = 0;
55 for (StringRef Feature : Features) {
56 if (auto Ext = parseFMVExtension(Feature)) {
57 Priority = std::max(Priority, Ext->Priority);
58 NumFeatures++;
59 }
60 }
61 return Priority + MaxFMVPriority * NumFeatures;
62}
63
65 // Transitively enable the Arch Extensions which correspond to each feature.
66 ExtensionSet FeatureBits;
67 for (const StringRef Feature : Features)
68 if (std::optional<FMVInfo> Info = parseFMVExtension(Feature))
69 if (Info->ID)
70 FeatureBits.enable(*Info->ID);
71
72 // Construct a bitmask for all the transitively enabled Arch Extensions.
73 uint64_t FeaturesMask = 0;
74 for (const FMVInfo &Info : getFMVInfo())
75 if (Info.ID && FeatureBits.Enabled.test(*Info.ID))
76 FeaturesMask |= (1ULL << Info.Bit);
77
78 return FeaturesMask;
79}
80
82 const AArch64::ExtensionBitset &InputExts,
83 std::vector<StringRef> &Features) {
84 for (const auto &E : Extensions)
85 /* INVALID and NONE have no feature name. */
86 if (InputExts.test(E.ID) && !E.PosTargetFeature.empty())
87 Features.push_back(E.PosTargetFeature);
88
89 return true;
90}
91
93 for (const auto &A : CpuAliases)
94 if (A.AltName == Name)
95 return A.Name;
96 return Name;
97}
98
100 bool IsNegated = ArchExt.starts_with("no");
101 StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(2) : ArchExt;
102
103 if (auto AE = parseArchExtension(ArchExtBase)) {
104 assert(!(AE.has_value() && AE->NegTargetFeature.empty()));
105 return IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature;
106 }
107
108 return StringRef();
109}
110
112 for (const auto &C : CpuInfos)
113 Values.push_back(C.Name);
114
115 for (const auto &Alias : CpuAliases)
116 // The apple-latest alias is backend only, do not expose it to clang's -mcpu.
117 if (Alias.AltName != "apple-latest")
118 Values.push_back(Alias.AltName);
119
120 llvm::sort(Values);
121}
122
124 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
125 TT.isOSWindows() || TT.isOHOSFamily();
126}
127
128// Allows partial match, ex. "v8a" matches "armv8a".
131 if (checkArchVersion(Arch) < 8)
132 return {};
133
135 for (const auto *A : ArchInfos) {
136 if (A->Name.ends_with(Syn))
137 return A;
138 }
139 return {};
140}
141
142std::optional<AArch64::ExtensionInfo>
144 if (ArchExt.empty())
145 return {};
146 for (const auto &A : Extensions) {
147 if (ArchExt == A.UserVisibleName || ArchExt == A.Alias)
148 return A;
149 }
150 return {};
151}
152
153std::optional<AArch64::FMVInfo> AArch64::parseFMVExtension(StringRef FMVExt) {
154 // FIXME introduce general alias functionality, or remove this exception.
155 if (FMVExt == "rdma")
156 FMVExt = "rdm";
157
158 for (const auto &I : getFMVInfo()) {
159 if (FMVExt == I.Name)
160 return I;
161 }
162 return {};
163}
164
165std::optional<AArch64::ExtensionInfo>
167 for (const auto &E : Extensions)
168 if (TargetFeature == E.PosTargetFeature)
169 return E;
170 return {};
171}
172
173std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
174 // Resolve aliases first.
176
177 // Then find the CPU name.
178 for (const auto &C : CpuInfos)
179 if (Name == C.Name)
180 return C;
181
182 return {};
183}
184
186 outs() << "All available -march extensions for AArch64\n\n"
187 << " " << left_justify("Name", 20)
188 << left_justify("Architecture Feature(s)", 55)
189 << "Description\n";
190 for (const auto &Ext : Extensions) {
191 // Extensions without a feature cannot be used with -march.
192 if (!Ext.UserVisibleName.empty() && !Ext.PosTargetFeature.empty()) {
193 outs() << " "
194 << format(Ext.Description.empty() ? "%-20s%s\n" : "%-20s%-55s%s\n",
195 Ext.UserVisibleName.str().c_str(),
196 Ext.ArchFeatureName.str().c_str(),
197 Ext.Description.str().c_str());
198 }
199 }
200}
201
202void
203AArch64::printEnabledExtensions(const std::set<StringRef> &EnabledFeatureNames) {
204 outs() << "Extensions enabled for the given AArch64 target\n\n"
205 << " " << left_justify("Architecture Feature(s)", 55)
206 << "Description\n";
207 std::vector<ExtensionInfo> EnabledExtensionsInfo;
208 for (const auto &FeatureName : EnabledFeatureNames) {
209 std::string PosFeatureName = '+' + FeatureName.str();
210 if (auto ExtInfo = targetFeatureToExtension(PosFeatureName))
211 EnabledExtensionsInfo.push_back(*ExtInfo);
212 }
213
214 std::sort(EnabledExtensionsInfo.begin(), EnabledExtensionsInfo.end(),
215 [](const ExtensionInfo &Lhs, const ExtensionInfo &Rhs) {
216 return Lhs.ArchFeatureName < Rhs.ArchFeatureName;
217 });
218
219 for (const auto &Ext : EnabledExtensionsInfo) {
220 outs() << " "
221 << format("%-55s%s\n",
222 Ext.ArchFeatureName.str().c_str(),
223 Ext.Description.str().c_str());
224 }
225}
226
228lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID) {
229 for (const auto &E : llvm::AArch64::Extensions)
230 if (E.ID == ExtID)
231 return E;
232 llvm_unreachable("Invalid extension ID");
233}
234
235void AArch64::ExtensionSet::enable(ArchExtKind E) {
236 if (Enabled.test(E))
237 return;
238
239 LLVM_DEBUG(llvm::dbgs() << "Enable " << lookupExtensionByID(E).UserVisibleName << "\n");
240
241 Touched.set(E);
242 Enabled.set(E);
243
244 // Recursively enable all features that this one depends on. This handles all
245 // of the simple cases, where the behaviour doesn't depend on the base
246 // architecture version.
247 for (auto Dep : ExtensionDependencies)
248 if (E == Dep.Later)
249 enable(Dep.Earlier);
250
251 // Special cases for dependencies which vary depending on the base
252 // architecture version.
253 if (BaseArch) {
254 // +fp16 implies +fp16fml for v8.4A+, but not v9.0-A+
255 if (E == AEK_FP16 && BaseArch->is_superset(ARMV8_4A) &&
256 !BaseArch->is_superset(ARMV9A))
257 enable(AEK_FP16FML);
258
259 // For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
260 if (E == AEK_CRYPTO && BaseArch->is_superset(ARMV8_4A)) {
261 enable(AEK_SHA3);
262 enable(AEK_SM4);
263 }
264 }
265}
266
268 // -crypto always disables aes, sha2, sha3 and sm4, even for architectures
269 // where the latter two would not be enabled by +crypto.
270 if (E == AEK_CRYPTO) {
271 disable(AEK_AES);
272 disable(AEK_SHA2);
273 disable(AEK_SHA3);
274 disable(AEK_SM4);
275 }
276
277 // sve2-aes was historically associated with both FEAT_SVE2 and FEAT_SVE_AES,
278 // the latter is now associated with sve-aes and sve2-aes has become shorthand
279 // for +sve2+sve-aes. For backwards compatibility, when we disable sve2-aes we
280 // must also disable sve-aes.
281 if (E == AEK_SVE2AES)
282 disable(AEK_SVEAES);
283
284 if (!Enabled.test(E))
285 return;
286
287 LLVM_DEBUG(llvm::dbgs() << "Disable " << lookupExtensionByID(E).UserVisibleName << "\n");
288
289 Touched.set(E);
290 Enabled.reset(E);
291
292 // Recursively disable all features that depends on this one.
293 for (auto Dep : ExtensionDependencies)
294 if (E == Dep.Earlier)
295 disable(Dep.Later);
296}
297
299 LLVM_DEBUG(llvm::dbgs() << "addCPUDefaults(" << CPU.Name << ")\n");
300 BaseArch = &CPU.Arch;
301
303 for (const auto &E : Extensions)
304 if (CPUExtensions.test(E.ID))
305 enable(E.ID);
306}
307
309 LLVM_DEBUG(llvm::dbgs() << "addArchDefaults(" << Arch.Name << ")\n");
310 BaseArch = &Arch;
311
312 for (const auto &E : Extensions)
313 if (Arch.DefaultExts.test(E.ID))
314 enable(E.ID);
315}
316
318 const bool AllowNoDashForm) {
319 LLVM_DEBUG(llvm::dbgs() << "parseModifier(" << Modifier << ")\n");
320
321 size_t NChars = 0;
322 // The "no-feat" form is allowed in the target attribute but nowhere else.
323 if (AllowNoDashForm && Modifier.starts_with("no-"))
324 NChars = 3;
325 else if (Modifier.starts_with("no"))
326 NChars = 2;
327 bool IsNegated = NChars != 0;
328 StringRef ArchExt = Modifier.drop_front(NChars);
329
330 if (auto AE = parseArchExtension(ArchExt)) {
331 if (AE->PosTargetFeature.empty() || AE->NegTargetFeature.empty())
332 return false;
333 if (IsNegated)
334 disable(AE->ID);
335 else
336 enable(AE->ID);
337 return true;
338 }
339 return false;
340}
341
343 const std::vector<std::string> &Features,
344 std::vector<std::string> &NonExtensions) {
345 assert(Touched.none() && "Bitset already initialized");
346 for (auto &F : Features) {
347 bool IsNegated = F[0] == '-';
348 if (auto AE = targetFeatureToExtension(F)) {
349 Touched.set(AE->ID);
350 if (IsNegated)
351 Enabled.reset(AE->ID);
352 else
353 Enabled.set(AE->ID);
354 continue;
355 }
356 NonExtensions.push_back(F);
357 }
358}
359
361 std::vector<StringRef> Features;
362 toLLVMFeatureList(Features);
363 for (StringRef F : Features)
364 llvm::outs() << F << " ";
365 llvm::outs() << "\n";
366}
367
369AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
370 return lookupExtensionByID(ExtID);
371}
static unsigned checkArchVersion(llvm::StringRef Arch)
const llvm::AArch64::ExtensionInfo & lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DEBUG(...)
Definition: Debug.h:106
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static cl::opt< std::set< SPIRV::Extension::Extension >, false, SPIRVExtensionsParser > Extensions("spirv-ext", cl::desc("Specify list of enabled SPIR-V extensions"))
static bool Enabled
Definition: Statistic.cpp:46
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
constexpr bool test(unsigned I) const
Definition: Bitset.h:77
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:609
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isX18ReservedByDefault(const Triple &TT)
StringRef getArchExtFeature(StringRef ArchExt)
std::optional< ExtensionInfo > parseArchExtension(StringRef Extension)
std::optional< CpuInfo > parseCpu(StringRef Name)
const ArchInfo * parseArch(StringRef Arch)
const ArchInfo * getArchForCpu(StringRef CPU)
void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values)
const ExtensionInfo & getExtensionByID(ArchExtKind(ExtID))
void printEnabledExtensions(const std::set< StringRef > &EnabledFeatureNames)
std::optional< FMVInfo > parseFMVExtension(StringRef Extension)
const std::vector< FMVInfo > & getFMVInfo()
std::optional< ExtensionInfo > targetFeatureToExtension(StringRef TargetFeature)
StringRef resolveCPUAlias(StringRef CPU)
bool getExtensionFeatures(const AArch64::ExtensionBitset &Extensions, std::vector< StringRef > &Features)
unsigned getFMVPriority(ArrayRef< StringRef > Features)
uint64_t getCpuSupportsMask(ArrayRef< StringRef > Features)
StringRef getCanonicalArchName(StringRef Arch)
MArch is expected to be of the form (arm|thumb)?(eb)?(v.
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.
Definition: AddressRanges.h:18
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:1664
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition: Format.h:146
AArch64::ExtensionBitset DefaultExts
static std::optional< ArchInfo > findBySubArch(StringRef SubArch)
AArch64::ExtensionBitset getImpliedExtensions() const
bool parseModifier(StringRef Modifier, const bool AllowNoDashForm=false)
void addCPUDefaults(const CpuInfo &CPU)
void addArchDefaults(const ArchInfo &Arch)
void reconstructFromParsedFeatures(const std::vector< std::string > &Features, std::vector< std::string > &NonExtensions)