aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib
diff options
context:
space:
mode:
authorKai Sasaki <lewuathe@gmail.com>2024-03-26 13:50:28 +0900
committerGitHub <noreply@github.com>2024-03-26 13:50:28 +0900
commit08a321e1b7ffdd72154ae808821d20fe511c83b6 (patch)
tree86f675d598c04b060a8823538669f8ba56444f58 /mlir/lib
parent3e046ee0c944b5a55f30b3ce0153791d91dbb134 (diff)
downloadllvm-08a321e1b7ffdd72154ae808821d20fe511c83b6.zip
llvm-08a321e1b7ffdd72154ae808821d20fe511c83b6.tar.gz
llvm-08a321e1b7ffdd72154ae808821d20fe511c83b6.tar.bz2
[mlir][complex] Canonicalize complex.div by one (#85513)
We can canonicalize the complex.div if the divisor is one (real = 1.0, imag = 0.0) with the input number itself. Ref: https://www.cuemath.com/numbers/division-of-complex-numbers/
Diffstat (limited to 'mlir/lib')
-rw-r--r--mlir/lib/Dialect/Complex/IR/ComplexOps.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index 5529dcc..1c81433 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -370,6 +370,32 @@ OpFoldResult MulOp::fold(FoldAdaptor adaptor) {
}
//===----------------------------------------------------------------------===//
+// DivOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult DivOp::fold(FoldAdaptor adaptor) {
+ auto rhs = adaptor.getRhs();
+ if (!rhs)
+ return {};
+
+ ArrayAttr arrayAttr = rhs.dyn_cast<ArrayAttr>();
+ if (!arrayAttr || arrayAttr.size() != 2)
+ return {};
+
+ APFloat real = cast<FloatAttr>(arrayAttr[0]).getValue();
+ APFloat imag = cast<FloatAttr>(arrayAttr[1]).getValue();
+
+ if (!imag.isZero())
+ return {};
+
+ // complex.div(a, complex.constant<1.0, 0.0>) -> a
+ if (real == APFloat(real.getSemantics(), 1))
+ return getLhs();
+
+ return {};
+}
+
+//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//