summaryrefslogtreecommitdiff
path: root/mlir
diff options
context:
space:
mode:
authorKai Sasaki <lewuathe@gmail.com>2023-05-12 11:40:00 +0900
committerKai Sasaki <lewuathe@gmail.com>2023-05-12 12:03:26 +0900
commit9b158debe0e40bab76c4a22574f89a9881de2ca1 (patch)
tree7d3378a8aa158fce0c9d725c2c609773ad1363cc /mlir
parent2ed77846a9161106a26d1efa9b68a3122856b434 (diff)
downloadllvm-9b158debe0e40bab76c4a22574f89a9881de2ca1.tar.gz
[mlir][tosa] Fold exp(log) operation into no-op
Element-wise exp(log) can be canonicalized as no-op. Reviewed By: eric-k256 Differential Revision: https://reviews.llvm.org/D150342
Diffstat (limited to 'mlir')
-rw-r--r--mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td2
-rw-r--r--mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp10
-rw-r--r--mlir/test/Dialect/Tosa/canonicalize.mlir10
3 files changed, 22 insertions, 0 deletions
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index 015be1f8f488..b594b35d4b1b 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -999,6 +999,8 @@ def Tosa_ExpOp : Tosa_Op<"exp", [
let results = (outs
Tosa_Tensor:$output
);
+
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 7e161b129821..11a9661800e5 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -1006,3 +1006,13 @@ OpFoldResult tosa::LogOp::fold(FoldAdaptor adaptor) {
return {};
}
+
+OpFoldResult tosa::ExpOp::fold(FoldAdaptor adaptor) {
+ auto input = getInput1();
+ // Element-wise exp(log(x)) = x
+ if (auto op = input.getDefiningOp<tosa::LogOp>()) {
+ return op.getInput1();
+ }
+
+ return {};
+}
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 9633a9405514..53567fb25fb2 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -515,3 +515,13 @@ func.func @fold_log_exp(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
%1 = "tosa.log"(%0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
return %1 : tensor<?x1xf32>
}
+
+// -----
+
+// CHECK-LABEL: @fold_exp_log
+func.func @fold_exp_log(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
+ // CHECK: return %arg{{.*}} : tensor<?x1xf32>
+ %0 = "tosa.log"(%arg0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
+ %1 = "tosa.exp"(%0) : (tensor<?x1xf32>) -> tensor<?x1xf32>
+ return %1 : tensor<?x1xf32>
+}