Skip to content

Fix TOSA FP16->INT16 CAST lowering #79299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,23 +480,88 @@ createLinalgBodyCalculationForElementwiseOp(Operation *op, ValueRange args,
}

if (arith::FPToSIOp::areCastCompatible(srcTy, dstTy)) {
auto intMin = rewriter.create<arith::ConstantOp>(
auto rounded = rewriter.create<math::RoundEvenOp>(loc, args[0]);

const auto &fltSemantics = cast<FloatType>(srcTy).getFloatSemantics();
// Check whether neither int min nor int max can be represented in the
// input floating-point type due to too short exponent range.
if (static_cast<int>(dstTy.getIntOrFloatBitWidth()) - 1 >
APFloat::semanticsMaxExponent(fltSemantics)) {
// Use cmp + select to replace infinites by int min / int max. Other
// integral values can be represented in the integer space.
auto conv = rewriter.create<arith::FPToSIOp>(loc, dstTy, rounded);
auto posInf = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(getElementTypeOrSelf(srcTy),
APFloat::getInf(fltSemantics)));
auto negInf = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
APFloat::getInf(fltSemantics, /*Negative=*/true)));
auto overflow = rewriter.create<arith::CmpFOp>(
loc, arith::CmpFPredicate::UEQ, rounded, posInf);
auto underflow = rewriter.create<arith::CmpFOp>(
loc, arith::CmpFPredicate::UEQ, rounded, negInf);
auto intMin = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(
getElementTypeOrSelf(dstTy),
APInt::getSignedMinValue(dstTy.getIntOrFloatBitWidth())));
auto intMax = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(
getElementTypeOrSelf(dstTy),
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())));
auto maxClamped =
rewriter.create<arith::SelectOp>(loc, overflow, intMax, conv);
return rewriter.create<arith::SelectOp>(loc, underflow, intMin,
maxClamped);
}

auto intMinFP = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
APInt::getSignedMinValue(dstTy.getIntOrFloatBitWidth())
.getSExtValue()));

auto intMax = rewriter.create<arith::ConstantOp>(
// Check whether the mantissa has enough bits to represent int max.
if (cast<FloatType>(srcTy).getFPMantissaWidth() >=
dstTy.getIntOrFloatBitWidth() - 1) {
// Int min can also be represented since it is a power of two and thus
// consists of a single leading bit. Therefore we can clamp the input
// in the floating-point domain.

auto intMaxFP = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
.getSExtValue()));

Value clamped =
clampFloatHelper(loc, rounded, intMinFP, intMaxFP, rewriter);
return rewriter.create<arith::FPToSIOp>(loc, dstTy, clamped);
}

// Due to earlier check we know exponant range is big enough to represent
// int min. We can therefore rely on int max + 1 being representable as
// well because it's just int min with a positive sign. So clamp the min
// value and compare against that to select the max int value if needed.
auto intMaxPlusOneFP = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
.getSExtValue()));

auto rounded = rewriter.create<math::RoundEvenOp>(loc, args[0]);

auto clamped = clampFloatHelper(loc, rounded, intMin, intMax, rewriter);
.getSExtValue() +
1));

return rewriter.create<arith::FPToSIOp>(loc, dstTy, clamped);
auto intMax = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(
getElementTypeOrSelf(dstTy),
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())));
auto minClampedFP =
rewriter.create<arith::MaximumFOp>(loc, rounded, intMinFP);
auto minClamped =
rewriter.create<arith::FPToSIOp>(loc, dstTy, minClampedFP);
auto overflow = rewriter.create<arith::CmpFOp>(
loc, arith::CmpFPredicate::UGE, rounded, intMaxPlusOneFP);
return rewriter.create<arith::SelectOp>(loc, overflow, intMax,
minClamped);
}

