[Mlir-commits] [mlir] [mlir][arith] wide integer emulation support for fpto*i ops (PR #132375)
Jakub Kuderski
llvmlistbot at llvm.org
Wed Mar 26 08:55:36 PDT 2025
================
@@ -974,6 +974,126 @@ struct ConvertUIToFP final : OpConversionPattern<arith::UIToFPOp> {
}
};
+//===----------------------------------------------------------------------===//
+// ConvertFPToSI
+//===----------------------------------------------------------------------===//
+
+struct ConvertFPToSI final : OpConversionPattern<arith::FPToSIOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(arith::FPToSIOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ Location loc = op.getLoc();
+ // Get the input float type.
+ Value inFp = adaptor.getIn();
+ Type fpTy = inFp.getType();
+
+ Type intTy = op.getType();
+ unsigned oldBitWidth = getElementTypeOrSelf(intTy).getIntOrFloatBitWidth();
+
+ auto newTy = getTypeConverter()->convertType<VectorType>(intTy);
+ if (!newTy)
+ return rewriter.notifyMatchFailure(
+ loc, llvm::formatv("unsupported type: {0}", intTy));
+
+ // Work on the absolute value and then convert the result to signed integer.
+ // Defer absolute value to fptoui. If minSInt < fp < maxSInt, i.e.
+ // if the fp is representable in signed i2N, emits the correct result.
+ // Else, the result is UB.
+
+ TypedAttr zeroAttr = rewriter.getZeroAttr(fpTy);
+ Value zeroCst = rewriter.create<arith::ConstantOp>(loc, zeroAttr);
+
+ Value oneCst = createScalarOrSplatConstant(rewriter, loc, intTy, 1);
+ Value allOnesCst = createScalarOrSplatConstant(
+ rewriter, loc, intTy, APInt::getAllOnes(oldBitWidth));
+
+ // Get the absolute value.
+ Value isNeg = rewriter.create<arith::CmpFOp>(loc, arith::CmpFPredicate::OLT,
+ inFp, zeroCst);
+ Value negInFp = rewriter.create<arith::NegFOp>(loc, inFp);
+
+ Value absVal = rewriter.create<arith::SelectOp>(loc, isNeg, negInFp, inFp);
+
+ // Defer the absolute value to fptoui.
+ Value res = rewriter.create<arith::FPToUIOp>(loc, intTy, absVal);
+
+ // Negate the value if < 0 .
+ Value bitwiseNeg = rewriter.create<arith::XOrIOp>(loc, res, allOnesCst);
----------------
kuhar wrote:
Since this is a second time for this to come up, I think we should add subi emulation and use that in both places:
https://github.com/llvm/llvm-project/blob/236f938ef65cc9028801303bb0bd914afca37836/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp#L894-L903
Would you be able to land that in a separate PR and then rebase this one on top?
https://github.com/llvm/llvm-project/pull/132375
More information about the Mlir-commits
mailing list