[llvm] [TableGen] Eliminate static CodeGenIntrinsicMap in PatternParser (PR #107339)

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


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/107339

>From 654e2599fbd28d3ba7ab8042b8742d948c0dd4de Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 4 Sep 2024 18:01:16 -0700
Subject: [PATCH] [TableGen] Eliminate static CodeGenIntrinsicMap in
 PatternParser

Instead, create an object at the top-level and pass it to the
PatternParser.
---
 .../Common/GlobalISel/PatternParser.cpp       | 13 ++++--------
 .../Common/GlobalISel/PatternParser.h         | 13 ++++++++++--
 .../TableGen/GlobalISelCombinerEmitter.cpp    | 21 ++++++++++++-------
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
index 73b6097554edad..d622f9b2b52b5f 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
@@ -104,15 +104,6 @@ getInstrForIntrinsic(const CodeGenTarget &CGT, const CodeGenIntrinsic *I) {
   return CGT.getInstruction(RK.getDef(Opc));
 }
 
-static const CodeGenIntrinsic *getCodeGenIntrinsic(Record *R) {
-  // Intrinsics need to have a static lifetime because the match table keeps
-  // references to CodeGenIntrinsic objects.
-  static CodeGenIntrinsicMap *AllIntrinsics;
-  if (!AllIntrinsics)
-    AllIntrinsics = new CodeGenIntrinsicMap(R->getRecords());
-  return &(*AllIntrinsics)[R];
-}
-
 std::unique_ptr<Pattern>
 PatternParser::parseInstructionPattern(const Init &Arg, StringRef Name) {
   const DagInit *DagPat = dyn_cast<DagInit>(&Arg);
@@ -455,5 +446,9 @@ const PatFrag *PatternParser::parsePatFrag(const Record *Def) {
   return Res;
 }
 
+const CodeGenIntrinsic *PatternParser::getCodeGenIntrinsic(const Record *Def) {
+  return &Intrinsics[Def];
+}
+
 } // namespace gi
 } // namespace llvm
diff --git a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.h b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.h
index 2f95d3c1c023fe..81eacdb66c4158 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.h
+++ b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.h
@@ -26,6 +26,8 @@ class Init;
 class Record;
 class StringRef;
 class StringInit;
+class CodeGenIntrinsicMap;
+struct CodeGenIntrinsic;
 
 namespace gi {
 class InstructionPattern;
@@ -38,12 +40,14 @@ class PatFrag;
 class PatternParser {
   const CodeGenTarget &CGT;
   ArrayRef<SMLoc> DiagLoc;
+  CodeGenIntrinsicMap &Intrinsics;
 
   mutable SmallPtrSet<const PatFrag *, 2> SeenPatFrags;
 
 public:
-  PatternParser(const CodeGenTarget &CGT, ArrayRef<SMLoc> DiagLoc)
-      : CGT(CGT), DiagLoc(DiagLoc) {}
+  PatternParser(const CodeGenTarget &CGT, ArrayRef<SMLoc> DiagLoc,
+                CodeGenIntrinsicMap &Intrinsics)
+      : CGT(CGT), DiagLoc(DiagLoc), Intrinsics(Intrinsics) {}
 
   /// Parses a list of patterns such as:
   ///   (Operator (Pattern1 ...), (Pattern2 ...))
@@ -110,6 +114,11 @@ class PatternParser {
   /// \param Def PatFrag def to parsee.
   /// \return the parsed PatFrag on success, nullptr on failure.
   const PatFrag *parsePatFrag(const Record *Def);
+
+  /// Cached `CodeGenIntrinsics` objects.
+  /// \param Def The definition Record for the intrinsic.
+  /// \return Pointer to a `CodeGenIntrinsic` for that intrinsic.
+  const CodeGenIntrinsic *getCodeGenIntrinsic(const Record *Def);
 };
 
 } // namespace gi
diff --git a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
index 0f8f1cce817002..9373c62c302298 100644
--- a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
@@ -637,8 +637,9 @@ class CombineRuleBuilder {
   CombineRuleBuilder(const CodeGenTarget &CGT,
                      SubtargetFeatureInfoMap &SubtargetFeatures,
                      Record &RuleDef, unsigned ID,
-                     std::vector<RuleMatcher> &OutRMs)
-      : Parser(CGT, RuleDef.getLoc()), CGT(CGT),
+                     std::vector<RuleMatcher> &OutRMs,
+                     CodeGenIntrinsicMap &Intrinsics)
+      : Parser(CGT, RuleDef.getLoc(), Intrinsics), CGT(CGT),
         SubtargetFeatures(SubtargetFeatures), RuleDef(RuleDef), RuleID(ID),
         OutRMs(OutRMs) {}
 
@@ -2369,6 +2370,7 @@ class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
   StringRef Name;
   const CodeGenTarget &Target;
   Record *Combiner;
+  CodeGenIntrinsicMap &Intrinsics;
   unsigned NextRuleID = 0;
 
   // List all combine rules (ID, name) imported.
@@ -2412,7 +2414,8 @@ class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
 
 public:
   explicit GICombinerEmitter(RecordKeeper &RK, const CodeGenTarget &Target,
-                             StringRef Name, Record *Combiner);
+                             StringRef Name, Record *Combiner,
+                             CodeGenIntrinsicMap &Intrinsics);
   ~GICombinerEmitter() {}
 
   void run(raw_ostream &OS);
@@ -2635,8 +2638,10 @@ void GICombinerEmitter::emitRunCustomAction(raw_ostream &OS) {
 
 GICombinerEmitter::GICombinerEmitter(RecordKeeper &RK,
                                      const CodeGenTarget &Target,
-                                     StringRef Name, Record *Combiner)
-    : Records(RK), Name(Name), Target(Target), Combiner(Combiner) {}
+                                     StringRef Name, Record *Combiner,
+                                     CodeGenIntrinsicMap &Intrinsics)
+    : Records(RK), Name(Name), Target(Target), Combiner(Combiner),
+      Intrinsics(Intrinsics) {}
 
 MatchTable
 GICombinerEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules) {
@@ -2699,7 +2704,7 @@ void GICombinerEmitter::gatherRules(
 
     AllCombineRules.emplace_back(NextRuleID, Rec->getName().str());
     CombineRuleBuilder CRB(Target, SubtargetFeatures, *Rec, NextRuleID++,
-                           ActiveRules);
+                           ActiveRules, Intrinsics);
 
     if (!CRB.parseAll()) {
       assert(ErrorsPrinted && "Parsing failed without errors!");
@@ -2802,13 +2807,15 @@ static void EmitGICombiner(RecordKeeper &RK, raw_ostream &OS) {
   EnablePrettyStackTrace();
   CodeGenTarget Target(RK);
 
+  CodeGenIntrinsicMap Intrinsics(RK);
+
   if (SelectedCombiners.empty())
     PrintFatalError("No combiners selected with -combiners");
   for (const auto &Combiner : SelectedCombiners) {
     Record *CombinerDef = RK.getDef(Combiner);
     if (!CombinerDef)
       PrintFatalError("Could not find " + Combiner);
-    GICombinerEmitter(RK, Target, Combiner, CombinerDef).run(OS);
+    GICombinerEmitter(RK, Target, Combiner, CombinerDef, Intrinsics).run(OS);
   }
 }
 



More information about the llvm-commits mailing list