[llvm] [RISCV] Specify FilterClassField to filter out unneeded pseudos (PR #65460)

Wang Pengcheng via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 19:53:54 PDT 2023


https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/65460:

>From dad30d3bb4906c25b21b0079ef458eb94ade2de5 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Wed, 6 Sep 2023 17:13:40 +0800
Subject: [PATCH 1/3] [TableGen] Add a field to filter out GenericTable entries

A field `FilterClassField` is added to `GenericTable` class, which
is an optional bit field of `FilterClass`. If specified, only those
records with this field being true will have corresponding entries
in the table.
---
 llvm/docs/TableGen/BackEnds.rst               | 20 +++++++++++++------
 llvm/include/llvm/TableGen/SearchableTable.td |  6 ++++++
 .../utils/TableGen/SearchableTableEmitter.cpp | 18 ++++++++++++++++-
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/llvm/docs/TableGen/BackEnds.rst b/llvm/docs/TableGen/BackEnds.rst
index 07dff5a30e2168b..7576a9219a8ea72 100644
--- a/llvm/docs/TableGen/BackEnds.rst
+++ b/llvm/docs/TableGen/BackEnds.rst
@@ -689,6 +689,10 @@ This class provides six fields.
 * ``string FilterClass``. The table will have one entry for each record
   that derives from this class.
 
+* ``string FilterClassField``. This is an optional field of ``FilterClass``
+  which should be `bit` type. If specified, only those records with this field
+  being true will have corresponding entries in the table.
+
 * ``string CppTypeName``. The name of the C++ struct/class type of the
   table that holds the entries. If unspecified, the ``FilterClass`` name is
   used.
@@ -734,22 +738,25 @@ irrelevant.
 
   def ATable : GenericTable {
     let FilterClass = "AEntry";
+    let FilterClassField = "IsNeeded";
     let Fields = ["Str", "Val1", "Val2"];
     let PrimaryKey = ["Val1", "Val2"];
     let PrimaryKeyName = "lookupATableByValues";
   }
 
-  class AEntry<string str, int val1, int val2> {
+  class AEntry<string str, int val1, int val2, bit isNeeded> {
     string Str = str;
     bits<8> Val1 = val1;
     bits<10> Val2 = val2;
+    bit IsNeeded = isNeeded;
   }
 
-  def : AEntry<"Bob",   5, 3>;
-  def : AEntry<"Carol", 2, 6>;
-  def : AEntry<"Ted",   4, 4>;
-  def : AEntry<"Alice", 4, 5>;
-  def : AEntry<"Costa", 2, 1>;
+  def : AEntry<"Bob",   5, 3, 1>;
+  def : AEntry<"Carol", 2, 6, 1>;
+  def : AEntry<"Ted",   4, 4, 1>;
+  def : AEntry<"Alice", 4, 5, 1>;
+  def : AEntry<"Costa", 2, 1, 1>;
+  def : AEntry<"Dale",  2, 1, 0>;
 
 Here is the generated C++ code. The declaration of ``lookupATableByValues``
 is guarded by ``GET_ATable_DECL``, while the definitions are guarded by
@@ -768,6 +775,7 @@ is guarded by ``GET_ATable_DECL``, while the definitions are guarded by
     { "Ted", 0x4, 0x4 }, // 2
     { "Alice", 0x4, 0x5 }, // 3
     { "Bob", 0x5, 0x3 }, // 4
+    /* { "Dale", 0x2, 0x1 }, // 5 */ // No this line as `IsNeeded` is 0.
   };
 
   const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
diff --git a/llvm/include/llvm/TableGen/SearchableTable.td b/llvm/include/llvm/TableGen/SearchableTable.td
index 61dfa5c7070646b..9dddd5e578ff125 100644
--- a/llvm/include/llvm/TableGen/SearchableTable.td
+++ b/llvm/include/llvm/TableGen/SearchableTable.td
@@ -60,6 +60,12 @@ class GenericTable {
   // derives from that class.
   string FilterClass;
 
+  // A field of FilterClass to filter out entries. This is an optional field
+  // of ``FilterClass`` which should be `bit` type. If specified, only those
+  // records with this field being true will have corresponding entries in the
+  // table.
+  string FilterClassField = ?;
+
   // Name of the C++ struct/class type that holds table entries. The
   // declaration of this type is not generated automatically.
   string CppTypeName = FilterClass;
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index b6af02c28a80640..ee1af0ec70df08b 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -720,7 +720,23 @@ void SearchableTableEmitter::run(raw_ostream &OS) {
                       Twine("Table FilterClass '") +
                           FilterClass + "' does not exist");
 
-    collectTableEntries(*Table, Records.getAllDerivedDefinitions(FilterClass));
+    RecordVal *FilterClassFieldVal = TableRec->getValue("FilterClassField");
+    std::vector<Record *> Definitions =
+        Records.getAllDerivedDefinitions(FilterClass);
+    if (auto *FilterClassFieldInit =
+            dyn_cast<StringInit>(FilterClassFieldVal->getValue())) {
+      StringRef FilterClassField = FilterClassFieldInit->getValue();
+      llvm::erase_if(Definitions, [&](const Record *R) {
+        const RecordVal *Filter = R->getValue(FilterClassField);
+        if (auto *BitV = dyn_cast<BitInit>(Filter->getValue()))
+          return !BitV->getValue();
+
+        PrintFatalError(Filter, Twine("FilterClassField '") + FilterClass +
+                                    "' should be a bit value");
+        return true;
+      });
+    }
+    collectTableEntries(*Table, Definitions);
 
     if (!TableRec->isValueUnset("PrimaryKey")) {
       Table->PrimaryKey =

>From 44ec8eabbfcf1a9d8bf01027ddc2ba1185baa6fd Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Wed, 6 Sep 2023 17:16:23 +0800
Subject: [PATCH 2/3] [RISCV] Specify FilterClassField to filter out unneeded
 pseudos

`VMCLR` and `VMSET` will be expanded before MC emitting, so we
don't need them being in RISCVVPseudosTable.
---
 llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
index c3f2f1035bb7471..2348b78e2a8339b 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
@@ -527,11 +527,17 @@ class RISCVVPseudo {
   Instruction BaseInstr = !cast<Instruction>(PseudoToVInst<NAME>.VInst);
   // SEW = 0 is used to denote that the Pseudo is not SEW specific (or unknown).
   bits<8> SEW = 0;
+  bit IsNeeded = !cond(
+    !ne(!find(NAME, "PseudoVMCLR"), -1): 0,
+    !ne(!find(NAME, "PseudoVMSET"), -1): 0,
+    true: 1
+  );
 }
 
 // The actual table.
 def RISCVVPseudosTable : GenericTable {
   let FilterClass = "RISCVVPseudo";
+  let FilterClassField = "IsNeeded";
   let CppTypeName = "PseudoInfo";
   let Fields = [ "Pseudo", "BaseInstr" ];
   let PrimaryKey = [ "Pseudo" ];

>From 9419e139c2b68992ec010e9a766bf1aabe15a653 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Thu, 7 Sep 2023 10:53:17 +0800
Subject: [PATCH 3/3] fixup! [RISCV] Specify FilterClassField to filter out
 unneeded pseudos

---
 llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
index 2348b78e2a8339b..233f06ddb479777 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
@@ -527,17 +527,13 @@ class RISCVVPseudo {
   Instruction BaseInstr = !cast<Instruction>(PseudoToVInst<NAME>.VInst);
   // SEW = 0 is used to denote that the Pseudo is not SEW specific (or unknown).
   bits<8> SEW = 0;
-  bit IsNeeded = !cond(
-    !ne(!find(NAME, "PseudoVMCLR"), -1): 0,
-    !ne(!find(NAME, "PseudoVMSET"), -1): 0,
-    true: 1
-  );
+  bit NeedBeInPseudoTable = 1;
 }
 
 // The actual table.
 def RISCVVPseudosTable : GenericTable {
   let FilterClass = "RISCVVPseudo";
-  let FilterClassField = "IsNeeded";
+  let FilterClassField = "NeedBeInPseudoTable";
   let CppTypeName = "PseudoInfo";
   let Fields = [ "Pseudo", "BaseInstr" ];
   let PrimaryKey = [ "Pseudo" ];
@@ -1004,6 +1000,8 @@ class VPseudoNullaryPseudoM<string BaseInst> :
   // BaseInstr is not used in RISCVExpandPseudoInsts pass.
   // Just fill a corresponding real v-inst to pass tablegen check.
   let BaseInstr = !cast<Instruction>(BaseInst);
+  // We exclude them from RISCVVPseudoTable.
+  let NeedBeInPseudoTable = 0;
 }
 
 class VPseudoUnaryNoMask<DAGOperand RetClass,



More information about the llvm-commits mailing list