[Mlir-commits] [mlir] 3ebb3ef - [mlir][bytecode] Fix crash when reading DenseIntOrFPElementsAttr with unsupported element type (#184773)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Mar 5 04:17:51 PST 2026
Author: Mehdi Amini
Date: 2026-03-05T12:17:46Z
New Revision: 3ebb3ef542aa51307b12b748ad62567cbc0722cb
URL: https://github.com/llvm/llvm-project/commit/3ebb3ef542aa51307b12b748ad62567cbc0722cb
DIFF: https://github.com/llvm/llvm-project/commit/3ebb3ef542aa51307b12b748ad62567cbc0722cb.diff
LOG: [mlir][bytecode] Fix crash when reading DenseIntOrFPElementsAttr with unsupported element type (#184773)
When a bytecode type callback substitutes a type that does not implement
DenseElementTypeInterface (e.g., \!test.i32 replacing i32), the bytecode
reader attempted to reconstruct a DenseIntOrFPElementsAttr with that
type. This unconditionally called getDenseElementBitWidth() which hit an
llvm_unreachable on unsupported types.
Fix this by validating the element type implements
DenseElementTypeInterface in readDenseIntOrFPElementsAttr before
proceeding. If the check fails, a proper diagnostic is emitted and
reading fails gracefully instead of crashing.
Fixes #128317
Added:
mlir/test/Bytecode/invalid/invalid-dense-elem-type-interface.mlir
Modified:
mlir/lib/IR/BuiltinDialectBytecode.cpp
Removed:
################################################################################
diff --git a/mlir/lib/IR/BuiltinDialectBytecode.cpp b/mlir/lib/IR/BuiltinDialectBytecode.cpp
index 8cf25391ba11a..73ec3eec69615 100644
--- a/mlir/lib/IR/BuiltinDialectBytecode.cpp
+++ b/mlir/lib/IR/BuiltinDialectBytecode.cpp
@@ -151,6 +151,17 @@ static void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
static LogicalResult
readDenseIntOrFPElementsAttr(DialectBytecodeReader &reader, ShapedType type,
SmallVectorImpl<char> &rawData) {
+ // Validate that the element type implements DenseElementTypeInterface.
+ // Without this check, downstream code unconditionally calls
+ // getDenseElementBitWidth() which asserts on unsupported types.
+ if (!llvm::isa<DenseElementType>(type.getElementType())) {
+ reader.emitError()
+ << "DenseIntOrFPElementsAttr element type must implement "
+ "DenseElementTypeInterface, but got: "
+ << type.getElementType();
+ return failure();
+ }
+
ArrayRef<char> blob;
if (failed(reader.readBlob(blob)))
return failure();
diff --git a/mlir/test/Bytecode/invalid/invalid-dense-elem-type-interface.mlir b/mlir/test/Bytecode/invalid/invalid-dense-elem-type-interface.mlir
new file mode 100644
index 0000000000000..2a03dd8ef4b23
--- /dev/null
+++ b/mlir/test/Bytecode/invalid/invalid-dense-elem-type-interface.mlir
@@ -0,0 +1,15 @@
+// RUN: not mlir-opt %s --test-bytecode-roundtrip="test-kind=2" 2>&1 | FileCheck %s
+
+// Regression test: test-kind=2 replaces i32 with !test.i32 (a type that does
+// not implement DenseElementTypeInterface). This should produce a proper error
+// instead of an assertion failure when deserializing DenseIntOrFPElementsAttr.
+
+// CHECK: DenseIntOrFPElementsAttr element type must implement DenseElementTypeInterface, but got: '!test.i32'
+// CHECK: failed to read bytecode
+
+module {
+ func.func @test() -> tensor<10xi32> {
+ %0 = arith.constant dense<42> : tensor<10xi32>
+ return %0 : tensor<10xi32>
+ }
+}
More information about the Mlir-commits
mailing list