Skip to content

Commit 951292b

Browse files
authored
[TableGen] Print a warning when a Processor contains duplicate Features / TuneFeatures (#137864)
I don't think a processor should normally contains duplicate features. This patch prints a warning whenever that happens in either the features or tune features.
1 parent a7402b0 commit 951292b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: llvm-tblgen -gen-subtarget -I %p/../../include %s 2>&1 | FileCheck %s
2+
3+
include "llvm/Target/Target.td"
4+
5+
def MyTarget : Target;
6+
7+
def FeatureA : SubtargetFeature<"NameA", "", "", "">;
8+
def FeatureB : SubtargetFeature<"NameB", "", "", "">;
9+
def FeatureC : SubtargetFeature<"NameC", "", "", "">;
10+
11+
// CHECK: warning: Processor CPU0 contains duplicate feature 'NameA'
12+
def P0 : ProcessorModel<"CPU0", NoSchedModel, [FeatureA, FeatureB, FeatureA]>;
13+
// CHECK: warning: Processor CPU1 contains duplicate tune feature 'NameB'
14+
// CHECK: warning: Processor CPU1 has 'NameC' in both feature and tune feature sets
15+
def P1 : ProcessorModel<"CPU1", NoSchedModel,
16+
/*Features=*/[FeatureC],
17+
/*TuneFeatures=*/[FeatureB, FeatureB, FeatureC]>;

llvm/utils/TableGen/SubtargetEmitter.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,30 @@ unsigned SubtargetEmitter::cpuNames(raw_ostream &OS) {
333333
return Names.size();
334334
}
335335

336+
static void checkDuplicateCPUFeatures(StringRef CPUName,
337+
ArrayRef<const Record *> Features,
338+
ArrayRef<const Record *> TuneFeatures) {
339+
// We have made sure each SubtargetFeature Record has a unique name, so we can
340+
// simply use pointer sets here.
341+
SmallPtrSet<const Record *, 8> FeatureSet, TuneFeatureSet;
342+
for (const auto *FeatureRec : Features) {
343+
if (!FeatureSet.insert(FeatureRec).second)
344+
PrintWarning("Processor " + CPUName + " contains duplicate feature '" +
345+
FeatureRec->getValueAsString("Name") + "'");
346+
}
347+
348+
for (const auto *TuneFeatureRec : TuneFeatures) {
349+
if (!TuneFeatureSet.insert(TuneFeatureRec).second)
350+
PrintWarning("Processor " + CPUName +
351+
" contains duplicate tune feature '" +
352+
TuneFeatureRec->getValueAsString("Name") + "'");
353+
if (FeatureSet.contains(TuneFeatureRec))
354+
PrintWarning("Processor " + CPUName + " has '" +
355+
TuneFeatureRec->getValueAsString("Name") +
356+
"' in both feature and tune feature sets");
357+
}
358+
}
359+
336360
//
337361
// CPUKeyValues - Emit data of all the subtarget processors. Used by command
338362
// line.
@@ -360,6 +384,10 @@ unsigned SubtargetEmitter::cpuKeyValues(raw_ostream &OS,
360384
ConstRecVec TuneFeatureList =
361385
Processor->getValueAsListOfDefs("TuneFeatures");
362386

387+
// Warn the user if there are duplicate processor features or tune
388+
// features.
389+
checkDuplicateCPUFeatures(Name, FeatureList, TuneFeatureList);
390+
363391
// Emit as "{ "cpu", "description", 0, { f1 , f2 , ... fn } },".
364392
OS << " { "
365393
<< "\"" << Name << "\", ";

0 commit comments

Comments
 (0)