Skip to content
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

TosaToLinalg: Fix unsigned tosa.clamp #155

Merged
merged 2 commits into from
Apr 12, 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
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 64-bit or larger integers is 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());
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved
} 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-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ func.func @tensor_with_unknown_rank(%arg0: tensor<*xi8>) -> tensor<*xi8> {
%0 = "tosa.abs"(%arg0) : (tensor<*xi8>) -> tensor<*xi8>
return %0 : tensor<*xi8>
}

// -----

// CHECK-LABEL: @clamp_on_large_int
func.func @clamp_on_large_int(%arg0: tensor<1xui64>) -> tensor<1xui64> {
// expected-error@+1 {{failed to legalize operation 'tosa.clamp'}}
%0 = tosa.clamp %arg0 {min_int = -1 : i64, max_int = 5 : i64, min_fp = 1.0 : f32, max_fp = 5.0 : f32} : (tensor<1xui64>) -> tensor<1xui64>
return %0 : tensor<1xui64>
}
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
Loading