Skip to content

[MLIR] Check that the prop-dict dictionnary does not have extra unknown entries #138668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mlir/test/IR/invalid-custom-print-parse.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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}>


22 changes: 19 additions & 3 deletions mlir/tools/mlir-tblgen/OpFormatGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.";
Expand Down Expand Up @@ -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 "
Expand All @@ -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) {
Expand Down
Loading