Skip to content

Commit 9bd2730

Browse files
committed
[Clang][AArch64] Add TargetParser support for defining CPU aliases
This adds AArch64 TargetParser support to define CPU aliases, and ports the definition of Grace over to that. This is following up on D136425. Differential Revision: https://reviews.llvm.org/D136611
1 parent e95c74b commit 9bd2730

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

clang/test/Misc/target-invalid-cpu-note.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
// RUN: not %clang_cc1 -triple arm64--- -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix AARCH64
77
// AARCH64: error: unknown target CPU 'not-a-cpu'
8-
// AARCH64-NEXT: note: valid target CPU values are: cortex-a34, cortex-a35, cortex-a53, cortex-a55, cortex-a510, cortex-a57, cortex-a65, cortex-a65ae, cortex-a72, cortex-a73, cortex-a75, cortex-a76, cortex-a76ae, cortex-a77, cortex-a78, cortex-a78c, cortex-a710, cortex-r82, cortex-x1, cortex-x1c, cortex-x2, neoverse-e1, neoverse-n1, neoverse-n2, neoverse-512tvb, neoverse-v1, neoverse-v2, cyclone, apple-a7, apple-a8, apple-a9, apple-a10, apple-a11, apple-a12, apple-a13, apple-a14, apple-a15, apple-a16, apple-m1, apple-m2, apple-s4, apple-s5, exynos-m3, exynos-m4, exynos-m5, falkor, saphira, kryo, thunderx2t99, thunderx3t110, thunderx, thunderxt88, thunderxt81, thunderxt83, tsv110, a64fx, carmel, ampere1{{$}}
8+
// AARCH64-NEXT: note: valid target CPU values are: cortex-a34, cortex-a35, cortex-a53, cortex-a55, cortex-a510, cortex-a57, cortex-a65, cortex-a65ae, cortex-a72, cortex-a73, cortex-a75, cortex-a76, cortex-a76ae, cortex-a77, cortex-a78, cortex-a78c, cortex-a710, cortex-r82, cortex-x1, cortex-x1c, cortex-x2, neoverse-e1, neoverse-n1, neoverse-n2, neoverse-512tvb, neoverse-v1, neoverse-v2, cyclone, apple-a7, apple-a8, apple-a9, apple-a10, apple-a11, apple-a12, apple-a13, apple-a14, apple-a15, apple-a16, apple-m1, apple-m2, apple-s4, apple-s5, exynos-m3, exynos-m4, exynos-m5, falkor, saphira, kryo, thunderx2t99, thunderx3t110, thunderx, thunderxt88, thunderxt81, thunderxt83, tsv110, a64fx, carmel, ampere1, grace{{$}}
99

1010
// RUN: not %clang_cc1 -triple arm64--- -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE_AARCH64
1111
// TUNE_AARCH64: error: unknown target CPU 'not-a-cpu'
12-
// TUNE_AARCH64-NEXT: note: valid target CPU values are: cortex-a34, cortex-a35, cortex-a53, cortex-a55, cortex-a510, cortex-a57, cortex-a65, cortex-a65ae, cortex-a72, cortex-a73, cortex-a75, cortex-a76, cortex-a76ae, cortex-a77, cortex-a78, cortex-a78c, cortex-a710, cortex-r82, cortex-x1, cortex-x1c, cortex-x2, neoverse-e1, neoverse-n1, neoverse-n2, neoverse-512tvb, neoverse-v1, neoverse-v2, cyclone, apple-a7, apple-a8, apple-a9, apple-a10, apple-a11, apple-a12, apple-a13, apple-a14, apple-a15, apple-a16, apple-m1, apple-m2, apple-s4, apple-s5, exynos-m3, exynos-m4, exynos-m5, falkor, saphira, kryo, thunderx2t99, thunderx3t110, thunderx, thunderxt88, thunderxt81, thunderxt83, tsv110, a64fx, carmel, ampere1{{$}}
12+
// TUNE_AARCH64-NEXT: note: valid target CPU values are: cortex-a34, cortex-a35, cortex-a53, cortex-a55, cortex-a510, cortex-a57, cortex-a65, cortex-a65ae, cortex-a72, cortex-a73, cortex-a75, cortex-a76, cortex-a76ae, cortex-a77, cortex-a78, cortex-a78c, cortex-a710, cortex-r82, cortex-x1, cortex-x1c, cortex-x2, neoverse-e1, neoverse-n1, neoverse-n2, neoverse-512tvb, neoverse-v1, neoverse-v2, cyclone, apple-a7, apple-a8, apple-a9, apple-a10, apple-a11, apple-a12, apple-a13, apple-a14, apple-a15, apple-a16, apple-m1, apple-m2, apple-s4, apple-s5, exynos-m3, exynos-m4, exynos-m5, falkor, saphira, kryo, thunderx2t99, thunderx3t110, thunderx, thunderxt88, thunderxt81, thunderxt83, tsv110, a64fx, carmel, ampere1, grace{{$}}
1313

1414
// RUN: not %clang_cc1 -triple i386--- -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix X86
1515
// X86: error: unknown target CPU 'not-a-cpu'

