Skip to content

Commit 7f1d7b2

Browse files
committed
[MLIR][TableGen] Add gen-attrdef-list
This adds a new mlir-tblgen option (-gen-attrdef-list) to generate a list of AttrDefs from a .td file in a format suitable for use in patterns where a macro is defined to expand various repeated code snippets for each item in the list. Specifically, the file will contain a list in this format ATTRDEF(MyAttr) This will be used in ClangIR to create an attribute visitor.
1 parent 3e2afe5 commit 7f1d7b2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
22
// RUN: mlir-tblgen -gen-attrdef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
3+
// RUN: mlir-tblgen -gen-attrdef-list -I %S/../../include %s | FileCheck %s --check-prefix=LIST
34

45
include "mlir/IR/AttrTypeBase.td"
56
include "mlir/IR/OpBase.td"
@@ -19,6 +20,12 @@ include "mlir/IR/OpBase.td"
1920
// DEF: ::test::CompoundAAttr,
2021
// DEF: ::test::SingleParameterAttr
2122

23+
// LIST: ATTRDEF(IndexAttr)
24+
// LIST: ATTRDEF(SimpleAAttr)
25+
// LIST: ATTRDEF(CompoundAAttr)
26+
// LIST: ATTRDEF(SingleParameterAttr)
27+
28+
// LIST: #undef ATTRDEF
2229
// DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
2330
// DEF-SAME: ::mlir::AsmParser &parser,
2431
// DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ class DefGenerator {
690690
public:
691691
bool emitDecls(StringRef selectedDialect);
692692
bool emitDefs(StringRef selectedDialect);
693+
bool emitList(StringRef selectedDialect);
693694

694695
protected:
695696
DefGenerator(ArrayRef<const Record *> defs, raw_ostream &os,
@@ -1025,6 +1026,23 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
10251026
return false;
10261027
}
10271028

1029+
bool DefGenerator::emitList(StringRef selectedDialect) {
1030+
emitSourceFileHeader(("List of " + defType + "Def Definitions").str(), os);
1031+
1032+
SmallVector<AttrOrTypeDef, 16> defs;
1033+
collectAllDefs(selectedDialect, defRecords, defs);
1034+
if (defs.empty())
1035+
return false;
1036+
1037+
auto interleaveFn = [&](const AttrOrTypeDef &def) {
1038+
os << defType.upper() << "DEF(" << def.getCppClassName() << ")";
1039+
};
1040+
llvm::interleave(defs, os, interleaveFn, "\n");
1041+
os << "\n\n";
1042+
os << "#undef " << defType.upper() << "DEF" << "\n";
1043+
return false;
1044+
}
1045+
10281046
//===----------------------------------------------------------------------===//
10291047
// Type Constraints
10301048
//===----------------------------------------------------------------------===//
@@ -1099,7 +1117,12 @@ static mlir::GenRegistration
10991117
AttrDefGenerator generator(records, os);
11001118
return generator.emitDecls(attrDialect);
11011119
});
1102-
1120+
static mlir::GenRegistration
1121+
genAttrList("gen-attrdef-list", "Generate an AttrDef list",
1122+
[](const RecordKeeper &records, raw_ostream &os) {
1123+
AttrDefGenerator generator(records, os);
1124+
return generator.emitList(attrDialect);
1125+
});
11031126
//===----------------------------------------------------------------------===//
11041127
// TypeDef
11051128

0 commit comments

Comments
 (0)