Skip to content

Commit b6c22a4

Browse files
authored
Add processor aliases back to -print-supported-cpus and -mcpu=help (#118581)
They were accidentally dropped in #96249 rdar://140853882
1 parent 2344cc4 commit b6c22a4

File tree

8 files changed

+135
-36
lines changed

8 files changed

+135
-36
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test that --print-supported-cpus lists supported CPU models, including aliases.
2+
3+
// REQUIRES: aarch64-registered-target
4+
5+
// RUN: %clang --target=arm64-apple-macosx --print-supported-cpus 2>&1 | \
6+
// RUN: FileCheck %s --check-prefix=CHECK --implicit-check-not=apple-latest
7+
8+
// CHECK: Target: arm64-apple-macosx
9+
10+
// CHECK: apple-a11
11+
// CHECK: apple-a12
12+
// CHECK: apple-a13
13+
// CHECK: apple-a14
14+
// CHECK: apple-a15
15+
// CHECK: apple-a16
16+
// CHECK: apple-a17
17+
// CHECK: apple-a7
18+
// CHECK: apple-a8
19+
// CHECK: apple-a9
20+
// CHECK: apple-m1
21+
// CHECK: apple-m2
22+
// CHECK: apple-m3
23+
// CHECK: apple-m4
24+
// CHECK: apple-s4
25+
// CHECK: apple-s5
26+
27+
// CHECK: Use -mcpu or -mtune to specify the target's processor.

llvm/include/llvm/CodeGen/TargetSubtargetInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class Triple;
6363
class TargetSubtargetInfo : public MCSubtargetInfo {
6464
protected: // Can only create subclasses...
6565
TargetSubtargetInfo(const Triple &TT, StringRef CPU, StringRef TuneCPU,
66-
StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
66+
StringRef FS, ArrayRef<StringRef> PN,
67+
ArrayRef<SubtargetFeatureKV> PF,
6768
ArrayRef<SubtargetSubTypeKV> PD,
6869
const MCWriteProcResEntry *WPR,
6970
const MCWriteLatencyEntry *WL,

llvm/include/llvm/MC/MCSubtargetInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class MCSubtargetInfo {
7777
Triple TargetTriple;
7878
std::string CPU; // CPU being targeted.
7979
std::string TuneCPU; // CPU being tuned for.
80+
ArrayRef<StringRef> ProcNames; // Processor list, including aliases
8081
ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
8182
ArrayRef<SubtargetSubTypeKV> ProcDesc; // Processor descriptions
8283

@@ -95,7 +96,8 @@ class MCSubtargetInfo {
9596
public:
9697
MCSubtargetInfo(const MCSubtargetInfo &) = default;
9798
MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef TuneCPU,
98-
StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
99+
StringRef FS, ArrayRef<StringRef> PN,
100+
ArrayRef<SubtargetFeatureKV> PF,
99101
ArrayRef<SubtargetSubTypeKV> PD,
100102
const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
101103
const MCReadAdvanceEntry *RA, const InstrStage *IS,

llvm/lib/CodeGen/TargetSubtargetInfo.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ using namespace llvm;
1616

1717
TargetSubtargetInfo::TargetSubtargetInfo(
1818
const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS,
19-
ArrayRef<SubtargetFeatureKV> PF, ArrayRef<SubtargetSubTypeKV> PD,
20-
const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
21-
const MCReadAdvanceEntry *RA, const InstrStage *IS, const unsigned *OC,
22-
const unsigned *FP)
23-
: MCSubtargetInfo(TT, CPU, TuneCPU, FS, PF, PD, WPR, WL, RA, IS, OC, FP) {}
19+
ArrayRef<StringRef> PN, ArrayRef<SubtargetFeatureKV> PF,
20+
ArrayRef<SubtargetSubTypeKV> PD, const MCWriteProcResEntry *WPR,
21+
const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA,
22+
const InstrStage *IS, const unsigned *OC, const unsigned *FP)
23+
: MCSubtargetInfo(TT, CPU, TuneCPU, FS, PN, PF, PD, WPR, WL, RA, IS, OC,
24+
FP) {}
2425

2526
TargetSubtargetInfo::~TargetSubtargetInfo() = default;
2627

llvm/lib/MC/MCSubtargetInfo.cpp

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,22 @@ static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
8585
}
8686

8787
/// Return the length of the longest entry in the table.
88-
template <typename T>
89-
static size_t getLongestEntryLength(ArrayRef<T> Table) {
88+
static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
9089
size_t MaxLen = 0;
9190
for (auto &I : Table)
9291
MaxLen = std::max(MaxLen, std::strlen(I.Key));
9392
return MaxLen;
9493
}
9594

95+
static size_t getLongestEntryLength(ArrayRef<StringRef> Table) {
96+
size_t MaxLen = 0;
97+
for (StringRef I : Table)
98+
MaxLen = std::max(MaxLen, I.size());
99+
return MaxLen;
100+
}
101+
96102
/// Display help for feature and mcpu choices.
97-
static void Help(ArrayRef<SubtargetSubTypeKV> CPUTable,
103+
static void Help(ArrayRef<StringRef> CPUNames,
98104
ArrayRef<SubtargetFeatureKV> FeatTable) {
99105
// the static variable ensures that the help information only gets
100106
// printed once even though a target machine creates multiple subtargets
@@ -104,14 +110,20 @@ static void Help(ArrayRef<SubtargetSubTypeKV> CPUTable,
104110
}
105111

106112
// Determine the length of the longest CPU and Feature entries.
107-
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
113+
unsigned MaxCPULen = getLongestEntryLength(CPUNames);
108114
unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
109115

110116
// Print the CPU table.
111117
errs() << "Available CPUs for this target:\n\n";
112-
for (auto &CPU : CPUTable)
113-
errs() << format(" %-*s - Select the %s processor.\n", MaxCPULen, CPU.Key,
114-
CPU.Key);
118+
for (auto &CPUName : CPUNames) {
119+
// Skip apple-latest, as that's only meant to be used in
120+
// disassemblers/debuggers, and we don't want normal code to be built with
121+
// it as an -mcpu=
122+
if (CPUName == "apple-latest")
123+
continue;
124+
errs() << format(" %-*s - Select the %s processor.\n", MaxCPULen,
125+
CPUName.str().c_str(), CPUName.str().c_str());
126+
}
115127
errs() << '\n';
116128

117129
// Print the Feature table.
@@ -127,7 +139,7 @@ static void Help(ArrayRef<SubtargetSubTypeKV> CPUTable,
127139
}
128140

129141
/// Display help for mcpu choices only
130-
static void cpuHelp(ArrayRef<SubtargetSubTypeKV> CPUTable) {
142+
static void cpuHelp(ArrayRef<StringRef> CPUNames) {
131143
// the static variable ensures that the help information only gets
132144
// printed once even though a target machine creates multiple subtargets
133145
static bool PrintOnce = false;
@@ -137,8 +149,14 @@ static void cpuHelp(ArrayRef<SubtargetSubTypeKV> CPUTable) {
137149

138150
// Print the CPU table.
139151
errs() << "Available CPUs for this target:\n\n";
140-
for (auto &CPU : CPUTable)
141-
errs() << "\t" << CPU.Key << "\n";
152+
for (auto &CPU : CPUNames) {
153+
// Skip apple-latest, as that's only meant to be used in
154+
// disassemblers/debuggers, and we don't want normal code to be built with
155+
// it as an -mcpu=
156+
if (CPU == "apple-latest")
157+
continue;
158+
errs() << "\t" << CPU << "\n";
159+
}
142160
errs() << '\n';
143161

144162
errs() << "Use -mcpu or -mtune to specify the target's processor.\n"
@@ -148,7 +166,9 @@ static void cpuHelp(ArrayRef<SubtargetSubTypeKV> CPUTable) {
148166
PrintOnce = true;
149167
}
150168

151-
static FeatureBitset getFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS,
169+
static FeatureBitset getFeatures(MCSubtargetInfo &STI, StringRef CPU,
170+
StringRef TuneCPU, StringRef FS,
171+
ArrayRef<StringRef> ProcNames,
152172
ArrayRef<SubtargetSubTypeKV> ProcDesc,
153173
ArrayRef<SubtargetFeatureKV> ProcFeatures) {
154174
SubtargetFeatures Features(FS);
@@ -163,7 +183,7 @@ static FeatureBitset getFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS,
163183

164184
// Check if help is needed
165185
if (CPU == "help")
166-
Help(ProcDesc, ProcFeatures);
186+
Help(ProcNames, ProcFeatures);
167187

168188
// Find CPU entry if CPU name is specified.
169189
else if (!CPU.empty()) {
@@ -196,9 +216,9 @@ static FeatureBitset getFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS,
196216
for (const std::string &Feature : Features.getFeatures()) {
197217
// Check for help
198218
if (Feature == "+help")
199-
Help(ProcDesc, ProcFeatures);
219+
Help(ProcNames, ProcFeatures);
200220
else if (Feature == "+cpuhelp")
201-
cpuHelp(ProcDesc);
221+
cpuHelp(ProcNames);
202222
else
203223
ApplyFeatureFlag(Bits, Feature, ProcFeatures);
204224
}
@@ -208,7 +228,8 @@ static FeatureBitset getFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS,
208228

209229
void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,
210230
StringRef FS) {
211-
FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);
231+
FeatureBits =
232+
getFeatures(*this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures);
212233
FeatureString = std::string(FS);
213234

214235
if (!TuneCPU.empty())
@@ -219,20 +240,19 @@ void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,
219240

220241
void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef TuneCPU,
221242
StringRef FS) {
222-
FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);
243+
FeatureBits =
244+
getFeatures(*this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures);
223245
FeatureString = std::string(FS);
224246
}
225247

226-
MCSubtargetInfo::MCSubtargetInfo(const Triple &TT, StringRef C, StringRef TC,
227-
StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
228-
ArrayRef<SubtargetSubTypeKV> PD,
229-
const MCWriteProcResEntry *WPR,
230-
const MCWriteLatencyEntry *WL,
231-
const MCReadAdvanceEntry *RA,
232-
const InstrStage *IS, const unsigned *OC,
233-
const unsigned *FP)
248+
MCSubtargetInfo::MCSubtargetInfo(
249+
const Triple &TT, StringRef C, StringRef TC, StringRef FS,
250+
ArrayRef<StringRef> PN, ArrayRef<SubtargetFeatureKV> PF,
251+
ArrayRef<SubtargetSubTypeKV> PD, const MCWriteProcResEntry *WPR,
252+
const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA,
253+
const InstrStage *IS, const unsigned *OC, const unsigned *FP)
234254
: TargetTriple(TT), CPU(std::string(C)), TuneCPU(std::string(TC)),
235-
ProcFeatures(PF), ProcDesc(PD), WriteProcResTable(WPR),
255+
ProcNames(PN), ProcFeatures(PF), ProcDesc(PD), WriteProcResTable(WPR),
236256
WriteLatencyTable(WL), ReadAdvanceTable(RA), Stages(IS),
237257
OperandCycles(OC), ForwardingPaths(FP) {
238258
InitMCProcessorInfo(CPU, TuneCPU, FS);

llvm/unittests/CodeGen/MFCommon.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public:
7878
class BogusSubtarget : public TargetSubtargetInfo {
7979
public:
8080
BogusSubtarget(TargetMachine &TM)
81-
: TargetSubtargetInfo(Triple(""), "", "", "", {}, {}, nullptr, nullptr,
82-
nullptr, nullptr, nullptr, nullptr),
81+
: TargetSubtargetInfo(Triple(""), "", "", "", {}, {}, {}, nullptr,
82+
nullptr, nullptr, nullptr, nullptr, nullptr),
8383
FL(), TL(TM) {}
8484
~BogusSubtarget() override {}
8585

llvm/unittests/Target/AArch64/AArch64InstPrinterTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static std::string AArch64InstPrinterTestPrintAlignedLabel(uint64_t value) {
3636
MCAsmInfo MAI;
3737
MCInstrInfo MII;
3838
MCRegisterInfo MRI;
39-
MCSubtargetInfo STI(Triple(""), "", "", "",
39+
MCSubtargetInfo STI(Triple(""), "", "", "", {},
4040
ArrayRef((SubtargetFeatureKV *)NULL, (size_t)0),
4141
ArrayRef((SubtargetSubTypeKV *)NULL, (size_t)0), NULL,
4242
NULL, NULL, NULL, NULL, NULL);

llvm/utils/TableGen/SubtargetEmitter.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/StringExtras.h"
2222
#include "llvm/ADT/StringMap.h"
2323
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/ADT/StringSwitch.h"
2425
#include "llvm/MC/MCInstrItineraries.h"
2526
#include "llvm/MC/MCSchedule.h"
2627
#include "llvm/Support/Debug.h"
@@ -91,6 +92,7 @@ class SubtargetEmitter {
9192
void emitSubtargetInfoMacroCalls(raw_ostream &OS);
9293
unsigned featureKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
9394
unsigned cpuKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
95+
unsigned cpuNames(raw_ostream &OS);
9496
void formItineraryStageString(const std::string &Names,
9597
const Record *ItinData, std::string &ItinString,
9698
unsigned &NStages);
@@ -297,6 +299,40 @@ unsigned SubtargetEmitter::featureKeyValues(raw_ostream &OS,
297299
return FeatureList.size();
298300
}
299301

302+
unsigned SubtargetEmitter::cpuNames(raw_ostream &OS) {
303+
// Begin processor name table.
304+
OS << "// Sorted array of names of CPU subtypes, including aliases.\n"
305+
<< "extern const llvm::StringRef " << Target << "Names[] = {\n";
306+
307+
std::vector<const Record *> ProcessorList =
308+
Records.getAllDerivedDefinitions("Processor");
309+
310+
std::vector<const Record *> ProcessorAliasList =
311+
Records.getAllDerivedDefinitionsIfDefined("ProcessorAlias");
312+
313+
SmallVector<StringRef> Names;
314+
Names.reserve(ProcessorList.size() + ProcessorAliasList.size());
315+
316+
for (const Record *Processor : ProcessorList) {
317+
StringRef Name = Processor->getValueAsString("Name");
318+
Names.push_back(Name);
319+
}
320+
321+
for (const Record *Rec : ProcessorAliasList) {
322+
auto Name = Rec->getValueAsString("Name");
323+
Names.push_back(Name);
324+
}
325+
326+
llvm::sort(Names);
327+
llvm::interleave(
328+
Names, OS, [&](StringRef Name) { OS << '"' << Name << '"'; }, ",\n");
329+
330+
// End processor name table.
331+
OS << "};\n";
332+
333+
return Names.size();
334+
}
335+
300336
//
301337
// CPUKeyValues - Emit data of all the subtarget processors. Used by command
302338
// line.
@@ -1926,13 +1962,14 @@ void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
19261962
<< "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
19271963
OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT,\n"
19281964
<< " StringRef CPU, StringRef TuneCPU, StringRef FS,\n"
1965+
<< " ArrayRef<StringRef> PN,\n"
19291966
<< " ArrayRef<SubtargetFeatureKV> PF,\n"
19301967
<< " ArrayRef<SubtargetSubTypeKV> PD,\n"
19311968
<< " const MCWriteProcResEntry *WPR,\n"
19321969
<< " const MCWriteLatencyEntry *WL,\n"
19331970
<< " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
19341971
<< " const unsigned *OC, const unsigned *FP) :\n"
1935-
<< " MCSubtargetInfo(TT, CPU, TuneCPU, FS, PF, PD,\n"
1972+
<< " MCSubtargetInfo(TT, CPU, TuneCPU, FS, PN, PF, PD,\n"
19361973
<< " WPR, WL, RA, IS, OC, FP) { }\n\n"
19371974
<< " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
19381975
<< " const MCInst *MI, const MCInstrInfo *MCII,\n"
@@ -2001,6 +2038,8 @@ void SubtargetEmitter::run(raw_ostream &OS) {
20012038
OS << "\n";
20022039
unsigned NumProcs = cpuKeyValues(OS, FeatureMap);
20032040
OS << "\n";
2041+
unsigned NumNames = cpuNames(OS);
2042+
OS << "\n";
20042043

20052044
// MCInstrInfo initialization routine.
20062045
emitGenMCSubtargetInfo(OS);
@@ -2013,6 +2052,10 @@ void SubtargetEmitter::run(raw_ostream &OS) {
20132052
<< " TuneCPU = AArch64::resolveCPUAlias(TuneCPU);\n";
20142053
OS << " return new " << Target
20152054
<< "GenMCSubtargetInfo(TT, CPU, TuneCPU, FS, ";
2055+
if (NumNames)
2056+
OS << Target << "Names, ";
2057+
else
2058+
OS << "{}, ";
20162059
if (NumFeatures)
20172060
OS << Target << "FeatureKV, ";
20182061
else
@@ -2096,6 +2139,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
20962139

20972140
OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
20982141
OS << "namespace llvm {\n";
2142+
OS << "extern const llvm::StringRef " << Target << "Names[];\n";
20992143
OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
21002144
OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
21012145
OS << "extern const llvm::MCWriteProcResEntry " << Target
@@ -2119,6 +2163,10 @@ void SubtargetEmitter::run(raw_ostream &OS) {
21192163
<< " AArch64::resolveCPUAlias(TuneCPU), FS, ";
21202164
else
21212165
OS << " : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, ";
2166+
if (NumNames)
2167+
OS << "ArrayRef(" << Target << "Names, " << NumNames << "), ";
2168+
else
2169+
OS << "{}, ";
21222170
if (NumFeatures)
21232171
OS << "ArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
21242172
else

0 commit comments

Comments
 (0)