Skip to content

TOSA: Fold concat where one argument has zero elements #41

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
Jun 12, 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
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,8 @@ def Tosa_ConcatOp : Tosa_Op<"concat", [
Tosa_Tensor:$output
);

let hasFolder = 1;

let hasCanonicalizer = 1;

let extraClassDeclaration = [{
Expand Down
24 changes: 24 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,30 @@ void SliceOp::getCanonicalizationPatterns(RewritePatternSet &results,
// Operator Folders.
//===----------------------------------------------------------------------===//

static bool hasZeroSize(Type ty) {
auto ranked = dyn_cast<RankedTensorType>(ty);
if (!ranked)
return false;
return any_of(ranked.getShape(), [](auto d) { return d == 0; });
}

OpFoldResult ConcatOp::fold(FoldAdaptor adaptor) {
/// Remove operands that have zero elements.
bool changed = false;
for (size_t i = 0; i < getInput1().size(); ) {
auto input = getInput1()[i];
if (hasZeroSize(input.getType())) {
getInput1Mutable().erase(i);
changed = true;
} else {
++i;
}
}
if (changed)
return getResult();
return {};
}

template <typename IntFolder, typename FloatFolder>
DenseElementsAttr binaryFolder(DenseElementsAttr lhs, DenseElementsAttr rhs,
RankedTensorType returnTy) {
Expand Down
7 changes: 7 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func.func @clamp_twice_is_single_clamp(%arg0: tensor<4xi8>) -> tensor<4xi8> {
return %1 : tensor<4xi8>
}

// 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}>
%0 = "tosa.concat"(%arg0, %arg1, %arg2) {axis = 1 : i64}: (tensor<?x0xf32>, tensor<?x1xf32>, tensor<?x2xf32>) -> tensor<?x3xf32>
return %0 : tensor<?x3xf32>
}

// CHECK-LABEL: @concat_fold
func.func @concat_fold(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
// CHECK: return %arg0
Expand Down