Skip to content

Commit

Permalink
TosaToLinalg: Fix unsigned tosa.clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
mgehre-amd committed Apr 8, 2024
1 parent 93ba754 commit 00dc051
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/Tosa/Utils/ConversionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Value clampFloatHelper(Location loc, Value arg, Value min, Value max,
// Takes the parameters for a clamp and turns it into a series of ops for
// integer inputs.
Value clampIntHelper(Location loc, Value arg, Value min, Value max,
OpBuilder &rewriter);
OpBuilder &rewriter, bool isUnsigned = false);

// Determines whether the integer value falls witin the range of integer type.
bool validIntegerRange(IntegerType ty, int64_t value);
Expand Down
14 changes: 10 additions & 4 deletions mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,15 @@ static Value createLinalgBodyCalculationForElementwiseOp(
cast<IntegerAttr>(op->getAttr("max_int")).getValue().getSExtValue();

if (intTy.isUnsignedInteger()) {
if (intTy.getIntOrFloatBitWidth() > 63) {
(void)rewriter.notifyMatchFailure(
op, "support for larger integers it not implemented");
return {};
}
min = std::max(min, (int64_t)0);
max = std::min(
max,
APInt::getMaxValue(intTy.getIntOrFloatBitWidth()).getSExtValue());
max = std::min(max,
(int64_t)APInt::getMaxValue(intTy.getIntOrFloatBitWidth())
.getZExtValue());
} else {
min =
std::max(min, APInt::getSignedMinValue(intTy.getIntOrFloatBitWidth())
Expand All @@ -426,7 +431,8 @@ static Value createLinalgBodyCalculationForElementwiseOp(
loc, min, intTy.getIntOrFloatBitWidth());
auto maxVal = rewriter.create<arith::ConstantIntOp>(
loc, max, intTy.getIntOrFloatBitWidth());
return clampIntHelper(loc, args[0], minVal, maxVal, rewriter);
return clampIntHelper(loc, args[0], minVal, maxVal, rewriter,
intTy.isUnsignedInteger());
}

// tosa::SigmoidOp
Expand Down
9 changes: 5 additions & 4 deletions mlir/lib/Dialect/Tosa/Utils/ConversionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ Value mlir::tosa::clampFloatHelper(Location loc, Value arg, Value min,
}

Value mlir::tosa::clampIntHelper(Location loc, Value arg, Value min, Value max,
OpBuilder &rewriter) {
OpBuilder &rewriter, bool isUnsigned) {
auto predicate =
isUnsigned ? arith::CmpIPredicate::ult : arith::CmpIPredicate::slt;
auto smallerThanMin =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, arg, min);
rewriter.create<arith::CmpIOp>(loc, predicate, arg, min);
auto minOrArg =
rewriter.create<arith::SelectOp>(loc, smallerThanMin, min, arg);
auto largerThanMax =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, max, arg);
auto largerThanMax = rewriter.create<arith::CmpIOp>(loc, predicate, max, arg);
return rewriter.create<arith::SelectOp>(loc, largerThanMax, max, minOrArg);
}

Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,15 @@ func.func @test_simple_i32(%arg0: tensor<1xi32>, %arg1: tensor<1xui32>) -> () {
// CHECK: select
%19 = tosa.clamp %0 {min_int = 1 : i64, max_int = 5 : i64, min_fp = 1.0 : f32, max_fp = 5.0 : f32} : (tensor<1xi32>) -> tensor<1xi32>

// CHECK: linalg.generic
// CHECK: bb0(%[[IN:.*]]: i32,
// CHECK-DAG: %[[LB:.*]] = arith.constant 0 : i32
// CHECK-DAG: %[[UB:.*]] = arith.constant 5 : i32
// CHECK-DAG: arith.cmpi ult, %[[IN]], %[[LB]]
// CHECK-DAG: arith.cmpi ult, %[[UB]], %[[IN]]
// CHECK: select
%u19 = tosa.clamp %arg1 {min_int = -1 : i64, max_int = 5 : i64, min_fp = 1.0 : f32, max_fp = 5.0 : f32} : (tensor<1xui32>) -> tensor<1xui32>

// CHECK: linalg.generic
// CHECK: arith.trunci
%20 = tosa.cast %0 : (tensor<1xi32>) -> tensor<1xi16>
Expand Down

0 comments on commit 00dc051

Please sign in to comment.