[Mlir-commits] [mlir] d7128d8 - [MLIR] Fix crash when parsing typed attribute via parseOptionalAttribute (#186192)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Mar 23 05:52:21 PDT 2026
Author: Mehdi Amini
Date: 2026-03-23T13:52:16+01:00
New Revision: d7128d8edcb3a3f7cf67a606a418042f6fba1c9f
URL: https://github.com/llvm/llvm-project/commit/d7128d8edcb3a3f7cf67a606a418042f6fba1c9f
DIFF: https://github.com/llvm/llvm-project/commit/d7128d8edcb3a3f7cf67a606a418042f6fba1c9f.diff
LOG: [MLIR] Fix crash when parsing typed attribute via parseOptionalAttribute (#186192)
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
Added:
Modified:
mlir/include/mlir/IR/OpImplementation.h
mlir/test/IR/invalid-custom-print-parse.mlir
Removed:
################################################################################
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
diff erent 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/IR/invalid-custom-print-parse.mlir b/mlir/test/IR/invalid-custom-print-parse.mlir
index 7bc6644e70de0..41e8d97e1db49 100644
--- a/mlir/test/IR/invalid-custom-print-parse.mlir
+++ b/mlir/test/IR/invalid-custom-print-parse.mlir
@@ -25,4 +25,11 @@ 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}>
+// -----
+
+// 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
diff erent type}}
+// expected-error at +1 {{failed to parse TestTypeAPFloat parameter}}
+func.func private @test_ap_float_wrong_attr_type() -> !test.ap_float<5U0>
More information about the Mlir-commits
mailing list