[Mlir-commits] [mlir] bcd538a - [mlir][complex] Canonicalize consecutive complex.conj

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jul 28 18:06:23 PDT 2022


Author: lewuathe
Date: 2022-07-29T09:41:09+09:00
New Revision: bcd538ab92b79464fd538de7b2ec27a603558f88

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

LOG: [mlir][complex] Canonicalize consecutive complex.conj

We can canonicalize consecutive complex.conj just by removing all conjugate operations.

Reviewed By: pifon2a

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

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 c16cb21e6bacc..44a695287077a 100644
--- a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
+++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
@@ -590,6 +590,7 @@ def ConjOp : ComplexUnaryOp<"conj", [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 96e0583f6a4aa..d642a09f6a506 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -166,6 +166,20 @@ OpFoldResult ExpOp::fold(ArrayRef<Attribute> operands) {
   return {};
 }
 
+//===----------------------------------------------------------------------===//
+// ConjOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult ConjOp::fold(ArrayRef<Attribute> operands) {
+  assert(operands.size() == 1 && "unary op takes 1 operand");
+
+  // complex.conj(complex.conj(a)) -> a
+  if (auto conjOp = getOperand().getDefiningOp<ConjOp>())
+    return conjOp.getOperand();
+
+  return {};
+}
+
 //===----------------------------------------------------------------------===//
 // TableGen'd op method definitions
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 9086ee797f3d9..21ad95af39987 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -113,4 +113,14 @@ func.func @complex_exp_log() -> complex<f32> {
   %log = complex.log %complex1 : complex<f32>
   %exp = complex.exp %log : complex<f32>
   return %exp : complex<f32>
+}
+
+// CHECK-LABEL: func @complex_conj_conj
+func.func @complex_conj_conj() -> 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>
+  %conj1 = complex.conj %complex1 : complex<f32>
+  %conj2 = complex.conj %conj1 : complex<f32>
+  return %conj2 : complex<f32>
 }
\ No newline at end of file


        


More information about the Mlir-commits mailing list