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

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 6 09:19:01 PDT 2024


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

>From 10915387627b3c946767dd6ac9e544d4c2a6fb84 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.
---
 llvm/lib/Target/AMDGPU/InstCombineTables.td   |  3 +--
 .../TableGen/searchabletables-intrinsic.td    |  4 +++-
 llvm/utils/TableGen/Common/CodeGenTarget.cpp  |  2 +-
 llvm/utils/TableGen/Common/CodeGenTarget.h    |  7 ++++++
 .../Common/GlobalISel/PatternParser.cpp       | 11 +---------
 .../utils/TableGen/SearchableTableEmitter.cpp | 22 +++++++++----------
 6 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/InstCombineTables.td b/llvm/lib/Target/AMDGPU/InstCombineTables.td
index 98b2adc442fa3d..3d626414cc5fdb 100644
--- a/llvm/lib/Target/AMDGPU/InstCombineTables.td
+++ b/llvm/lib/Target/AMDGPU/InstCombineTables.td
@@ -1,5 +1,4 @@
-include "llvm/TableGen/SearchableTable.td"
-include "llvm/IR/Intrinsics.td"
+include "AMDGPU.td"
 
 def AMDGPUImageDMaskIntrinsicTable : GenericTable {
   let FilterClass = "AMDGPUImageDMaskIntrinsic";
diff --git a/llvm/test/TableGen/searchabletables-intrinsic.td b/llvm/test/TableGen/searchabletables-intrinsic.td
index d4ec105f0243b1..ab26261293c62f 100644
--- a/llvm/test/TableGen/searchabletables-intrinsic.td
+++ b/llvm/test/TableGen/searchabletables-intrinsic.td
@@ -2,7 +2,9 @@
 // XFAIL: vg_leak
 
 include "llvm/TableGen/SearchableTable.td"
-include "llvm/IR/Intrinsics.td"
+include "llvm/Target/Target.td"
+
+def DummyTarget : Target;
 
 def int_abc : Intrinsic<[llvm_anyint_ty]>;
 def int_xyz : Intrinsic<[llvm_anyint_ty]>;
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index 8b9d97d5ad9c12..6298f4f134d59c 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -86,7 +86,7 @@ std::string llvm::getQualifiedName(const Record *R) {
 /// getTarget - Return the current instance of the Target class.
 ///
 CodeGenTarget::CodeGenTarget(RecordKeeper &records)
-    : Records(records), CGH(records) {
+    : Records(records), CGH(records), Intrinsics(records) {
   std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
   if (Targets.size() == 0)
     PrintFatalError("No 'Target' subclasses defined!");
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.h b/llvm/utils/TableGen/Common/CodeGenTarget.h
index d49bbe8464baf7..02ed73d0a2ca5e 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.h
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.h
@@ -16,6 +16,7 @@
 #ifndef LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H
 #define LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H
 
+#include "Basic/CodeGenIntrinsics.h"
 #include "Basic/SDNodeProperties.h"
 #include "CodeGenHwModes.h"
 #include "CodeGenInstruction.h"
@@ -75,6 +76,8 @@ class CodeGenTarget {
 
   mutable StringRef InstNamespace;
   mutable std::vector<const CodeGenInstruction *> InstrsByEnum;
+  mutable CodeGenIntrinsicMap Intrinsics;
+
   mutable unsigned NumPseudoInstructions = 0;
 
 public:
@@ -225,6 +228,10 @@ class CodeGenTarget {
   /// properties?
   bool guessInstructionProperties() const;
 
+  const CodeGenIntrinsic &getIntrinsic(const Record *Def) const {
+    return Intrinsics[Def];
+  }
+
 private:
   void ComputeInstrsByEnum() const;
 };
diff --git a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
index 73b6097554edad..4761df353e1e98 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);
@@ -127,7 +118,7 @@ PatternParser::parseInstructionPattern(const Init &Arg, StringRef Name) {
   } else if (const DagInit *IP =
                  getDagWithOperatorOfSubClass(Arg, "Intrinsic")) {
     Record *TheDef = IP->getOperatorAsDef(DiagLoc);
-    const CodeGenIntrinsic *Intrin = getCodeGenIntrinsic(TheDef);
+    const CodeGenIntrinsic *Intrin = &CGT.getIntrinsic(TheDef);
     const CodeGenInstruction &Instr = getInstrForIntrinsic(CGT, Intrin);
     Pat =
         std::make_unique<CodeGenInstructionPattern>(Instr, insertStrRef(Name));
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index 8d394f8051ced5..37534fe4ca6afe 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -139,20 +139,15 @@ class SearchableTableEmitter {
                              "'; expected: bit, bits, string, or code");
   }
 
-  bool isIntrinsic(Init *I) {
-    if (DefInit *DI = dyn_cast<DefInit>(I))
+  bool isIntrinsic(const Init *I) {
+    if (const DefInit *DI = dyn_cast<DefInit>(I))
       return DI->getDef()->isSubClassOf("Intrinsic");
     return false;
   }
 
-  CodeGenIntrinsic &getIntrinsic(Init *I) {
+  const CodeGenIntrinsic &getIntrinsic(const Init *I) {
     const Record *Def = cast<DefInit>(I)->getDef();
-    // Build the Intrinsics map on demand. If we instantiate one in the
-    // constructor, we may get errors if the TableGen file being processed does
-    // not include Intrinsics.td and does not do anything with intrinsics.
-    if (!Intrinsics)
-      Intrinsics = std::make_unique<CodeGenIntrinsicMap>(Records);
-    return (*Intrinsics)[Def];
+    return Target->getIntrinsic(Def);
   }
 
   bool compareBy(Record *LHS, Record *RHS, const SearchIndex &Index);
@@ -249,8 +244,8 @@ bool SearchableTableEmitter::compareBy(Record *LHS, Record *RHS,
       if (LHSi > RHSi)
         return false;
     } else if (Field.IsIntrinsic) {
-      CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
-      CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
+      const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
+      const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
       if (std::tie(LHSi.TargetPrefix, LHSi.Name) <
           std::tie(RHSi.TargetPrefix, RHSi.Name))
         return true;
@@ -716,7 +711,10 @@ void SearchableTableEmitter::run(raw_ostream &OS) {
   // Emit tables in a deterministic order to avoid needless rebuilds.
   SmallVector<std::unique_ptr<GenericTable>, 4> Tables;
   DenseMap<Record *, GenericTable *> TableMap;
-  if (!Records.getAllDerivedDefinitionsIfDefined("Instruction").empty())
+  bool NeedsTarget =
+      !Records.getAllDerivedDefinitionsIfDefined("Instruction").empty() ||
+      !Records.getAllDerivedDefinitionsIfDefined("Intrinsic").empty();
+  if (NeedsTarget)
     Target = std::make_unique<CodeGenTarget>(Records);
 
   // Collect all definitions first.



More information about the llvm-commits mailing list