[Mlir-commits] [mlir] 0180709 - [mlir][complex] Canonicalization for consecutive complex.neg

Alexander Belyaev llvmlistbot at llvm.org
Wed Jun 29 02:11:57 PDT 2022


Author: lewuathe
Date: 2022-06-29T11:11:40+02:00
New Revision: 01807095902d78313ef454896f34c801513b1d83

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
index 5ca24398843cb..b76276c726ee5 100644
--- a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
+++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
@@ -365,6 +365,8 @@ def NegOp : ComplexUnaryOp<"neg", [SameOperandsAndResultType]> {
   }];
 
   let results = (outs Complex<AnyFloat>:$result);
+
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index 0390a00cf6844..00dbb564481ee 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -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
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 8bca3232774a1..093e92f6a0308 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -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>
 }
\ No newline at end of file


        


More information about the Mlir-commits mailing list