1 //===- BufferizableOpInterface.cpp - Bufferizable Ops  ---=----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
10 #include "mlir/Dialect/Bufferization/IR/Bufferization.h"
11 #include "mlir/Dialect/Func/IR/FuncOps.h"
12 #include "mlir/Dialect/MemRef/IR/MemRef.h"
13 #include "mlir/IR/AsmState.h"
14 #include "mlir/IR/BlockAndValueMapping.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/IR/Operation.h"
17 #include "mlir/IR/TypeUtilities.h"
18 #include "mlir/IR/Value.h"
19 #include "llvm/Support/Debug.h"
20 
21 namespace mlir {
22 namespace bufferization {
23 
24 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.cpp.inc"
25 
26 } // namespace bufferization
27 } // namespace mlir
28 
29 #define DEBUG_TYPE "bufferizable-op-interface"
30 #define DBGS() (llvm::dbgs() << '[' << DEBUG_TYPE << "] ")
31 #define LDBG(X) LLVM_DEBUG(DBGS() << (X))
32 
33 using namespace mlir;
34 using namespace bufferization;
35 
36 /// Attribute name used to mark region arguments that can be bufferized
37 /// in-place during linalg comprehensive bufferization.
38 constexpr const ::llvm::StringLiteral
39     bufferization::BufferizableOpInterface::kInplaceableAttrName;
40 
41 /// Attribute name used to mark allocs that are created by the bufferization.
42 static const char *kBufferAllocationAttr = "bufferization.allocation";
43 
44 /// Attribute name used to mark allocs that should not be deallocated.
45 static const char *kSkipDeallocAttr = "bufferization.skip_dealloc";
46 
47 //===----------------------------------------------------------------------===//
48 // BufferizationOptions
49 //===----------------------------------------------------------------------===//
50 
51 // Default constructor for BufferizationOptions.
52 BufferizationOptions::BufferizationOptions() = default;
53 
54 bool BufferizationOptions::isOpAllowed(Operation *op) const {
55   // Special case: If function boundary bufferization is deactivated, do not
56   // allow ops that belong to the `func` dialect.
57   bool isFuncBoundaryOp = isa_and_nonnull<func::FuncDialect>(op->getDialect());
58   if (!bufferizeFunctionBoundaries && isFuncBoundaryOp)
59     return false;
60 
61   // All other ops: Allow/disallow according to filter.
62   bool isAllowed = !filterHasAllowRule();
63   for (const OpFilterEntry &entry : opFilter) {
64     bool filterResult = entry.fn(op);
65     switch (entry.type) {
66     case OpFilterEntry::ALLOW:
67       isAllowed |= filterResult;
68       break;
69     case OpFilterEntry::DENY:
70       if (filterResult)
71         // DENY filter matches. This op is no allowed. (Even if other ALLOW
72         // filters may match.)
73         return false;
74     };
75   }
76   return isAllowed;
77 }
78 
79 BufferizableOpInterface
80 BufferizationOptions::dynCastBufferizableOp(Operation *op) const {
81   auto bufferizableOp = dyn_cast<BufferizableOpInterface>(op);
82   if (!bufferizableOp)
83     return nullptr;
84   if (!isOpAllowed(op))
85     return nullptr;
86   return bufferizableOp;
87 }
88 
89 BufferizableOpInterface
90 BufferizationOptions::dynCastBufferizableOp(Value value) const {
91   if (auto bufferizableOp = value.getDefiningOp<BufferizableOpInterface>())
92     if (isOpAllowed(bufferizableOp.getOperation()))
93       return bufferizableOp;
94   return nullptr;
95 }
96 
97 void BufferizationOptions::addDialectStateInitializer(
98     StringRef name, const DialectStateInitFn &fn) {
99   stateInitializers.push_back(
100       [=](AnalysisState &state) { state.insertDialectState(name, fn()); });
101 }
102 
103 //===----------------------------------------------------------------------===//
104 // Helper functions for BufferizableOpInterface
105 //===----------------------------------------------------------------------===//
106 
107 static void setInsertionPointAfter(OpBuilder &b, Value value) {
108   if (auto bbArg = value.dyn_cast<BlockArgument>()) {
109     b.setInsertionPointToStart(bbArg.getOwner());
110   } else {
111     b.setInsertionPointAfter(value.getDefiningOp());
112   }
113 }
114 
115 /// Determine which OpOperand* will alias with `result` if the op is bufferized
116 /// in place. Return an empty vector if the op is not bufferizable.
117 SmallVector<OpOperand *>
118 AnalysisState::getAliasingOpOperand(OpResult result) const {
119   if (Operation *op = result.getDefiningOp())
120     if (auto bufferizableOp = getOptions().dynCastBufferizableOp(op))
121       return bufferizableOp.getAliasingOpOperand(result, *this);
122   return {};
123 }
124 
125 /// Determine which OpResult will alias with `opOperand` if the op is bufferized
126 /// in place. Return an empty vector if the op is not bufferizable.
127 SmallVector<OpResult>
128 AnalysisState::getAliasingOpResult(OpOperand &opOperand) const {
129   if (auto bufferizableOp =
130           getOptions().dynCastBufferizableOp(opOperand.getOwner()))
131     return bufferizableOp.getAliasingOpResult(opOperand, *this);
132   return {};
133 }
134 
135 /// Return true if `opOperand` bufferizes to a memory read. Return `true` if the
136 /// op is not bufferizable.
137 bool AnalysisState::bufferizesToMemoryRead(OpOperand &opOperand) const {
138   if (auto bufferizableOp =
139           getOptions().dynCastBufferizableOp(opOperand.getOwner()))
140     return bufferizableOp.bufferizesToMemoryRead(opOperand, *this);
141 
142   // Unknown op that returns a tensor. The inplace analysis does not support it.
143   // Conservatively return true.
144   return true;
145 }
146 
147 /// Return true if `opOperand` bufferizes to a memory write. Return
148 /// `true` if the op is not bufferizable.
149 bool AnalysisState::bufferizesToMemoryWrite(OpOperand &opOperand) const {
150   if (auto bufferizableOp =
151           getOptions().dynCastBufferizableOp(opOperand.getOwner()))
152     return bufferizableOp.bufferizesToMemoryWrite(opOperand, *this);
153 
154   // Unknown op that returns a tensor. The inplace analysis does not support it.
155   // Conservatively return true.
156   return true;
157 }
158 
159 /// Return true if `opOperand` does neither read nor write but bufferizes to an
160 /// alias. Return false if the op is not bufferizable.
161 bool AnalysisState::bufferizesToAliasOnly(OpOperand &opOperand) const {
162   if (auto bufferizableOp =
163           getOptions().dynCastBufferizableOp(opOperand.getOwner()))
164     return bufferizableOp.bufferizesToAliasOnly(opOperand, *this);
165 
166   // Unknown op that returns a tensor. The inplace analysis does not support it.
167   // Conservatively return false.
168   return false;
169 }
170 
171 /// Return true if the given value is read by an op that bufferizes to a memory
172 /// read. Also takes into account ops that create an alias but do not read by
173 /// themselves (e.g., ExtractSliceOp).
174 bool AnalysisState::isValueRead(Value value) const {
175   assert(value.getType().isa<TensorType>() && "expected TensorType");
176   SmallVector<OpOperand *> workingSet;
177   for (OpOperand &use : value.getUses())
178     workingSet.push_back(&use);
179 
180   while (!workingSet.empty()) {
181     OpOperand *uMaybeReading = workingSet.pop_back_val();
182     // Skip over all ops that neither read nor write (but create an alias).
183     if (bufferizesToAliasOnly(*uMaybeReading))
184       for (OpResult opResult : getAliasingOpResult(*uMaybeReading))
185         for (OpOperand &use : opResult.getUses())
186           workingSet.push_back(&use);
187     if (bufferizesToMemoryRead(*uMaybeReading))
188       return true;
189   }
190 
191   return false;
192 }
193 
194 // Starting from `value`, follow the use-def chain in reverse, always selecting
195 // the aliasing OpOperands. Find and return Values for which `condition`
196 // evaluates to true. OpOperands of such matching Values are not traversed any
197 // further.
198 llvm::SetVector<Value> AnalysisState::findValueInReverseUseDefChain(
199     Value value, llvm::function_ref<bool(Value)> condition) const {
200   llvm::SetVector<Value> result, workingSet;
201   workingSet.insert(value);
202 
203   while (!workingSet.empty()) {
204     Value value = workingSet.pop_back_val();
205     if (condition(value) || value.isa<BlockArgument>()) {
206       result.insert(value);
207       continue;
208     }
209 
210     OpResult opResult = value.cast<OpResult>();
211     SmallVector<OpOperand *> opOperands = getAliasingOpOperand(opResult);
212     if (opOperands.empty() || !options.isOpAllowed(value.getDefiningOp())) {
213       result.insert(value);
214       continue;
215     }
216 
217     for (OpOperand *o : opOperands)
218       workingSet.insert(o->get());
219   }
220 
221   return result;
222 }
223 
224 // Find the Values of the last preceding write of a given Value.
225 llvm::SetVector<Value>
226 AnalysisState::findLastPrecedingWrite(Value value) const {
227   return findValueInReverseUseDefChain(value, [&](Value value) {
228     Operation *op = value.getDefiningOp();
229     if (!op)
230       return true;
231     auto bufferizableOp = options.dynCastBufferizableOp(op);
232     if (!bufferizableOp)
233       return true;
234     return bufferizableOp.isMemoryWrite(value.cast<OpResult>(), *this);
235   });
236 }
237 
238 AnalysisState::AnalysisState(const BufferizationOptions &options)
239     : options(options) {
240   for (const BufferizationOptions::AnalysisStateInitFn &fn :
241        options.stateInitializers)
242     fn(*this);
243 }
244 
245 // bufferization.to_memref is not allowed to change the rank.
246 static void ensureToMemrefOpIsValid(Value tensor, Type memrefType) {
247 #ifndef NDEBUG
248   auto rankedTensorType = tensor.getType().dyn_cast<RankedTensorType>();
249   assert((!rankedTensorType || memrefType.cast<MemRefType>().getRank() ==
250                                    rankedTensorType.getRank()) &&
251          "to_memref would be invalid: mismatching ranks");
252 #endif
253 }
254 
255 Value mlir::bufferization::lookupBuffer(RewriterBase &rewriter, Value tensor,
256                                         const BufferizationOptions &options) {
257   auto tensorType = tensor.getType().dyn_cast<TensorType>();
258   assert(tensorType && "unexpected non-tensor type");
259 
260   // Replace "%t = to_tensor %m" with %m.
261   if (auto toTensorOp = tensor.getDefiningOp<bufferization::ToTensorOp>())
262     return toTensorOp.memref();
263 
264   // Insert to_memref op.
265   OpBuilder::InsertionGuard g(rewriter);
266   setInsertionPointAfter(rewriter, tensor);
267   Type memrefType = getMemRefType(tensorType, options);
268   ensureToMemrefOpIsValid(tensor, memrefType);
269   return rewriter.create<bufferization::ToMemrefOp>(tensor.getLoc(), memrefType,
270                                                     tensor);
271 }
272 
273 /// Return the buffer (memref) for a given OpOperand (tensor). Allocate
274 /// a new buffer and copy over data from the existing buffer if out-of-place
275 /// bufferization was decided.
276 FailureOr<Value>
277 BufferizationState::getBuffer(RewriterBase &rewriter, OpOperand &opOperand,
278                               Optional<ForceInPlacability> overrideInPlace,
279                               Optional<Operation *> customCopyInsertionPoint) {
280   const BufferizationOptions &options = analysisState.getOptions();
281   OpBuilder::InsertionGuard guard(rewriter);
282   Operation *op = opOperand.getOwner();
283   Location loc = op->getLoc();
284   SmallVector<OpResult> aliasingOpResults =
285       analysisState.getAliasingOpResult(opOperand);
286   Value operand = opOperand.get();
287   Value operandBuffer = lookupBuffer(rewriter, operand, options);
288 
289   // Can `operandBuffer` be used directly or do we need a copy?
290   bool inplace =
291       overrideInPlace != FORCE_OUT_OF_PLACE &&
292       (overrideInPlace == FORCE_INPLACE || analysisState.isInPlace(opOperand));
293   if (inplace)
294     return operandBuffer;
295 
296   // Bufferizing out-of-place: Allocate a new buffer.
297   // Move insertion point right after `operandBuffer`. That is where the
298   // allocation should be inserted (in the absence of allocation hoisting).
299   setInsertionPointAfter(rewriter, operandBuffer);
300   // Allocate the result buffer. The buffer should be deallocated if the tensor
301   // is not yielded and deallocs are enabled in general.
302   bool dealloc = llvm::none_of(aliasingOpResults, [&](Value v) {
303     return getAnalysisState().isTensorYielded(v);
304   });
305   FailureOr<Value> resultBuffer = createAlloc(
306       rewriter, loc, operandBuffer, dealloc && getOptions().createDeallocs);
307   if (failed(resultBuffer))
308     return failure();
309   // Do not copy the buffer if its contents are undefined.
310   if (analysisState.hasUndefinedContents(&opOperand))
311     return resultBuffer;
312   // Do not copy if the copied data is never read.
313   if (!aliasingOpResults.empty() &&
314       !analysisState.bufferizesToMemoryRead(opOperand) &&
315       llvm::none_of(aliasingOpResults, [&](OpResult opResult) {
316         return analysisState.isValueRead(opResult);
317       }))
318     return resultBuffer;
319   // Do not copy if this op does not read the data, but writes it.
320   if (analysisState.bufferizesToMemoryWrite(opOperand) &&
321       !analysisState.bufferizesToMemoryRead(opOperand))
322     return resultBuffer;
323 
324   if (customCopyInsertionPoint) {
325     rewriter.setInsertionPoint(*customCopyInsertionPoint);
326   } else {
327     // The copy happens right before the op that is bufferized.
328     rewriter.setInsertionPoint(op);
329   }
330   if (failed(options.createMemCpy(rewriter, loc, operandBuffer, *resultBuffer)))
331     return failure();
332 
333   return resultBuffer;
334 }
335 
336 /// Return the buffer type for a given Value (tensor) after bufferization.
337 BaseMemRefType BufferizationState::getBufferType(Value value) const {
338   auto tensorType = value.getType().dyn_cast<TensorType>();
339   assert(tensorType && "unexpected non-tensor type");
340 
341   if (auto toTensorOp = value.getDefiningOp<bufferization::ToTensorOp>())
342     return toTensorOp.memref().getType().cast<BaseMemRefType>();
343 
344   return getMemRefType(tensorType, getOptions());
345 }
346 
347 void bufferization::replaceOpWithBufferizedValues(RewriterBase &rewriter,
348                                                   Operation *op,
349                                                   ValueRange values) {
350   assert(values.size() == op->getNumResults() &&
351          "expected one value per OpResult");
352   OpBuilder::InsertionGuard g(rewriter);
353 
354   // Replace all OpResults with the given values.
355   SmallVector<Value> replacements;
356   for (OpResult opResult : op->getOpResults()) {
357     Value replacement = values[opResult.getResultNumber()];
358     if (opResult.getType().isa<TensorType>()) {
359       // The OpResult is a tensor. Such values are replaced with memrefs during
360       // bufferization.
361       assert((replacement.getType().isa<MemRefType>() ||
362               replacement.getType().isa<UnrankedMemRefType>()) &&
363              "tensor op result should be replaced with a memref value");
364       // The existing uses of the OpResult still expect a tensor. Insert a
365       // ToTensorOp. Throughout bufferization, this ToTensorOp will gradually
366       // loose all of its users and eventually DCE away.
367       rewriter.setInsertionPointAfter(op);
368       replacement = rewriter.create<bufferization::ToTensorOp>(
369           replacement.getLoc(), replacement);
370     }
371     replacements.push_back(replacement);
372   }
373 
374   rewriter.replaceOp(op, replacements);
375 }
376 
377 AlwaysCopyAnalysisState::AlwaysCopyAnalysisState(
378     const BufferizationOptions &options)
379     : AnalysisState(options) {
380   // Note: Allocations must be deallocated with a subsequent run of the buffer
381   // deallocation pass.
382   assert(!options.createDeallocs &&
383          "cannot create deallocs with AlwaysCopyBufferizationState");
384 }
385 
386 /// Return `true` if the given OpResult has been decided to bufferize inplace.
387 bool AlwaysCopyAnalysisState::isInPlace(OpOperand &opOperand) const {
388   // OpOperands that bufferize to a memory write are out-of-place, i.e., an
389   // alloc and copy is inserted.
390   return !bufferizesToMemoryWrite(opOperand);
391 }
392 
393 /// Return true if `v1` and `v2` bufferize to equivalent buffers.
394 bool AlwaysCopyAnalysisState::areEquivalentBufferizedValues(Value v1,
395                                                             Value v2) const {
396   // There is no analysis, so we do not know if the values are equivalent. The
397   // conservative answer is "false".
398   return false;
399 }
400 
401 /// Return `true` if the given tensor has undefined contents.
402 bool AlwaysCopyAnalysisState::hasUndefinedContents(OpOperand *opOperand) const {
403   // There is no analysis, so the conservative answer is "false".
404   return false;
405 }
406 
407 /// Return true if the given tensor (or an aliasing tensor) is yielded from
408 /// the containing block. Also include all aliasing tensors in the same block.
409 bool AlwaysCopyAnalysisState::isTensorYielded(Value tensor) const {
410   // There is no analysis, so conservatively answer "true".
411   return true;
412 }
413 
414 //===----------------------------------------------------------------------===//
415 // Bufferization-specific scoped alloc/dealloc insertion support.
416 //===----------------------------------------------------------------------===//
417 
418 /// Create a memref allocation with the given type and dynamic extents.
419 FailureOr<Value> BufferizationOptions::createAlloc(OpBuilder &b, Location loc,
420                                                    MemRefType type,
421                                                    ValueRange dynShape) const {
422   if (allocationFn)
423     return (*allocationFn)(b, loc, type, dynShape, bufferAlignment);
424 
425   // Default bufferallocation via AllocOp.
426   Value allocated = b.create<memref::AllocOp>(
427       loc, type, dynShape, b.getI64IntegerAttr(bufferAlignment));
428   return allocated;
429 }
430 
431 /// Creates a memref deallocation. The given memref buffer must have been
432 /// allocated using `createAlloc`.
433 LogicalResult BufferizationOptions::createDealloc(OpBuilder &b, Location loc,
434                                                   Value allocatedBuffer) const {
435   if (deallocationFn)
436     return (*deallocationFn)(b, loc, allocatedBuffer);
437 
438   // Default buffer deallocation via DeallocOp.
439   b.create<memref::DeallocOp>(loc, allocatedBuffer);
440   return success();
441 }
442 
443 static MemRefType getContiguousMemRefType(ShapedType shapedType,
444                                           Attribute memorySpace = {}) {
445   MemRefLayoutAttrInterface layout = {};
446   return MemRefType::get(shapedType.getShape(), shapedType.getElementType(),
447                          layout, memorySpace);
448 }
449 
450 /// Compute the type of the `memref` to use for allocating the buffer for
451 /// `shapedValue`. Also returns (by reference in `dynShape`), the value for the
452 /// dynamic dimensions in the returned `memref` type.
453 static MemRefType getAllocationTypeAndShape(OpBuilder &b, Location loc,
454                                             Value shapedValue,
455                                             SmallVectorImpl<Value> &dynShape) {
456   MemRefType allocMemRefType =
457       getContiguousMemRefType(shapedValue.getType().cast<ShapedType>());
458 
459   // Compute the dynamic part of the shape.
460   bool reifiedShapes = false;
461   if (auto rankedOp = dyn_cast_or_null<ReifyRankedShapedTypeOpInterface>(
462           shapedValue.getDefiningOp())) {
463     ReifiedRankedShapedTypeDims resultDims;
464     if (succeeded(rankedOp.reifyResultShapes(b, resultDims))) {
465       reifiedShapes = true;
466       OpResult resultValue = shapedValue.dyn_cast<OpResult>();
467       auto &shape = resultDims[resultValue.getResultNumber()];
468       for (const auto &dim : enumerate(allocMemRefType.getShape()))
469         if (ShapedType::isDynamic(dim.value()))
470           dynShape.push_back(shape[dim.index()]);
471     }
472   }
473 
474   if (!reifiedShapes) {
475     for (const auto &dim : enumerate(allocMemRefType.getShape()))
476       if (ShapedType::isDynamic(dim.value())) {
477         assert((shapedValue.getType().isa<UnrankedMemRefType>() ||
478                 shapedValue.getType().isa<MemRefType>()) &&
479                "expected MemRef type");
480         dynShape.push_back(
481             b.create<memref::DimOp>(loc, shapedValue, dim.index()));
482       }
483   }
484 
485   return allocMemRefType;
486 }
487 
488 static Value createBufferAllocation(OpBuilder &b, Location loc, MemRefType type,
489                                     ValueRange dynShape, bool skipDealloc) {
490   auto allocaOp = b.create<memref::AllocaOp>(loc, type, dynShape);
491   allocaOp->setAttr(kBufferAllocationAttr, b.getUnitAttr());
492   if (skipDealloc)
493     allocaOp->setAttr(kSkipDeallocAttr, b.getUnitAttr());
494   return allocaOp.getResult();
495 }
496 
497 /// Create an allocation after `shapedValue.getDefiningOp` (or at the top of the
498 /// block in case of a bbArg).
499 FailureOr<Value> BufferizationState::createAlloc(OpBuilder &b, Location loc,
500                                                  Value shapedValue,
501                                                  Optional<bool> dealloc) {
502   // Take a guard before anything else.
503   OpBuilder::InsertionGuard g(b);
504 
505   // Compute allocation memref type.
506   assert(shapedValue.getType().isa<ShapedType>());
507   SmallVector<Value> dynShape;
508   MemRefType allocMemRefType =
509       getAllocationTypeAndShape(b, loc, shapedValue, dynShape);
510 
511   // Should be the buffer be deallocated again or should we let it leak?
512   bool skipDealloc;
513   if (dealloc) {
514     skipDealloc = !dealloc.getValue();
515   } else {
516     assert(shapedValue.getType().isa<TensorType>() &&
517            "must specify `dealloc` if non-tensor value is passed");
518     // Buffer should be not be deallocated if deallocs are generally deactivated
519     // or if the tensor is yielded from a block.
520     skipDealloc = !getOptions().createDeallocs ||
521                   getAnalysisState().isTensorYielded(shapedValue);
522   }
523 
524   // Create the buffer allocation.
525   return createBufferAllocation(b, loc, allocMemRefType, dynShape, skipDealloc);
526 }
527 
528 /// Create a memory copy between two memref buffers.
529 LogicalResult BufferizationOptions::createMemCpy(OpBuilder &b, Location loc,
530                                                  Value from, Value to) const {
531   if (memCpyFn)
532     return (*memCpyFn)(b, loc, from, to);
533 
534   b.create<memref::CopyOp>(loc, from, to);
535   return success();
536 }
537 
538 LogicalResult
539 bufferization::createAllocDeallocOps(Operation *op,
540                                      const BufferizationOptions &options,
541                                      bool onlyLeakingAllocs, bool *changed) {
542   IRRewriter rewriter(op->getContext());
543   if (changed)
544     *changed = false;
545 
546   // Bufferization creates memref.alloca ops. After bufferization, these must be
547   // rewritten to alloc/dealloc ops as specified in the bufferization options.
548   WalkResult status = op->walk([&](memref::AllocaOp allocaOp) {
549     // Ignore memref.alloca ops that were not created by the bufferization.
550     if (!allocaOp->hasAttr(kBufferAllocationAttr))
551       return WalkResult::skip();
552     // If `onlyLeakingAllocs`, process only ops that are marked as
553     // "skip dealloc".
554     bool skipDealloc = allocaOp->hasAttr(kSkipDeallocAttr);
555     if (onlyLeakingAllocs && !skipDealloc)
556       return WalkResult::skip();
557 
558     // Create alloc.
559     Block *block = allocaOp->getBlock();
560     rewriter.setInsertionPoint(allocaOp);
561     FailureOr<Value> alloc =
562         options.createAlloc(rewriter, allocaOp->getLoc(), allocaOp.getType(),
563                             allocaOp.dynamicSizes());
564     if (failed(alloc))
565       return WalkResult::interrupt();
566     rewriter.replaceOp(allocaOp, *alloc);
567     if (changed)
568       *changed = true;
569 
570     // Stop here if the buffer should not be deallocated.
571     if (skipDealloc)
572       return WalkResult::advance();
573 
574     // Create dealloc.
575     rewriter.setInsertionPoint(block->getTerminator());
576     if (failed(options.createDealloc(rewriter, alloc->getLoc(), *alloc)))
577       return WalkResult::interrupt();
578 
579     return WalkResult::advance();
580   });
581 
582   return success(!status.wasInterrupted());
583 }
584 
585 /// Try to hoist all new buffer allocations until the next hoisting barrier.
586 // TODO: Consolidate this function with the existing buffer hoisting pass.
587 LogicalResult
588 bufferization::hoistBufferAllocations(Operation *op,
589                                       const BufferizationOptions &options) {
590   // Nothing to do if allocation hoisting is deactivated.
591   if (!options.hoistAllocations)
592     return success();
593 
594   // Gather all buffer allocations that were created by the bufferization.
595   SmallVector<Operation *> allocaOps;
596   op->walk([&](memref::AllocaOp allocaOp) {
597     if (allocaOp->hasAttr(kBufferAllocationAttr))
598       allocaOps.push_back(allocaOp);
599   });
600 
601   for (Operation *allocaOp : allocaOps) {
602     // TODO: Hoisting of allocs with dynamic shape not implemented.
603     if (!allocaOp->getOpOperands().empty())
604       continue;
605 
606     Operation *op = allocaOp->getParentOp();
607     while (op) {
608       if (auto bufferizableOp = dyn_cast<BufferizableOpInterface>(op)) {
609         if (bufferizableOp.isAllocationHoistingBarrier()) {
610           break;
611         }
612       } else {
613         // Op is not bufferizable: It may not be safe to hoist across this op.
614         break;
615       }
616       op = op->getParentOp();
617     }
618 
619     // FuncOp is an allocation hoisting barrier, so this should never happen.
620     assert(op && "allocation hoisting barrier not found");
621 
622     // Nothing to do if the insertion point is in the same block.
623     if (op == allocaOp->getParentOp())
624       continue;
625 
626     // `op` may have multiple blocks. Make sure that we insert in the right one.
627     SmallVector<Block *> blocks;
628     for (Region &r : op->getRegions())
629       for (Block &b : r.getBlocks())
630         blocks.push_back(&b);
631     auto *insertionBlock = llvm::find_if(
632         blocks, [&](Block *b) { return b->findAncestorOpInBlock(*allocaOp); });
633     assert(insertionBlock != blocks.end() && "owning block not found");
634 
635     // Move to the beginning of the block.
636     allocaOp->moveBefore(&(*insertionBlock)->front());
637   }
638 
639   return success();
640 }
641 
642 //===----------------------------------------------------------------------===//
643 // Bufferization-specific BlockAndValueMapping support with debugging.
644 //===----------------------------------------------------------------------===//
645 
646 bool bufferization::isFunctionArgument(Value value) {
647   auto bbArg = value.dyn_cast<BlockArgument>();
648   if (!bbArg)
649     return false;
650   return isa<func::FuncOp>(bbArg.getOwner()->getParentOp());
651 }
652 
653 BaseMemRefType bufferization::getMemRefType(TensorType tensorType,
654                                             const BufferizationOptions &options,
655                                             MemRefLayoutAttrInterface layout,
656                                             Attribute memorySpace) {
657   // Case 1: Unranked memref type.
658   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
659     assert(!layout && "UnrankedTensorType cannot have a layout map");
660     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
661                                    memorySpace);
662   }
663 
664   // Case 2: Ranked memref type with specified layout.
665   auto rankedTensorType = tensorType.cast<RankedTensorType>();
666   if (layout) {
667     return MemRefType::get(rankedTensorType.getShape(),
668                            rankedTensorType.getElementType(), layout,
669                            memorySpace);
670   }
671 
672   // Case 3: Configured with "fully dynamic layout maps".
673   if (options.unknownTypeConversion ==
674       BufferizationOptions::LayoutMapOption::FullyDynamicLayoutMap)
675     return getMemRefTypeWithFullyDynamicLayout(tensorType, memorySpace);
676 
677   // Case 4: Configured with "static identity layout maps".
678   if (options.unknownTypeConversion ==
679       BufferizationOptions::LayoutMapOption::IdentityLayoutMap)
680     return getMemRefTypeWithStaticIdentityLayout(tensorType, memorySpace);
681 
682   llvm_unreachable("InferLayoutMap is an invalid option");
683 }
684 
685 BaseMemRefType
686 bufferization::getMemRefTypeWithFullyDynamicLayout(TensorType tensorType,
687                                                    Attribute memorySpace) {
688   // Case 1: Unranked memref type.
689   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
690     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
691                                    memorySpace);
692   }
693 
694   // Case 2: Ranked memref type.
695   auto rankedTensorType = tensorType.cast<RankedTensorType>();
696   int64_t dynamicOffset = ShapedType::kDynamicStrideOrOffset;
697   SmallVector<int64_t> dynamicStrides(rankedTensorType.getRank(),
698                                       ShapedType::kDynamicStrideOrOffset);
699   AffineMap stridedLayout = makeStridedLinearLayoutMap(
700       dynamicStrides, dynamicOffset, rankedTensorType.getContext());
701   return MemRefType::get(rankedTensorType.getShape(),
702                          rankedTensorType.getElementType(), stridedLayout,
703                          memorySpace);
704 }
705 
706 /// Return a MemRef type with a static identity layout (i.e., no layout map). If
707 /// the given tensor type is unranked, return an unranked MemRef type.
708 BaseMemRefType
709 bufferization::getMemRefTypeWithStaticIdentityLayout(TensorType tensorType,
710                                                      Attribute memorySpace) {
711   // Case 1: Unranked memref type.
712   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
713     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
714                                    memorySpace);
715   }
716 
717   // Case 2: Ranked memref type.
718   auto rankedTensorType = tensorType.cast<RankedTensorType>();
719   MemRefLayoutAttrInterface layout = {};
720   return MemRefType::get(rankedTensorType.getShape(),
721                          rankedTensorType.getElementType(), layout,
722                          memorySpace);
723 }
724