llvm-project
343 строки · 10.1 Кб
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/Support/Debug.h"
16#include "llvm/Support/Format.h"
17#include "llvm/Support/raw_ostream.h"
18#include "llvm/TargetParser/ARMTargetParserCommon.h"
19#include "llvm/TargetParser/Triple.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) {
31if (Arch.size() >= 2 && Arch[0] == 'v' && std::isdigit(Arch[1]))
32return (Arch[1] - 48);
33return 0;
34}
35
36const AArch64::ArchInfo *AArch64::getArchForCpu(StringRef CPU) {
37// Note: this now takes cpu aliases into account
38std::optional<CpuInfo> Cpu = parseCpu(CPU);
39if (!Cpu)
40return nullptr;
41return &Cpu->Arch;
42}
43
44std::optional<AArch64::ArchInfo> AArch64::ArchInfo::findBySubArch(StringRef SubArch) {
45for (const auto *A : AArch64::ArchInfos)
46if (A->getSubArch() == SubArch)
47return *A;
48return {};
49}
50
51uint64_t AArch64::getCpuSupportsMask(ArrayRef<StringRef> FeatureStrs) {
52uint64_t FeaturesMask = 0;
53for (const StringRef &FeatureStr : FeatureStrs) {
54if (auto Ext = parseFMVExtension(FeatureStr))
55FeaturesMask |= (1ULL << Ext->Bit);
56}
57return FeaturesMask;
58}
59
60bool AArch64::getExtensionFeatures(
61const AArch64::ExtensionBitset &InputExts,
62std::vector<StringRef> &Features) {
63for (const auto &E : Extensions)
64/* INVALID and NONE have no feature name. */
65if (InputExts.test(E.ID) && !E.PosTargetFeature.empty())
66Features.push_back(E.PosTargetFeature);
67
68return true;
69}
70
71StringRef AArch64::resolveCPUAlias(StringRef Name) {
72for (const auto &A : CpuAliases)
73if (A.AltName == Name)
74return A.Name;
75return Name;
76}
77
78StringRef AArch64::getArchExtFeature(StringRef ArchExt) {
79bool IsNegated = ArchExt.starts_with("no");
80StringRef ArchExtBase = IsNegated ? ArchExt.drop_front(2) : ArchExt;
81
82if (auto AE = parseArchExtension(ArchExtBase)) {
83assert(!(AE.has_value() && AE->NegTargetFeature.empty()));
84return IsNegated ? AE->NegTargetFeature : AE->PosTargetFeature;
85}
86
87return StringRef();
88}
89
90void AArch64::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
91for (const auto &C : CpuInfos)
92Values.push_back(C.Name);
93
94for (const auto &Alias : CpuAliases)
95// The apple-latest alias is backend only, do not expose it to clang's -mcpu.
96if (Alias.AltName != "apple-latest")
97Values.push_back(Alias.AltName);
98
99llvm::sort(Values);
100}
101
102bool AArch64::isX18ReservedByDefault(const Triple &TT) {
103return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
104TT.isOSWindows() || TT.isOHOSFamily();
105}
106
107// Allows partial match, ex. "v8a" matches "armv8a".
108const AArch64::ArchInfo *AArch64::parseArch(StringRef Arch) {
109Arch = llvm::ARM::getCanonicalArchName(Arch);
110if (checkArchVersion(Arch) < 8)
111return {};
112
113StringRef Syn = llvm::ARM::getArchSynonym(Arch);
114for (const auto *A : ArchInfos) {
115if (A->Name.ends_with(Syn))
116return A;
117}
118return {};
119}
120
121std::optional<AArch64::ExtensionInfo>
122AArch64::parseArchExtension(StringRef ArchExt) {
123if (ArchExt.empty())
124return {};
125for (const auto &A : Extensions) {
126if (ArchExt == A.UserVisibleName || ArchExt == A.Alias)
127return A;
128}
129return {};
130}
131
132std::optional<AArch64::FMVInfo> AArch64::parseFMVExtension(StringRef FMVExt) {
133// FIXME introduce general alias functionality, or remove this exception.
134if (FMVExt == "rdma")
135FMVExt = "rdm";
136
137for (const auto &I : getFMVInfo()) {
138if (FMVExt == I.Name)
139return I;
140}
141return {};
142}
143
144std::optional<AArch64::ExtensionInfo>
145AArch64::targetFeatureToExtension(StringRef TargetFeature) {
146for (const auto &E : Extensions)
147if (TargetFeature == E.PosTargetFeature)
148return E;
149return {};
150}
151
152std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
153// Resolve aliases first.
154Name = resolveCPUAlias(Name);
155
156// Then find the CPU name.
157for (const auto &C : CpuInfos)
158if (Name == C.Name)
159return C;
160
161return {};
162}
163
164void AArch64::PrintSupportedExtensions() {
165outs() << "All available -march extensions for AArch64\n\n"
166<< " " << left_justify("Name", 20)
167<< left_justify("Architecture Feature(s)", 55)
168<< "Description\n";
169for (const auto &Ext : Extensions) {
170// Extensions without a feature cannot be used with -march.
171if (!Ext.UserVisibleName.empty() && !Ext.PosTargetFeature.empty()) {
172outs() << " "
173<< format(Ext.Description.empty() ? "%-20s%s\n" : "%-20s%-55s%s\n",
174Ext.UserVisibleName.str().c_str(),
175Ext.ArchFeatureName.str().c_str(),
176Ext.Description.str().c_str());
177}
178}
179}
180
181void
182AArch64::printEnabledExtensions(const std::set<StringRef> &EnabledFeatureNames) {
183outs() << "Extensions enabled for the given AArch64 target\n\n"
184<< " " << left_justify("Architecture Feature(s)", 55)
185<< "Description\n";
186std::vector<ExtensionInfo> EnabledExtensionsInfo;
187for (const auto &FeatureName : EnabledFeatureNames) {
188std::string PosFeatureName = '+' + FeatureName.str();
189if (auto ExtInfo = targetFeatureToExtension(PosFeatureName))
190EnabledExtensionsInfo.push_back(*ExtInfo);
191}
192
193std::sort(EnabledExtensionsInfo.begin(), EnabledExtensionsInfo.end(),
194[](const ExtensionInfo &Lhs, const ExtensionInfo &Rhs) {
195return Lhs.ArchFeatureName < Rhs.ArchFeatureName;
196});
197
198for (const auto &Ext : EnabledExtensionsInfo) {
199outs() << " "
200<< format("%-55s%s\n",
201Ext.ArchFeatureName.str().c_str(),
202Ext.Description.str().c_str());
203}
204}
205
206const llvm::AArch64::ExtensionInfo &
207lookupExtensionByID(llvm::AArch64::ArchExtKind ExtID) {
208for (const auto &E : llvm::AArch64::Extensions)
209if (E.ID == ExtID)
210return E;
211llvm_unreachable("Invalid extension ID");
212}
213
214void AArch64::ExtensionSet::enable(ArchExtKind E) {
215if (Enabled.test(E))
216return;
217
218LLVM_DEBUG(llvm::dbgs() << "Enable " << lookupExtensionByID(E).UserVisibleName << "\n");
219
220Touched.set(E);
221Enabled.set(E);
222
223// Recursively enable all features that this one depends on. This handles all
224// of the simple cases, where the behaviour doesn't depend on the base
225// architecture version.
226for (auto Dep : ExtensionDependencies)
227if (E == Dep.Later)
228enable(Dep.Earlier);
229
230// Special cases for dependencies which vary depending on the base
231// architecture version.
232if (BaseArch) {
233// +fp16 implies +fp16fml for v8.4A+, but not v9.0-A+
234if (E == AEK_FP16 && BaseArch->is_superset(ARMV8_4A) &&
235!BaseArch->is_superset(ARMV9A))
236enable(AEK_FP16FML);
237
238// For v8.4A+ and v9.0A+, +crypto also enables +sha3 and +sm4.
239if (E == AEK_CRYPTO && BaseArch->is_superset(ARMV8_4A)) {
240enable(AEK_SHA3);
241enable(AEK_SM4);
242}
243}
244}
245
246void AArch64::ExtensionSet::disable(ArchExtKind E) {
247// -crypto always disables aes, sha2, sha3 and sm4, even for architectures
248// where the latter two would not be enabled by +crypto.
249if (E == AEK_CRYPTO) {
250disable(AEK_AES);
251disable(AEK_SHA2);
252disable(AEK_SHA3);
253disable(AEK_SM4);
254}
255
256if (!Enabled.test(E))
257return;
258
259LLVM_DEBUG(llvm::dbgs() << "Disable " << lookupExtensionByID(E).UserVisibleName << "\n");
260
261Touched.set(E);
262Enabled.reset(E);
263
264// Recursively disable all features that depends on this one.
265for (auto Dep : ExtensionDependencies)
266if (E == Dep.Earlier)
267disable(Dep.Later);
268}
269
270void AArch64::ExtensionSet::addCPUDefaults(const CpuInfo &CPU) {
271LLVM_DEBUG(llvm::dbgs() << "addCPUDefaults(" << CPU.Name << ")\n");
272BaseArch = &CPU.Arch;
273
274AArch64::ExtensionBitset CPUExtensions = CPU.getImpliedExtensions();
275for (const auto &E : Extensions)
276if (CPUExtensions.test(E.ID))
277enable(E.ID);
278}
279
280void AArch64::ExtensionSet::addArchDefaults(const ArchInfo &Arch) {
281LLVM_DEBUG(llvm::dbgs() << "addArchDefaults(" << Arch.Name << ")\n");
282BaseArch = &Arch;
283
284for (const auto &E : Extensions)
285if (Arch.DefaultExts.test(E.ID))
286enable(E.ID);
287}
288
289bool AArch64::ExtensionSet::parseModifier(StringRef Modifier,
290const bool AllowNoDashForm) {
291LLVM_DEBUG(llvm::dbgs() << "parseModifier(" << Modifier << ")\n");
292
293size_t NChars = 0;
294// The "no-feat" form is allowed in the target attribute but nowhere else.
295if (AllowNoDashForm && Modifier.starts_with("no-"))
296NChars = 3;
297else if (Modifier.starts_with("no"))
298NChars = 2;
299bool IsNegated = NChars != 0;
300StringRef ArchExt = Modifier.drop_front(NChars);
301
302if (auto AE = parseArchExtension(ArchExt)) {
303if (AE->PosTargetFeature.empty() || AE->NegTargetFeature.empty())
304return false;
305if (IsNegated)
306disable(AE->ID);
307else
308enable(AE->ID);
309return true;
310}
311return false;
312}
313
314void AArch64::ExtensionSet::reconstructFromParsedFeatures(
315const std::vector<std::string> &Features,
316std::vector<std::string> &NonExtensions) {
317assert(Touched.none() && "Bitset already initialized");
318for (auto &F : Features) {
319bool IsNegated = F[0] == '-';
320if (auto AE = targetFeatureToExtension(F)) {
321Touched.set(AE->ID);
322if (IsNegated)
323Enabled.reset(AE->ID);
324else
325Enabled.set(AE->ID);
326continue;
327}
328NonExtensions.push_back(F);
329}
330}
331
332void AArch64::ExtensionSet::dump() const {
333std::vector<StringRef> Features;
334toLLVMFeatureList(Features);
335for (StringRef F : Features)
336llvm::outs() << F << " ";
337llvm::outs() << "\n";
338}
339
340const AArch64::ExtensionInfo &
341AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
342return lookupExtensionByID(ExtID);
343}
344