// Casting to boolean, integers need to only be checked as not-equal to
Expand Down
39 changes: 27 additions & 12 deletions mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,14 @@ func.func @test_simple_f32(%arg0: tensor<1xf32>) -> () {
%19 = tosa.sigmoid %0 : (tensor<1xf32>) -> tensor<1xf32>

// CHECK: linalg.generic
// CHECK: arith.constant -2.14748365E+9
// CHECK: arith.constant 2.14748365E+9
// CHECK: math.roundeven
// CHECK: arith.minimumf
// CHECK: arith.maximumf
// CHECK: arith.fptosi
// CHECK: [[ROUND:%.+]] = math.roundeven {{%.+}} : f32
// CHECK: [[CSTMIN:%.+]] = arith.constant -2.14748365E+9 : f32
// CHECK: [[CSTMAXP1:%.+]] = arith.constant 2.14748365E+9 : f32
// CHECK: [[CSTMAX:%.+]] = arith.constant 2147483647 : i32
// CHECK: [[MAX:%.+]] = arith.maximumf [[ROUND]], [[CSTMIN]] : f32
// CHECK: [[CONV:%.+]] = arith.fptosi [[MAX]] : f32 to i32
// CHECK: [[CMP:%.+]] = arith.cmpf uge, [[ROUND]], [[CSTMAXP1]] : f32
// CHECK: arith.select [[CMP]], [[CSTMAX]], [[CONV]] : i32
%20 = tosa.cast %0 : (tensor<1xf32>) -> tensor<1xi32>

// CHECK: linalg.generic
Expand Down Expand Up @@ -552,13 +554,26 @@ func.func @test_simple_f16(%arg0: tensor<1xf16>) -> () {
%0 = tosa.cast %arg0 : (tensor<1xf16>) -> tensor<1xf32>

// CHECK: linalg.generic
// CHECK: arith.constant -1.280000e+02
// CHECK: arith.constant 1.270000e+02
// CHECK: math.roundeven
// CHECK: arith.minimumf
// CHECK: arith.maximumf
// CHECK: arith.fptosi
// CHECK: [[ROUND:%.+]] = math.roundeven {{%.+}} : f16
// CHECK: [[CSTMIN:%.+]] = arith.constant -1.280000e+02 : f16
// CHECK: [[CSTMAX:%.+]] = arith.constant 1.270000e+02 : f16
// CHECK: [[MIN:%.+]] = arith.minimumf [[ROUND]], [[CSTMAX]] : f16
// CHECK: [[CLAMP:%.+]] = arith.maximumf [[MIN]], [[CSTMIN]] : f16
// CHECK: arith.fptosi [[CLAMP]] : f16 to i8
%1 = "tosa.cast"(%arg0) : (tensor<1xf16>) -> tensor<1xi8>

// CHECK: linalg.generic
// CHECK: [[ROUND:%.+]] = math.roundeven {{%[a-z0-9_]+}} : f16
// CHECK: [[CONV:%.+]] = arith.fptosi [[ROUND]] : f16 to i32
// CHECK: [[POSINF:%.+]] = arith.constant 0x7C00 : f16
// CHECK: [[NEGINF:%.+]] = arith.constant 0xFC00 : f16
// CHECK: [[OVERFLOW:%.+]] = arith.cmpf ueq, [[ROUND]], [[POSINF]] : f16
// CHECK: [[UNDERFLOW:%.+]] = arith.cmpf ueq, [[ROUND]], [[NEGINF]] : f16
// CHECK: [[MININT:%.+]] = arith.constant -2147483648 : i32
// CHECK: [[MAXINT:%.+]] = arith.constant 2147483647 : i32
// CHECK: [[CLAMPPOSINF:%.+]] = arith.select [[OVERFLOW]], [[MAXINT]], [[CONV]] : i32
// CHECK: arith.select [[UNDERFLOW]], [[MININT]], [[CLAMPPOSINF]] : i32
%2 = "tosa.cast"(%arg0) : (tensor<1xf16>) -> tensor<1xi32>
return
}

Expand Down