[MLIR] Check that the prop-dict dictionnary does not have extra unknown entries (#138668)
At the moment we would just ignore them, which can be surprising and is error prone (a typo for a unit attribute flag for example).
This commit is contained in:
@@ -19,3 +19,10 @@ test.custom_dimension_list_attr dimension_list = [2x3]
|
||||
|
||||
// expected-error @below {{expected attribute value}}
|
||||
test.optional_custom_attr foo
|
||||
|
||||
// -----
|
||||
|
||||
// expected-error @below {{unknown key '"foo"' when parsing properties dictionary}}
|
||||
test.op_with_enum_prop_attr_form <{value = 0 : i32, foo}>
|
||||
|
||||
|
||||
|
||||
@@ -1300,6 +1300,11 @@ if (!dict) {
|
||||
emitError() << "expected DictionaryAttr to set properties";
|
||||
return ::mlir::failure();
|
||||
}
|
||||
// keep track of used keys in the input dictionary to be able to error out
|
||||
// if there are some unknown ones.
|
||||
DenseSet<StringAttr> usedKeys;
|
||||
MLIRContext *ctx = dict.getContext();
|
||||
(void)ctx;
|
||||
)decl";
|
||||
|
||||
// {0}: fromAttribute call
|
||||
@@ -1310,7 +1315,9 @@ auto setFromAttr = [] (auto &propStorage, ::mlir::Attribute propAttr,
|
||||
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) -> ::mlir::LogicalResult {{
|
||||
{0};
|
||||
};
|
||||
auto attr = dict.get("{1}");
|
||||
auto {1}AttrName = StringAttr::get(ctx, "{1}");
|
||||
usedKeys.insert({1}AttrName);
|
||||
auto attr = dict.get({1}AttrName);
|
||||
if (!attr && {2}) {{
|
||||
emitError() << "expected key entry for {1} in DictionaryAttr to set "
|
||||
"Properties.";
|
||||
@@ -1356,7 +1363,9 @@ if (attr && ::mlir::failed(setFromAttr(prop.{1}, attr, emitError)))
|
||||
bool isRequired = !attr.isOptional() && !attr.hasDefaultValue();
|
||||
body << formatv(R"decl(
|
||||
auto &propStorage = prop.{0};
|
||||
auto attr = dict.get("{0}");
|
||||
auto {0}AttrName = StringAttr::get(ctx, "{0}");
|
||||
auto attr = dict.get({0}AttrName);
|
||||
usedKeys.insert(StringAttr::get(ctx, "{1}"));
|
||||
if (attr || /*isRequired=*/{1}) {{
|
||||
if (!attr) {{
|
||||
emitError() << "expected key entry for {0} in DictionaryAttr to set "
|
||||
@@ -1374,7 +1383,14 @@ if (attr || /*isRequired=*/{1}) {{
|
||||
)decl",
|
||||
namedAttr.name, isRequired);
|
||||
}
|
||||
body << "return ::mlir::success();\n";
|
||||
body << R"decl(
|
||||
for (NamedAttribute attr : dict) {
|
||||
if (!usedKeys.contains(attr.getName()))
|
||||
return emitError() << "unknown key '" << attr.getName() <<
|
||||
"' when parsing properties dictionary";
|
||||
}
|
||||
return ::mlir::success();
|
||||
)decl";
|
||||
}
|
||||
|
||||
void OperationFormat::genParser(Operator &op, OpClass &opClass) {
|
||||
|
||||
Reference in New Issue
Block a user