[llvm-branch-commits] [mlir] 7a922cd - [mlir][complex] Canonicalize complex.sub zero
Kai Sasaki via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 6 19:34:24 PST 2022
Author: Kai Sasaki
Date: 2022-11-07T12:34:10+09:00
New Revision: 7a922cdc4c39b2e6dbf9e4a30b889d59d6d5fb02
URL: https://github.com/llvm/llvm-project/commit/7a922cdc4c39b2e6dbf9e4a30b889d59d6d5fb02
DIFF: https://github.com/llvm/llvm-project/commit/7a922cdc4c39b2e6dbf9e4a30b889d59d6d5fb02.diff
LOG: [mlir][complex] Canonicalize complex.sub zero
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 llvm-branch-commits
mailing list