[Mlir-commits] [mlir] [mlir][arith] wide integer emulation support for fpto*i ops (PR #132375)
Jakub Kuderski
llvmlistbot at llvm.org
Fri Mar 21 07:48:48 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 */
+ auto inFp = adaptor.getIn();
+ auto fpTy = inFp.getType();
+ auto fpElemTy = getElementTypeOrSelf(fpTy);
+
+ 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.getFloatAttr(fpElemTy, 0.0);
+
+ if (auto vecTy = dyn_cast<VectorType>(fpTy))
+ zeroAttr = SplatElementsAttr::get(vecTy, zeroAttr);
+
+ Value zeroCst = rewriter.create<arith::ConstantOp>(loc, zeroAttr);
----------------
kuhar wrote:
I think there should be a helper like `rewriter.getZeroAttr` and/or `rewriter.create<arith::ConstantFloatOp>(...)`
https://github.com/llvm/llvm-project/pull/132375
More information about the Mlir-commits
mailing list