[Mlir-commits] [mlir] c9741ba - [mlir][complex] Canonicalize complex.sub zero

Alexander Belyaev llvmlistbot at llvm.org
Wed Nov 9 03:17:15 PST 2022


Author: Kai Sasaki
Date: 2022-11-09T12:12:05+01:00
New Revision: c9741baf531386b0e4cbd91767263f2b16f70588

URL: https://github.com/llvm/llvm-project/commit/c9741baf531386b0e4cbd91767263f2b16f70588
DIFF: https://github.com/llvm/llvm-project/commit/c9741baf531386b0e4cbd91767263f2b16f70588.diff

LOG: [mlir][complex] Canonicalize complex.sub zero

Subtracting zero constant can be fold in no complex.sub operation.

Reviewed By: pifon2a

Differential Revision: https://reviews.llvm.org/D137523

Added: 
    

Modified: 
    mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
    mlir/test/Dialect/Complex/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index 59f58fdcce1b7..44ebf69a65199 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -150,6 +150,15 @@ OpFoldResult SubOp::fold(ArrayRef<Attribute> operands) {
     if (getRhs() == add.getRhs())
       return add.getLhs();
 
+  // complex.sub(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 {};
 }
 

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 54cbf21f3b981..1b85837b18b82 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -144,4 +144,14 @@ func.func @complex_sub_add_lhs() -> complex<f32> {
   %add = complex.add %complex1, %complex2 : complex<f32>
   %sub = complex.sub %add, %complex2 : complex<f32>
   return %sub : complex<f32>
+}
+
+// CHECK-LABEL: func @complex_sub_zero
+func.func @complex_sub_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>
+  %sub = complex.sub %complex1, %complex2 : complex<f32>
+  return %sub : complex<f32>
 }
\ No newline at end of file


        


More information about the Mlir-commits mailing list