Skip to content
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
48 changes: 21 additions & 27 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,14 @@ struct ClampIsNoOp : public OpRewritePattern<tosa::ClampOp> {
auto inputType = llvm::dyn_cast<RankedTensorType>(op.getInput().getType());
auto inputElementType = inputType.getElementType();

if (!inputType.hasStaticShape()) {
return failure();
}

if (isa<FloatType>(inputElementType)) {
// Unlike integer types, floating point types can represent infinity.
auto minClamp =
const auto minClamp =
llvm::cast<mlir::FloatAttr>(op.getMinValAttr()).getValue();
auto maxClamp =
const auto maxClamp =
llvm::cast<mlir::FloatAttr>(op.getMaxValAttr()).getValue();
bool isMin = minClamp.isNegInfinity();
bool isMax = maxClamp.isInfinity();
const bool isMin = minClamp.isNegInfinity();
const bool isMax = maxClamp.isInfinity();

if (isMin && isMax) {
rewriter.replaceOp(op, input);
Expand All @@ -472,18 +468,19 @@ struct ClampIsNoOp : public OpRewritePattern<tosa::ClampOp> {
return failure();
}

if (inputElementType.isUnsignedInteger()) {
int64_t minClamp =
llvm::cast<mlir::IntegerAttr>(op.getMinValAttr()).getUInt();
int64_t maxClamp =
llvm::cast<mlir::IntegerAttr>(op.getMaxValAttr()).getUInt();
// i1 types are boolean in TOSA
const bool isBoolean = inputElementType.isInteger(1);
if (inputElementType.isUnsignedInteger() || isBoolean) {
const int64_t minClamp = llvm::cast<mlir::IntegerAttr>(op.getMinValAttr())
.getValue()
.getZExtValue();
const int64_t maxClamp = llvm::cast<mlir::IntegerAttr>(op.getMaxValAttr())
.getValue()
.getZExtValue();

int64_t intMin =
APInt::getMinValue(inputElementType.getIntOrFloatBitWidth())
.getZExtValue();
int64_t intMax =
APInt::getMaxValue(inputElementType.getIntOrFloatBitWidth())
.getZExtValue();
const unsigned bitWidth = inputElementType.getIntOrFloatBitWidth();
const int64_t intMin = APInt::getMinValue(bitWidth).getZExtValue();
const int64_t intMax = APInt::getMaxValue(bitWidth).getZExtValue();

if (minClamp <= intMin && maxClamp >= intMax) {
rewriter.replaceOp(op, input);
Expand All @@ -493,17 +490,14 @@ struct ClampIsNoOp : public OpRewritePattern<tosa::ClampOp> {
}

if (llvm::isa<IntegerType>(inputElementType)) {
int64_t minClamp =
const int64_t minClamp =
llvm::cast<mlir::IntegerAttr>(op.getMinValAttr()).getInt();
int64_t maxClamp =
const int64_t maxClamp =
llvm::cast<mlir::IntegerAttr>(op.getMaxValAttr()).getInt();

int64_t intMin =
APInt::getSignedMinValue(inputElementType.getIntOrFloatBitWidth())
.getSExtValue();
int64_t intMax =
APInt::getSignedMaxValue(inputElementType.getIntOrFloatBitWidth())
.getSExtValue();
const unsigned bitWidth = inputElementType.getIntOrFloatBitWidth();
const int64_t intMin = APInt::getSignedMinValue(bitWidth).getSExtValue();
const int64_t intMax = APInt::getSignedMaxValue(bitWidth).getSExtValue();

if (minClamp <= intMin && maxClamp >= intMax) {
rewriter.replaceOp(op, input);
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ func.func @clamp_f32_is_noop(%arg0: tensor<4xf32>) -> tensor<4xf32> {

// -----

// CHECK-LABEL: @clamp_boolean_is_noop
func.func @clamp_boolean_is_noop(%arg0: tensor<4xi1>) -> tensor<4xi1> {
// CHECK: return %arg0
// CHECK-NOT: tosa.clamp
%0 = tosa.clamp %arg0 {min_val = false, max_val = true} : (tensor<4xi1>) -> tensor<4xi1>
return %0 : tensor<4xi1>
}

// -----

// CHECK-LABEL: @clamp_boolean_dynamic_is_noop
func.func @clamp_boolean_dynamic_is_noop(%arg0: tensor<?xi1>) -> tensor<?xi1> {
// CHECK: return %arg0
// CHECK-NOT: tosa.clamp
%0 = tosa.clamp %arg0 {min_val = false, max_val = true} : (tensor<?xi1>) -> tensor<?xi1>
return %0 : tensor<?xi1>
}

// -----

// CHECK-LABEL: @clamp_int8_is_noop
func.func @clamp_int8_is_noop(%arg0: tensor<4xi8>) -> tensor<4xi8> {
// CHECK: return %arg0
Expand Down