Skip to content

[mlir][tensor] Fold pack and unpack of empty input tensor #92247

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 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4200,6 +4200,12 @@ LogicalResult PackOp::canonicalize(PackOp packOp, PatternRewriter &rewriter) {
return success();
}

// Fold away packing an empty source tensor.
if (auto emptyTensor = packOp.getSource().getDefiningOp<tensor::EmptyOp>()) {
rewriter.replaceOp(packOp, packOp.getDest());
return success();
}

// Insert tensor.cast ops if static shape inference is available..
SmallVector<int64_t> srcShape, destShape;
if (inferStaticShape(packOp, srcShape, destShape)) {
Expand Down Expand Up @@ -4435,6 +4441,13 @@ LogicalResult UnPackOp::canonicalize(UnPackOp unPackOp,
return success();
}

// Fold away unpacking an empty source tensor.
if (auto emptyTensor =
unPackOp.getSource().getDefiningOp<tensor::EmptyOp>()) {
rewriter.replaceOp(unPackOp, unPackOp.getDest());
return success();
}

// Insert tensor.cast ops if static shape inference is available..
SmallVector<int64_t> srcShape, destShape;
if (inferStaticShape(unPackOp, srcShape, destShape)) {
Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/Tensor/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2486,3 +2486,24 @@ func.func @dim_out_of_bounds() -> vector<7xi32> {
return %16 : vector<7xi32>
}

// -----

// CHECK: func.func @pack_empty(
// CHECK-SAME: %[[T:.+]]: tensor<8x8x32x32xf32>
// CHECK: return %[[T]] : tensor<8x8x32x32xf32>
func.func @pack_empty(%arg0: tensor<8x8x32x32xf32>) -> tensor<8x8x32x32xf32> {
%empty_unpacked = tensor.empty() : tensor<256x256xf32>
%packed = tensor.pack %empty_unpacked inner_dims_pos = [0, 1] inner_tiles = [32, 32] into %arg0 : tensor<256x256xf32> -> tensor<8x8x32x32xf32>
return %packed : tensor<8x8x32x32xf32>
}

// -----

// CHECK: func.func @unpack_empty(
// CHECK-SAME: %[[T:.+]]: tensor<256x256xf32>
// CHECK: return %[[T]] : tensor<256x256xf32>
func.func @unpack_empty(%arg0: tensor<256x256xf32>) -> tensor<256x256xf32> {
%empty_packed = tensor.empty() : tensor<8x8x32x32xf32>
%unpacked = tensor.unpack %empty_packed inner_dims_pos = [0, 1] inner_tiles = [32, 32] into %arg0 : tensor<8x8x32x32xf32> -> tensor<256x256xf32>
return %unpacked : tensor<256x256xf32>
}