[Mlir-commits] [mlir] [mlir][math] `powf(a, b)` drop support when a < 0 (PR #126338)

Han-Chung Wang llvmlistbot at llvm.org
Wed Feb 12 08:41:12 PST 2025


================
@@ -311,40 +311,83 @@ static LogicalResult convertFPowIOp(math::FPowIOp op,
   return success();
 }
 
-// Converts  Powf(float a, float b) (meaning a^b) to exp^(b * ln(a))
+// Convert Powf(float a, float b) for special cases when b is constant:
+// when b == 0, or |b| == 0.5, 1.0, or 2.0.
+static LogicalResult convertSpecialPowfOp(math::PowFOp op,
+                                          PatternRewriter &rewriter) {
+  ImplicitLocOpBuilder b(op->getLoc(), rewriter);
+  Value operandA = op.getOperand(0);
+  Value operandB = op.getOperand(1);
+  auto opType = operandA.getType();
+  auto baseType = operandB.getType();
+
+  auto &sem = dyn_cast<mlir::FloatType>(getElementTypeOrSelf(baseType))
+                  .getFloatSemantics();
+
+  auto valueB = APFloat(sem);
+  if (!matchPattern(operandB, m_ConstantFloat(&valueB))) {
+    // Not a constant, return failure
+    return failure();
+  }
+  float floatValueB = valueB.convertToFloat();
----------------
hanhanW wrote:

I'm not very familiar with it either. After skimming through the doc, I think we can use `isExactlyValue` method. I think we do not have precision issue for `0`, `+-1`, `+-0.5`, `+-2` numbers. 

https://github.com/llvm/llvm-project/blob/bee9664970d51df3f4e1d298d1bcb95bba364e17/llvm/include/llvm/ADT/APFloat.h#L1420-L1433

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


More information about the Mlir-commits mailing list