Skip to content

Commit 94bb511

Browse files
committed
[MLIR] Add FieldParser implementation for Optional<> integer types.
This patch introduces a templated FieldParser to handle optional signed and unsigned integer types - NFC. Additionally, I've added an extra test to ensure that both signed and unsigned integers are properly tested in the templated integer types for FieldParser as well.
1 parent 86ea67e commit 94bb511

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

mlir/include/mlir/IR/DialectImplementation.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ struct FieldParser<std::string> {
105105
}
106106
};
107107

108+
/// Parse an Optional integer.
109+
template <typename IntT>
110+
struct FieldParser<
111+
llvm::Optional<IntT>,
112+
std::enable_if_t<std::is_integral<IntT>::value, Optional<IntT>>> {
113+
static FailureOr<Optional<IntT>> parse(AsmParser &parser) {
114+
IntT value;
115+
OptionalParseResult result = parser.parseOptionalInteger(value);
116+
if (result.has_value()) {
117+
if (succeeded(*result))
118+
return {Optional<IntT>(value)};
119+
return failure();
120+
}
121+
return {llvm::None};
122+
}
123+
};
124+
108125
/// Parse any container that supports back insertion as a list.
109126
template <typename ContainerT>
110127
struct FieldParser<

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,16 @@ def TestParamFour : ArrayRefParameter<"int", ""> {
176176
let printer = "::printIntArray($_printer, $_self)";
177177
}
178178

179+
def TestParamUnsigned : AttrParameter<"uint64_t", ""> {}
180+
179181
def TestAttrWithFormat : Test_Attr<"TestAttrWithFormat"> {
180182
let parameters = (
181183
ins
182184
TestParamOne:$one,
183185
TestParamTwo:$two,
184186
"::mlir::IntegerAttr":$three,
185187
TestParamFour:$four,
188+
TestParamUnsigned:$five,
186189
// Array of another attribute.
187190
ArrayRefParameter<
188191
"AttrWithTypeBuilderAttr", // The parameter C++ type.
@@ -192,12 +195,24 @@ def TestAttrWithFormat : Test_Attr<"TestAttrWithFormat"> {
192195

193196
let mnemonic = "attr_with_format";
194197
let assemblyFormat = [{
195-
`<` $one `:` struct($two, $four) `:` $three `,`
198+
`<` $one `:` struct($two, $four) `:` $three `:` $five `,`
196199
`[` `` $arrayOfAttrWithTypeBuilderAttr `]` `>`
197200
}];
198201
let genVerifyDecl = 1;
199202
}
200203

204+
def TestAttrWithOptionalSigned : Test_Attr<"TestAttrWithOptionalSigned"> {
205+
let parameters = (ins OptionalParameter<"mlir::Optional<int64_t>">:$value);
206+
let assemblyFormat = "`<` $value `>`";
207+
let mnemonic = "attr_with_optional_signed";
208+
}
209+
210+
def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {
211+
let parameters = (ins OptionalParameter<"mlir::Optional<uint64_t>">:$value);
212+
let assemblyFormat = "`<` $value `>`";
213+
let mnemonic = "attr_with_optional_unsigned";
214+
}
215+
201216
def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
202217
let parameters = (ins "::mlir::Attribute":$attr);
203218

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ TestI64ElementsAttr::verify(function_ref<InFlightDiagnostic()> emitError,
101101
LogicalResult
102102
TestAttrWithFormatAttr::verify(function_ref<InFlightDiagnostic()> emitError,
103103
int64_t one, std::string two, IntegerAttr three,
104-
ArrayRef<int> four,
104+
ArrayRef<int> four, uint64_t five,
105105
ArrayRef<AttrWithTypeBuilderAttr> arrayOfAttrs) {
106106
if (four.size() != static_cast<unsigned>(one))
107107
return emitError() << "expected 'one' to equal 'four.size()'";

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
// CHECK: !test.type_with_format<2147, three = "hi", two = "hi">
66
func.func private @test_roundtrip_parameter_parsers(!test.type_with_format<111, three = #test<attr_ugly begin 5 : index end>, two = "foo">) -> !test.type_with_format<2147, two = "hi", three = "hi">
77
attributes {
8-
// CHECK: #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64, [ 10 : i16]
9-
attr0 = #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64, [10 : i16]>,
10-
// CHECK: #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8, [ 10 : i16]>,
11-
attr1 = #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8, [10 : i16]>,
8+
// CHECK: #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64 : 0, [ 10 : i16]
9+
attr0 = #test.attr_with_format<3 : two = "hello", four = [1, 2, 3] : 42 : i64 : 0, [10 : i16]>,
10+
// CHECK: #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8 : 255, [ 10 : i16]>,
11+
attr1 = #test.attr_with_format<5 : two = "a_string", four = [4, 5, 6, 7, 8] : 8 : i8 : 255, [10 : i16]>,
1212
// CHECK: #test<attr_ugly begin 5 : index end>
1313
attr2 = #test<attr_ugly begin 5 : index end>,
1414
// CHECK: #test.attr_params<42, 24>
@@ -22,7 +22,11 @@ attributes {
2222
// CHECK: #test.custom_anchor<5>
2323
attr7 = #test.custom_anchor<5>,
2424
// CHECK: #test.custom_anchor<5, true>
25-
attr8 = #test.custom_anchor<5, true>
25+
attr8 = #test.custom_anchor<5, true>,
26+
// CHECK: #test.attr_with_optional_signed<-12>
27+
attr9 = #test.attr_with_optional_signed<-12>,
28+
// CHECK: #test.attr_with_optional_unsigned<22>
29+
attr_10 = #test.attr_with_optional_unsigned<22>
2630
}
2731

2832
// CHECK-LABEL: @test_roundtrip_default_parsers_struct

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func.func private @test_type_syntax_error() -> !test.type_with_format<42, two =
123123

124124
func.func private @test_verifier_fails() -> () attributes {
125125
// expected-error@+1 {{expected 'one' to equal 'four.size()'}}
126-
attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64, [10 : i16]>
126+
attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64 : 0, [10 : i16]>
127127
}
128128

129129
// -----

0 commit comments

Comments
 (0)