diff --git a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td --- a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td +++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td @@ -590,6 +590,7 @@ }]; let results = (outs Complex:$result); + let hasFolder = 1; } //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp --- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp +++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp @@ -166,6 +166,20 @@ return {}; } +//===----------------------------------------------------------------------===// +// ConjOp +//===----------------------------------------------------------------------===// + +OpFoldResult ConjOp::fold(ArrayRef operands) { + assert(operands.size() == 1 && "unary op takes 1 operand"); + + // complex.conj(complex.conj(a)) -> a + if (auto conjOp = getOperand().getDefiningOp()) + 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 --- a/mlir/test/Dialect/Complex/canonicalize.mlir +++ b/mlir/test/Dialect/Complex/canonicalize.mlir @@ -113,4 +113,14 @@ %log = complex.log %complex1 : complex %exp = complex.exp %log : complex return %exp : complex +} + +// CHECK-LABEL: func @complex_conj_conj +func.func @complex_conj_conj() -> complex { + %complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex + // CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex + // CHECK-NEXT: return %[[CPLX:.*]] : complex + %conj1 = complex.conj %complex1 : complex + %conj2 = complex.conj %conj1 : complex + return %conj2 : complex } \ No newline at end of file