Skip to content

Commit 44b3cf4

Browse files
authored
add prop-dict support for custom directive for mlir-tblgen (#77061)
According to https://mlir.llvm.org/docs/DefiningDialects/Operations/#custom-directives, custom directive supports attr-dict > attr-dict Directive: NamedAttrList & But it doesn't support prop-dict which is introduced into MLIR recently. It's useful to have tblgen support prop-dict like attr-dict. This PR enable tblgen to support prop-dict ```bash error: only variables and types may be used as parameters to a custom directive ... custom<Print>(prop-dict) ``` Co-authored-by: Fung Xie <[email protected]>
1 parent 567941b commit 44b3cf4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

mlir/test/mlir-tblgen/op-format-spec.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def DirectiveCustomValidC : TestFormat_Op<[{
4444
def DirectiveCustomValidD : TestFormat_Op<[{
4545
(`(` custom<MyDirective>($operand)^ `)`)? attr-dict
4646
}]>, Arguments<(ins Optional<I64>:$operand)>;
47+
def DirectiveCustomValidE : TestFormat_Op<[{
48+
custom<MyDirective>(prop-dict) attr-dict
49+
}]>, Arguments<(ins UnitAttr:$flag)>;
4750

4851
//===----------------------------------------------------------------------===//
4952
// functional-type

mlir/test/mlir-tblgen/op-format.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def CustomStringLiteralC : TestFormat_Op<[{
4242
custom<Foo>("$_builder.getStringAttr(\"foo\")") attr-dict
4343
}]>;
4444

45+
// CHECK-LABEL: CustomStringLiteralD::parse
46+
// CHECK: parseFoo({{.*}}, result)
47+
// CHECK-LABEL: CustomStringLiteralD::print
48+
// CHECK: printFoo({{.*}}, getProperties())
49+
def CustomStringLiteralD : TestFormat_Op<[{
50+
custom<Foo>(prop-dict) attr-dict
51+
}]>;
52+
4553
//===----------------------------------------------------------------------===//
4654
// Optional Groups
4755
//===----------------------------------------------------------------------===//

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ static void genCustomParameterParser(FormatElement *param, MethodBody &body) {
895895
body << attr->getVar()->name << "Attr";
896896
} else if (isa<AttrDictDirective>(param)) {
897897
body << "result.attributes";
898+
} else if (isa<PropDictDirective>(param)) {
899+
body << "result";
898900
} else if (auto *operand = dyn_cast<OperandVariable>(param)) {
899901
StringRef name = operand->getVar()->name;
900902
ArgumentLengthKind lengthKind = getArgumentLengthKind(operand->getVar());
@@ -1854,6 +1856,9 @@ static void genCustomDirectiveParameterPrinter(FormatElement *element,
18541856
} else if (isa<AttrDictDirective>(element)) {
18551857
body << "getOperation()->getAttrDictionary()";
18561858

1859+
} else if (isa<PropDictDirective>(element)) {
1860+
body << "getProperties()";
1861+
18571862
} else if (auto *operand = dyn_cast<OperandVariable>(element)) {
18581863
body << op.getGetterName(operand->getVar()->name) << "()";
18591864

@@ -3138,9 +3143,9 @@ OpFormatParser::parsePropDictDirective(SMLoc loc, Context context) {
31383143
LogicalResult OpFormatParser::verifyCustomDirectiveArguments(
31393144
SMLoc loc, ArrayRef<FormatElement *> arguments) {
31403145
for (FormatElement *argument : arguments) {
3141-
if (!isa<AttrDictDirective, AttributeVariable, OperandVariable,
3142-
PropertyVariable, RefDirective, RegionVariable, SuccessorVariable,
3143-
StringElement, TypeDirective>(argument)) {
3146+
if (!isa<AttrDictDirective, PropDictDirective, AttributeVariable,
3147+
OperandVariable, PropertyVariable, RefDirective, RegionVariable,
3148+
SuccessorVariable, StringElement, TypeDirective>(argument)) {
31443149
// TODO: FormatElement should have location info attached.
31453150
return emitError(loc, "only variables and types may be used as "
31463151
"parameters to a custom directive");

0 commit comments

Comments
 (0)