diff --git a/mlir/include/mlir/IR/DialectImplementation.h b/mlir/include/mlir/IR/DialectImplementation.h --- a/mlir/include/mlir/IR/DialectImplementation.h +++ b/mlir/include/mlir/IR/DialectImplementation.h @@ -70,6 +70,18 @@ } }; +/// Parse a type. +template +struct FieldParser< + TypeT, std::enable_if_t::value, TypeT>> { + static FailureOr parse(AsmParser &parser) { + TypeT value; + if (parser.parseType(value)) + return failure(); + return value; + } +}; + /// Parse any integer. template struct FieldParser { + let parameters = (ins "::mlir::IntegerType":$int_type, + "::mlir::Type":$any_type); + let mnemonic = "attr_with_type"; + let assemblyFormat = "`<` $int_type `,` $any_type `>`"; +} + #endif // TEST_ATTRDEFS 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 @@ -12,7 +12,9 @@ // CHECK: #test<"attr_ugly begin 5 : index end"> attr2 = #test<"attr_ugly begin 5 : index end">, // CHECK: #test.attr_params<42, 24> - attr3 = #test.attr_params<42, 24> + attr3 = #test.attr_params<42, 24>, + // CHECK: #test.attr_with_type> + attr4 = #test.attr_with_type> } // CHECK-LABEL: @test_roundtrip_default_parsers_struct 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 @@ -125,3 +125,11 @@ // expected-error@+1 {{expected 'one' to equal 'four.size()'}} attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64> } + +// ----- + +func private @test_attr_with_type_failed_to_parse_type() -> () attributes { + // expected-error@+2 {{invalid kind of type specified}} + // expected-error@+1 {{failed to parse TestAttrWithTypeParam parameter 'int_type'}} + attr = #test.attr_with_type, vector<4xi32>> +}