Skip to content

[mlir][tensor][bufferize] tensor.empty bufferizes to an allocation #68080

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ struct DimOpInterface
}
};

/// Bufferization of tensor.empty. This op does not bufferize, but we need an
/// interface implementation, so that the result of this op is considered
/// "writable" (default impl. of `isWritable`). Results of ops that do not
/// implement `BufferizableOpInterface` are not writable.
/// Bufferization of "tensor.empty". Replace with "bufferization.alloc_tensor".
struct EmptyOpInterface
: public BufferizableOpInterface::ExternalModel<EmptyOpInterface,
tensor::EmptyOp> {
Expand All @@ -268,17 +265,21 @@ struct EmptyOpInterface

LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
auto emptyOp = cast<tensor::EmptyOp>(op);

// Optimization: Fold away the op if it has no uses.
if (op->getUses().empty()) {
rewriter.eraseOp(op);
return success();
}

// tensor.empty ops are used to indicate the shape of a tensor. They have
// no defined contents and cannot be bufferized. However, they can be
// converted to bufferization.alloc_tensor ops, which then bufferize to an
// allocation (--empty-tensor-to-alloc-tensor).
return op->emitOpError("cannot be bufferized, but can be converted to "
"bufferization.alloc_tensor");
// Allocate a tensor. This emits a "bufferization.alloc_tensor" op.
FailureOr<Value> allocTensor = allocateTensorForShapedValue(
rewriter, op->getLoc(), emptyOp.getResult(), options, /*copy=*/false);
if (failed(allocTensor))
return failure();
rewriter.replaceOp(op, *allocTensor);
return success();
}
};

Expand Down
9 changes: 6 additions & 3 deletions mlir/test/Dialect/Tensor/bufferize.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: mlir-opt %s -tensor-bufferize -cse -split-input-file -verify-diagnostics | FileCheck %s
// RUN: mlir-opt %s -tensor-bufferize -cse -split-input-file | FileCheck %s

// CHECK-LABEL: func @dim(
// CHECK-SAME: %[[TENSOR:.*]]: tensor<*xf32>,
Expand Down Expand Up @@ -62,9 +62,12 @@ func.func @tensor.cast_to_unranked(%arg0: tensor<2xf32>) -> tensor<*xf32> {
}

// -----

// CHECK-LABEL: func @tensor.empty(
// CHECK: %[[ALLOC:.*]] = memref.alloc() {{.*}} : memref<5xf32>
// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[ALLOC]] : memref<5xf32>
// CHECK: return %[[RET]] : tensor<5xf32>
func.func @tensor.empty() -> tensor<5xf32> {
// expected-error@+2 {{failed to bufferize op}}
// expected-error@+1 {{cannot be bufferized, but can be converted to bufferization.alloc_tensor}}
%0 = tensor.empty() : tensor<5xf32>
return %0 : tensor<5xf32>
}
Expand Down