[Mlir-commits] [mlir] Add verifier for tosa.slice operator (PR #68693)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Oct 10 05:01:43 PDT 2023


https://github.com/LiqinWeng created https://github.com/llvm/llvm-project/pull/68693

None

>From 19c35d2f36ba237d9dbf90e1c17821e0eb58666c Mon Sep 17 00:00:00 2001
From: "liqin.weng" <liqin.weng at spacemit.com>
Date: Tue, 10 Oct 2023 19:59:04 +0800
Subject: [PATCH] Add verifier for tosa.slice operator

---
 mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td |  1 +
 mlir/lib/Dialect/Tosa/IR/TosaOps.cpp         | 25 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index a80111aedfe0b59..4e4b3a8eaa398c4 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1613,6 +1613,7 @@ def Tosa_SliceOp : Tosa_InferShapedTypeOp<"slice"> {
 
   let hasCanonicalizer = 1;
   let hasFolder = 1;
+  let hasVerifier = 1;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index a719171b2b359d2..a1407d21f576ea3 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -264,6 +264,31 @@ LogicalResult tosa::AvgPool2dOp::verify() {
   return emitOpError("input/output element types are incompatible.");
 }
 
+LogicalResult tosa::SliceOp::verify() {
+  auto inputType = llvm::cast<ShapedType>(getInput().getType());
+  auto resultType = llvm::cast<ShapedType>(getOutput().getType());
+  int64_t inputRank = inputType.getRank();
+  int64_t resultRank = resultType.getRank();
+  int64_t sizeRank = static_cast<int64_t>(getSize().size());
+  int64_t startRank = static_cast<int64_t>(getStart().size());
+  // FIXME: Should Input/Ouput be RankedTensorType ?
+  if (inputRank != resultRank)
+    return emitOpError("input and output of ranks must be equal");
+
+  if (sizeRank != inputRank || startRank != inputRank)
+    return emitOpError(
+        "The number of elements of the silce.getSize()/sliceStart() should be "
+        "equal to the rank of the input");
+
+  for (int i = 0; i < inputRank; i++) {
+    if (getStart()[i] + getSize()[i] > inputType.getShape()[i])
+      return emitOpError("The size of each dimension should be smaller than "
+                         "the size of each dimension of the input");
+  }
+
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // TOSA Operator Quantization Builders.
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list