[Mlir-commits] [mlir] 5148c68 - [mlir][complex] Inverse canonicalization between exp and log

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Jul 2 17:37:29 PDT 2022


Author: lewuathe
Date: 2022-07-03T09:26:55+09:00
New Revision: 5148c685e3bbb8d271ebe71da78ff993591f98a7

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

LOG: [mlir][complex] Inverse canonicalization between exp and log

We can canonicalize consecutive complex.exp and complex.log which are inverse functions each other.

Reviewed By: bixia

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
    mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
    mlir/test/Dialect/Complex/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
index b76276c726ee5..c16cb21e6bacc 100644
--- a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
+++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
@@ -240,6 +240,8 @@ def ExpOp : ComplexUnaryOp<"exp", [SameOperandsAndResultType]> {
   }];
 
   let results = (outs Complex<AnyFloat>:$result);
+
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//
@@ -309,6 +311,8 @@ def LogOp : ComplexUnaryOp<"log", [SameOperandsAndResultType]> {
   }];
 
   let results = (outs Complex<AnyFloat>:$result);
+
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index 00dbb564481ee..96e0583f6a4aa 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -138,6 +138,34 @@ OpFoldResult NegOp::fold(ArrayRef<Attribute> operands) {
   return {};
 }
 
+//===----------------------------------------------------------------------===//
+// LogOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult LogOp::fold(ArrayRef<Attribute> operands) {
+  assert(operands.size() == 1 && "unary op takes 1 operand");
+
+  // complex.log(complex.exp(a)) -> a
+  if (auto expOp = getOperand().getDefiningOp<ExpOp>())
+    return expOp.getOperand();
+
+  return {};
+}
+
+//===----------------------------------------------------------------------===//
+// ExpOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult ExpOp::fold(ArrayRef<Attribute> operands) {
+  assert(operands.size() == 1 && "unary op takes 1 operand");
+
+  // complex.exp(complex.log(a)) -> a
+  if (auto logOp = getOperand().getDefiningOp<LogOp>())
+    return logOp.getOperand();
+
+  return {};
+}
+
 //===----------------------------------------------------------------------===//
 // TableGen'd op method definitions
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 093e92f6a0308..9086ee797f3d9 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -93,4 +93,24 @@ func.func @complex_neg_neg() -> complex<f32> {
   %neg1 = complex.neg %complex1 : complex<f32>
   %neg2 = complex.neg %neg1 : complex<f32>
   return %neg2 : complex<f32>
+}
+
+// CHECK-LABEL: func @complex_log_exp
+func.func @complex_log_exp() -> complex<f32> {
+  %complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex<f32>
+  // CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex<f32>
+  // CHECK-NEXT: return %[[CPLX:.*]] : complex<f32>
+  %exp = complex.exp %complex1 : complex<f32>
+  %log = complex.log %exp : complex<f32>
+  return %log : complex<f32>
+}
+
+// CHECK-LABEL: func @complex_exp_log
+func.func @complex_exp_log() -> complex<f32> {
+  %complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex<f32>
+  // CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex<f32>
+  // CHECK-NEXT: return %[[CPLX:.*]] : complex<f32>
+  %log = complex.log %complex1 : complex<f32>
+  %exp = complex.exp %log : complex<f32>
+  return %exp : complex<f32>
 }
\ No newline at end of file


        


More information about the Mlir-commits mailing list