[mlir][complex] Canonicalization for consecutive complex.neg

Consecutive complex.neg are redundant so that we can canonicalize them to the original operands.

Reviewed By: pifon2a

Differential Revision: https://reviews.llvm.org/D128781
This commit is contained in:
lewuathe 2022-06-29 10:32:23 +02:00 committed by Alexander Belyaev
parent d8ad018869
commit 0180709590
3 changed files with 26 additions and 0 deletions

View File

@ -365,6 +365,8 @@ def NegOp : ComplexUnaryOp<"neg", [SameOperandsAndResultType]> {
}];
let results = (outs Complex<AnyFloat>:$result);
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//

View File

@ -124,6 +124,20 @@ OpFoldResult AddOp::fold(ArrayRef<Attribute> operands) {
return {};
}
//===----------------------------------------------------------------------===//
// NegOp
//===----------------------------------------------------------------------===//
OpFoldResult NegOp::fold(ArrayRef<Attribute> operands) {
assert(operands.size() == 1 && "unary op takes 1 operand");
// complex.neg(complex.neg(a)) -> a
if (auto negOp = getOperand().getDefiningOp<NegOp>())
return negOp.getOperand();
return {};
}
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//

View File

@ -83,4 +83,14 @@ func.func @complex_add_sub_rhs() -> complex<f32> {
%sub = complex.sub %complex1, %complex2 : complex<f32>
%add = complex.add %complex2, %sub : complex<f32>
return %add : complex<f32>
}
// CHECK-LABEL: func @complex_neg_neg
func.func @complex_neg_neg() -> 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>
%neg1 = complex.neg %complex1 : complex<f32>
%neg2 = complex.neg %neg1 : complex<f32>
return %neg2 : complex<f32>
}