Skip to content

TOSA: Fold clamp after cast if dynamic range allows #89

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def Tosa_ClampOp : Tosa_ElemWiseUnaryOp<"clamp"> {
);

let hasCanonicalizer = 1;
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
13 changes: 13 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ void ClampOp::getCanonicalizationPatterns(RewritePatternSet &results,
results.add<ClampClampOptimization>(context);
}

OpFoldResult ClampOp::fold(FoldAdaptor adaptor) {
// TODO: This can generalize to any cast (or other operation)
// where the output values are within a computable range.
if (auto cast = llvm::dyn_cast_or_null<CastOp>(getInput().getDefiningOp())) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment on why this fold makes sense, something about the range being wider than the type width we just casted from.

if (cast.getType().getElementType().isF32() &&
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we allow all of {fp16_t, bf16_t, fp32_t}?

cast.getInput().getType().getElementType().isInteger(8) &&
getMinFp().convertToFloat() <= -128.0 &&
getMaxFp().convertToFloat() >= 127.0)
return getInput();
}
return {};
}

struct ConcatSliceOptimization : public OpRewritePattern<tosa::SliceOp> {
using OpRewritePattern<tosa::SliceOp>::OpRewritePattern;

Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ func.func @clamp_maximum_f32(%arg0: tensor<4xf32>) -> tensor<4xf32> {
return %1 : tensor<4xf32>
}

// CHECK-LABEL: @cast_clamp
func.func @cast_clamp(%arg0: tensor<4xi8>) -> tensor<4xf32> {
// CHECK-NOT: tosa.clamp
%0 = "tosa.cast"(%arg0) : (tensor<4xi8>) -> tensor<4xf32>
%1 = "tosa.clamp"(%0) {min_fp = -129.0 : f32, max_fp = 200.0 : f32, min_int = -2 : i64, max_int = 4 : i64} : (tensor<4xf32>) -> tensor<4xf32>
return %1 : tensor<4xf32>
}

// CHECK-LABEL: @cast_clamp2
func.func @cast_clamp2(%arg0: tensor<4xi8>) -> tensor<4xf32> {
// CHECK-NOT: tosa.clamp
%0 = "tosa.cast"(%arg0) : (tensor<4xi8>) -> tensor<4xf32>
%1 = "tosa.clamp"(%0) {min_fp = -128.0 : f32, max_fp = 127.0 : f32, min_int = -2 : i64, max_int = 4 : i64} : (tensor<4xf32>) -> tensor<4xf32>
return %1 : tensor<4xf32>
}

// CHECK-LABEL: @cast_clamp_negative
func.func @cast_clamp_negative(%arg0: tensor<4xi8>) -> tensor<4xf32> {
// CHECK: tosa.clamp
%0 = "tosa.cast"(%arg0) : (tensor<4xi8>) -> tensor<4xf32>
%1 = "tosa.clamp"(%0) {min_fp = -5.0 : f32, max_fp = 3.0 : f32, min_int = -2 : i64, max_int = 4 : i64} : (tensor<4xf32>) -> tensor<4xf32>
return %1 : tensor<4xf32>
}


// CHECK-LABEL: @concat_fold_zero
func.func @concat_fold_zero(%arg0: tensor<?x0xf32>, %arg1: tensor<?x1xf32>, %arg2: tensor<?x2xf32>) -> tensor<?x3xf32> {
// CHECK: "tosa.concat"(%arg1, %arg2) <{axis = 1 : i64}>
Expand Down