From a042a6502c17fe9a29be191298ca76d2c5394fc8 Mon Sep 17 00:00:00 2001 From: Fabian Mora Date: Mon, 10 Jul 2023 19:55:12 +0000 Subject: [PATCH] [mlir] Add a parameter for passing default values to `StringRefParameter` **For an explanation of these patches see D154153.** Commit message: Currently the `StringRefParameter` parameter doesn't support default values, this patch allows the usage of them, eg: `StringRefParameter<"description", [{"default_value"}]>` Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D154097 --- mlir/include/mlir/IR/AttrTypeBase.td | 3 ++- mlir/test/lib/Dialect/Test/TestTypeDefs.td | 6 ++++++ mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/IR/AttrTypeBase.td b/mlir/include/mlir/IR/AttrTypeBase.td index 996ad71502d3..abefd4047b2d 100644 --- a/mlir/include/mlir/IR/AttrTypeBase.td +++ b/mlir/include/mlir/IR/AttrTypeBase.td @@ -342,11 +342,12 @@ class DefaultValuedParameter : } // 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 index 68588cd23265..15dbd74aec11 100644 --- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td +++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td @@ -350,6 +350,12 @@ def TestTypeCustomString : Test_Type<"TestTypeCustomString"> { 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 index 8e421bd42411..12289b4d7325 100644 --- 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 @@ attributes { // 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 @@ func.func private @test_roundtrip_default_parsers_struct( !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"> )