[Mlir-commits] [mlir] ca8cf90 - [mlir][tensor] Check the EmptyOp's dynamicSize to be non-negative (#65577)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Sep 10 18:38:58 PDT 2023
Author: Kohei Yamaguchi
Date: 2023-09-10T18:38:54-07:00
New Revision: ca8cf90c8ca8a6a8f63ef8d05e226c51f4f6711f
URL: https://github.com/llvm/llvm-project/commit/ca8cf90c8ca8a6a8f63ef8d05e226c51f4f6711f
DIFF: https://github.com/llvm/llvm-project/commit/ca8cf90c8ca8a6a8f63ef8d05e226c51f4f6711f.diff
LOG: [mlir][tensor] Check the EmptyOp's dynamicSize to be non-negative (#65577)
This patch addresses a crash that occurs when negative dynamic sizes are
provided in tensor.emptyOp by adding a check to ensure that dynamic
sizes are non-negative.
Fixes #64064
Added:
Modified:
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
mlir/test/Dialect/Tensor/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
index 25ddf2fc48d6f89..3e30e320bee8f83 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
@@ -712,6 +712,9 @@ struct ReplaceEmptyTensorStaticShapeDims : OpRewritePattern<EmptyOp> {
Value dynamicSize = op.getDynamicSizes()[ctr++];
std::optional<int64_t> cst = getConstantIntValue(dynamicSize);
if (cst.has_value()) {
+ // dynamic size must be non-negative.
+ if (cst.value() < 0)
+ return failure();
staticShape[i] = *cst;
changedType = true;
} else {
diff --git a/mlir/test/Dialect/Tensor/canonicalize.mlir b/mlir/test/Dialect/Tensor/canonicalize.mlir
index 4a757500920d50b..c40c9efeb152ac6 100644
--- a/mlir/test/Dialect/Tensor/canonicalize.mlir
+++ b/mlir/test/Dialect/Tensor/canonicalize.mlir
@@ -1848,3 +1848,16 @@ func.func @pack_unpack_dynamic_with_padding(%t: tensor<?x?x?x?xf32>, %dim1: inde
%packed = tensor.pack %unpacked padding_value(%pad: f32) inner_dims_pos = [0, 1] inner_tiles = [%tile1, %tile2] into %tensor_empty1 : tensor<?x?xf32> -> tensor<?x?x?x?xf32>
return %packed : tensor<?x?x?x?xf32>
}
+
+// -----
+
+// CHECK: func.func @invalid_empty_negative_size
+// CHECK: %[[IDX:.*]] = index.constant
+// CHECK: %[[T:.*]] = tensor.empty(%[[IDX]]) : tensor<4x5x?xf32>
+func.func @invalid_empty_negative_size() -> (tensor<4x5x?xf32>) {
+ %c1 = arith.constant 1 : index
+ %cn2 = arith.constant 2 : index
+ %0 = index.sub %c1, %cn2
+ %1 = tensor.empty(%0) : tensor<4x5x?xf32>
+ return %1 : tensor<4x5x?xf32>
+}
More information about the Mlir-commits
mailing list