Skip to content

Commit bfa8519

Browse files
authored
[LLVM][TableGen] Change GlobalISelEmitter to use const RecordKeeper (#110109)
Change GlobalISelEmitter 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 054eadc commit bfa8519

File tree

5 files changed

+65
-60
lines changed

5 files changed

+65
-60
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,9 @@ void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) {
888888
RM.getGISelFlags());
889889
}
890890

891-
void RuleMatcher::definePhysRegOperand(Record *Reg, OperandMatcher &OM) {
892-
if (!PhysRegOperands.contains(Reg)) {
891+
void RuleMatcher::definePhysRegOperand(const Record *Reg, OperandMatcher &OM) {
892+
if (!PhysRegOperands.contains(Reg))
893893
PhysRegOperands[Reg] = &OM;
894-
return;
895-
}
896894
}
897895

898896
InstructionMatcher &
@@ -904,7 +902,8 @@ RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const {
904902
("Failed to lookup instruction " + SymbolicName).str().c_str());
905903
}
906904

907-
const OperandMatcher &RuleMatcher::getPhysRegOperandMatcher(Record *Reg) const {
905+
const OperandMatcher &
906+
RuleMatcher::getPhysRegOperandMatcher(const Record *Reg) const {
908907
const auto &I = PhysRegOperands.find(Reg);
909908

910909
if (I == PhysRegOperands.end()) {
@@ -1717,7 +1716,8 @@ OperandMatcher &InstructionMatcher::getOperand(unsigned OpIdx) {
17171716
llvm_unreachable("Failed to lookup operand");
17181717
}
17191718

1720-
OperandMatcher &InstructionMatcher::addPhysRegInput(Record *Reg, unsigned OpIdx,
1719+
OperandMatcher &InstructionMatcher::addPhysRegInput(const Record *Reg,
1720+
unsigned OpIdx,
17211721
unsigned TempOpIdx) {
17221722
assert(SymbolicName.empty());
17231723
OperandMatcher *OM = new OperandMatcher(*this, OpIdx, "", TempOpIdx);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class RuleMatcher : public Matcher {
494494

495495
/// A map of anonymous physical register operands defined by the matchers that
496496
/// may be referenced by the renderers.
497-
DenseMap<Record *, OperandMatcher *> PhysRegOperands;
497+
DenseMap<const Record *, OperandMatcher *> PhysRegOperands;
498498

499499
/// ID for the next instruction variable defined with
500500
/// implicitlyDefineInsnVar()
@@ -651,7 +651,7 @@ class RuleMatcher : public Matcher {
651651

652652
void defineOperand(StringRef SymbolicName, OperandMatcher &OM);
653653

654-
void definePhysRegOperand(Record *Reg, OperandMatcher &OM);
654+
void definePhysRegOperand(const Record *Reg, OperandMatcher &OM);
655655

656656
Error defineComplexSubOperand(StringRef SymbolicName,
657657
const Record *ComplexPattern,
@@ -669,7 +669,7 @@ class RuleMatcher : public Matcher {
669669
InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const;
670670
OperandMatcher &getOperandMatcher(StringRef Name);
671671
const OperandMatcher &getOperandMatcher(StringRef Name) const;
672-
const OperandMatcher &getPhysRegOperandMatcher(Record *) const;
672+
const OperandMatcher &getPhysRegOperandMatcher(const Record *) const;
673673

674674
void optimize() override;
675675
void emit(MatchTable &Table) override;
@@ -1759,7 +1759,7 @@ class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
17591759
/// PhysRegInputs - List list has an entry for each explicitly specified
17601760
/// physreg input to the pattern. The first elt is the Register node, the
17611761
/// second is the recorded slot number the input pattern match saved it in.
1762-
SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs;
1762+
SmallVector<std::pair<const Record *, unsigned>, 2> PhysRegInputs;
17631763

17641764
bool canAddNumOperandsCheck() const {
17651765
// Add if it's allowed, and:
@@ -1799,10 +1799,10 @@ class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
17991799
unsigned AllocatedTemporariesBaseID,
18001800
bool IsVariadic = false);
18011801
OperandMatcher &getOperand(unsigned OpIdx);
1802-
OperandMatcher &addPhysRegInput(Record *Reg, unsigned OpIdx,
1802+
OperandMatcher &addPhysRegInput(const Record *Reg, unsigned OpIdx,
18031803
unsigned TempOpIdx);
18041804

1805-
ArrayRef<std::pair<Record *, unsigned>> getPhysRegInputs() const {
1805+
ArrayRef<std::pair<const Record *, unsigned>> getPhysRegInputs() const {
18061806
return PhysRegInputs;
18071807
}
18081808

@@ -1969,10 +1969,10 @@ class CopyRenderer : public OperandRenderer {
19691969
class CopyPhysRegRenderer : public OperandRenderer {
19701970
protected:
19711971
unsigned NewInsnID;
1972-
Record *PhysReg;
1972+
const Record *PhysReg;
19731973

19741974
public:
1975-
CopyPhysRegRenderer(unsigned NewInsnID, Record *Reg)
1975+
CopyPhysRegRenderer(unsigned NewInsnID, const Record *Reg)
19761976
: OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID), PhysReg(Reg) {
19771977
assert(PhysReg);
19781978
}
@@ -1981,7 +1981,7 @@ class CopyPhysRegRenderer : public OperandRenderer {
19811981
return R->getKind() == OR_CopyPhysReg;
19821982
}
19831983

1984-
Record *getPhysReg() const { return PhysReg; }
1984+
const Record *getPhysReg() const { return PhysReg; }
19851985

19861986
void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override;
19871987
};

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void GlobalISelMatchTableExecutorEmitter::emitSubtargetFeatureBitsetImpl(
103103
}
104104

105105
void GlobalISelMatchTableExecutorEmitter::emitComplexPredicates(
106-
raw_ostream &OS, ArrayRef<Record *> ComplexOperandMatchers) {
106+
raw_ostream &OS, ArrayRef<const Record *> ComplexOperandMatchers) {
107107
// Emit complex predicate table and an enum to reference them with.
108108
OS << "// ComplexPattern predicates.\n"
109109
<< "enum {\n"
@@ -174,7 +174,8 @@ void GlobalISelMatchTableExecutorEmitter::emitMatchTable(
174174

175175
void GlobalISelMatchTableExecutorEmitter::emitExecutorImpl(
176176
raw_ostream &OS, const MatchTable &Table, ArrayRef<LLTCodeGen> TypeObjects,
177-
ArrayRef<RuleMatcher> Rules, ArrayRef<Record *> ComplexOperandMatchers,
177+
ArrayRef<RuleMatcher> Rules,
178+
ArrayRef<const Record *> ComplexOperandMatchers,
178179
ArrayRef<StringRef> CustomOperandRenderers, StringRef IfDefName) {
179180
OS << "#ifdef " << IfDefName << "\n";
180181
emitTypeObjects(OS, TypeObjects);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class GlobalISelMatchTableExecutorEmitter {
4242
/// Emits an enum + an array that stores references to
4343
/// \p ComplexOperandMatchers.
4444
void emitComplexPredicates(raw_ostream &OS,
45-
ArrayRef<Record *> ComplexOperandMatchers);
45+
ArrayRef<const Record *> ComplexOperandMatchers);
4646

4747
/// Emits an enum + an array that stores references to
4848
/// \p CustomOperandRenderers.
@@ -206,7 +206,7 @@ class GlobalISelMatchTableExecutorEmitter {
206206
void emitExecutorImpl(raw_ostream &OS, const gi::MatchTable &Table,
207207
ArrayRef<gi::LLTCodeGen> TypeObjects,
208208
ArrayRef<gi::RuleMatcher> Rules,
209-
ArrayRef<Record *> ComplexOperandMatchers,
209+
ArrayRef<const Record *> ComplexOperandMatchers,
210210
ArrayRef<StringRef> CustomOperandRenderers,
211211
StringRef IfDefName);
212212
void emitPredicateBitset(raw_ostream &OS, StringRef IfDefName);

llvm/utils/TableGen/GlobalISelEmitter.cpp

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ static Expected<LLTCodeGen> getInstResultType(const TreePatternNode &Dst,
314314

315315
class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
316316
public:
317-
explicit GlobalISelEmitter(RecordKeeper &RK);
317+
explicit GlobalISelEmitter(const RecordKeeper &RK);
318318

319319
void emitAdditionalImpl(raw_ostream &OS) override;
320320

@@ -335,18 +335,18 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
335335
private:
336336
std::string ClassName;
337337

338-
RecordKeeper &RK;
338+
const RecordKeeper &RK;
339339
const CodeGenDAGPatterns CGP;
340340
const CodeGenTarget &Target;
341341
CodeGenRegBank &CGRegs;
342342

343-
std::vector<Record *> AllPatFrags;
343+
ArrayRef<const Record *> AllPatFrags;
344344

345345
/// Keep track of the equivalence between SDNodes and Instruction by mapping
346346
/// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to
347347
/// check for attributes on the relation such as CheckMMOIsNonAtomic.
348348
/// This is defined using 'GINodeEquiv' in the target description.
349-
DenseMap<const Record *, Record *> NodeEquivs;
349+
DenseMap<const Record *, const Record *> NodeEquivs;
350350

351351
/// Keep track of the equivalence between ComplexPattern's and
352352
/// GIComplexOperandMatcher. Map entries are specified by subclassing
@@ -379,8 +379,8 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
379379
void gatherTypeIDValues();
380380
void gatherNodeEquivs();
381381

382-
Record *findNodeEquiv(const Record *N) const;
383-
const CodeGenInstruction *getEquivNode(Record &Equiv,
382+
const Record *findNodeEquiv(const Record *N) const;
383+
const CodeGenInstruction *getEquivNode(const Record &Equiv,
384384
const TreePatternNode &N) const;
385385

386386
Error importRulePredicates(RuleMatcher &M,
@@ -472,7 +472,7 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
472472
InstructionMatcher &InsnMatcher, bool &HasAddedMatcher);
473473
};
474474

475-
StringRef getPatFragPredicateEnumName(Record *R) { return R->getName(); }
475+
StringRef getPatFragPredicateEnumName(const Record *R) { return R->getName(); }
476476

477477
void GlobalISelEmitter::gatherOpcodeValues() {
478478
InstructionOpcodeMatcher::initOpcodeValuesMap(Target);
@@ -484,32 +484,35 @@ void GlobalISelEmitter::gatherTypeIDValues() {
484484

485485
void GlobalISelEmitter::gatherNodeEquivs() {
486486
assert(NodeEquivs.empty());
487-
for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
487+
for (const Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
488488
NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv;
489489

490490
assert(ComplexPatternEquivs.empty());
491-
for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
492-
Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
491+
for (const Record *Equiv :
492+
RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
493+
const Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
493494
if (!SelDAGEquiv)
494495
continue;
495496
ComplexPatternEquivs[SelDAGEquiv] = Equiv;
496497
}
497498

498499
assert(SDNodeXFormEquivs.empty());
499-
for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
500-
Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
500+
for (const Record *Equiv :
501+
RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
502+
const Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
501503
if (!SelDAGEquiv)
502504
continue;
503505
SDNodeXFormEquivs[SelDAGEquiv] = Equiv;
504506
}
505507
}
506508

507-
Record *GlobalISelEmitter::findNodeEquiv(const Record *N) const {
509+
const Record *GlobalISelEmitter::findNodeEquiv(const Record *N) const {
508510
return NodeEquivs.lookup(N);
509511
}
510512

511513
const CodeGenInstruction *
512-
GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode &N) const {
514+
GlobalISelEmitter::getEquivNode(const Record &Equiv,
515+
const TreePatternNode &N) const {
513516
if (N.getNumChildren() >= 1) {
514517
// setcc operation maps to two different G_* instructions based on the type.
515518
if (!Equiv.isValueUnset("IfFloatingPoint") &&
@@ -536,7 +539,7 @@ GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode &N) const {
536539
return &Target.getInstruction(Equiv.getValueAsDef("I"));
537540
}
538541

539-
GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
542+
GlobalISelEmitter::GlobalISelEmitter(const RecordKeeper &RK)
540543
: GlobalISelMatchTableExecutorEmitter(), RK(RK), CGP(RK),
541544
Target(CGP.getTargetInfo()), CGRegs(Target.getRegBank()) {
542545
ClassName = Target.getName().str() + "InstructionSelector";
@@ -721,7 +724,7 @@ Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
721724
const TreePatternNode &Src, unsigned &TempOpIdx) {
722725
const auto SavedFlags = Rule.setGISelFlags(Src.getGISelFlagsRecord());
723726

724-
Record *SrcGIEquivOrNull = nullptr;
727+
const Record *SrcGIEquivOrNull = nullptr;
725728
const CodeGenInstruction *SrcGIOrNull = nullptr;
726729

727730
// Start with the defined operands (i.e., the results of the root operator).
@@ -942,7 +945,7 @@ Error GlobalISelEmitter::importComplexPatternOperandMatcher(
942945
// Get the name to use for a pattern operand. For an anonymous physical register
943946
// input, this should use the register name.
944947
static StringRef getSrcChildName(const TreePatternNode &SrcChild,
945-
Record *&PhysReg) {
948+
const Record *&PhysReg) {
946949
StringRef SrcChildName = SrcChild.getName();
947950
if (SrcChildName.empty() && SrcChild.isLeaf()) {
948951
if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild.getLeafValue())) {
@@ -962,7 +965,7 @@ Error GlobalISelEmitter::importChildMatcher(
962965
const TreePatternNode &SrcChild, bool OperandIsAPointer,
963966
bool OperandIsImmArg, unsigned OpIdx, unsigned &TempOpIdx) {
964967

965-
Record *PhysReg = nullptr;
968+
const Record *PhysReg = nullptr;
966969
std::string SrcChildName = std::string(getSrcChildName(SrcChild, PhysReg));
967970
if (!SrcChild.isLeaf() &&
968971
SrcChild.getOperator()->isSubClassOf("ComplexPattern")) {
@@ -1196,7 +1199,8 @@ Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer(
11961199
auto &Child = DstChild.getChild(0);
11971200
auto I = SDNodeXFormEquivs.find(DstChild.getOperator());
11981201
if (I != SDNodeXFormEquivs.end()) {
1199-
Record *XFormOpc = DstChild.getOperator()->getValueAsDef("Opcode");
1202+
const Record *XFormOpc =
1203+
DstChild.getOperator()->getValueAsDef("Opcode");
12001204
if (XFormOpc->getName() == "timm") {
12011205
// If this is a TargetConstant, there won't be a corresponding
12021206
// instruction to transform. Instead, this will refer directly to an
@@ -2290,65 +2294,65 @@ void GlobalISelEmitter::emitAdditionalImpl(raw_ostream &OS) {
22902294
}
22912295

22922296
void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) {
2293-
std::vector<Record *> MatchedRecords;
2297+
std::vector<const Record *> MatchedRecords;
22942298
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
2295-
std::back_inserter(MatchedRecords), [&](Record *R) {
2299+
std::back_inserter(MatchedRecords), [](const Record *R) {
22962300
return !R->getValueAsString("GISelPredicateCode").empty();
22972301
});
2298-
emitMIPredicateFnsImpl<Record *>(
2302+
emitMIPredicateFnsImpl<const Record *>(
22992303
OS,
23002304
" const MachineFunction &MF = *MI.getParent()->getParent();\n"
23012305
" const MachineRegisterInfo &MRI = MF.getRegInfo();\n"
23022306
" const auto &Operands = State.RecordedOperands;\n"
23032307
" (void)Operands;\n"
23042308
" (void)MRI;",
2305-
ArrayRef<Record *>(MatchedRecords), &getPatFragPredicateEnumName,
2306-
[&](Record *R) { return R->getValueAsString("GISelPredicateCode"); },
2309+
ArrayRef<const Record *>(MatchedRecords), &getPatFragPredicateEnumName,
2310+
[](const Record *R) { return R->getValueAsString("GISelPredicateCode"); },
23072311
"PatFrag predicates.");
23082312
}
23092313

23102314
void GlobalISelEmitter::emitI64ImmPredicateFns(raw_ostream &OS) {
2311-
std::vector<Record *> MatchedRecords;
2315+
std::vector<const Record *> MatchedRecords;
23122316
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
2313-
std::back_inserter(MatchedRecords), [&](Record *R) {
2317+
std::back_inserter(MatchedRecords), [](const Record *R) {
23142318
bool Unset;
23152319
return !R->getValueAsString("ImmediateCode").empty() &&
23162320
!R->getValueAsBitOrUnset("IsAPFloat", Unset) &&
23172321
!R->getValueAsBit("IsAPInt");
23182322
});
2319-
emitImmPredicateFnsImpl<Record *>(
2320-
OS, "I64", "int64_t", ArrayRef<Record *>(MatchedRecords),
2323+
emitImmPredicateFnsImpl<const Record *>(
2324+
OS, "I64", "int64_t", ArrayRef<const Record *>(MatchedRecords),
23212325
&getPatFragPredicateEnumName,
2322-
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
2326+
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
23232327
"PatFrag predicates.");
23242328
}
23252329

23262330
void GlobalISelEmitter::emitAPFloatImmPredicateFns(raw_ostream &OS) {
2327-
std::vector<Record *> MatchedRecords;
2331+
std::vector<const Record *> MatchedRecords;
23282332
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
2329-
std::back_inserter(MatchedRecords), [&](Record *R) {
2333+
std::back_inserter(MatchedRecords), [](const Record *R) {
23302334
bool Unset;
23312335
return !R->getValueAsString("ImmediateCode").empty() &&
23322336
R->getValueAsBitOrUnset("IsAPFloat", Unset);
23332337
});
2334-
emitImmPredicateFnsImpl<Record *>(
2335-
OS, "APFloat", "const APFloat &", ArrayRef<Record *>(MatchedRecords),
2336-
&getPatFragPredicateEnumName,
2337-
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
2338+
emitImmPredicateFnsImpl<const Record *>(
2339+
OS, "APFloat", "const APFloat &",
2340+
ArrayRef<const Record *>(MatchedRecords), &getPatFragPredicateEnumName,
2341+
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
23382342
"PatFrag predicates.");
23392343
}
23402344

23412345
void GlobalISelEmitter::emitAPIntImmPredicateFns(raw_ostream &OS) {
2342-
std::vector<Record *> MatchedRecords;
2346+
std::vector<const Record *> MatchedRecords;
23432347
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
2344-
std::back_inserter(MatchedRecords), [&](Record *R) {
2348+
std::back_inserter(MatchedRecords), [](const Record *R) {
23452349
return !R->getValueAsString("ImmediateCode").empty() &&
23462350
R->getValueAsBit("IsAPInt");
23472351
});
2348-
emitImmPredicateFnsImpl<Record *>(
2349-
OS, "APInt", "const APInt &", ArrayRef<Record *>(MatchedRecords),
2352+
emitImmPredicateFnsImpl<const Record *>(
2353+
OS, "APInt", "const APInt &", ArrayRef<const Record *>(MatchedRecords),
23502354
&getPatFragPredicateEnumName,
2351-
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
2355+
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
23522356
"PatFrag predicates.");
23532357
}
23542358

@@ -2461,7 +2465,7 @@ void GlobalISelEmitter::run(raw_ostream &OS) {
24612465
return A->getName() < B->getName();
24622466
};
24632467

2464-
std::vector<Record *> ComplexPredicates =
2468+
std::vector<const Record *> ComplexPredicates =
24652469
RK.getAllDerivedDefinitions("GIComplexOperandMatcher");
24662470
llvm::sort(ComplexPredicates, OrderByName);
24672471

0 commit comments

Comments
 (0)