diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -70,6 +70,20 @@ attrOrType.print(*this); } + /// Print the provided array of attributes or types in the context of an + /// operation custom printer/parser: this will invoke directly the print + /// method on the attribute class and skip the `#dialect.mnemonic` prefix in + /// most cases. + template ::value> + *sfinae = nullptr> + void printStrippedAttrOrType(ArrayRef attrOrTypes) { + llvm::interleaveComma(attrOrTypes, getStream(), + [this](AttrOrType attrOrType) { + printStrippedAttrOrType(attrOrType); + }); + } + /// SFINAE for printing the provided attribute in the context of an operation /// custom printer in the case where the attribute does not define a print /// method. diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td --- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td +++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td @@ -175,11 +175,19 @@ TestParamOne:$one, TestParamTwo:$two, "::mlir::IntegerAttr":$three, - TestParamFour:$four + TestParamFour:$four, + // Array of another attribute. + ArrayRefParameter< + "AttrWithTypeBuilderAttr", // The parameter C++ type. + "An example of an array of another Attribute" // Parameter description. + >: $arrayOfAttrWithTypeBuilderAttr ); let mnemonic = "attr_with_format"; - let assemblyFormat = "`<` $one `:` struct($two, $four) `:` $three `>`"; + let assemblyFormat = [{ + `<` $one `:` struct($two, $four) `:` $three `,` + `[` `` $arrayOfAttrWithTypeBuilderAttr `]` `>` + }]; let genVerifyDecl = 1; } diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp --- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp +++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp @@ -114,10 +114,10 @@ return success(); } -LogicalResult -TestAttrWithFormatAttr::verify(function_ref emitError, - int64_t one, std::string two, IntegerAttr three, - ArrayRef four) { +LogicalResult TestAttrWithFormatAttr::verify( + function_ref emitError, int64_t one, std::string two, + IntegerAttr three, ArrayRef four, + ArrayRef arrayOfAttrWithTypeBuilderAttr) { if (four.size() != static_cast(one)) return emitError() << "expected 'one' to equal 'four.size()'"; return success(); diff --git a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir --- a/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir +++ b/mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir @@ -5,10 +5,10 @@ // CHECK: !test.type_with_format<2147, three = "hi", two = "hi"> func.func private @test_roundtrip_parameter_parsers(!test.type_with_format<111, three = #test<"attr_ugly begin 5 : index end">, two = "foo">) -> !test.type_with_format<2147, two = "hi", three = "hi"> attributes { - // CHECK: #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64> - attr0 = #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64>, - // CHECK: #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8> - attr1 = #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8>, + // CHECK: #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64, [ 10 : i16] + attr0 = #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64, [10 : i16]>, + // CHECK: #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8, [ 10 : i16]>, + attr1 = #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8, [10 : i16]>, // CHECK: #test<"attr_ugly begin 5 : index end"> attr2 = #test<"attr_ugly begin 5 : index end">, // CHECK: #test.attr_params<42, 24> diff --git a/mlir/test/mlir-tblgen/attr-or-type-format.mlir b/mlir/test/mlir-tblgen/attr-or-type-format.mlir --- a/mlir/test/mlir-tblgen/attr-or-type-format.mlir +++ b/mlir/test/mlir-tblgen/attr-or-type-format.mlir @@ -123,7 +123,7 @@ func.func private @test_verifier_fails() -> () attributes { // expected-error@+1 {{expected 'one' to equal 'four.size()'}} - attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64> + attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64, [10 : i16]> } // -----