Skip to content

Commit 87e8b53

Browse files
authored
[LLVM][TableGen] Change CodeGenDAGPatterns to use const RecordKeeper (#108762)
Change CodeGenDAGPatterns 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 2395379 commit 87e8b53

File tree

6 files changed

+53
-62
lines changed

6 files changed

+53
-62
lines changed

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ std::string PatternToMatch::getPredicateCheck() const {
15291529
// SDTypeConstraint implementation
15301530
//
15311531

1532-
SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) {
1532+
SDTypeConstraint::SDTypeConstraint(const Record *R, const CodeGenHwModes &CGH) {
15331533
OperandNo = R->getValueAsInt("OperandNum");
15341534

15351535
if (R->isSubClassOf("SDTCisVT")) {
@@ -1799,7 +1799,7 @@ bool TreePatternNode::setDefaultMode(unsigned Mode) {
17991799
//===----------------------------------------------------------------------===//
18001800
// SDNodeInfo implementation
18011801
//
1802-
SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) {
1802+
SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
18031803
EnumName = R->getValueAsString("Opcode");
18041804
SDClassName = R->getValueAsString("SDClass");
18051805
Record *TypeProfile = R->getValueAsDef("TypeProfile");
@@ -2296,7 +2296,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
22962296
assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
22972297
if (NotRegisters)
22982298
return TypeSetByHwMode(); // Unknown.
2299-
Record *T = CDP.getComplexPattern(R).getValueType();
2299+
const Record *T = CDP.getComplexPattern(R).getValueType();
23002300
const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
23012301
return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
23022302
}
@@ -2700,7 +2700,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
27002700

27012701
if (!NotRegisters) {
27022702
assert(Types.size() == 1 && "ComplexPatterns only produce one result!");
2703-
Record *T = CDP.getComplexPattern(getOperator()).getValueType();
2703+
const Record *T = CDP.getComplexPattern(getOperator()).getValueType();
27042704
const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
27052705
const ValueTypeByHwMode VVT = getValueTypeByHwMode(T, CGH);
27062706
// TODO: AArch64 and AMDGPU use ComplexPattern<untyped, ...> and then
@@ -3157,7 +3157,7 @@ void TreePattern::dump() const { print(errs()); }
31573157
// CodeGenDAGPatterns implementation
31583158
//
31593159

3160-
CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R,
3160+
CodeGenDAGPatterns::CodeGenDAGPatterns(const RecordKeeper &R,
31613161
PatternRewriterFn PatternRewriter)
31623162
: Records(R), Target(R), Intrinsics(R),
31633163
LegalVTS(Target.getLegalValueTypes()), PatternRewriter(PatternRewriter) {
@@ -3198,14 +3198,10 @@ Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
31983198

31993199
// Parse all of the SDNode definitions for the target, populating SDNodes.
32003200
void CodeGenDAGPatterns::ParseNodeInfo() {
3201-
std::vector<Record *> Nodes = Records.getAllDerivedDefinitions("SDNode");
32023201
const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
32033202

3204-
while (!Nodes.empty()) {
3205-
Record *R = Nodes.back();
3203+
for (const Record *R : reverse(Records.getAllDerivedDefinitions("SDNode")))
32063204
SDNodes.insert(std::pair(R, SDNodeInfo(R, CGH)));
3207-
Nodes.pop_back();
3208-
}
32093205

32103206
// Get the builtin intrinsic nodes.
32113207
intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
@@ -3216,26 +3212,18 @@ void CodeGenDAGPatterns::ParseNodeInfo() {
32163212
/// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
32173213
/// map, and emit them to the file as functions.
32183214
void CodeGenDAGPatterns::ParseNodeTransforms() {
3219-
std::vector<Record *> Xforms =
3220-
Records.getAllDerivedDefinitions("SDNodeXForm");
3221-
while (!Xforms.empty()) {
3222-
Record *XFormNode = Xforms.back();
3223-
Record *SDNode = XFormNode->getValueAsDef("Opcode");
3215+
for (const Record *XFormNode :
3216+
reverse(Records.getAllDerivedDefinitions("SDNodeXForm"))) {
3217+
const Record *SDNode = XFormNode->getValueAsDef("Opcode");
32243218
StringRef Code = XFormNode->getValueAsString("XFormFunction");
3225-
SDNodeXForms.insert(
3226-
std::pair(XFormNode, NodeXForm(SDNode, std::string(Code))));
3227-
3228-
Xforms.pop_back();
3219+
SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, std::string(Code))});
32293220
}
32303221
}
32313222

32323223
void CodeGenDAGPatterns::ParseComplexPatterns() {
3233-
std::vector<Record *> AMs =
3234-
Records.getAllDerivedDefinitions("ComplexPattern");
3235-
while (!AMs.empty()) {
3236-
ComplexPatterns.insert(std::pair(AMs.back(), AMs.back()));
3237-
AMs.pop_back();
3238-
}
3224+
for (const Record *R :
3225+
reverse(Records.getAllDerivedDefinitions("ComplexPattern")))
3226+
ComplexPatterns.insert({R, R});
32393227
}
32403228

32413229
/// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
@@ -3244,11 +3232,10 @@ void CodeGenDAGPatterns::ParseComplexPatterns() {
32443232
/// inside a pattern fragment to a pattern fragment.
32453233
///
32463234
void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
3247-
std::vector<Record *> Fragments =
3248-
Records.getAllDerivedDefinitions("PatFrags");
3249-
32503235
// First step, parse all of the fragments.
3251-
for (Record *Frag : Fragments) {
3236+
ArrayRef<const Record *> Fragments =
3237+
Records.getAllDerivedDefinitions("PatFrags");
3238+
for (const Record *Frag : Fragments) {
32523239
if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
32533240
continue;
32543241

@@ -3307,7 +3294,7 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
33073294

33083295
// Now that we've parsed all of the tree fragments, do a closure on them so
33093296
// that there are not references to PatFrags left inside of them.
3310-
for (Record *Frag : Fragments) {
3297+
for (const Record *Frag : Fragments) {
33113298
if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
33123299
continue;
33133300

@@ -3331,8 +3318,8 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
33313318
}
33323319

33333320
void CodeGenDAGPatterns::ParseDefaultOperands() {
3334-
std::vector<Record *> DefaultOps;
3335-
DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
3321+
ArrayRef<const Record *> DefaultOps =
3322+
Records.getAllDerivedDefinitions("OperandWithDefaultOps");
33363323

33373324
// Find some SDNode.
33383325
assert(!SDNodes.empty() && "No SDNodes parsed?");
@@ -3947,10 +3934,7 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
39473934
/// any fragments involved. This populates the Instructions list with fully
39483935
/// resolved instructions.
39493936
void CodeGenDAGPatterns::ParseInstructions() {
3950-
std::vector<Record *> Instrs =
3951-
Records.getAllDerivedDefinitions("Instruction");
3952-
3953-
for (Record *Instr : Instrs) {
3937+
for (const Record *Instr : Records.getAllDerivedDefinitions("Instruction")) {
39543938
ListInit *LI = nullptr;
39553939

39563940
if (isa<ListInit>(Instr->getValueInit("Pattern")))
@@ -4346,9 +4330,7 @@ void CodeGenDAGPatterns::ParseOnePattern(
43464330
}
43474331

43484332
void CodeGenDAGPatterns::ParsePatterns() {
4349-
std::vector<Record *> Patterns = Records.getAllDerivedDefinitions("Pattern");
4350-
4351-
for (Record *CurPattern : Patterns) {
4333+
for (const Record *CurPattern : Records.getAllDerivedDefinitions("Pattern")) {
43524334
DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
43534335

43544336
// If the pattern references the null_frag, there's nothing to do.
@@ -4407,10 +4389,10 @@ void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
44074389
return;
44084390
}
44094391

4410-
PatternsToMatch.emplace_back(
4411-
P.getSrcRecord(), P.getPredicates(), std::move(NewSrc),
4412-
std::move(NewDst), P.getDstRegs(), P.getAddedComplexity(),
4413-
Record::getNewUID(Records), P.getGISelShouldIgnore(), Check);
4392+
PatternsToMatch.emplace_back(P.getSrcRecord(), P.getPredicates(),
4393+
std::move(NewSrc), std::move(NewDst),
4394+
P.getDstRegs(), P.getAddedComplexity(),
4395+
getNewUID(), P.getGISelShouldIgnore(), Check);
44144396
};
44154397

44164398
for (PatternToMatch &P : Copy) {
@@ -4780,11 +4762,16 @@ void CodeGenDAGPatterns::GenerateVariants() {
47804762
PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
47814763
Variant, PatternsToMatch[i].getDstPatternShared(),
47824764
PatternsToMatch[i].getDstRegs(),
4783-
PatternsToMatch[i].getAddedComplexity(), Record::getNewUID(Records),
4765+
PatternsToMatch[i].getAddedComplexity(), getNewUID(),
47844766
PatternsToMatch[i].getGISelShouldIgnore(),
47854767
PatternsToMatch[i].getHwModeFeatures());
47864768
}
47874769

47884770
LLVM_DEBUG(errs() << "\n");
47894771
}
47904772
}
4773+
4774+
unsigned CodeGenDAGPatterns::getNewUID() {
4775+
RecordKeeper &MutableRC = const_cast<RecordKeeper &>(Records);
4776+
return Record::getNewUID(MutableRC);
4777+
}

llvm/utils/TableGen/Common/CodeGenDAGPatterns.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ typedef StringSet<> MultipleUseVarSet;
354354
/// SDTypeConstraint - This is a discriminated union of constraints,
355355
/// corresponding to the SDTypeConstraint tablegen class in Target.td.
356356
struct SDTypeConstraint {
357-
SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
357+
SDTypeConstraint(const Record *R, const CodeGenHwModes &CGH);
358358

359359
unsigned OperandNo; // The operand # this constraint applies to.
360360
enum {
@@ -435,7 +435,7 @@ class ScopedName {
435435
/// the target .td file. This represents the various dag nodes we will be
436436
/// processing.
437437
class SDNodeInfo {
438-
Record *Def;
438+
const Record *Def;
439439
StringRef EnumName;
440440
StringRef SDClassName;
441441
unsigned Properties;
@@ -445,14 +445,14 @@ class SDNodeInfo {
445445

446446
public:
447447
// Parse the specified record.
448-
SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
448+
SDNodeInfo(const Record *R, const CodeGenHwModes &CGH);
449449

450450
unsigned getNumResults() const { return NumResults; }
451451

452452
/// getNumOperands - This is the number of operands required or -1 if
453453
/// variadic.
454454
int getNumOperands() const { return NumOperands; }
455-
Record *getRecord() const { return Def; }
455+
const Record *getRecord() const { return Def; }
456456
StringRef getEnumName() const { return EnumName; }
457457
StringRef getSDClassName() const { return SDClassName; }
458458

@@ -1095,13 +1095,17 @@ class PatternToMatch {
10951095
};
10961096

10971097
class CodeGenDAGPatterns {
1098-
RecordKeeper &Records;
1098+
public:
1099+
using NodeXForm = std::pair<const Record *, std::string>;
1100+
1101+
private:
1102+
const RecordKeeper &Records;
10991103
CodeGenTarget Target;
11001104
CodeGenIntrinsicTable Intrinsics;
11011105

11021106
std::map<const Record *, SDNodeInfo, LessRecordByID> SDNodes;
1103-
std::map<const Record *, std::pair<Record *, std::string>, LessRecordByID>
1104-
SDNodeXForms;
1107+
1108+
std::map<const Record *, NodeXForm, LessRecordByID> SDNodeXForms;
11051109
std::map<const Record *, ComplexPattern, LessRecordByID> ComplexPatterns;
11061110
std::map<const Record *, std::unique_ptr<TreePattern>, LessRecordByID>
11071111
PatternFragments;
@@ -1125,7 +1129,7 @@ class CodeGenDAGPatterns {
11251129
unsigned NumScopes = 0;
11261130

11271131
public:
1128-
CodeGenDAGPatterns(RecordKeeper &R,
1132+
CodeGenDAGPatterns(const RecordKeeper &R,
11291133
PatternRewriterFn PatternRewriter = nullptr);
11301134

11311135
CodeGenTarget &getTargetInfo() { return Target; }
@@ -1141,7 +1145,6 @@ class CodeGenDAGPatterns {
11411145
}
11421146

11431147
// Node transformation lookups.
1144-
typedef std::pair<Record *, std::string> NodeXForm;
11451148
const NodeXForm &getSDNodeTransform(const Record *R) const {
11461149
auto F = SDNodeXForms.find(R);
11471150
assert(F != SDNodeXForms.end() && "Invalid transform!");
@@ -1254,6 +1257,7 @@ class CodeGenDAGPatterns {
12541257
MapVector<std::string, TreePatternNodePtr,
12551258
std::map<std::string, unsigned>> &InstResults,
12561259
std::vector<Record *> &InstImpResults);
1260+
unsigned getNewUID();
12571261
};
12581262

12591263
inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode &N,

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ bool CodeGenTarget::guessInstructionProperties() const {
402402
//===----------------------------------------------------------------------===//
403403
// ComplexPattern implementation
404404
//
405-
ComplexPattern::ComplexPattern(Record *R) {
405+
ComplexPattern::ComplexPattern(const Record *R) {
406406
Ty = R->getValueAsDef("Ty");
407407
NumOperands = R->getValueAsInt("NumOperands");
408408
SelectFunc = std::string(R->getValueAsString("SelectFunc"));
409-
RootNodes = R->getValueAsListOfDefs("RootNodes");
409+
RootNodes = R->getValueAsListOfConstDefs("RootNodes");
410410

411411
// FIXME: This is a hack to statically increase the priority of patterns which
412412
// maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. To get best

llvm/utils/TableGen/Common/CodeGenTarget.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,20 @@ class CodeGenTarget {
238238
/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
239239
/// tablegen class in TargetSelectionDAG.td
240240
class ComplexPattern {
241-
Record *Ty;
241+
const Record *Ty;
242242
unsigned NumOperands;
243243
std::string SelectFunc;
244-
std::vector<Record *> RootNodes;
244+
std::vector<const Record *> RootNodes;
245245
unsigned Properties; // Node properties
246246
unsigned Complexity;
247247

248248
public:
249-
ComplexPattern(Record *R);
249+
ComplexPattern(const Record *R);
250250

251-
Record *getValueType() const { return Ty; }
251+
const Record *getValueType() const { return Ty; }
252252
unsigned getNumOperands() const { return NumOperands; }
253253
const std::string &getSelectFunc() const { return SelectFunc; }
254-
const std::vector<Record *> &getRootNodes() const { return RootNodes; }
254+
const ArrayRef<const Record *> getRootNodes() const { return RootNodes; }
255255
bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
256256
unsigned getComplexity() const { return Complexity; }
257257
};

llvm/utils/TableGen/DAGISelMatcherEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
12141214
const CodeGenDAGPatterns::NodeXForm &Entry =
12151215
CGP.getSDNodeTransform(NodeXForms[i]);
12161216

1217-
Record *SDNode = Entry.first;
1217+
const Record *SDNode = Entry.first;
12181218
const std::string &Code = Entry.second;
12191219

12201220
OS << " case " << i << ": { ";

llvm/utils/TableGen/DAGISelMatcherGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ bool MatcherGen::EmitMatcherCode(unsigned Variant) {
555555
// check.
556556
if (const ComplexPattern *CP =
557557
Pattern.getSrcPattern().getComplexPatternInfo(CGP)) {
558-
const std::vector<Record *> &OpNodes = CP->getRootNodes();
558+
ArrayRef<const Record *> OpNodes = CP->getRootNodes();
559559
assert(!OpNodes.empty() &&
560560
"Complex Pattern must specify what it can match");
561561
if (Variant >= OpNodes.size())

0 commit comments

Comments
 (0)