Skip to content

mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp: cast-to-iN(cast-t… #21

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 1 commit into from
Apr 14, 2023
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
12 changes: 12 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,18 @@ OpFoldResult CastOp::fold(ArrayRef<Attribute> operands) {
if (getInput().getType() == getType())
return getInput();

// cast-to-iN(cast-to-iM(x)) -> cast-to-iN(x) when N <= M
if (auto cast = getInput().getDefiningOp<CastOp>()) {
auto intermediateElTy = cast.getType().getElementType().dyn_cast<IntegerType>();
auto finalElTy = getType().getElementType().dyn_cast<IntegerType>();
if (intermediateElTy && finalElTy &&
intermediateElTy.getSignedness() == finalElTy.getSignedness() &&
intermediateElTy.getWidth() >= finalElTy.getWidth()) {
getInputMutable().assign(cast.getInput());
return getResult();
}
}

auto operand = operands[0].dyn_cast_or_null<ElementsAttr>();
if (!operand)
return {};
Expand Down
26 changes: 26 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ func.func @cast_nofold(%arg0: tensor<?x1xf32>) -> tensor<?x1xi32> {
return %0 : tensor<?x1xi32>
}

// CHECK-LABEL: @cast_fold_double
func.func @cast_fold_double(%arg0: tensor<?x1xf32>) -> tensor<?x1xi8> {
// CHECK: "tosa.cast"{{.*}} (tensor<?x1xf32>) -> tensor<?x1xi8>
%0 = "tosa.cast"(%arg0) : (tensor<?x1xf32>) -> tensor<?x1xi16>
%1 = "tosa.cast"(%0) : (tensor<?x1xi16>) -> tensor<?x1xi8>
return %1 : tensor<?x1xi8>
}

// CHECK-LABEL: @cast_no_fold_double1
func.func @cast_no_fold_double1(%arg0: tensor<?x1xf32>) -> tensor<?x1xi8> {
// CHECK: "tosa.cast"{{.*}} (tensor<?x1xf32>) -> tensor<?x1xui16>
// CHECK: "tosa.cast"{{.*}} (tensor<?x1xui16>) -> tensor<?x1xi8>
%0 = "tosa.cast"(%arg0) : (tensor<?x1xf32>) -> tensor<?x1xui16>
%1 = "tosa.cast"(%0) : (tensor<?x1xui16>) -> tensor<?x1xi8>
return %1 : tensor<?x1xi8>
}

// CHECK-LABEL: @cast_no_fold_double2
func.func @cast_no_fold_double2(%arg0: tensor<?x1xf32>) -> tensor<?x1xi16> {
// CHECK: "tosa.cast"{{.*}} (tensor<?x1xf32>) -> tensor<?x1xi8>
// CHECK: "tosa.cast"{{.*}} (tensor<?x1xi8>) -> tensor<?x1xi16>
%0 = "tosa.cast"(%arg0) : (tensor<?x1xf32>) -> tensor<?x1xi8>
%1 = "tosa.cast"(%0) : (tensor<?x1xi8>) -> tensor<?x1xi16>
return %1 : tensor<?x1xi16>
}

// CHECK-LABEL: @clamp_not_noop
func.func @clamp_not_noop(%arg0: tensor<4xi32>) -> tensor<4xi32> {
// CHECK: "tosa.clamp"
Expand Down