[Mlir-commits] [mlir] a042a65 - [mlir] Add a parameter for passing default values to `StringRefParameter`

Fabian Mora llvmlistbot at llvm.org
Mon Jul 10 13:02:03 PDT 2023


Author: Fabian Mora
Date: 2023-07-10T20:01:56Z
New Revision: a042a6502c17fe9a29be191298ca76d2c5394fc8

URL: https://github.com/llvm/llvm-project/commit/a042a6502c17fe9a29be191298ca76d2c5394fc8
DIFF: https://github.com/llvm/llvm-project/commit/a042a6502c17fe9a29be191298ca76d2c5394fc8.diff

LOG: [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

Added: 
    

Modified: 
    mlir/include/mlir/IR/AttrTypeBase.td
    mlir/test/lib/Dialect/Test/TestTypeDefs.td
    mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/AttrTypeBase.td b/mlir/include/mlir/IR/AttrTypeBase.td
index 996ad71502d3f9..abefd4047b2ddd 100644
--- a/mlir/include/mlir/IR/AttrTypeBase.td
+++ b/mlir/include/mlir/IR/AttrTypeBase.td
@@ -342,11 +342,12 @@ class DefaultValuedParameter<string type, string value, string desc = ""> :
 }
 
 // For StringRefs, which require allocation.
-class StringRefParameter<string desc = ""> :
+class StringRefParameter<string desc = "", string value = ""> :
     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 68588cd2326582..15dbd74aec118f 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<BarString>(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<int>">:$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 8e421bd4241189..12289b4d732593 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">
 )


        


More information about the Mlir-commits mailing list