[llvm] [TableGen] Add a field to filter out GenericTable entries (PR #65458)
Wang Pengcheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 6 02:43:10 PDT 2023
https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/65458:
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.
>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] [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 =
More information about the llvm-commits
mailing list