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 @@ -365,6 +365,8 @@ }]; 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 @@ -124,6 +124,20 @@ return {}; } +//===----------------------------------------------------------------------===// +// NegOp +//===----------------------------------------------------------------------===// + +OpFoldResult NegOp::fold(ArrayRef operands) { + assert(operands.size() == 1 && "unary op takes 1 operand"); + + // complex.neg(complex.neg(a)) -> a + if (auto negOp = getOperand().getDefiningOp()) + return negOp.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 @@ -83,4 +83,14 @@ %sub = complex.sub %complex1, %complex2 : complex %add = complex.add %complex2, %sub : complex return %add : complex +} + +// CHECK-LABEL: func @complex_neg_neg +func.func @complex_neg_neg() -> 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 + %neg1 = complex.neg %complex1 : complex + %neg2 = complex.neg %neg1 : complex + return %neg2 : complex } \ No newline at end of file