[mlir][complex] Canonicalize complex.add zero

Adding complex value with 0 for real and imaginary part can be ignored.

NOTE: This type of canonicalization can be written in an easy and tidy format using `complex.number` after constant op supports custom attribute.

Differential Revision: https://reviews.llvm.org/D130748
This commit is contained in:
lewuathe 2022-07-29 14:31:55 +02:00 committed by Alexander Belyaev
parent b5a9361c90
commit 730cb82226
2 changed files with 19 additions and 0 deletions

View File

@ -121,6 +121,15 @@ OpFoldResult AddOp::fold(ArrayRef<Attribute> operands) {
if (getLhs() == sub.getRhs())
return sub.getLhs();
// complex.add(a, complex.constant<0.0, 0.0>) -> a
if (auto constantOp = getRhs().getDefiningOp<ConstantOp>()) {
auto arrayAttr = constantOp.getValue();
if (arrayAttr[0].cast<FloatAttr>().getValue().isZero() &&
arrayAttr[1].cast<FloatAttr>().getValue().isZero()) {
return getLhs();
}
}
return {};
}

View File

@ -124,3 +124,13 @@ func.func @complex_conj_conj() -> complex<f32> {
%conj2 = complex.conj %conj1 : complex<f32>
return %conj2 : complex<f32>
}
// CHECK-LABEL: func @complex_add_zero
func.func @complex_add_zero() -> complex<f32> {
%complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex<f32>
%complex2 = complex.constant [0.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>
%add = complex.add %complex1, %complex2 : complex<f32>
return %add : complex<f32>
}