Skip to content

Commit b3d392a

Browse files
authored
[mlir][tblgen] Fix bug around parsing optional prop-dict keys (#120045)
The printer for prop-dict would elide properties that had their default value, such as optional properties that were not present. The parser would similarly not raise an error if such a key was missing. However, after not raising an error, the parser would attempt to convert the null attribute to a property anyway, causing failures. This commit fixes the issue and adds tests.
1 parent 6d1a513 commit b3d392a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ def FormatOptionalWithElse : TEST_Op<"format_optional_else"> {
431431
let assemblyFormat = "(`then` $isFirstBranchPresent^):(`else`)? attr-dict";
432432
}
433433

434+
def FormatOptionalPropDict : TEST_Op<"format_optional_prop_dict"> {
435+
let arguments = (ins
436+
OptionalProperty<StringProperty>:$a,
437+
DefaultValuedProperty<I32Property, "1">:$b);
438+
let assemblyFormat = "prop-dict attr-dict";
439+
}
440+
434441
def FormatCompoundAttr : TEST_Op<"format_compound_attr"> {
435442
let arguments = (ins CompoundAttrA:$compound);
436443
let assemblyFormat = "$compound attr-dict-with-keyword";

mlir/test/mlir-tblgen/op-format.mlir

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ test.format_optional_else then
276276
// CHECK: test.format_optional_else else
277277
test.format_optional_else else
278278

279+
//===----------------------------------------------------------------------===//
280+
// Default-valued properties (ex. optional) elided in property dictionary
281+
// TODO: elisions generate extra spaces
282+
//===----------------------------------------------------------------------===//
283+
284+
// CHECK: test.format_optional_prop_dict {{$}}
285+
test.format_optional_prop_dict
286+
287+
// CHECK: test.format_optional_prop_dict {{$}}
288+
test.format_optional_prop_dict <{a = [], b = 1 : i32}>
289+
290+
// CHECK: test.format_optional_prop_dict {{$}}
291+
test.format_optional_prop_dict <{}>
292+
293+
// CHECK: test.format_optional_prop_dict < {a = ["foo"]}>
294+
test.format_optional_prop_dict <{a = ["foo"]}>
295+
296+
// CHECK: test.format_optional_prop_dict < {b = 2 : i32}>
297+
test.format_optional_prop_dict <{b = 2 : i32}>
298+
299+
// CHECK: test.format_optional_prop_dict <{a = ["foo"], b = 2 : i32}>
300+
test.format_optional_prop_dict <{a = ["foo"], b = 2 : i32}>
301+
279302
//===----------------------------------------------------------------------===//
280303
// Format a custom attribute
281304
//===----------------------------------------------------------------------===//

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ if (!attr && {2}) {{
13091309
"Properties.";
13101310
return ::mlir::failure();
13111311
}
1312-
if (::mlir::failed(setFromAttr(prop.{1}, attr, emitError)))
1312+
if (attr && ::mlir::failed(setFromAttr(prop.{1}, attr, emitError)))
13131313
return ::mlir::failure();
13141314
)decl";
13151315

0 commit comments

Comments
 (0)