[llvm] [LLVM][TableGen] Change GISelCombinerEmitter to use const RecordKeeper (PR #109187)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 13:06:12 PDT 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/109187

Change GISelCombinerEmitter to use const RecordKeeper.

>From 92ce66423f0494d46e4483897e485c03d306c34b Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 18 Sep 2024 13:04:02 -0700
Subject: [PATCH] [LLVM][TableGen] Change GISelCombinerEmitter to use const
 RecordKeeper

---
 .../GlobalISel/GlobalISelMatchTable.cpp       | 12 +---
 .../Common/GlobalISel/GlobalISelMatchTable.h  | 12 ++--
 .../GlobalISelMatchTableExecutorEmitter.cpp   |  6 +-
 .../TableGen/GlobalISelCombinerEmitter.cpp    | 60 +++++++++----------
 4 files changed, 41 insertions(+), 49 deletions(-)

diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
index 0779b1e3502d2b..2702e0ae33c775 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
@@ -80,10 +80,10 @@ void emitEncodingMacrosUndef(raw_ostream &OS) {
      << "#undef " << EncodeMacroName << "8\n";
 }
 
-std::string getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset,
+std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
                                     int HwModeIdx) {
   std::string Name = "GIFBS";
-  for (const auto &Feature : FeatureBitset)
+  for (const Record *Feature : FeatureBitset)
     Name += ("_" + Feature->getName()).str();
   if (HwModeIdx >= 0)
     Name += ("_HwMode" + std::to_string(HwModeIdx));
@@ -861,14 +861,6 @@ const std::vector<std::string> &RuleMatcher::getRequiredSimplePredicates() {
   return RequiredSimplePredicates;
 }
 
-void RuleMatcher::addRequiredFeature(Record *Feature) {
-  RequiredFeatures.push_back(Feature);
-}
-
-const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const {
-  return RequiredFeatures;
-}
-
 unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) {
   unsigned NewInsnVarID = NextInsnVarID++;
   InsnVariableIDs[&Matcher] = NewInsnVarID;
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h
index 94f26d85488af6..a4addff7592cf1 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h
@@ -62,7 +62,7 @@ using GISelFlags = std::uint16_t;
 void emitEncodingMacrosDef(raw_ostream &OS);
 void emitEncodingMacrosUndef(raw_ostream &OS);
 
-std::string getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset,
+std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
                                     int HwModeIdx);
 
 /// Takes a sequence of \p Rules and group them based on the predicates
