@@ -469,7 +469,8 @@ createLinalgBodyCalculationForElementwiseOp(Operation *op, ValueRange args,
469
469
args.front (), zero);
470
470
}
471
471
472
- if (arith::FPToSIOp::areCastCompatible (srcTy, dstTy)) {
472
+ if (dstTy.isSignlessInteger () &&
473
+ arith::FPToSIOp::areCastCompatible (srcTy, dstTy)) {
473
474
auto intMin = rewriter.create <arith::ConstantOp>(
474
475
loc, rewriter.getF32FloatAttr (
475
476
APInt::getSignedMinValue (dstTy.getIntOrFloatBitWidth ())
@@ -484,16 +485,33 @@ createLinalgBodyCalculationForElementwiseOp(Operation *op, ValueRange args,
484
485
485
486
auto clamped = clampFloatHelper (loc, rounded, intMin, intMax, rewriter);
486
487
487
- if (dstTy.isUnsignedInteger ()) {
488
- auto cast = rewriter.create <arith::FPToUIOp>(
489
- loc, rewriter.getIntegerType (dstTy.getIntOrFloatBitWidth ()), clamped);
490
- return rewriter.create <UnrealizedConversionCastOp>(
491
- loc, dstTy, cast->getResult (0 )).getResult (0 );
492
- }
493
-
494
488
return rewriter.create <arith::FPToSIOp>(loc, dstTy, clamped);
495
489
}
496
490
491
+ if (dstTy.isUnsignedInteger () &&
492
+ arith::FPToUIOp::areCastCompatible (srcTy, dstTy)) {
493
+ auto intMin = rewriter.create <arith::ConstantOp>(
494
+ loc, rewriter.getF32FloatAttr (
495
+ APInt::getMinValue (dstTy.getIntOrFloatBitWidth ())
496
+ .getZExtValue ()));
497
+
498
+ auto intMax = rewriter.create <arith::ConstantOp>(
499
+ loc, rewriter.getF32FloatAttr (
500
+ APInt::getMaxValue (dstTy.getIntOrFloatBitWidth ())
501
+ .getZExtValue ()));
502
+
503
+ auto rounded = rewriter.create <math::RoundEvenOp>(loc, args[0 ]);
504
+
505
+ auto clamped = clampFloatHelper (loc, rounded, intMin, intMax, rewriter);
506
+
507
+ auto cast = rewriter.create <arith::FPToUIOp>(
508
+ loc, rewriter.getIntegerType (dstTy.getIntOrFloatBitWidth ()), clamped);
509
+ // arith is signless, so temporarily cast back to being unsigned.
510
+ return rewriter
511
+ .create <UnrealizedConversionCastOp>(loc, dstTy, cast->getResult (0 ))
512
+ .getResult (0 );
513
+ }
514
+
497
515
// Casting to boolean, integers need to only be checked as not-equal to
498
516
// zero.
499
517
if (srcTy.isa <IntegerType>() && dstTy.isInteger (1 )) {
0 commit comments