[flang-commits] [flang] [flang] Use saturated intrinsic for floating point conversions (PR #130686)
Kiran Chandramohan via flang-commits
flang-commits at lists.llvm.org
Tue Mar 11 04:17:33 PDT 2025
================
@@ -835,10 +835,20 @@ struct ConvertOpConversion : public fir::FIROpConversion<fir::ConvertOp> {
return mlir::success();
}
if (mlir::isa<mlir::IntegerType>(toTy)) {
- if (toTy.isUnsignedInteger())
- rewriter.replaceOpWithNewOp<mlir::LLVM::FPToUIOp>(convert, toTy, op0);
- else
- rewriter.replaceOpWithNewOp<mlir::LLVM::FPToSIOp>(convert, toTy, op0);
+ // NOTE: We are checking the fir type here because toTy is an LLVM type
+ // which is signless, and we need to use the intrinsic that matches the
+ // sign of the output in fir.
+ if (toFirTy.isUnsignedInteger()) {
+ auto intrinsicName =
+ mlir::StringAttr::get(convert.getContext(), "llvm.fptoui.sat");
+ rewriter.replaceOpWithNewOp<mlir::LLVM::CallIntrinsicOp>(
+ convert, toTy, intrinsicName, op0);
+ } else {
+ auto intrinsicName =
+ mlir::StringAttr::get(convert.getContext(), "llvm.fptosi.sat");
+ rewriter.replaceOpWithNewOp<mlir::LLVM::CallIntrinsicOp>(
+ convert, toTy, intrinsicName, op0);
+ }
----------------
kiranchandramohan wrote:
Would you know what these saturation intrinsics get lowered to? And is there a big difference in performance? Would it be possible to use the saturation intrinsic only when necessary? Or can that not be determined at compile time?
https://github.com/llvm/llvm-project/pull/130686
More information about the flang-commits
mailing list