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 uint64_t Priority = 0;
53 for (StringRef Feature : Features)
54 if (std::optional<FMVInfo> Info = parseFMVExtension(Feature))
55 Priority |= (1ULL << Info->PriorityBit);
56 return Priority;
57}
58
60 // Transitively enable the Arch Extensions which correspond to each feature.
61 ExtensionSet FeatureBits;
62 for (const StringRef Feature : Features)
63 if (std::optional<FMVInfo> Info = parseFMVExtension(Feature))
64 if (Info->ID)
65 FeatureBits.enable(*Info->ID);
66
67 // Construct a bitmask for all the transitively enabled Arch Extensions.
68 uint64_t FeaturesMask = 0;
69 for (const FMVInfo &Info : getFMVInfo())
70 if (Info.ID && FeatureBits.Enabled.test(*Info.ID))
71 FeaturesMask |= (1ULL << Info.FeatureBit);
72
73 return FeaturesMask;
74}
75
77 const AArch64::ExtensionBitset &InputExts,
78 std::vector<StringRef> &Features) {
79 for (const auto &E : Extensions)
80 /* INVALID and NONE have no feature name. */
81 if (InputExts.test(E.ID) && !E.PosTargetFeature.empty())
82 Features.push_back(E.PosTargetFeature);
83
84 return true;
85}
86
88 for (const auto &A : CpuAliases)
89 if (A.AltName == Name)
90 return A.Name;
91 return Name;
92}
93
95 bool IsNegated = ArchExt.starts_with("no");
96 StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(2) : ArchExt;
97
98 if (auto AE = parseArchExtension(ArchExtBase)) {
99 assert(!(AE.has_value() && AE->NegTargetFeature.empty()));
100 return IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature;
101 }
102
103 return StringRef();
104}
105
107 for (const auto &C : CpuInfos)
108 Values.push_back(C.Name);
109
110 for (const auto &Alias : CpuAliases)
111 // The apple-latest alias is backend only, do not expose it to clang's -mcpu.
112 if (Alias.AltName != "apple-latest")
113 Values.push_back(Alias.AltName);
114
115 llvm::sort(Values);
116}
117
119 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
120 TT.isOSWindows() || TT.isOHOSFamily();
121}
122
123// Allows partial match, ex. "v8a" matches "armv8a".
126 if (checkArchVersion(Arch) < 8)
127 return {};
128
130 for (const auto *A : ArchInfos) {
131 if (A->Name.ends_with(Syn))
132 return A;
133 }
134 return {};
135}
136
137std::optional<AArch64::ExtensionInfo>
139 if (ArchExt.empty())
140 return {};
141 for (const auto &A : Extensions) {
142 if (ArchExt == A.UserVisibleName || ArchExt == A.Alias)
143 return A;
144 }
145 return {};
146}
147
148std::optional<AArch64::FMVInfo> AArch64::parseFMVExtension(StringRef FMVExt) {
149 // FIXME introduce general alias functionality, or remove this exception.
150 if (FMVExt == "rdma")
151 FMVExt = "rdm";
152
153 for (const auto &I : getFMVInfo()) {
154 if (FMVExt == I.Name)
155 return I;
156 }
157 return {};
158}
159
160std::optional<AArch64::ExtensionInfo>
162 for (const auto &E : Extensions)
163 if (TargetFeature == E.PosTargetFeature)
164 return E;
165 return {};
166}
167
168std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
169 // Resolve aliases first.
171
172 // Then find the CPU name.
173 for (const auto &C : CpuInfos)
174 if (Name == C.Name)
175 return C;
176
177 return {};
178}
179
181 outs() << "All available -march extensions for AArch64\n\n"
182 << " " << left_justify("Name", 20)
183 << left_justify("Architecture Feature(s)", 55)
184 << "Description\n";
185 for (const auto &Ext : Extensions) {
186 // Extensions without a feature cannot be used with -march.
187 if (!Ext.UserVisibleName.empty() && !Ext.PosTargetFeature.empty()) {
188 outs() << " "
189 << format(Ext.Description.empty() ? "%-20s%s\n" : "%-20s%-55s%s\n",
190 Ext.UserVisibleName.str().c_str(),
191 Ext.ArchFeatureName.str().c_str(),
192 Ext.Description.str().c_str());
193 }
194 }
195}
196
197void
198AArch64::printEnabledExtensions(const std::set<StringRef> &EnabledFeatureNames) {
199 outs() << "Extensions enabled for the given AArch64 target\n\n"
200 << " " << left_justify("Architecture Feature(s)", 55)
201 << "Description\n";
202 std::vector<ExtensionInfo> EnabledExtensionsInfo;
203 for (const auto &FeatureName : EnabledFeatureNames) {
204 std::string PosFeatureName = '+' + FeatureName.str();
205 if (auto ExtInfo = targetFeatureToExtension(PosFeatureName))
206 EnabledExtensionsInfo.push_back(*ExtInfo);
207 }
208
209 std::sort(EnabledExtensionsInfo.begin(), EnabledExtensionsInfo.end(),
210 [](const ExtensionInfo &Lhs, const ExtensionInfo &Rhs) {
211 return Lhs.ArchFeatureName < Rhs.ArchFeatureName;
212 });
213
214 for (const auto &Ext : EnabledExtensionsInfo) {
215 outs() << " "
216 << format("%-55s%s\n",
217 Ext.ArchFeatureName.str().c_str(),
218 Ext.Description.str().c_str());
219 }
220}
221
223lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID) {
224 for (const auto &E : llvm::AArch64::Extensions)
225 if (E.ID == ExtID)
226 return E;
227 llvm_unreachable("Invalid extension ID");
228}
229
230void AArch64::ExtensionSet::enable(ArchExtKind E) {
231 if (Enabled.test(E))
232 return;
233
234 LLVM_DEBUG(llvm::dbgs() << "Enable " << lookupExtensionByID(E).UserVisibleName << "\n");
235
236 Touched.set(E);
237 Enabled.set(E);
238
239 // Recursively enable all features that this one depends on. This handles all
240 // of the simple cases, where the behaviour doesn't depend on the base
241 // architecture version.
242 for (auto Dep : ExtensionDependencies)
243 if (E == Dep.Later)
244 enable(Dep.Earlier);
245
246 // Special cases for dependencies which vary depending on the base
247 // architecture version.
248 if (BaseArch) {
249 // +fp16 implies +fp16fml for v8.4A+, but not v9.0-A+
250 if (E == AEK_FP16 && BaseArch->is_superset(ARMV8_4A) &&
251 !BaseArch->is_superset(ARMV9A))
252 enable(AEK_FP16FML);
253
254 // For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
255 if (E == AEK_CRYPTO && BaseArch->is_superset(ARMV8_4A)) {
256 enable(AEK_SHA3);
257 enable(AEK_SM4);
258 }
259 }
260}
261
263 // -crypto always disables aes, sha2, sha3 and sm4, even for architectures
264 // where the latter two would not be enabled by +crypto.
265 if (E == AEK_CRYPTO) {
266 disable(AEK_AES);
267 disable(AEK_SHA2);
268 disable(AEK_SHA3);
269 disable(AEK_SM4);
270 }
271
272 // sve2-aes was historically associated with both FEAT_SVE2 and FEAT_SVE_AES,
273 // the latter is now associated with sve-aes and sve2-aes has become shorthand
274 // for +sve2+sve-aes. For backwards compatibility, when we disable sve2-aes we
275 // must also disable sve-aes.
276 if (E == AEK_SVE2AES)
277 disable(AEK_SVEAES);
278
279 if (E == AEK_SVE2BITPERM){
280 disable(AEK_SVEBITPERM);
281 disable(AEK_SVE2);
282 }
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)
uint64_t getFMVPriority(ArrayRef< StringRef > Features)
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)
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)