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
7 changes: 5 additions & 2 deletions mlir/lib/Dialect/Tensor/Transforms/EmptyOpPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ struct FoldEmptyTensorWithReshapeOp : public OpRewritePattern<ReshapeOp> {
!llvm::hasSingleElement(resultShapes))
return failure();

Attribute encoding;
if (auto tensorTy = dyn_cast<RankedTensorType>(reshapeOp.getResultType()))
encoding = tensorTy.getEncoding();

// Create new tensor.empty op.
// TODO: Do not drop tensor type encoding.
Value emptyTensor =
EmptyOp::create(rewriter, loc, resultShapes[0],
reshapeOp.getResultType().getElementType());
reshapeOp.getResultType().getElementType(), encoding);
if (emptyTensor.getType() != reshapeOp.getResultType()) {
rewriter.replaceOpWithNewOp<tensor::CastOp>(
reshapeOp, reshapeOp.getResultType(), emptyTensor);
Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/Tensor/fold-empty-op.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func.func @empty_reshape_collapse(%arg0 : index) -> tensor<6x5x?xf32> {
// CHECK-NEXT: %[[INIT:.+]] = tensor.empty(%[[D]])
// CHECK-NEXT: return %[[INIT]]

#encoding = #test.tensor_encoding<"encoding">

func.func @empty_expand_encoding() -> tensor<2x3x4x2xf32, #encoding> {
%0 = tensor.empty() : tensor<6x8xf32, #encoding>
%1 = tensor.expand_shape %0 [[0, 1], [2, 3]] output_shape [2, 3, 4, 2] : tensor<6x8xf32, #encoding> into tensor<2x3x4x2xf32, #encoding>
return %1 : tensor<2x3x4x2xf32, #encoding>
}
// CHECK-LABEL: func.func @empty_expand_encoding
// CHECK: %[[INIT:.+]] = tensor.empty() : tensor<2x3x4x2xf32, #test.tensor_encoding<"encoding">>
// CHECK-NEXT: return %[[INIT]]

func.func @empty_collapse_encoding() -> tensor<6x8xf32, #encoding> {
%0 = tensor.empty() : tensor<2x3x4x2xf32, #encoding>
%1 = tensor.collapse_shape %0 [[0, 1], [2, 3]]
: tensor<2x3x4x2xf32, #encoding> into tensor<6x8xf32, #encoding>
return %1 : tensor<6x8xf32, #encoding>
}
// CHECK-LABEL: func.func @empty_collapse_encoding
// CHECK: %[[EMPTY_0:.*]] = tensor.empty() : tensor<6x8xf32, #test.tensor_encoding<"encoding">>
// CHECK-NEXT: return %[[EMPTY_0]]

func.func @fold_empty_tensor_with_slice
(%arg0 : index, %arg1 : index) -> tensor<5x?x20xf32>
{
Expand Down