diff --git a/mlir/test/IR/invalid-custom-print-parse.mlir b/mlir/test/IR/invalid-custom-print-parse.mlir index 00da145e35e0..7bc6644e70de 100644 --- a/mlir/test/IR/invalid-custom-print-parse.mlir +++ b/mlir/test/IR/invalid-custom-print-parse.mlir @@ -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}> + + diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp index a0d947fe8a0d..0a9d14d6603a 100644 --- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp +++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp @@ -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 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) {