Skip to content

Commit bcd538a

Browse files
committed
[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
1 parent 60e1206 commit bcd538a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ def ConjOp : ComplexUnaryOp<"conj", [SameOperandsAndResultType]> {
590590
}];
591591

592592
let results = (outs Complex<AnyFloat>:$result);
593+
let hasFolder = 1;
593594
}
594595

595596
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/Complex/IR/ComplexOps.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ OpFoldResult ExpOp::fold(ArrayRef<Attribute> operands) {
166166
return {};
167167
}
168168

169+
//===----------------------------------------------------------------------===//
170+
// ConjOp
171+
//===----------------------------------------------------------------------===//
172+
173+
OpFoldResult ConjOp::fold(ArrayRef<Attribute> operands) {
174+
assert(operands.size() == 1 && "unary op takes 1 operand");
175+
176+
// complex.conj(complex.conj(a)) -> a
177+
if (auto conjOp = getOperand().getDefiningOp<ConjOp>())
178+
return conjOp.getOperand();
179+
180+
return {};
181+
}
182+
169183
//===----------------------------------------------------------------------===//
170184
// TableGen'd op method definitions
171185
//===----------------------------------------------------------------------===//

mlir/test/Dialect/Complex/canonicalize.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ func.func @complex_exp_log() -> complex<f32> {
113113
%log = complex.log %complex1 : complex<f32>
114114
%exp = complex.exp %log : complex<f32>
115115
return %exp : complex<f32>
116+
}
117+
118+
// CHECK-LABEL: func @complex_conj_conj
119+
func.func @complex_conj_conj() -> complex<f32> {
120+
%complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex<f32>
121+
// CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex<f32>
122+
// CHECK-NEXT: return %[[CPLX:.*]] : complex<f32>
123+
%conj1 = complex.conj %complex1 : complex<f32>
124+
%conj2 = complex.conj %conj1 : complex<f32>
125+
return %conj2 : complex<f32>
116126
}

0 commit comments

Comments
 (0)