[Mlir-commits] [mlir] [mlir][tensor] Check the EmptyOp's dynamicSize to be non-negative (PR #65577)

Kohei Yamaguchi llvmlistbot at llvm.org
Thu Sep 7 18:48:37 PDT 2023


https://github.com/sott0n updated https://github.com/llvm/llvm-project/pull/65577:

>From 5c81c38650499b749223c4e54d32aac87378f48b Mon Sep 17 00:00:00 2001
From: Kohei Yamaguchi <fix7211 at gmail.com>
Date: Thu, 7 Sep 2023 16:18:23 +0000
Subject: [PATCH 1/2] [mlir][tensor] Check the EmptyOp's dynamicSize to be
 non-negative

---
 mlir/lib/Dialect/Tensor/IR/TensorOps.cpp   | 15 +++++++++++++++
 mlir/test/Dialect/Tensor/canonicalize.mlir | 13 ++++++++++++-
 mlir/test/Dialect/Tensor/invalid.mlir      |  9 +++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
index 42d89cd5a76208a..7c3b641cea757ca 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
@@ -621,6 +621,18 @@ LogicalResult EmptyOp::verify() {
     return emitOpError("incorrect number of dynamic sizes, has ")
            << getDynamicSizes().size() << ", expected "
            << getType().getNumDynamicDims();
+
+  if (getDynamicSizes().size() > 0) {
+    if (llvm::any_of(getDynamicSizes(), [](Value operand) {
+          APInt constSizeArg;
+          if (!matchPattern(operand, m_ConstantInt(&constSizeArg))) {
+            return false;
+          }
+          return constSizeArg.isNegative();
+        }))
+      return emitOpError("dynamic size must be non-negative");
+  }
+
   return success();
 }
 
@@ -691,6 +703,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..70c274b6e0b63ca 100644
--- a/mlir/test/Dialect/Tensor/canonicalize.mlir
+++ b/mlir/test/Dialect/Tensor/canonicalize.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -split-input-file -canonicalize="test-convergence" | FileCheck %s
+// RUN: mlir-opt %s -split-input-file -canonicalize="test-convergence" -verify-diagnostics | FileCheck %s
 
 // CHECK-LABEL: @tensor_bitcast_chain_ok
 // CHECK-SAME: %[[IN:.*]]: tensor<2xi32>
@@ -1848,3 +1848,14 @@ 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>
 }
