[Mlir-commits] [mlir] f243f74 - [mlir][tosa] Fold consecutive negate as no-op

Kai Sasaki llvmlistbot at llvm.org
Mon May 15 18:15:58 PDT 2023


Author: Kai Sasaki
Date: 2023-05-16T10:11:24+09:00
New Revision: f243f74602961529a8e08b3cecd0e6062b5395c5

URL: https://github.com/llvm/llvm-project/commit/f243f74602961529a8e08b3cecd0e6062b5395c5
DIFF: https://github.com/llvm/llvm-project/commit/f243f74602961529a8e08b3cecd0e6062b5395c5.diff

LOG: [mlir][tosa] Fold consecutive negate as no-op

Consecutive element-wise negate should be canonicalized as no-op.

Reviewed By: eric-k256

Differential Revision: https://reviews.llvm.org/D150518

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 45a031dda8a88..f4827a9ee54c4 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1101,6 +1101,8 @@ def Tosa_NegateOp : Tosa_Op<"negate", [
   );
 
   let builders = [Tosa_UnaryOpQuantInfoBuilder];
+
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index d5263a3ff0567..3eb83e015dac0 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -1017,3 +1017,13 @@ OpFoldResult tosa::ExpOp::fold(FoldAdaptor adaptor) {
 
   return {};
 }
+
+OpFoldResult tosa::NegateOp::fold(FoldAdaptor adaptor) {
+  auto input = getInput1();
+  // Element-wise negate(negate(x)) = x
+  if (auto op = input.getDefiningOp<tosa::NegateOp>()) {
+    return op.getInput1();
+  }
+
+  return {};
+}

diff  --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 53567fb25fb22..e9bd1225fe7ca 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -525,3 +525,14 @@ func.func @fold_exp_log(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
   %1 = "tosa.exp"(%0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
   return %1 : tensor<?x1xf32>
 }
+
+// -----
+
+// CHECK-LABEL: @fold_negate_negate
+func.func @fold_negate_negate(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
+  // CHECK: return %arg{{.*}} : tensor<?x1xf32>
+  %0 = "tosa.negate"(%arg0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
+  %1 = "tosa.negate"(%0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
+  return %1 : tensor<?x1xf32>
+}
+


        


More information about the Mlir-commits mailing list