Skip to content

Commit c7c59dc

Browse files
committed
[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.
1 parent fab2594 commit c7c59dc

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

llvm/docs/TableGen/BackEnds.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ This class provides six fields.
689689
* ``string FilterClass``. The table will have one entry for each record
690690
that derives from this class.
691691

692+
* ``string FilterClassField``. This is an optional field of ``FilterClass``
693+
which should be `bit` type. If specified, only those records with this field
694+
being true will have corresponding entries in the table.
695+
692696
* ``string CppTypeName``. The name of the C++ struct/class type of the
693697
table that holds the entries. If unspecified, the ``FilterClass`` name is
694698
used.
@@ -734,22 +738,25 @@ irrelevant.
734738
735739
def ATable : GenericTable {
736740
let FilterClass = "AEntry";
741+
let FilterClassField = "IsNeeded";
737742
let Fields = ["Str", "Val1", "Val2"];
738743
let PrimaryKey = ["Val1", "Val2"];
739744
let PrimaryKeyName = "lookupATableByValues";
740745
}
741746
742-
class AEntry<string str, int val1, int val2> {
747+
class AEntry<string str, int val1, int val2, bit isNeeded> {
743748
string Str = str;
744749
bits<8> Val1 = val1;
745750
bits<10> Val2 = val2;
751+
bit IsNeeded = isNeeded;
746752
}
747753
748-
def : AEntry<"Bob", 5, 3>;
749-
def : AEntry<"Carol", 2, 6>;
750-
def : AEntry<"Ted", 4, 4>;
751-
def : AEntry<"Alice", 4, 5>;
752-
def : AEntry<"Costa", 2, 1>;
754+
def : AEntry<"Bob", 5, 3, 1>;
755+
def : AEntry<"Carol", 2, 6, 1>;
756+
def : AEntry<"Ted", 4, 4, 1>;
757+
def : AEntry<"Alice", 4, 5, 1>;
758+
def : AEntry<"Costa", 2, 1, 1>;
759+
def : AEntry<"Dale", 2, 1, 0>;
753760
754761
Here is the generated C++ code. The declaration of ``lookupATableByValues``
755762
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
768775
{ "Ted", 0x4, 0x4 }, // 2
769776
{ "Alice", 0x4, 0x5 }, // 3
770777
{ "Bob", 0x5, 0x3 }, // 4
778+
/* { "Dale", 0x2, 0x1 }, // 5 */ // No this line as `IsNeeded` is 0.
771779
};
772780
773781
const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {

llvm/include/llvm/TableGen/SearchableTable.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class GenericTable {
6060
// derives from that class.
6161
string FilterClass;
6262

63+
// A field of FilterClass to filter out entries. This is an optional field
64+
// of ``FilterClass`` which should be `bit` type. If specified, only those
65+
// records with this field being true will have corresponding entries in the
66+
// table.
67+
string FilterClassField = ?;
68+
6369
// Name of the C++ struct/class type that holds table entries. The
6470
// declaration of this type is not generated automatically.
6571
string CppTypeName = FilterClass;

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,23 @@ void SearchableTableEmitter::run(raw_ostream &OS) {
720720
Twine("Table FilterClass '") +
721721
FilterClass + "' does not exist");
722722

723-
collectTableEntries(*Table, Records.getAllDerivedDefinitions(FilterClass));
723+
RecordVal *FilterClassFieldVal = TableRec->getValue("FilterClassField");
724+
std::vector<Record *> Definitions =
725+
Records.getAllDerivedDefinitions(FilterClass);
726+
if (auto *FilterClassFieldInit =
727+
dyn_cast<StringInit>(FilterClassFieldVal->getValue())) {
728+
StringRef FilterClassField = FilterClassFieldInit->getValue();
729+
llvm::erase_if(Definitions, [&](const Record *R) {
730+
const RecordVal *Filter = R->getValue(FilterClassField);
731+
if (auto *BitV = dyn_cast<BitInit>(Filter->getValue()))
732+
return !BitV->getValue();
733+
734+
PrintFatalError(Filter, Twine("FilterClassField '") + FilterClass +
735+
"' should be a bit value");
736+
return true;
737+
});
738+
}
739+
collectTableEntries(*Table, Definitions);
724740

725741
if (!TableRec->isValueUnset("PrimaryKey")) {
726742
Table->PrimaryKey =

0 commit comments

Comments
 (0)