[Mlir-commits] [mlir] [mlir][math] Add constant folding for `math.fpowi` (PR #193761)

Longsheng Mou llvmlistbot at llvm.org
Thu Apr 23 08:00:35 PDT 2026


================
@@ -776,6 +776,34 @@ OpFoldResult math::TruncOp::fold(FoldAdaptor adaptor) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// FPowIOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::FPowIOp::fold(FoldAdaptor adaptor) {
+  return constFoldBinaryOpConditional<FloatAttr, IntegerAttr>(
+      adaptor.getOperands(),
+      [](const APFloat &base, const APInt &exp) -> std::optional<APFloat> {
+        const llvm::fltSemantics &sem = base.getSemantics();
+        // Fold when the exponent is exactly representable in the
+        // floating-point type of the base.
+        APFloat fExp(sem);
+        if (fExp.convertFromAPInt(exp, /*isSigned=*/true,
+                                  APFloat::rmNearestTiesToEven) !=
+            APFloat::opOK)
+          return {};
+
+        switch (APFloat::getSizeInBits(sem)) {
+        case 64:
+          return APFloat(pow(base.convertToDouble(), fExp.convertToDouble()));
+        case 32:
----------------
CoTinker wrote:

I referred to the code style in the Math folder, such as math::TruncOp::fold.

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


More information about the Mlir-commits mailing list