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 @@ -150,6 +150,15 @@ if (getRhs() == add.getRhs()) return add.getLhs(); + // complex.sub(a, complex.constant<0.0, 0.0>) -> a + if (auto constantOp = getRhs().getDefiningOp()) { + auto arrayAttr = constantOp.getValue(); + if (arrayAttr[0].cast().getValue().isZero() && + arrayAttr[1].cast().getValue().isZero()) { + return getLhs(); + } + } + return {}; } 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 @@ -144,4 +144,14 @@ %add = complex.add %complex1, %complex2 : complex %sub = complex.sub %add, %complex2 : complex return %sub : complex +} + +// CHECK-LABEL: func @complex_sub_zero +func.func @complex_sub_zero() -> complex { + %complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex + %complex2 = complex.constant [0.0 : f32, 0.0 : f32] : complex + // CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex + // CHECK-NEXT: return %[[CPLX:.*]] : complex + %sub = complex.sub %complex1, %complex2 : complex + return %sub : complex } \ No newline at end of file