[Mlir-commits] [mlir] [MLIR] Check that the prop-dict dictionnary does not have extra unknown entries (PR #138668)

Mehdi Amini llvmlistbot at llvm.org
Tue May 6 02:57:58 PDT 2025


https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/138668

At the moment we would just ignore them, which can be surprising if the user had a typo for a unit attribute for example.

>From 4455fa83ee83b07c1e14a557964a2430edf4a40b Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Tue, 6 May 2025 02:55:39 -0700
Subject: [PATCH] [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.
---
 mlir/test/IR/invalid-custom-print-parse.mlir |  7 +++++++
 mlir/tools/mlir-tblgen/OpFormatGen.cpp       | 16 +++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/mlir/test/IR/invalid-custom-print-parse.mlir b/mlir/test/IR/invalid-custom-print-parse.mlir
index 00da145e35e0b..29e201c6f12f5 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 dictionnary}}
+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 a0d947fe8a0df..09550b7196a17 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 dictionnary 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,6 +1315,7 @@ auto setFromAttr = [] (auto &propStorage, ::mlir::Attribute propAttr,
          ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) -> ::mlir::LogicalResult {{
   {0};
 };
+usedKeys.insert(StringAttr::get(ctx, "{1}"));
 auto attr = dict.get("{1}");
 if (!attr && {2}) {{
   emitError() << "expected key entry for {1} in DictionaryAttr to set "
@@ -1357,6 +1363,7 @@ if (attr && ::mlir::failed(setFromAttr(prop.{1}, attr, emitError)))
     body << formatv(R"decl(
 auto &propStorage = prop.{0};
 auto attr = dict.get("{0}");
+usedKeys.insert(StringAttr::get(ctx, "{0}"));
 if (attr || /*isRequired=*/{1}) {{
   if (!attr) {{
     emitError() << "expected key entry for {0} in DictionaryAttr to set "
@@ -1374,7 +1381,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 dictionnary";
+}
+return ::mlir::success();
+)decl";
 }
 
 void OperationFormat::genParser(Operator &op, OpClass &opClass) {



More information about the Mlir-commits mailing list