[flang-commits] [flang] [mlir] [MLIR] Add cpow support in ComplexToROCDLLibraryCalls (PR #153183)

Krzysztof Drewniak via flang-commits flang-commits at lists.llvm.org
Wed Aug 13 15:52:43 PDT 2025


================
@@ -56,10 +56,43 @@ struct ComplexOpToROCDLLibraryCalls : public OpRewritePattern<Op> {
 private:
   std::string funcName;
 };
+
+// Rewrite complex.pow(z, w) -> complex.exp(w * complex.log(z))
+struct PowOpToROCDLLibraryCalls : public OpRewritePattern<complex::PowOp> {
+  using OpRewritePattern<complex::PowOp>::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(complex::PowOp op,
+                                PatternRewriter &rewriter) const final {
+    auto loc = op.getLoc();
+    if (auto constOp = op.getRhs().getDefiningOp<complex::ConstantOp>()) {
+      ArrayAttr value = constOp.getValue();
+      if (value.size() == 2) {
+        auto real = dyn_cast<FloatAttr>(value[0]);
+        auto imag = dyn_cast<FloatAttr>(value[1]);
+        if (real && imag && imag.getValue().isZero())
+          for (int i = 2; i <= 8; ++i)
----------------
krzysz00 wrote:

Ok, higher-level question: is this "integer in [2, 8] => multiplication" not an optimization that could be performed on any target?

Or maybe not for [2, 8], but ... this feels like a rewrite that should be on the `complex` dialect itself, where you'd pass in a minimum and maximum "efficient" integer and do the rewrites to a multiply sequence there.

Then, flang would know, for each target, which integers should be expanded.

(For example, I have the strong suspicion that `cpow(x, 2.0+0i) => cmul(x, x)` is a profitable rewrite everywhere)

https://github.com/llvm/llvm-project/pull/153183


More information about the flang-commits mailing list