[Mlir-commits] [mlir] bba48df - [mlir][tosa] tosa.resize canonicalizer for trivial noop
Rob Suderman
llvmlistbot at llvm.org
Wed Oct 5 16:30:04 PDT 2022
Author: Rob Suderman
Date: 2022-10-05T16:28:25-07:00
New Revision: bba48dfe4ac4be72d74bb33e646d0b3984eeded1
URL: https://github.com/llvm/llvm-project/commit/bba48dfe4ac4be72d74bb33e646d0b3984eeded1
DIFF: https://github.com/llvm/llvm-project/commit/bba48dfe4ac4be72d74bb33e646d0b3984eeded1.diff
LOG: [mlir][tosa] tosa.resize canonicalizer for trivial noop
If the scaling factor is by 1 with no offset or border, then the
resize is a no-op.
Reviewed By: dcaballe
Differential Revision: https://reviews.llvm.org/D135329
Added:
Modified:
mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
mlir/test/Dialect/Tosa/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index edecc2cee96d0..30dc14bb13ac9 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1634,6 +1634,8 @@ def Tosa_ResizeOp : Tosa_Op<"resize", [
let results = (outs
Tosa_Tensor4D:$output
);
+
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index b346b11cbb1ed..dd1a78108e67d 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -14,6 +14,7 @@
#include "mlir/Dialect/Quant/QuantOps.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
+#include "mlir/Dialect/Tosa/Utils/CoversionUtils.h"
#include "mlir/Dialect/Tosa/Utils/QuantUtils.h"
#include "mlir/Dialect/Tosa/Utils/ShapeUtils.h"
#include "mlir/IR/BuiltinTypes.h"
@@ -846,6 +847,38 @@ OpFoldResult PadOp::fold(ArrayRef<Attribute> operands) {
return {};
}
+// Fold away cases where a tosa.resize operation returns a copy
+// of the input image.
+OpFoldResult ResizeOp::fold(ArrayRef<Attribute> operands) {
+ SmallVector<int32_t> scale, offset, border;
+ getValuesFromIntArrayAttribute(getScale(), scale);
+ getValuesFromIntArrayAttribute(getOffset(), offset);
+ getValuesFromIntArrayAttribute(getBorder(), border);
+
+ // Check unit scaling.
+ if (scale[0] != scale[1] || scale[2] != scale[3]) {
+ return {};
+ }
+
+ // There should be no offset.
+ if (offset[0] != 0 || offset[1] != 0) {
+ return {};
+ }
+
+ // There should be no border.
+ if (border[0] != 0 || border[1] != 0) {
+ return {};
+ }
+
+ auto input = getInput();
+ auto inputTy = input.getType().cast<RankedTensorType>();
+ auto resultTy = getType().cast<RankedTensorType>();
+ if (inputTy != resultTy)
+ return {};
+
+ return input;
+}
+
OpFoldResult ReverseOp::fold(ArrayRef<Attribute> operands) {
auto operand = getInput();
auto operandTy = operand.getType().cast<ShapedType>();
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index aa7179c0b33e7..ca25100bb5fcd 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -408,3 +408,21 @@ func.func @single_bit_reshape() -> tensor<1xi1> {
%1 = "tosa.reshape"(%0) {new_shape = [1]} : (tensor<1x1xi1>) -> tensor<1xi1>
return %1 : tensor<1xi1>
}
+
+// -----
+
+// CHECK-LABEL: @fold_resize_nearest
+func.func @fold_resize_nearest(%arg0 : tensor<1x15x13x1xi8>) -> tensor<1x15x13x1xi8> {
+ // CHECK: return %arg0
+ %resize = "tosa.resize"(%arg0) {mode = "NEAREST_NEIGHBOR", scale = [2, 2, 1, 1], offset = [0, 0], border = [0, 0]} : (tensor<1x15x13x1xi8>) -> tensor<1x15x13x1xi8>
+ return %resize : tensor<1x15x13x1xi8>
+}
+
+// -----
+
+// CHECK-LABEL: @fold_resize_bilinear
+func.func @fold_resize_bilinear(%arg0 : tensor<1x15x13x1xi8>) -> tensor<1x15x13x1xi8> {
+ // CHECK: return %arg0
+ %resize = "tosa.resize"(%arg0) {mode = "BILINEAR", scale = [2, 2, 1, 1], offset = [0, 0], border = [0, 0]} : (tensor<1x15x13x1xi8>) -> tensor<1x15x13x1xi8>
+ return %resize : tensor<1x15x13x1xi8>
+}
More information about the Mlir-commits
mailing list