24struct AllocTensorOpInterface
25 :
public BufferizableOpInterface::ExternalModel<AllocTensorOpInterface,
27 bool bufferizesToAllocation(Operation *op, Value value)
const {
return true; }
29 bool resultBufferizesToMemoryWrite(Operation *op, OpResult opResult,
30 const AnalysisState &state)
const {
32 return static_cast<bool>(cast<AllocTensorOp>(op).getCopy());
35 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
36 const AnalysisState &state)
const {
37 assert(&opOperand == &cast<AllocTensorOp>(op).getCopyMutable()[0] &&
38 "expected copy operand");
42 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
43 const AnalysisState &state)
const {
44 assert(&opOperand == &cast<AllocTensorOp>(op).getCopyMutable()[0] &&
45 "expected copy operand");
49 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
50 const AnalysisState &state)
const {
55 FailureOr<BufferLikeType>
57 const BufferizationState &state,
58 SmallVector<Value> &invocationStack)
const {
59 auto allocTensorOp = cast<AllocTensorOp>(op);
60 assert(value == allocTensorOp.getResult() &&
"invalid value");
62 FailureOr<BufferLikeType> copyBufferType;
63 if (allocTensorOp.getCopy()) {
64 copyBufferType = bufferization::getBufferType(
65 allocTensorOp.getCopy(),
options, state, invocationStack);
66 if (
failed(copyBufferType))
71 Attribute memorySpace;
72 if (allocTensorOp.getMemorySpace().has_value()) {
73 memorySpace = *allocTensorOp.getMemorySpace();
74 }
else if (allocTensorOp.getCopy()) {
76 if (
auto copyMemRefType = dyn_cast<BaseMemRefType>(*copyBufferType))
77 memorySpace = copyMemRefType.getMemorySpace();
78 }
else if (
auto ms =
options.defaultMemorySpaceFn(
79 cast<TensorLikeType>(allocTensorOp.getType()))) {
82 return op->
emitError(
"could not infer memory space");
87 if (allocTensorOp.getCopy())
88 if (
auto copyMemRefType = dyn_cast<MemRefType>(*copyBufferType))
89 return cast<BufferLikeType>(MemRefType::get(
90 copyMemRefType.getShape(), copyMemRefType.getElementType(),
91 MemRefLayoutAttrInterface{}, memorySpace));
93 if (
auto tensorType = dyn_cast<RankedTensorType>(allocTensorOp.getType()))
94 return cast<BufferLikeType>(
95 getMemRefTypeWithStaticIdentityLayout(tensorType, memorySpace));
96 return options.unknownTypeConverterFn(allocTensorOp.getType(), memorySpace,
100 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
101 const BufferizationOptions &
options,
102 BufferizationState &state)
const {
103 auto allocTensorOp = cast<AllocTensorOp>(op);
104 OpBuilder::InsertionGuard g(rewriter);
105 Location loc = allocTensorOp.getLoc();
115 if (allocTensorOp.getCopy()) {
116 FailureOr<Value> maybeCopyBuffer = bufferization::getBuffer(
117 rewriter, allocTensorOp.getCopy(),
options, state);
118 if (
failed(maybeCopyBuffer))
120 copyBuffer = *maybeCopyBuffer;
125 bufferization::getBufferType(allocTensorOp.getResult(),
options, state);
130 auto memRefType = dyn_cast<MemRefType>(*allocType);
132 return op->
emitError() <<
"allocating a buffer of type " << *allocType
133 <<
" is not supported";
134 SmallVector<Value> dynamicDims = allocTensorOp.getDynamicSizes();
136 if (allocTensorOp.getCopy() && isa<MemRefType>(copyBuffer.
getType())) {
137 assert(dynamicDims.empty() &&
"expected either `copy` or `dynamicDims`");
143 if (memRefType.getNumDynamicDims() != dynamicDims.size())
145 <<
"allocating " << memRefType
146 <<
" needs a size for each dynamic dimension, which the op "
147 "takes only for a ranked tensor result";
148 FailureOr<Value> alloc =
options.allocationFn(
149 rewriter, loc, memRefType, dynamicDims,
options.bufferAlignment);
154 if (allocTensorOp.getCopy()) {
155 if (
failed(
options.memCpyFn(rewriter, loc, copyBuffer, *alloc)))
160 replaceOpWithBufferizedValues(rewriter, op, *alloc);
166struct DeallocTensorOpInterface
167 :
public BufferizableOpInterface::ExternalModel<DeallocTensorOpInterface,
169 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
170 const AnalysisState &state)
const {
174 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
175 const AnalysisState &state)
const {
179 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
180 const AnalysisState &state)
const {
184 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
185 const BufferizationOptions &
options,
186 BufferizationState &state)
const {
187 auto deallocTensorOp = cast<DeallocTensorOp>(op);
188 FailureOr<Value> buffer = bufferization::getBuffer(
189 rewriter, deallocTensorOp.getTensor(),
options, state);
192 memref::DeallocOp::create(rewriter, deallocTensorOp.getLoc(), *buffer);
198struct MaterializeInDestinationOpInterface
199 :
public BufferizableOpInterface::ExternalModel<
200 MaterializeInDestinationOpInterface, MaterializeInDestinationOp> {
201 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
202 const AnalysisState &state)
const {
203 return opOperand == cast<MaterializeInDestinationOp>(op).getSourceMutable();
206 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
207 const AnalysisState &state)
const {
208 auto materializeOp = cast<MaterializeInDestinationOp>(op);
209 if (opOperand == materializeOp.getDestMutable()) {
210 assert(isa<TensorType>(materializeOp.getDest().getType()) &&
211 "expected tensor type");
217 bool mustBufferizeInPlace(Operation *op, OpOperand &opOperand,
218 const AnalysisState &state)
const {
225 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
226 const AnalysisState &state)
const {
227 auto materializeOp = cast<MaterializeInDestinationOp>(op);
228 if (opOperand == materializeOp.getDestMutable()) {
229 assert(isa<TensorType>(materializeOp.getDest().getType()) &&
230 "expected tensor type");
231 return {{op->
getResult(0), BufferRelation::Equivalent}};
236 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
237 const BufferizationOptions &
options,
238 BufferizationState &state)
const {
239 auto materializeOp = cast<MaterializeInDestinationOp>(op);
240 bool tensorDest = isa<TensorType>(materializeOp.getDest().getType());
243 FailureOr<Value> maybeBuffer = bufferization::getBuffer(
244 rewriter, materializeOp.getDest(),
options, state);
247 buffer = *maybeBuffer;
249 assert(isa<BaseMemRefType>(materializeOp.getDest().getType()) &&
250 "expected memref type");
251 buffer = materializeOp.getDest();
253 auto srcBuffer = bufferization::getBuffer(
254 rewriter, materializeOp.getSource(),
options, state);
257 if (
failed(
options.memCpyFn(rewriter, materializeOp.getLoc(), *srcBuffer,
260 replaceOpWithBufferizedValues(
265 bool bufferizesToElementwiseAccess(Operation *op,
const AnalysisState &state,
266 ArrayRef<OpOperand *> opOperands)
const {
272 bool isWritable(Operation *op, Value value,
273 const AnalysisState &state)
const {
274 auto materializeOp = cast<MaterializeInDestinationOp>(op);
275 return isa<TensorType>(materializeOp.getDest().getType())
277 : materializeOp.getWritable();
287struct ToTensorOpInterface
288 :
public BufferizableOpInterface::ExternalModel<ToTensorOpInterface,
290 bool isWritable(Operation *op, Value value,
291 const AnalysisState &state)
const {
292 return cast<ToTensorOp>(op).getWritable();
295 FailureOr<BufferLikeType>
297 const BufferizationState &state,
298 SmallVector<Value> &invocationStack)
const {
299 return cast<ToTensorOp>(op).getBuffer().getType();
302 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
303 const BufferizationOptions &
options,
304 BufferizationState &state)
const {
310struct ToBufferOpInterface
311 :
public BufferizableOpInterface::ExternalModel<ToBufferOpInterface,
313 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
314 const AnalysisState &state)
const {
319 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
320 const AnalysisState &state)
const {
321 return !cast<ToBufferOp>(op).getReadOnly();
324 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
325 const AnalysisState &state)
const {
329 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
330 const BufferizationOptions &
options,
331 BufferizationState &state)
const {
347 AllocTensorOp::attachInterface<AllocTensorOpInterface>(*ctx);
348 DeallocTensorOp::attachInterface<DeallocTensorOpInterface>(*ctx);
349 MaterializeInDestinationOp::attachInterface<
350 MaterializeInDestinationOpInterface>(*ctx);
351 ToBufferOp::attachInterface<ToBufferOpInterface>(*ctx);
352 ToTensorOp::attachInterface<ToTensorOpInterface>(*ctx);
true
Given two iterators into the same block, return "true" if a is before `b.
static llvm::ManagedStatic< PassManagerOptions > options
static RankedTensorType getBufferType(const SparseTensorType &stt, bool needTmpCOO)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
use_range getUses()
Returns a range of all uses, which is useful for iterating over all uses.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
Type getType() const
Return the type of this value.
LogicalResult foldToBufferToTensorPair(RewriterBase &rewriter, ToBufferOp toBuffer, const BufferizationOptions &options)
Try to fold to_buffer(to_tensor(x)).
void registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry)
void populateDynamicDimSizes(OpBuilder &b, Location loc, Value shapedValue, SmallVector< Value > &dynamicDims)
Populate dynamicDims with tensor::DimOp / memref::DimOp results for all dynamic dimensions of the giv...
Include the generated interface declarations.