[Mlir-commits] [mlir] [mlir][tensor] Fix element-count overflow assertion in reshape verifier (PR #222117)
Stanislav Bardyuk
llvmlistbot at llvm.org
Sat Oct 24 09:48:27 PDT 2026
https://github.com/kodlan updated https://github.com/llvm/llvm-project/pull/222117
>From 284f7139a6659484c2b5306ab3b17c5d77f72e57 Mon Sep 17 00:00:00 2001
From: Stanislav Bardyuk <sbardyuk at google.com>
Date: Tue, 8 Sep 2026 19:43:34 +0000
Subject: [PATCH] [mlir][tensor] Fix element-count overflow assertion in
reshape verifier
verifyTensorReshapeOp compared the static element counts of the expanded
and collapsed types with ShapedType::getNumElements(), which asserts on
int64 overflow. Use tryGetNumElements() and reject the op with a
diagnostic when either count is not representable.
Fixes #221590
---
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp | 13 ++++++++-----
mlir/test/Dialect/Tensor/invalid.mlir | 24 ++++++++++++++++++++++++
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
index 2ee6c93b313e69..f3d23ef513c073 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
@@ -2074,12 +2074,15 @@ static LogicalResult verifyTensorReshapeOp(TensorReshapeOp op,
// Reshape must preserve the number of elements when statically known.
if (expandedType.hasStaticShape() && collapsedType.hasStaticShape()) {
- int64_t expandedNumElements = expandedType.getNumElements();
- int64_t collapsedNumElements = collapsedType.getNumElements();
- if (expandedNumElements != collapsedNumElements) {
+ std::optional<int64_t> expandedNumElements =
+ ShapedType::tryGetNumElements(expandedType.getShape());
+ std::optional<int64_t> collapsedNumElements =
+ ShapedType::tryGetNumElements(collapsedType.getShape());
+ if (!expandedNumElements || !collapsedNumElements)
+ return op.emitOpError("number of elements exceeds the int64 range");
+ if (*expandedNumElements != *collapsedNumElements)
return op.emitOpError("number of elements must be preserved: ")
- << expandedNumElements << " != " << collapsedNumElements;
- }
+ << *expandedNumElements << " != " << *collapsedNumElements;
}
auto maps = op.getReassociationMaps();
diff --git a/mlir/test/Dialect/Tensor/invalid.mlir b/mlir/test/Dialect/Tensor/invalid.mlir
index f613d309f1db45..cec61091368d2e 100644
--- a/mlir/test/Dialect/Tensor/invalid.mlir
+++ b/mlir/test/Dialect/Tensor/invalid.mlir
@@ -763,3 +763,27 @@ func.func @pad_negative_dynamic_dim(%arg0: tensor<?xf32>, %arg1: f32) -> tensor<
} : tensor<?xf32> to tensor<?xf32>
return %0 : tensor<?xf32>
}
+
+// -----
+
+func.func @collapse_shape_num_elements_overflow(%arg0: tensor<4611686018427387904x2xf32>) {
+ // expected-error at +1 {{'tensor.collapse_shape' op number of elements exceeds the int64 range}}
+ %0 = tensor.collapse_shape %arg0 [[0], [1]] : tensor<4611686018427387904x2xf32> into tensor<4611686018427387904x2xf32>
+ return
+}
+
+// -----
+
+func.func @collapse_shape_num_elements_overflow_to_scalar(%arg0: tensor<4611686018427387904x2xf32>) {
+ // expected-error at +1 {{'tensor.collapse_shape' op number of elements exceeds the int64 range}}
+ %0 = tensor.collapse_shape %arg0 [] : tensor<4611686018427387904x2xf32> into tensor<f32>
+ return
+}
+
+// -----
+
+func.func @expand_shape_num_elements_overflow(%arg0: tensor<4611686018427387904x2xf32>) {
+ // expected-error at +1 {{'tensor.expand_shape' op number of elements exceeds the int64 range}}
+ %0 = tensor.expand_shape %arg0 [[0], [1]] output_shape [4611686018427387904, 2] : tensor<4611686018427387904x2xf32> into tensor<4611686018427387904x2xf32>
+ return
+}
More information about the Mlir-commits
mailing list