diff --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp --- a/mlir/test/lib/Dialect/Test/TestDialect.cpp +++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp @@ -345,6 +345,17 @@ varSuccessors.append(2, varSuccessor); return success(); } +static ParseResult parseCustomDirectiveAttributes(OpAsmParser &parser, + IntegerAttr &attr, + IntegerAttr &optAttr) { + if (parser.parseAttribute(attr)) + return failure(); + if (succeeded(parser.parseOptionalComma())) { + if (parser.parseAttribute(optAttr)) + return failure(); + } + return success(); +} //===----------------------------------------------------------------------===// // Printing @@ -390,6 +401,13 @@ if (!varSuccessors.empty()) printer << ", " << varSuccessors.front(); } +static void printCustomDirectiveAttributes(OpAsmPrinter &printer, + Attribute attribute, + Attribute optAttribute) { + printer << attribute; + if (optAttribute) + printer << ", " << optAttribute; +} //===----------------------------------------------------------------------===// // Test IsolatedRegionOp - parse passthrough region arguments. diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -1530,6 +1530,17 @@ }]; } +def FormatCustomDirectiveAttributes + : TEST_Op<"format_custom_directive_attributes"> { + let arguments = (ins I64Attr:$attr, OptionalAttr:$optAttr); + let assemblyFormat = [{ + custom( + $attr, $optAttr + ) + attr-dict + }]; +} + //===----------------------------------------------------------------------===// // AllTypesMatch type inference diff --git a/mlir/test/mlir-tblgen/op-format.mlir b/mlir/test/mlir-tblgen/op-format.mlir --- a/mlir/test/mlir-tblgen/op-format.mlir +++ b/mlir/test/mlir-tblgen/op-format.mlir @@ -213,6 +213,12 @@ // CHECK: test.format_custom_directive_operands_and_types %[[I64]] -> (%[[I64]]) : i64 -> (i64) test.format_custom_directive_operands_and_types %i64 -> (%i64) : i64 -> (i64) +// CHECK: test.format_custom_directive_attributes 54 : i64 +test.format_custom_directive_attributes 54 : i64 + +// CHECK: test.format_custom_directive_attributes 54 : i64, 46 : i64 +test.format_custom_directive_attributes 54 : i64, 46 : i64 + // CHECK: test.format_custom_directive_regions { // CHECK-NEXT: test.return // CHECK-NEXT: } diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp --- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp +++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp @@ -898,8 +898,8 @@ if (var->attr.isOptional()) body << llvm::formatv(" if ({0}Attr)\n ", var->name); - body << llvm::formatv( - " result.attributes.addAttribute(\"{0}\", {0}Attr);", var->name); + body << llvm::formatv(" result.addAttribute(\"{0}\", {0}Attr);\n", + var->name); } else if (auto *operand = dyn_cast(¶m)) { const NamedTypeConstraint *var = operand->getVar(); if (!var->isOptional())