diff options
author | Kai Sasaki <lewuathe@gmail.com> | 2023-05-16 09:41:28 +0900 |
---|---|---|
committer | Kai Sasaki <lewuathe@gmail.com> | 2023-05-16 10:11:24 +0900 |
commit | f243f74602961529a8e08b3cecd0e6062b5395c5 (patch) | |
tree | 5ad46d2812b3043321d66352f7f0953f67e3a40d /mlir | |
parent | deca5e8f5038e70cbf2349e5df91109cae214ee7 (diff) | |
download | llvm-f243f74602961529a8e08b3cecd0e6062b5395c5.tar.gz |
[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
Diffstat (limited to 'mlir')
-rw-r--r-- | mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td | 2 | ||||
-rw-r--r-- | mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp | 10 | ||||
-rw-r--r-- | mlir/test/Dialect/Tosa/canonicalize.mlir | 11 |
3 files changed, 23 insertions, 0 deletions
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td index 45a031dda8a8..f4827a9ee54c 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 d5263a3ff056..3eb83e015dac 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 53567fb25fb2..e9bd1225fe7c 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> +} + |