Skip to content

Commit d109636

Browse files
authored
[LLVM][TableGen] Change GISelCombinerEmitter to use const RecordKeeper (#109187)
Change GISelCombinerEmitter to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent b594b93 commit d109636

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ void emitEncodingMacrosUndef(raw_ostream &OS) {
8080
<< "#undef " << EncodeMacroName << "8\n";
8181
}
8282

83-
std::string getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset,
83+
std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
8484
int HwModeIdx) {
8585
std::string Name = "GIFBS";
86-
for (const auto &Feature : FeatureBitset)
86+
for (const Record *Feature : FeatureBitset)
8787
Name += ("_" + Feature->getName()).str();
8888
if (HwModeIdx >= 0)
8989
Name += ("_HwMode" + std::to_string(HwModeIdx));
@@ -861,14 +861,6 @@ const std::vector<std::string> &RuleMatcher::getRequiredSimplePredicates() {
861861
return RequiredSimplePredicates;
862862
}
863863

864-
void RuleMatcher::addRequiredFeature(Record *Feature) {
865-
RequiredFeatures.push_back(Feature);
866-
}
867-
868-
const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const {
869-
return RequiredFeatures;
870-
}
871-
872864
unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) {
873865
unsigned NewInsnVarID = NextInsnVarID++;
874866
InsnVariableIDs[&Matcher] = NewInsnVarID;

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ using GISelFlags = std::uint16_t;
6262
void emitEncodingMacrosDef(raw_ostream &OS);
6363
void emitEncodingMacrosUndef(raw_ostream &OS);
6464

65-
std::string getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset,
65+
std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
6666
int HwModeIdx);
6767