+
+// -----
+
+func.func @invalid_empty_negative_size() -> (tensor<4x5x?xf32>) {
+  %c1 = arith.constant 1 : index
+  %cn2 = arith.constant 2 : index
+  %0 = index.sub %c1, %cn2
+  // expected-error at +1 {{dynamic size must be non-negative}}
+  %1 = tensor.empty(%0) : tensor<4x5x?xf32>
+  return %1 : tensor<4x5x?xf32>
+}
diff --git a/mlir/test/Dialect/Tensor/invalid.mlir b/mlir/test/Dialect/Tensor/invalid.mlir
index 389e7e675c0eeda..341be52b8e2b72a 100644
--- a/mlir/test/Dialect/Tensor/invalid.mlir
+++ b/mlir/test/Dialect/Tensor/invalid.mlir
@@ -553,6 +553,15 @@ func.func @empty_wrong_number_of_operands(%sz : index) {
 
 // -----
 
+func.func @empty_negative_size(%sz : index) {
+  %0 = arith.constant -1 : index
+  // expected-error at +1 {{dynamic size must be non-negative}}
+  %out = tensor.empty(%sz, %0) : tensor<2x?x?x5xf32>
+  return
+}
+
+// -----
+
 func.func @pack_invalid_no_padding_no_full_tiles(%input: tensor<256x128xf32>, %output: tensor<8x8x16x33xf32>) -> tensor<8x8x16x33xf32> {
   // expected-error at +1 {{invalid tile factor provided. Only full tiles are supported when padding_value is not set}}
   %0 = tensor.pack %input inner_dims_pos = [1, 0] inner_tiles = [16, 33] into %output : tensor<256x128xf32>  -> tensor<8x8x16x33xf32>

>From 1556a6b5502d07c9a0fdf48bbbf18a608f746a93 Mon Sep 17 00:00:00 2001
From: Kohei Yamaguchi <fix7211 at gmail.com>
Date: Fri, 8 Sep 2023 10:46:33 +0000
Subject: [PATCH 2/2] addressed comment

---
 mlir/lib/Dialect/Tensor/IR/TensorOps.cpp   | 12 ------------
 mlir/test/Dialect/Tensor/canonicalize.mlir |  6 ++++--
 mlir/test/Dialect/Tensor/invalid.mlir      |  9 ---------
 3 files changed, 4 insertions(+), 23 deletions(-)

diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
index 7c3b641cea757ca..3eaa788f84a0eda 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
@@ -621,18 +621,6 @@ LogicalResult EmptyOp::verify() {
     return emitOpError("incorrect number of dynamic sizes, has ")
            << getDynamicSizes().size() << ", expected "
            << getType().getNumDynamicDims();
-
-  if (getDynamicSizes().size() > 0) {
-    if (llvm::any_of(getDynamicSizes(), [](Value operand) {
-          APInt constSizeArg;
-          if (!matchPattern(operand, m_ConstantInt(&constSizeArg))) {
-            return false;
-          }
-          return constSizeArg.isNegative();
-        }))
-      return emitOpError("dynamic size must be non-negative");
-  }
-
   return success();
 }
 
diff --git a/mlir/test/Dialect/Tensor/canonicalize.mlir b/mlir/test/Dialect/Tensor/canonicalize.mlir
index 70c274b6e0b63ca..c40c9efeb152ac6 100644
--- a/mlir/test/Dialect/Tensor/canonicalize.mlir
+++ b/mlir/test/Dialect/Tensor/canonicalize.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -split-input-file -canonicalize="test-convergence" -verify-diagnostics | FileCheck %s
+// RUN: mlir-opt %s -split-input-file -canonicalize="test-convergence" | FileCheck %s
 
 // CHECK-LABEL: @tensor_bitcast_chain_ok
 // CHECK-SAME: %[[IN:.*]]: tensor<2xi32>
@@ -1851,11 +1851,13 @@ func.func @pack_unpack_dynamic_with_padding(%t: tensor<?x?x?x?xf32>, %dim1: inde
 
 // -----
 
+// 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
-  // expected-error at +1 {{dynamic size must be non-negative}}
   %1 = tensor.empty(%0) : tensor<4x5x?xf32>
   return %1 : tensor<4x5x?xf32>
 }
diff --git a/mlir/test/Dialect/Tensor/invalid.mlir b/mlir/test/Dialect/Tensor/invalid.mlir
index 341be52b8e2b72a..389e7e675c0eeda 100644
--- a/mlir/test/Dialect/Tensor/invalid.mlir
+++ b/mlir/test/Dialect/Tensor/invalid.mlir
@@ -553,15 +553,6 @@ func.func @empty_wrong_number_of_operands(%sz : index) {
 
 // -----
 
-func.func @empty_negative_size(%sz : index) {
-  %0 = arith.constant -1 : index
-  // expected-error at +1 {{dynamic size must be non-negative}}
-  %out = tensor.empty(%sz, %0) : tensor<2x?x?x5xf32>
-  return
-}
-
-// -----
-
 func.func @pack_invalid_no_padding_no_full_tiles(%input: tensor<256x128xf32>, %output: tensor<8x8x16x33xf32>) -> tensor<8x8x16x33xf32> {
   // expected-error at +1 {{invalid tile factor provided. Only full tiles are supported when padding_value is not set}}
   %0 = tensor.pack %input inner_dims_pos = [1, 0] inner_tiles = [16, 33] into %output : tensor<256x128xf32>  -> tensor<8x8x16x33xf32>



More information about the Mlir-commits mailing list