diff --git a/mlir/include/mlir/IR/AttrTypeBase.td b/mlir/include/mlir/IR/AttrTypeBase.td --- a/mlir/include/mlir/IR/AttrTypeBase.td +++ b/mlir/include/mlir/IR/AttrTypeBase.td @@ -342,11 +342,12 @@ } // For StringRefs, which require allocation. -class StringRefParameter : +class StringRefParameter : AttrOrTypeParameter<"::llvm::StringRef", desc> { let allocator = [{$_dst = $_allocator.copyInto($_self);}]; let printer = [{$_printer << '"' << $_self << '"';}]; let cppStorageType = "std::string"; + let defaultValue = value; } // For APFloats, which require comparison. diff --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td --- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td +++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td @@ -350,6 +350,12 @@ custom(ref($foo)) `>` }]; } +def TestTypeOptionalString : Test_Type<"TestTypeOptionalString"> { + let parameters = (ins StringRefParameter<"description", [{"default"}]>:$str); + let mnemonic = "optional_type_string"; + let assemblyFormat = [{ (`<` $str^ `>`)? }]; +} + def TestTypeElseAnchor : Test_Type<"TestTypeElseAnchor"> { let parameters = (ins OptionalParameter<"std::optional">:$a); let mnemonic = "else_anchor"; 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 @@ -67,6 +67,9 @@ // CHECK: !test.custom_type_spacing<1 2> // CHECK: !test.custom_type_string<"foo" foo> // CHECK: !test.custom_type_string<"bar" bar> +// CHECK: !test.optional_type_string +// CHECK: !test.optional_type_string +// CHECK: !test.optional_type_string<"non default"> func.func private @test_roundtrip_default_parsers_struct( !test.no_parser<255, [1, 2, 3, 4, 5], "foobar", 4> @@ -105,5 +108,8 @@ !test.custom_type<2 9 9 5>, !test.custom_type_spacing<1 2>, !test.custom_type_string<"foo" foo>, - !test.custom_type_string<"bar" bar> + !test.custom_type_string<"bar" bar>, + !test.optional_type_string, + !test.optional_type_string<"default">, + !test.optional_type_string<"non default"> )