[Mlir-commits] [mlir] 9b158de - [mlir][tosa] Fold exp(log) operation into no-op
Kai Sasaki
llvmlistbot at llvm.org
Thu May 11 20:03:36 PDT 2023
Author: Kai Sasaki
Date: 2023-05-12T12:03:26+09:00
New Revision: 9b158debe0e40bab76c4a22574f89a9881de2ca1
URL: https://github.com/llvm/llvm-project/commit/9b158debe0e40bab76c4a22574f89a9881de2ca1
DIFF: https://github.com/llvm/llvm-project/commit/9b158debe0e40bab76c4a22574f89a9881de2ca1.diff
LOG: [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
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 015be1f8f4885..b594b35d4b1b5 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 7e161b1298211..11a9661800e5b 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 9633a9405514b..53567fb25fb22 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>
+}
More information about the Mlir-commits
mailing list