llvm/include/llvm/Support/AArch64TargetParser.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,9 @@ AARCH64_CPU_NAME("ampere1", ARMV8_6A, FK_CRYPTO_NEON_FP_ARMV8, false,
315315
// Invalid CPU
316316
AARCH64_CPU_NAME("invalid", INVALID, FK_INVALID, true, AArch64::AEK_INVALID)
317317
#undef AARCH64_CPU_NAME
318+
319+
#ifndef AARCH64_CPU_ALIAS
320+
#define AARCH64_CPU_ALIAS(ALIAS,NAME)
321+
#endif
322+
AARCH64_CPU_ALIAS("grace", "neoverse-v2")
323+
#undef AARCH64_CPU_ALIAS

llvm/include/llvm/Support/AArch64TargetParser.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ const ARM::CpuNames<ArchKind> AArch64CPUNames[] = {
109109
#include "AArch64TargetParser.def"
110110
};
111111

112+
const struct {
113+
const char *Alias;
114+
size_t AliasLength;
115+
const char *Name;
116+
size_t NameLength;
117+
118+
StringRef getAlias() const { return StringRef(Alias, AliasLength); }
119+
StringRef getName() const { return StringRef(Name, NameLength); }
120+
} AArch64CPUAliases[] = {
121+
#define AARCH64_CPU_ALIAS(ALIAS,NAME) \
122+
{ALIAS, sizeof(ALIAS) - 1, NAME, sizeof(NAME) - 1},
123+
#include "AArch64TargetParser.def"
124+
};
125+
112126
const ArchKind ArchKinds[] = {
113127
#define AARCH64_ARCH(NAME, ID, CPU_ATTR, SUB_ARCH, ARCH_ATTR, ARCH_FPU, ARCH_BASE_EXT) \
114128
ArchKind::ID,
@@ -138,13 +152,7 @@ StringRef getSubArch(ArchKind AK);
138152
StringRef getArchExtName(unsigned ArchExtKind);
139153
StringRef getArchExtFeature(StringRef ArchExt);
140154
ArchKind convertV9toV8(ArchKind AK);
141-
142-
// FIXME: We should be able to define CPU aliases in TargetParser.
143-
inline StringRef resolveCPUAlias(StringRef CPU) {
144-
if (CPU == "grace")
145-
return "neoverse-v2";
146-
return CPU;
147-
}
155+
StringRef resolveCPUAlias(StringRef CPU);
148156

149157
// Information by Name
150158
unsigned getDefaultFPU(StringRef CPU, ArchKind AK);

llvm/lib/Support/AArch64TargetParser.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ bool AArch64::getExtensionFeatures(uint64_t Extensions,
8585
return true;
8686
}
8787

88+
StringRef AArch64::resolveCPUAlias(StringRef CPU) {
89+
return StringSwitch<StringRef>(CPU)
90+
#define AARCH64_CPU_ALIAS(ALIAS,NAME) \
91+
.Case(ALIAS, NAME)
92+
#include "../../include/llvm/Support/AArch64TargetParser.def"
93+
.Default(CPU);
94+
}
95+
8896
bool AArch64::getArchFeatures(AArch64::ArchKind AK,
8997
std::vector<StringRef> &Features) {
9098
if (AK == ArchKind::INVALID)
@@ -164,6 +172,9 @@ void AArch64::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
164172
if (Arch.ArchID != ArchKind::INVALID)
165173
Values.push_back(Arch.getName());
166174
}
175+
176+
for (const auto &Alias: AArch64CPUAliases)
177+
Values.push_back(Alias.getAlias());
167178
}
168179

169180
bool AArch64::isX18ReservedByDefault(const Triple &TT) {
@@ -194,9 +205,17 @@ AArch64::ArchExtKind AArch64::parseArchExt(StringRef ArchExt) {
194205
}
195206

196207
AArch64::ArchKind AArch64::parseCPUArch(StringRef CPU) {
197-
for (const auto &C : AArch64CPUNames) {
208+
// Resolve aliases first.
209+
for (const auto &Alias : AArch64CPUAliases) {
210+
if (CPU == Alias.getAlias()) {
211+
CPU = Alias.getName();
212+
break;
213+
}
214+
}
215+
// Then find the CPU name.
216+
for (const auto &C : AArch64CPUNames)
198217
if (CPU == C.getName())
199218
return C.ArchID;
200-
}
219+
201220
return ArchKind::INVALID;
202221
}

llvm/unittests/Support/TargetParserTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ INSTANTIATE_TEST_SUITE_P(
12951295
AArch64::AEK_LSE | AArch64::AEK_RDM,
12961296
"8.2-A")));
12971297

1298-
static constexpr unsigned NumAArch64CPUArchs = 58;
1298+
// Note: number of CPUs includes aliases.
1299+
static constexpr unsigned NumAArch64CPUArchs = 59;
12991300

13001301
TEST(TargetParserTest, testAArch64CPUArchList) {
13011302
SmallVector<StringRef, NumAArch64CPUArchs> List;

0 commit comments

Comments
 (0)