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

via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 7 15:11:37 PDT 2024


Author: Rahul Joshi
Date: 2024-09-07T15:11:34-07:00
New Revision: c1e3b990a9c194cd6c379130c3db028bfea8cf9d

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

LOG: [TableGen] Eliminate static CodeGenIntrinsicMap in PatternParser (#107339)

Instead, move it to CodeGenTarget class, and use it in both
PatternParser and SearchableTableEmitter.

Added: 
    

Modified: 
    llvm/lib/Target/AMDGPU/InstCombineTables.td
    llvm/test/TableGen/searchabletables-intrinsic.td
    llvm/utils/TableGen/Common/CodeGenTarget.cpp
    llvm/utils/TableGen/Common/CodeGenTarget.h
    llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
    llvm/utils/TableGen/SearchableTableEmitter.cpp

Removed: 
    


################################################################################
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