@@ -516,7 +516,7 @@ class RuleMatcher : public Matcher {
   GISelFlags Flags = 0;
 
   std::vector<std::string> RequiredSimplePredicates;
-  std::vector<Record *> RequiredFeatures;
+  std::vector<const Record *> RequiredFeatures;
   std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers;
 
   DenseSet<unsigned> ErasedInsnIDs;
@@ -553,8 +553,12 @@ class RuleMatcher : public Matcher {
   uint64_t getRuleID() const { return RuleID; }
 
   InstructionMatcher &addInstructionMatcher(StringRef SymbolicName);
-  void addRequiredFeature(Record *Feature);
-  const std::vector<Record *> &getRequiredFeatures() const;
+  void addRequiredFeature(const Record *Feature) {
+    RequiredFeatures.push_back(Feature);
+  }
+  ArrayRef<const Record *> getRequiredFeatures() const {
+    return RequiredFeatures;
+  }
 
   void addHwModeIdx(unsigned Idx) { HwModeIdx = Idx; }
   int getHwModeIdx() const { return HwModeIdx; }
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTableExecutorEmitter.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTableExecutorEmitter.cpp
index 6edbfa350e350c..8790dc6028ef49 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTableExecutorEmitter.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTableExecutorEmitter.cpp
@@ -49,14 +49,14 @@ void GlobalISelMatchTableExecutorEmitter::emitSubtargetFeatureBitsetImpl(
 
   // Emit a table containing the PredicateBitsets objects needed by the matcher
   // and an enum for the matcher to reference them with.
-  std::vector<std::pair<std::vector<Record *>, int>> FeatureBitsets;
+  std::vector<std::pair<std::vector<const Record *>, int>> FeatureBitsets;
   FeatureBitsets.reserve(Rules.size());
   for (auto &Rule : Rules)
     FeatureBitsets.emplace_back(Rule.getRequiredFeatures(),
                                 Rule.getHwModeIdx());
   llvm::sort(FeatureBitsets,
-             [&](const std::pair<std::vector<Record *>, int> &A,
-                 const std::pair<std::vector<Record *>, int> &B) {
+             [&](const std::pair<std::vector<const Record *>, int> &A,
+                 const std::pair<std::vector<const Record *>, int> &B) {
                if (A.first.size() < B.first.size())
                  return true;
                if (A.first.size() > B.first.size())
diff --git a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
index b97b87e2117507..cca805ea4ae0c5 100644
--- a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
@@ -461,9 +461,10 @@ std::vector<std::string> CombineRuleOperandTypeChecker::getMCOIOperandTypes(
 
   std::vector<std::string> OpTypes;
   auto &CGI = CGP.getInst();
-  Record *VarArgsTy = CGI.TheDef->isSubClassOf("GenericInstruction")
-                          ? CGI.TheDef->getValueAsOptionalDef("variadicOpsType")
-                          : nullptr;
+  const Record *VarArgsTy =
+      CGI.TheDef->isSubClassOf("GenericInstruction")
+          ? CGI.TheDef->getValueAsOptionalDef("variadicOpsType")
+          : nullptr;
   std::string VarArgsTyName =
       VarArgsTy ? ("MCOI::" + VarArgsTy->getValueAsString("OperandType")).str()
                 : ("unknown_type_" + Twine(UnknownTypeIdx++)).str();
@@ -636,7 +637,7 @@ class CombineRuleBuilder {
 
   CombineRuleBuilder(const CodeGenTarget &CGT,
                      SubtargetFeatureInfoMap &SubtargetFeatures,
-                     Record &RuleDef, unsigned ID,
+                     const Record &RuleDef, unsigned ID,
                      std::vector<RuleMatcher> &OutRMs)
       : Parser(CGT, RuleDef.getLoc()), CGT(CGT),
         SubtargetFeatures(SubtargetFeatures), RuleDef(RuleDef), RuleID(ID),
@@ -765,7 +766,7 @@ class CombineRuleBuilder {
   PatternParser Parser;
   const CodeGenTarget &CGT;
   SubtargetFeatureInfoMap &SubtargetFeatures;
-  Record &RuleDef;
+  const Record &RuleDef;
   const unsigned RuleID;
   std::vector<RuleMatcher> &OutRMs;
 
@@ -1374,12 +1375,12 @@ bool CombineRuleBuilder::addFeaturePredicates(RuleMatcher &M) {
     return true;
 
   ListInit *Preds = RuleDef.getValueAsListInit("Predicates");
-  for (Init *PI : Preds->getValues()) {
-    DefInit *Pred = dyn_cast<DefInit>(PI);
+  for (const Init *PI : Preds->getValues()) {
+    const DefInit *Pred = dyn_cast<DefInit>(PI);
     if (!Pred)
       continue;
 
-    Record *Def = Pred->getDef();
+    const Record *Def = Pred->getDef();
     if (!Def->isSubClassOf("Predicate")) {
       ::PrintError(Def, "Unknown 'Predicate' Type");
       return false;
@@ -1525,7 +1526,7 @@ bool CombineRuleBuilder::parseDefs(const DagInit &Def) {
     // Subclasses of GIDefMatchData should declare that this rule needs to pass
     // data from the match stage to the apply stage, and ensure that the
     // generated matcher has a suitable variable for it to do so.
-    if (Record *MatchDataRec =
+    if (const Record *MatchDataRec =
             getDefOfSubClass(*Def.getArg(I), MatchDataClassName)) {
       MatchDatas.emplace_back(Def.getArgNameStr(I),
                               MatchDataRec->getValueAsString("Type"));
@@ -2360,10 +2361,10 @@ bool CombineRuleBuilder::emitCodeGenInstructionMatchPattern(
 /// static storage pools and wires them together to emit the match table &
 /// associated function/data structures.
 class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
   StringRef Name;
   const CodeGenTarget &Target;
-  Record *Combiner;
+  const Record *Combiner;
   unsigned NextRuleID = 0;
 
   // List all combine rules (ID, name) imported.
@@ -2403,11 +2404,12 @@ class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
   }
 
   void gatherRules(std::vector<RuleMatcher> &Rules,
-                   const std::vector<Record *> &&RulesAndGroups);
+                   ArrayRef<const Record *> RulesAndGroups);
 
 public:
-  explicit GICombinerEmitter(RecordKeeper &RK, const CodeGenTarget &Target,
-                             StringRef Name, Record *Combiner);
+  explicit GICombinerEmitter(const RecordKeeper &RK,
+                             const CodeGenTarget &Target, StringRef Name,
+                             const Record *Combiner);
   ~GICombinerEmitter() {}
 
   void run(raw_ostream &OS);
@@ -2628,9 +2630,9 @@ void GICombinerEmitter::emitRunCustomAction(raw_ostream &OS) {
      << "}\n";
 }
 
-GICombinerEmitter::GICombinerEmitter(RecordKeeper &RK,
+GICombinerEmitter::GICombinerEmitter(const RecordKeeper &RK,
                                      const CodeGenTarget &Target,
-                                     StringRef Name, Record *Combiner)
+                                     StringRef Name, const Record *Combiner)
     : Records(RK), Name(Name), Target(Target), Combiner(Combiner) {}
 
 MatchTable
@@ -2675,12 +2677,11 @@ GICombinerEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules) {
 }
 
 /// Recurse into GICombineGroup's and flatten the ruleset into a simple list.
-void GICombinerEmitter::gatherRules(
-    std::vector<RuleMatcher> &ActiveRules,
-    const std::vector<Record *> &&RulesAndGroups) {
-  for (Record *Rec : RulesAndGroups) {
+void GICombinerEmitter::gatherRules(std::vector<RuleMatcher> &ActiveRules,
+                                    ArrayRef<const Record *> RulesAndGroups) {
+  for (const Record *Rec : RulesAndGroups) {
     if (!Rec->isValueUnset("Rules")) {
-      gatherRules(ActiveRules, Rec->getValueAsListOfDefs("Rules"));
+      gatherRules(ActiveRules, Rec->getValueAsListOfConstDefs("Rules"));
       continue;
     }
 
@@ -2719,7 +2720,7 @@ void GICombinerEmitter::run(raw_ostream &OS) {
 
   Records.startTimer("Gather rules");
   std::vector<RuleMatcher> Rules;
-  gatherRules(Rules, Combiner->getValueAsListOfDefs("Rules"));
+  gatherRules(Rules, Combiner->getValueAsListOfConstDefs("Rules"));
   if (ErrorsPrinted)
     PrintFatalError(Combiner->getLoc(), "Failed to parse one or more rules");
 
@@ -2747,11 +2748,6 @@ void GICombinerEmitter::run(raw_ostream &OS) {
 
   emitSourceFileHeader(getClassName().str() + " Combiner Match Table", OS);
 
-  // Unused
-  std::vector<StringRef> CustomRendererFns;
-  // Unused
-  std::vector<Record *> ComplexPredicates;
-
   SmallVector<LLTCodeGen, 16> TypeObjects;
   append_range(TypeObjects, KnownTypes);
   llvm::sort(TypeObjects);
@@ -2780,8 +2776,8 @@ void GICombinerEmitter::run(raw_ostream &OS) {
   emitTemporariesDecl(OS, "GET_GICOMBINER_CLASS_MEMBERS");
 
   // GET_GICOMBINER_IMPL, which needs to be included outside the class.
-  emitExecutorImpl(OS, Table, TypeObjects, Rules, ComplexPredicates,
-                   CustomRendererFns, "GET_GICOMBINER_IMPL");
+  emitExecutorImpl(OS, Table, TypeObjects, Rules, {}, {},
+                   "GET_GICOMBINER_IMPL");
 
   // GET_GICOMBINER_CONSTRUCTOR_INITS, which are in the constructor's
   // initializer list.
@@ -2793,14 +2789,14 @@ void GICombinerEmitter::run(raw_ostream &OS) {
 
 //===----------------------------------------------------------------------===//
 
-static void EmitGICombiner(RecordKeeper &RK, raw_ostream &OS) {
+static void EmitGICombiner(const RecordKeeper &RK, raw_ostream &OS) {
   EnablePrettyStackTrace();
-  CodeGenTarget Target(RK);
+  const CodeGenTarget Target(RK);
 
   if (SelectedCombiners.empty())
     PrintFatalError("No combiners selected with -combiners");
   for (const auto &Combiner : SelectedCombiners) {
-    Record *CombinerDef = RK.getDef(Combiner);
+    const Record *CombinerDef = RK.getDef(Combiner);
     if (!CombinerDef)
       PrintFatalError("Could not find " + Combiner);
     GICombinerEmitter(RK, Target, Combiner, CombinerDef).run(OS);



More information about the llvm-commits mailing list