Skip to content

Commit a042a65

Browse files
committed
[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
1 parent 939f818 commit a042a65

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

mlir/include/mlir/IR/AttrTypeBase.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,12 @@ class DefaultValuedParameter<string type, string value, string desc = ""> :
342342
}
343343

344344
// For StringRefs, which require allocation.
345-
class StringRefParameter<string desc = ""> :
345+
class StringRefParameter<string desc = "", string value = ""> :
346346
AttrOrTypeParameter<"::llvm::StringRef", desc> {
347347
let allocator = [{$_dst = $_allocator.copyInto($_self);}];
348348
let printer = [{$_printer << '"' << $_self << '"';}];
349349
let cppStorageType = "std::string";
350+
let defaultValue = value;
350351
}
351352

352353
// For APFloats, which require comparison.

mlir/test/lib/Dialect/Test/TestTypeDefs.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ def TestTypeCustomString : Test_Type<"TestTypeCustomString"> {
350350
custom<BarString>(ref($foo)) `>` }];
351351
}
352352

353+
def TestTypeOptionalString : Test_Type<"TestTypeOptionalString"> {
354+
let parameters = (ins StringRefParameter<"description", [{"default"}]>:$str);
355+
let mnemonic = "optional_type_string";
356+
let assemblyFormat = [{ (`<` $str^ `>`)? }];
357+
}
358+
353359
def TestTypeElseAnchor : Test_Type<"TestTypeElseAnchor"> {
354360
let parameters = (ins OptionalParameter<"std::optional<int>">:$a);
355361
let mnemonic = "else_anchor";

mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ attributes {
6767
// CHECK: !test.custom_type_spacing<1 2>
6868
// CHECK: !test.custom_type_string<"foo" foo>
6969
// CHECK: !test.custom_type_string<"bar" bar>
70+
// CHECK: !test.optional_type_string
71+
// CHECK: !test.optional_type_string
72+
// CHECK: !test.optional_type_string<"non default">
7073

7174
func.func private @test_roundtrip_default_parsers_struct(
7275
!test.no_parser<255, [1, 2, 3, 4, 5], "foobar", 4>
@@ -105,5 +108,8 @@ func.func private @test_roundtrip_default_parsers_struct(
105108
!test.custom_type<2 9 9 5>,
106109
!test.custom_type_spacing<1 2>,
107110
!test.custom_type_string<"foo" foo>,
108-
!test.custom_type_string<"bar" bar>
111+
!test.custom_type_string<"bar" bar>,
112+
!test.optional_type_string,
113+
!test.optional_type_string<"default">,
114+
!test.optional_type_string<"non default">
109115
)

0 commit comments

Comments
 (0)