[Mlir-commits] [mlir] [MLIR] Fix crash when parsing typed attribute via parseOptionalAttribute (PR #186192)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 12 10:28:39 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Mehdi Amini (joker-eph)

<details>
<summary>Changes</summary>

When calling `parseOptionalAttribute(result, type)` with a typed result (e.g. `FloatAttr`), the call resolved to the generic `parseOptionalAttribute(Attribute&, Type)` overload via implicit conversion. This overload accepted any attribute, including ones that don't match the expected concrete type. The caller would then invoke type-specific methods (e.g. `FloatAttr::getValue()`) on storage that belongs to a different attribute kind (e.g. `IntegerAttr`), causing a type-confusion crash.

The fix adds a non-virtual template overload of `parseOptionalAttribute` in `AsmParser` that intercepts calls for concrete attribute types not covered by the dedicated virtual overloads (`Attribute`, `ArrayAttr`, `StringAttr`, `SymbolRefAttr`). It parses the attribute generically and then validates the result type via `dyn_cast`, emitting an error if the parsed attribute is of the wrong kind.

Fixes #<!-- -->108135

Assisted-by: Claude Code

---
Full diff: https://github.com/llvm/llvm-project/pull/186192.diff


2 Files Affected:

- (modified) mlir/include/mlir/IR/OpImplementation.h (+20) 
- (added) mlir/test/mlir-tblgen/attr-or-type-format-invalid-parse.mlir (+7) 


``````````diff
diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index 9ce92cc7e5d37..59323bc350ce7 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -1142,6 +1142,26 @@ class AsmParser {
   virtual OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result,
                                                      Type type = {}) = 0;
 
+  /// Parse an optional attribute of a specific typed result. This overload
+  /// handles concrete attribute types (e.g. FloatAttr) that are not covered by
+  /// a dedicated virtual overload. It parses any attribute and then validates
+  /// that the result is of the expected type, emitting an error if not.
+  template <typename AttrType>
+  std::enable_if_t<!llvm::is_one_of<AttrType, Attribute, ArrayAttr, StringAttr,
+                                    SymbolRefAttr>::value,
+                   OptionalParseResult>
+  parseOptionalAttribute(AttrType &result, Type type = {}) {
+    Attribute attr;
+    OptionalParseResult parseResult = parseOptionalAttribute(attr, type);
+    if (!parseResult.has_value() || failed(*parseResult))
+      return parseResult;
+    result = dyn_cast<AttrType>(attr);
+    if (!result)
+      return emitError(getCurrentLocation())
+             << "expected attribute of a different type";
+    return success();
+  }
+
   /// Parse an optional attribute of a specific type and add it to the list with
   /// the specified name.
   template <typename AttrType>
diff --git a/mlir/test/mlir-tblgen/attr-or-type-format-invalid-parse.mlir b/mlir/test/mlir-tblgen/attr-or-type-format-invalid-parse.mlir
new file mode 100644
index 0000000000000..973bc3e17acbc
--- /dev/null
+++ b/mlir/test/mlir-tblgen/attr-or-type-format-invalid-parse.mlir
@@ -0,0 +1,7 @@
+// RUN: mlir-opt -verify-diagnostics --split-input-file %s
+
+// Test that an integer literal cannot be used where an APFloat is expected.
+// parseOptionalAttribute(FloatAttr &) should reject a non-float attribute.
+// expected-error at +2 {{expected attribute of a different type}}
+// expected-error at +1 {{failed to parse TestTypeAPFloat parameter}}
+func.func private @test() -> !test.ap_float<5U0>

``````````

</details>


https://github.com/llvm/llvm-project/pull/186192


More information about the Mlir-commits mailing list