[Mlir-commits] [mlir] [mlir][math] Expand powfI operation for constant power operand. (PR #87081)
Jakub Kuderski
llvmlistbot at llvm.org
Sat Mar 30 18:17:27 PDT 2024
================
@@ -202,6 +202,70 @@ static LogicalResult convertCeilOp(math::CeilOp op, PatternRewriter &rewriter) {
rewriter.replaceOp(op, ret);
return success();
}
+
+// Convert `math.fpowi` to a series of `arith.mulf` operations.
+// If the power is negative, we divide one by the result.
+static LogicalResult convertFPowICstOp(math::FPowIOp op,
+ PatternRewriter &rewriter) {
+ ImplicitLocOpBuilder b(op->getLoc(), rewriter);
+ Value base = op.getOperand(0);
+ Value power = op.getOperand(1);
+ Type baseType = base.getType();
+ Value tempBase = op.getOperand(0);
+
+ Attribute cstAttr;
+ if (!matchPattern(power, m_Constant(&cstAttr)))
+ return failure();
+
+ int64_t powerInt;
+
+ // Check for Splat or Integer Attrs.
+ if (auto splatAttr = dyn_cast<SplatElementsAttr>(cstAttr)) {
+ powerInt = splatAttr.getSplatValue<int64_t>();
+ } else if (auto iAttr = dyn_cast<IntegerAttr>(cstAttr)) {
+ powerInt = iAttr.getInt();
+ } else {
+ return failure();
+ }
+
+ bool isNegative = powerInt < 0;
+ int64_t absPower = std::abs(powerInt);
+ Value one = createFloatConst(op->getLoc(), baseType, 1.00, rewriter);
+ Value res = createFloatConst(op->getLoc(), baseType, 1.00, rewriter);
+
+ Value zero = createFloatConst(op->getLoc(), baseType, 0.00, rewriter);
+ Value negZero = createFloatConst(op->getLoc(), baseType, -0.00, rewriter);
+ Value posInfinity =
+ createFloatConst(op->getLoc(), baseType,
+ std::numeric_limits<double_t>::infinity(), rewriter);
+ Value negInfinity =
+ createFloatConst(op->getLoc(), baseType,
+ -std::numeric_limits<double_t>::infinity(), rewriter);
----------------
kuhar wrote:
Some float types don't have infinity, see https://github.com/llvm/llvm-project/blob/a67b9326cd0448072a1192951f12f3927f31af8c/llvm/include/llvm/ADT/APFloat.h#L151-L194 and https://github.com/llvm/llvm-project/blob/a67b9326cd0448072a1192951f12f3927f31af8c/llvm/lib/Support/APFloat.cpp#L127-L143 .
It would be safer to construct an APFloat with the correct semantics for this float type and use `APFloat::makeInf` that handles this: https://github.com/llvm/llvm-project/blob/a67b9326cd0448072a1192951f12f3927f31af8c/llvm/lib/Support/APFloat.cpp#L4475-L4485 .
We should also have a test that covers this.
https://github.com/llvm/llvm-project/pull/87081
More information about the Mlir-commits
mailing list