6868
/// Takes a sequence of \p Rules and group them based on the predicates
@@ -516,7 +516,7 @@ class RuleMatcher : public Matcher {
516516
GISelFlags Flags = 0;
517517

518518
std::vector<std::string> RequiredSimplePredicates;
519-
std::vector<Record *> RequiredFeatures;
519+
std::vector<const Record *> RequiredFeatures;
520520
std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers;
521521

522522
DenseSet<unsigned> ErasedInsnIDs;
@@ -553,8 +553,12 @@ class RuleMatcher : public Matcher {
553553
uint64_t getRuleID() const { return RuleID; }
554554

555555
InstructionMatcher &addInstructionMatcher(StringRef SymbolicName);
556-
void addRequiredFeature(Record *Feature);
557-
const std::vector<Record *> &getRequiredFeatures() const;
556+
void addRequiredFeature(const Record *Feature) {
557+
RequiredFeatures.push_back(Feature);
558+
}
559+
ArrayRef<const Record *> getRequiredFeatures() const {
560+
return RequiredFeatures;
561+
}
558562

559563
void addHwModeIdx(unsigned Idx) { HwModeIdx = Idx; }
560564
int getHwModeIdx() const { return HwModeIdx; }

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTableExecutorEmitter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ void GlobalISelMatchTableExecutorEmitter::emitSubtargetFeatureBitsetImpl(
4949

5050
// Emit a table containing the PredicateBitsets objects needed by the matcher
5151
// and an enum for the matcher to reference them with.
52-
std::vector<std::pair<std::vector<Record *>, int>> FeatureBitsets;
52+
std::vector<std::pair<std::vector<const Record *>, int>> FeatureBitsets;
5353
FeatureBitsets.reserve(Rules.size());
5454
for (auto &Rule : Rules)
5555
FeatureBitsets.emplace_back(Rule.getRequiredFeatures(),
5656
Rule.getHwModeIdx());
5757
llvm::sort(FeatureBitsets,
58-
[&](const std::pair<std::vector<Record *>, int> &A,
59-
const std::pair<std::vector<Record *>, int> &B) {
58+
[&](const std::pair<std::vector<const Record *>, int> &A,
59+
const std::pair<std::vector<const Record *>, int> &B) {
6060
if (A.first.size() < B.first.size())
6161
return true;
6262
if (A.first.size() > B.first.size())

llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ std::vector<std::string> CombineRuleOperandTypeChecker::getMCOIOperandTypes(
461461

462462
std::vector<std::string> OpTypes;
463463
auto &CGI = CGP.getInst();
464-
Record *VarArgsTy = CGI.TheDef->isSubClassOf("GenericInstruction")
465-
? CGI.TheDef->getValueAsOptionalDef("variadicOpsType")
466-
: nullptr;
464+
const Record *VarArgsTy =
465+
CGI.TheDef->isSubClassOf("GenericInstruction")
466+
? CGI.TheDef->getValueAsOptionalDef("variadicOpsType")
467+
: nullptr;
467468
std::string VarArgsTyName =
468469
VarArgsTy ? ("MCOI::" + VarArgsTy->getValueAsString("OperandType")).str()
469470
: ("unknown_type_" + Twine(UnknownTypeIdx++)).str();
@@ -636,7 +637,7 @@ class CombineRuleBuilder {
636637

637638
CombineRuleBuilder(const CodeGenTarget &CGT,
638639
SubtargetFeatureInfoMap &SubtargetFeatures,
639-
Record &RuleDef, unsigned ID,
640+
const Record &RuleDef, unsigned ID,
640641
std::vector<RuleMatcher> &OutRMs)
641642
: Parser(CGT, RuleDef.getLoc()), CGT(CGT),
642643
SubtargetFeatures(SubtargetFeatures), RuleDef(RuleDef), RuleID(ID),
@@ -765,7 +766,7 @@ class CombineRuleBuilder {
765766
PatternParser Parser;
766767
const CodeGenTarget &CGT;
767768
SubtargetFeatureInfoMap &SubtargetFeatures;
768-
Record &RuleDef;
769+
const Record &RuleDef;
769770
const unsigned RuleID;
770771
std::vector<RuleMatcher> &OutRMs;
771772

@@ -1374,12 +1375,12 @@ bool CombineRuleBuilder::addFeaturePredicates(RuleMatcher &M) {
13741375
return true;
13751376

13761377
ListInit *Preds = RuleDef.getValueAsListInit("Predicates");
1377-
for (Init *PI : Preds->getValues()) {
1378-
DefInit *Pred = dyn_cast<DefInit>(PI);
1378+
for (const Init *PI : Preds->getValues()) {
1379+
const DefInit *Pred = dyn_cast<DefInit>(PI);
13791380
if (!Pred)
13801381
continue;
13811382

1382-
Record *Def = Pred->getDef();
1383+
const Record *Def = Pred->getDef();
13831384
if (!Def->isSubClassOf("Predicate")) {
13841385
::PrintError(Def, "Unknown 'Predicate' Type");
13851386
return false;
@@ -1525,7 +1526,7 @@ bool CombineRuleBuilder::parseDefs(const DagInit &Def) {
15251526
// Subclasses of GIDefMatchData should declare that this rule needs to pass
15261527
// data from the match stage to the apply stage, and ensure that the
15271528
// generated matcher has a suitable variable for it to do so.
1528-
if (Record *MatchDataRec =
1529+
if (const Record *MatchDataRec =
15291530
getDefOfSubClass(*Def.getArg(I), MatchDataClassName)) {
15301531
MatchDatas.emplace_back(Def.getArgNameStr(I),
15311532
MatchDataRec->getValueAsString("Type"));
@@ -2360,10 +2361,10 @@ bool CombineRuleBuilder::emitCodeGenInstructionMatchPattern(
23602361
/// static storage pools and wires them together to emit the match table &
23612362
/// associated function/data structures.
23622363
class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
2363-
RecordKeeper &Records;
2364+
const RecordKeeper &Records;
23642365
StringRef Name;
23652366
const CodeGenTarget &Target;
2366-
Record *Combiner;
2367+
const Record *Combiner;
23672368
unsigned NextRuleID = 0;
23682369

23692370
// List all combine rules (ID, name) imported.
@@ -2403,11 +2404,12 @@ class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
24032404
}
24042405

24052406
void gatherRules(std::vector<RuleMatcher> &Rules,
2406-
const std::vector<Record *> &&RulesAndGroups);
2407+
ArrayRef<const Record *> RulesAndGroups);
24072408

24082409
public:
2409-
explicit GICombinerEmitter(RecordKeeper &RK, const CodeGenTarget &Target,
2410-
StringRef Name, Record *Combiner);
2410+
explicit GICombinerEmitter(const RecordKeeper &RK,
2411+
const CodeGenTarget &Target, StringRef Name,
2412+
const Record *Combiner);
24112413
~GICombinerEmitter() {}
24122414

24132415
void run(raw_ostream &OS);
@@ -2628,9 +2630,9 @@ void GICombinerEmitter::emitRunCustomAction(raw_ostream &OS) {
26282630
<< "}\n";
26292631
}
26302632

2631-
GICombinerEmitter::GICombinerEmitter(RecordKeeper &RK,
2633+
GICombinerEmitter::GICombinerEmitter(const RecordKeeper &RK,
26322634
const CodeGenTarget &Target,
2633-
StringRef Name, Record *Combiner)
2635+
StringRef Name, const Record *Combiner)
26342636
: Records(RK), Name(Name), Target(Target), Combiner(Combiner) {}
26352637

26362638
MatchTable
@@ -2675,12 +2677,11 @@ GICombinerEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules) {
26752677
}
26762678

26772679
/// Recurse into GICombineGroup's and flatten the ruleset into a simple list.
2678-
void GICombinerEmitter::gatherRules(
2679-
std::vector<RuleMatcher> &ActiveRules,
2680-
const std::vector<Record *> &&RulesAndGroups) {
2681-
for (Record *Rec : RulesAndGroups) {
2680+
void GICombinerEmitter::gatherRules(std::vector<RuleMatcher> &ActiveRules,
2681+
ArrayRef<const Record *> RulesAndGroups) {
2682+
for (const Record *Rec : RulesAndGroups) {
26822683
if (!Rec->isValueUnset("Rules")) {
2683-
gatherRules(ActiveRules, Rec->getValueAsListOfDefs("Rules"));
2684+
gatherRules(ActiveRules, Rec->getValueAsListOfConstDefs("Rules"));
26842685
continue;
26852686
}
26862687

@@ -2719,7 +2720,7 @@ void GICombinerEmitter::run(raw_ostream &OS) {
27192720

27202721
Records.startTimer("Gather rules");
27212722
std::vector<RuleMatcher> Rules;
2722-
gatherRules(Rules, Combiner->getValueAsListOfDefs("Rules"));
2723+
gatherRules(Rules, Combiner->getValueAsListOfConstDefs("Rules"));
27232724
if (ErrorsPrinted)
27242725
PrintFatalError(Combiner->getLoc(), "Failed to parse one or more rules");
27252726

@@ -2747,11 +2748,6 @@ void GICombinerEmitter::run(raw_ostream &OS) {
27472748

27482749
emitSourceFileHeader(getClassName().str() + " Combiner Match Table", OS);
27492750

2750-
// Unused
2751-
std::vector<StringRef> CustomRendererFns;
2752-
// Unused
2753-
std::vector<Record *> ComplexPredicates;
2754-
27552751
SmallVector<LLTCodeGen, 16> TypeObjects;
27562752
append_range(TypeObjects, KnownTypes);
27572753
llvm::sort(TypeObjects);
@@ -2780,8 +2776,8 @@ void GICombinerEmitter::run(raw_ostream &OS) {
27802776
emitTemporariesDecl(OS, "GET_GICOMBINER_CLASS_MEMBERS");
27812777

27822778
// GET_GICOMBINER_IMPL, which needs to be included outside the class.
2783-
emitExecutorImpl(OS, Table, TypeObjects, Rules, ComplexPredicates,
2784-
CustomRendererFns, "GET_GICOMBINER_IMPL");
2779+
emitExecutorImpl(OS, Table, TypeObjects, Rules, {}, {},
2780+
"GET_GICOMBINER_IMPL");
27852781

27862782
// GET_GICOMBINER_CONSTRUCTOR_INITS, which are in the constructor's
27872783
// initializer list.
@@ -2793,14 +2789,14 @@ void GICombinerEmitter::run(raw_ostream &OS) {
27932789

27942790
//===----------------------------------------------------------------------===//
27952791

2796-
static void EmitGICombiner(RecordKeeper &RK, raw_ostream &OS) {
2792+
static void EmitGICombiner(const RecordKeeper &RK, raw_ostream &OS) {
27972793
EnablePrettyStackTrace();
2798-
CodeGenTarget Target(RK);
2794+
const CodeGenTarget Target(RK);
27992795

28002796
if (SelectedCombiners.empty())
28012797
PrintFatalError("No combiners selected with -combiners");
28022798
for (const auto &Combiner : SelectedCombiners) {
2803-
Record *CombinerDef = RK.getDef(Combiner);
2799+
const Record *CombinerDef = RK.getDef(Combiner);
28042800
if (!CombinerDef)
28052801
PrintFatalError("Could not find " + Combiner);
28062802
GICombinerEmitter(RK, Target, Combiner, CombinerDef).run(OS);

0 commit comments

Comments
 (0)