[llvm] 87e8b53 - [LLVM][TableGen] Change CodeGenDAGPatterns to use const RecordKeeper (#108762)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 10:26:06 PDT 2024


Author: Rahul Joshi
Date: 2024-09-15T10:26:03-07:00
New Revision: 87e8b53009f11033e6e6af0ccb3a97fae615526a

URL: https://github.com/llvm/llvm-project/commit/87e8b53009f11033e6e6af0ccb3a97fae615526a
DIFF: https://github.com/llvm/llvm-project/commit/87e8b53009f11033e6e6af0ccb3a97fae615526a.diff

LOG: [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

Added: 
    

Modified: 
    llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
    llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
    llvm/utils/TableGen/Common/CodeGenTarget.cpp
    llvm/utils/TableGen/Common/CodeGenTarget.h
    llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
    llvm/utils/TableGen/DAGISelMatcherGen.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index a77e2472ef2cbc..fd80bc681c70d9 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -1529,7 +1529,7 @@ std::string PatternToMatch::getPredicateCheck() const {
 // SDTypeConstraint implementation
 //
 
-SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) {
+SDTypeConstraint::SDTypeConstraint(const Record *R, const CodeGenHwModes &CGH) {
   OperandNo = R->getValueAsInt("OperandNum");
 
   if (R->isSubClassOf("SDTCisVT")) {
@@ -1799,7 +1799,7 @@ bool TreePatternNode::setDefaultMode(unsigned Mode) {
 //===----------------------------------------------------------------------===//
 // SDNodeInfo implementation
 //
-SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) {
+SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
   EnumName = R->getValueAsString("Opcode");
   SDClassName = R->getValueAsString("SDClass");
   Record *TypeProfile = R->getValueAsDef("TypeProfile");
@@ -2296,7 +2296,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
     assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
     if (NotRegisters)
       return TypeSetByHwMode(); // Unknown.
-    Record *T = CDP.getComplexPattern(R).getValueType();
+    const Record *T = CDP.getComplexPattern(R).getValueType();
     const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
     return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
   }
@@ -2700,7 +2700,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
     if (!NotRegisters) {
       assert(Types.size() == 1 && "ComplexPatterns only produce one result!");
-      Record *T = CDP.getComplexPattern(getOperator()).getValueType();
+      const Record *T = CDP.getComplexPattern(getOperator()).getValueType();
       const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
       const ValueTypeByHwMode VVT = getValueTypeByHwMode(T, CGH);
       // TODO: AArch64 and AMDGPU use ComplexPattern<untyped, ...> and then
@@ -3157,7 +3157,7 @@ void TreePattern::dump() const { print(errs()); }
 // CodeGenDAGPatterns implementation
 //
 
-CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R,
+CodeGenDAGPatterns::CodeGenDAGPatterns(const RecordKeeper &R,
                                        PatternRewriterFn PatternRewriter)
     : Records(R), Target(R), Intrinsics(R),
       LegalVTS(Target.getLegalValueTypes()), PatternRewriter(PatternRewriter) {
@@ -3198,14 +3198,10 @@ Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
 
 // Parse all of the SDNode definitions for the target, populating SDNodes.
 void CodeGenDAGPatterns::ParseNodeInfo() {
-  std::vector<Record *> Nodes = Records.getAllDerivedDefinitions("SDNode");
   const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
 
-  while (!Nodes.empty()) {
-    Record *R = Nodes.back();
+  for (const Record *R : reverse(Records.getAllDerivedDefinitions("SDNode")))
     SDNodes.insert(std::pair(R, SDNodeInfo(R, CGH)));
-    Nodes.pop_back();
-  }
 
   // Get the builtin intrinsic nodes.
   intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
@@ -3216,26 +3212,18 @@ void CodeGenDAGPatterns::ParseNodeInfo() {
 /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
 /// map, and emit them to the file as functions.
 void CodeGenDAGPatterns::ParseNodeTransforms() {
-  std::vector<Record *> Xforms =
-      Records.getAllDerivedDefinitions("SDNodeXForm");
-  while (!Xforms.empty()) {
-    Record *XFormNode = Xforms.back();
-    Record *SDNode = XFormNode->getValueAsDef("Opcode");
+  for (const Record *XFormNode :
+       reverse(Records.getAllDerivedDefinitions("SDNodeXForm"))) {
+    const Record *SDNode = XFormNode->getValueAsDef("Opcode");
     StringRef Code = XFormNode->getValueAsString("XFormFunction");
-    SDNodeXForms.insert(
-        std::pair(XFormNode, NodeXForm(SDNode, std::string(Code))));
-
-    Xforms.pop_back();
+    SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, std::string(Code))});
   }
 }
 
 void CodeGenDAGPatterns::ParseComplexPatterns() {
-  std::vector<Record *> AMs =
-      Records.getAllDerivedDefinitions("ComplexPattern");
-  while (!AMs.empty()) {
-    ComplexPatterns.insert(std::pair(AMs.back(), AMs.back()));
-    AMs.pop_back();
-  }
+  for (const Record *R :
+       reverse(Records.getAllDerivedDefinitions("ComplexPattern")))
+    ComplexPatterns.insert({R, R});
 }
 
 /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
@@ -3244,11 +3232,10 @@ void CodeGenDAGPatterns::ParseComplexPatterns() {
 /// inside a pattern fragment to a pattern fragment.
 ///
 void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
-  std::vector<Record *> Fragments =
-      Records.getAllDerivedDefinitions("PatFrags");
-
   // First step, parse all of the fragments.
-  for (Record *Frag : Fragments) {
+  ArrayRef<const Record *> Fragments =
+      Records.getAllDerivedDefinitions("PatFrags");
+  for (const Record *Frag : Fragments) {
     if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
       continue;
 
@@ -3307,7 +3294,7 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
 
   // Now that we've parsed all of the tree fragments, do a closure on them so
   // that there are not references to PatFrags left inside of them.
-  for (Record *Frag : Fragments) {
+  for (const Record *Frag : Fragments) {
     if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
       continue;
 
@@ -3331,8 +3318,8 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
 }
 
 void CodeGenDAGPatterns::ParseDefaultOperands() {
-  std::vector<Record *> DefaultOps;
-  DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
+  ArrayRef<const Record *> DefaultOps =
+      Records.getAllDerivedDefinitions("OperandWithDefaultOps");
 
   // Find some SDNode.
   assert(!SDNodes.empty() && "No SDNodes parsed?");
@@ -3947,10 +3934,7 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
 /// any fragments involved.  This populates the Instructions list with fully
 /// resolved instructions.
 void CodeGenDAGPatterns::ParseInstructions() {
-  std::vector<Record *> Instrs =
-      Records.getAllDerivedDefinitions("Instruction");
-
-  for (Record *Instr : Instrs) {
+  for (const Record *Instr : Records.getAllDerivedDefinitions("Instruction")) {
     ListInit *LI = nullptr;
 
     if (isa<ListInit>(Instr->getValueInit("Pattern")))
@@ -4346,9 +4330,7 @@ void CodeGenDAGPatterns::ParseOnePattern(
 }
 
 void CodeGenDAGPatterns::ParsePatterns() {
-  std::vector<Record *> Patterns = Records.getAllDerivedDefinitions("Pattern");
-
-  for (Record *CurPattern : Patterns) {
+  for (const Record *CurPattern : Records.getAllDerivedDefinitions("Pattern")) {
     DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
 
     // If the pattern references the null_frag, there's nothing to do.
@@ -4407,10 +4389,10 @@ void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
       return;
     }
 
-    PatternsToMatch.emplace_back(
-        P.getSrcRecord(), P.getPredicates(), std::move(NewSrc),
-        std::move(NewDst), P.getDstRegs(), P.getAddedComplexity(),
-        Record::getNewUID(Records), P.getGISelShouldIgnore(), Check);
+    PatternsToMatch.emplace_back(P.getSrcRecord(), P.getPredicates(),
+                                 std::move(NewSrc), std::move(NewDst),
+                                 P.getDstRegs(), P.getAddedComplexity(),
+                                 getNewUID(), P.getGISelShouldIgnore(), Check);
   };
 
   for (PatternToMatch &P : Copy) {
@@ -4780,7 +4762,7 @@ void CodeGenDAGPatterns::GenerateVariants() {
           PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
           Variant, PatternsToMatch[i].getDstPatternShared(),
           PatternsToMatch[i].getDstRegs(),
-          PatternsToMatch[i].getAddedComplexity(), Record::getNewUID(Records),
+          PatternsToMatch[i].getAddedComplexity(), getNewUID(),
           PatternsToMatch[i].getGISelShouldIgnore(),
           PatternsToMatch[i].getHwModeFeatures());
     }
@@ -4788,3 +4770,8 @@ void CodeGenDAGPatterns::GenerateVariants() {
     LLVM_DEBUG(errs() << "\n");
   }
 }
+
+unsigned CodeGenDAGPatterns::getNewUID() {
+  RecordKeeper &MutableRC = const_cast<RecordKeeper &>(Records);
+  return Record::getNewUID(MutableRC);
+}

diff  --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
index 4dc08e617d1fd6..0aa628798f412e 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
@@ -354,7 +354,7 @@ typedef StringSet<> MultipleUseVarSet;
 /// SDTypeConstraint - This is a discriminated union of constraints,
 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
 struct SDTypeConstraint {
-  SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
+  SDTypeConstraint(const Record *R, const CodeGenHwModes &CGH);
 
   unsigned OperandNo; // The operand # this constraint applies to.
   enum {
@@ -435,7 +435,7 @@ class ScopedName {
 /// the target .td file.  This represents the various dag nodes we will be
 /// processing.
 class SDNodeInfo {
-  Record *Def;
+  const Record *Def;
   StringRef EnumName;
   StringRef SDClassName;
   unsigned Properties;
@@ -445,14 +445,14 @@ class SDNodeInfo {
 
 public:
   // Parse the specified record.
-  SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
+  SDNodeInfo(const Record *R, const CodeGenHwModes &CGH);
 
   unsigned getNumResults() const { return NumResults; }
 
   /// getNumOperands - This is the number of operands required or -1 if
   /// variadic.
   int getNumOperands() const { return NumOperands; }
-  Record *getRecord() const { return Def; }
+  const Record *getRecord() const { return Def; }
   StringRef getEnumName() const { return EnumName; }
   StringRef getSDClassName() const { return SDClassName; }
 
@@ -1095,13 +1095,17 @@ class PatternToMatch {
 };
 
 class CodeGenDAGPatterns {
-  RecordKeeper &Records;
+public:
+  using NodeXForm = std::pair<const Record *, std::string>;
+
+private:
+  const RecordKeeper &Records;
   CodeGenTarget Target;
   CodeGenIntrinsicTable Intrinsics;
 
   std::map<const Record *, SDNodeInfo, LessRecordByID> SDNodes;
-  std::map<const Record *, std::pair<Record *, std::string>, LessRecordByID>
-      SDNodeXForms;
+
+  std::map<const Record *, NodeXForm, LessRecordByID> SDNodeXForms;
   std::map<const Record *, ComplexPattern, LessRecordByID> ComplexPatterns;
   std::map<const Record *, std::unique_ptr<TreePattern>, LessRecordByID>
       PatternFragments;
@@ -1125,7 +1129,7 @@ class CodeGenDAGPatterns {
   unsigned NumScopes = 0;
 
 public:
-  CodeGenDAGPatterns(RecordKeeper &R,
+  CodeGenDAGPatterns(const RecordKeeper &R,
                      PatternRewriterFn PatternRewriter = nullptr);
 
   CodeGenTarget &getTargetInfo() { return Target; }
@@ -1141,7 +1145,6 @@ class CodeGenDAGPatterns {
   }
 
   // Node transformation lookups.
-  typedef std::pair<Record *, std::string> NodeXForm;
   const NodeXForm &getSDNodeTransform(const Record *R) const {
     auto F = SDNodeXForms.find(R);
     assert(F != SDNodeXForms.end() && "Invalid transform!");
@@ -1254,6 +1257,7 @@ class CodeGenDAGPatterns {
       MapVector<std::string, TreePatternNodePtr,
                 std::map<std::string, unsigned>> &InstResults,
       std::vector<Record *> &InstImpResults);
+  unsigned getNewUID();
 };
 
 inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode &N,

diff  --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index 263b4f35f2a0b5..69d2c7006e61da 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -402,11 +402,11 @@ bool CodeGenTarget::guessInstructionProperties() const {
 //===----------------------------------------------------------------------===//
 // ComplexPattern implementation
 //
-ComplexPattern::ComplexPattern(Record *R) {
+ComplexPattern::ComplexPattern(const Record *R) {
   Ty = R->getValueAsDef("Ty");
   NumOperands = R->getValueAsInt("NumOperands");
   SelectFunc = std::string(R->getValueAsString("SelectFunc"));
-  RootNodes = R->getValueAsListOfDefs("RootNodes");
+  RootNodes = R->getValueAsListOfConstDefs("RootNodes");
 
   // FIXME: This is a hack to statically increase the priority of patterns which
   // maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. To get best

diff  --git a/llvm/utils/TableGen/Common/CodeGenTarget.h b/llvm/utils/TableGen/Common/CodeGenTarget.h
index 05978b88377a39..225bdd97128f85 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.h
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.h
@@ -238,20 +238,20 @@ class CodeGenTarget {
 /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
 /// tablegen class in TargetSelectionDAG.td
 class ComplexPattern {
-  Record *Ty;
+  const Record *Ty;
   unsigned NumOperands;
   std::string SelectFunc;
-  std::vector<Record *> RootNodes;
+  std::vector<const Record *> RootNodes;
   unsigned Properties; // Node properties
   unsigned Complexity;
 
 public:
-  ComplexPattern(Record *R);
+  ComplexPattern(const Record *R);
 
-  Record *getValueType() const { return Ty; }
+  const Record *getValueType() const { return Ty; }
   unsigned getNumOperands() const { return NumOperands; }
   const std::string &getSelectFunc() const { return SelectFunc; }
-  const std::vector<Record *> &getRootNodes() const { return RootNodes; }
+  const ArrayRef<const Record *> getRootNodes() const { return RootNodes; }
   bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
   unsigned getComplexity() const { return Complexity; }
 };

diff  --git a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
index 96a40f03d4b63a..06eb1f709c56c4 100644
--- a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
@@ -1214,7 +1214,7 @@ void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
       const CodeGenDAGPatterns::NodeXForm &Entry =
           CGP.getSDNodeTransform(NodeXForms[i]);
 
-      Record *SDNode = Entry.first;
+      const Record *SDNode = Entry.first;
       const std::string &Code = Entry.second;
 
       OS << "  case " << i << ": {  ";

diff  --git a/llvm/utils/TableGen/DAGISelMatcherGen.cpp b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
index bb8f4dcb85a1d8..5cb393ae7a538d 100644
--- a/llvm/utils/TableGen/DAGISelMatcherGen.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
@@ -555,7 +555,7 @@ bool MatcherGen::EmitMatcherCode(unsigned Variant) {
   // check.
   if (const ComplexPattern *CP =
           Pattern.getSrcPattern().getComplexPatternInfo(CGP)) {
-    const std::vector<Record *> &OpNodes = CP->getRootNodes();
+    ArrayRef<const Record *> OpNodes = CP->getRootNodes();
     assert(!OpNodes.empty() &&
            "Complex Pattern must specify what it can match");
     if (Variant >= OpNodes.size())


        


More information about the llvm-commits mailing list