Skip to content

Commit 4b5b1ab

Browse files
committed
[MLIR] Check that the prop-dict dictionnary does not have extra unknown entries
At the moment we would just ignore them, which can be surprising if the user had a typo for a unit attribute for example.
1 parent 4cc152c commit 4b5b1ab

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mlir/test/IR/invalid-custom-print-parse.mlir

+7
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ test.custom_dimension_list_attr dimension_list = [2x3]
1919

2020
// expected-error @below {{expected attribute value}}
2121
test.optional_custom_attr foo
22+
23+
// -----
24+
25+
// expected-error @below {{Unknown key '"foo"' when parsing properties dictionnary}}
26+
test.op_with_enum_prop_attr_form <{value = 0 : i32, foo}>
27+
28+

mlir/tools/mlir-tblgen/OpFormatGen.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,11 @@ if (!dict) {
13001300
emitError() << "expected DictionaryAttr to set properties";
13011301
return ::mlir::failure();
13021302
}
1303+
// keep track of used keys in the input dictionnary to be able to error out
1304+
// if there are some unknown ones.
1305+
DenseSet<StringAttr> usedKeys;
1306+
MLIRContext *ctx = dict.getContext();
1307+
(void)ctx;
13031308
)decl";
13041309

13051310
// {0}: fromAttribute call
@@ -1310,6 +1315,7 @@ auto setFromAttr = [] (auto &propStorage, ::mlir::Attribute propAttr,
13101315
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) -> ::mlir::LogicalResult {{
13111316
{0};
13121317
};
1318+
usedKeys.insert(StringAttr::get(ctx, "{1}"));
13131319
auto attr = dict.get("{1}");
13141320
if (!attr && {2}) {{
13151321
emitError() << "expected key entry for {1} in DictionaryAttr to set "
@@ -1357,6 +1363,7 @@ if (attr && ::mlir::failed(setFromAttr(prop.{1}, attr, emitError)))
13571363
body << formatv(R"decl(
13581364
auto &propStorage = prop.{0};
13591365
auto attr = dict.get("{0}");
1366+
usedKeys.insert(StringAttr::get(ctx, "{0}"));
13601367
if (attr || /*isRequired=*/{1}) {{
13611368
if (!attr) {{
13621369
emitError() << "expected key entry for {0} in DictionaryAttr to set "
@@ -1374,7 +1381,14 @@ if (attr || /*isRequired=*/{1}) {{
13741381
)decl",
13751382
namedAttr.name, isRequired);
13761383
}
1377-
body << "return ::mlir::success();\n";
1384+
body << R"decl(
1385+
for (NamedAttribute attr : dict) {
1386+
if (!usedKeys.contains(attr.getName()))
1387+
return emitError() << "Unknown key '" << attr.getName() <<
1388+
"' when parsing properties dictionary";
1389+
}
1390+
return ::mlir::success();
1391+
)decl";
13781392
}
13791393

13801394
void OperationFormat::genParser(Operator &op, OpClass &opClass) {

0 commit comments

Comments
 (0)