[Mlir-commits] [mlir] 730cb82 - [mlir][complex] Canonicalize complex.add zero

Alexander Belyaev llvmlistbot at llvm.org
Fri Jul 29 09:04:02 PDT 2022


Author: lewuathe
Date: 2022-07-29T18:03:45+02:00
New Revision: 730cb822261f2d02778054db03a81543e0942a15

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

LOG: [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

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 d642a09f6a506..445d391e65b7a 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -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 {};
 }
 

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 21ad95af39987..d57dc693d1604 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -123,4 +123,14 @@ func.func @complex_conj_conj() -> complex<f32> {
   %conj1 = complex.conj %complex1 : 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>
 }
\ No newline at end of file


        


More information about the Mlir-commits mailing list