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 = dyn_cast<BufferizableOpInterface>(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           dyn_cast<BufferizableOpInterface>(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           dyn_cast<BufferizableOpInterface>(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           dyn_cast<BufferizableOpInterface>(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           dyn_cast<BufferizableOpInterface>(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(
331           createMemCpy(rewriter, loc, operandBuffer, *resultBuffer, options)))
332     return failure();
333 
334   return resultBuffer;
335 }
336 
337 /// Return the buffer type for a given OpOperand (tensor) after bufferization.
338 BaseMemRefType BufferizationState::getBufferType(OpOperand &opOperand) const {
339   Value tensor = opOperand.get();
340   auto tensorType = tensor.getType().dyn_cast<TensorType>();
341   assert(tensorType && "unexpected non-tensor type");
342 
343   if (auto toTensorOp = tensor.getDefiningOp<bufferization::ToTensorOp>())
344     return toTensorOp.memref().getType().cast<BaseMemRefType>();
345 
346   return getMemRefType(tensorType, getOptions());
347 }
348 
349 void bufferization::replaceOpWithBufferizedValues(RewriterBase &rewriter,
350                                                   Operation *op,
351                                                   ValueRange values) {
352   assert(values.size() == op->getNumResults() &&
353          "expected one value per OpResult");
354   OpBuilder::InsertionGuard g(rewriter);
355 
356   // Replace all OpResults with the given values.
357   SmallVector<Value> replacements;
358   for (OpResult opResult : op->getOpResults()) {
359     Value replacement = values[opResult.getResultNumber()];
360     if (opResult.getType().isa<TensorType>()) {
361       // The OpResult is a tensor. Such values are replaced with memrefs during
362       // bufferization.
363       assert((replacement.getType().isa<MemRefType>() ||
364               replacement.getType().isa<UnrankedMemRefType>()) &&
365              "tensor op result should be replaced with a memref value");
366       // The existing uses of the OpResult still expect a tensor. Insert a
367       // ToTensorOp. Throughout bufferization, this ToTensorOp will gradually
368       // loose all of its users and eventually DCE away.
369       rewriter.setInsertionPointAfter(op);
370       replacement = rewriter.create<bufferization::ToTensorOp>(
371           replacement.getLoc(), replacement);
372     }
373     replacements.push_back(replacement);
374   }
375 
376   rewriter.replaceOp(op, replacements);
377 }
378 
379 AlwaysCopyAnalysisState::AlwaysCopyAnalysisState(
380     const BufferizationOptions &options)
381     : AnalysisState(options) {
382   // Note: Allocations must be deallocated with a subsequent run of the buffer
383   // deallocation pass.
384   assert(!options.createDeallocs &&
385          "cannot create deallocs with AlwaysCopyBufferizationState");
386 }
387 
388 /// Return `true` if the given OpResult has been decided to bufferize inplace.
389 bool AlwaysCopyAnalysisState::isInPlace(OpOperand &opOperand) const {
390   // OpOperands that bufferize to a memory write are out-of-place, i.e., an
391   // alloc and copy is inserted.
392   return !bufferizesToMemoryWrite(opOperand);
393 }
394 
395 /// Return true if `v1` and `v2` bufferize to equivalent buffers.
396 bool AlwaysCopyAnalysisState::areEquivalentBufferizedValues(Value v1,
397                                                             Value v2) const {
398   // There is no analysis, so we do not know if the values are equivalent. The
399   // conservative answer is "false".
400   return false;
401 }
402 
403 /// Return `true` if the given tensor has undefined contents.
404 bool AlwaysCopyAnalysisState::hasUndefinedContents(OpOperand *opOperand) const {
405   // There is no analysis, so the conservative answer is "false".
406   return false;
407 }
408 
409 /// Return true if the given tensor (or an aliasing tensor) is yielded from
410 /// the containing block. Also include all aliasing tensors in the same block.
411 bool AlwaysCopyAnalysisState::isTensorYielded(Value tensor) const {
412   // There is no analysis, so conservatively answer "true".
413   return true;
414 }
415 
416 //===----------------------------------------------------------------------===//
417 // Bufferization-specific scoped alloc/dealloc insertion support.
418 //===----------------------------------------------------------------------===//
419 
420 /// Create a memref allocation with the given type and dynamic extents.
421 static FailureOr<Value> createAlloc(OpBuilder &b, Location loc, MemRefType type,
422                                     ValueRange dynShape,
423                                     const BufferizationOptions &options) {
424   if (options.allocationFn)
425     return (*options.allocationFn)(b, loc, type, dynShape,
426                                    options.bufferAlignment);
427 
428   // Default bufferallocation via AllocOp.
429   Value allocated = b.create<memref::AllocOp>(
430       loc, type, dynShape, b.getI64IntegerAttr(options.bufferAlignment));
431   return allocated;
432 }
433 
434 /// Creates a memref deallocation. The given memref buffer must have been
435 /// allocated using `createAlloc`.
436 LogicalResult
437 bufferization::createDealloc(OpBuilder &b, Location loc, Value allocatedBuffer,
438                              const BufferizationOptions &options) {
439   if (options.deallocationFn)
440     return (*options.deallocationFn)(b, loc, allocatedBuffer);
441 
442   // Default buffer deallocation via DeallocOp.
443   b.create<memref::DeallocOp>(loc, allocatedBuffer);
444   return success();
445 }
446 
447 /// Compute the type of the `memref` to use for allocating the buffer for
448 /// `shapedValue`. Also returns (by reference in `dynShape`), the value for the
449 /// dynamic dimensions in the returned `memref` type.
450 static MemRefType getAllocationTypeAndShape(OpBuilder &b, Location loc,
451                                             Value shapedValue,
452                                             SmallVectorImpl<Value> &dynShape) {
453   MemRefType allocMemRefType =
454       getContiguousMemRefType(shapedValue.getType().cast<ShapedType>());
455 
456   // Compute the dynamic part of the shape.
457   bool reifiedShapes = false;
458   if (auto rankedOp = dyn_cast_or_null<ReifyRankedShapedTypeOpInterface>(
459           shapedValue.getDefiningOp())) {
460     ReifiedRankedShapedTypeDims resultDims;
461     if (succeeded(rankedOp.reifyResultShapes(b, resultDims))) {
462       reifiedShapes = true;
463       OpResult resultValue = shapedValue.dyn_cast<OpResult>();
464       auto &shape = resultDims[resultValue.getResultNumber()];
465       for (const auto &dim : enumerate(allocMemRefType.getShape()))
466         if (ShapedType::isDynamic(dim.value()))
467           dynShape.push_back(shape[dim.index()]);
468     }
469   }
470 
471   if (!reifiedShapes) {
472     for (const auto &dim : enumerate(allocMemRefType.getShape()))
473       if (ShapedType::isDynamic(dim.value())) {
474         assert((shapedValue.getType().isa<UnrankedMemRefType>() ||
475                 shapedValue.getType().isa<MemRefType>()) &&
476                "expected MemRef type");
477         dynShape.push_back(
478             b.create<memref::DimOp>(loc, shapedValue, dim.index()));
479       }
480   }
481 
482   return allocMemRefType;
483 }
484 
485 static Value createBufferAllocation(OpBuilder &b, Location loc, MemRefType type,
486                                     ValueRange dynShape, bool skipDealloc) {
487   auto allocaOp = b.create<memref::AllocaOp>(loc, type, dynShape);
488   allocaOp->setAttr(kBufferAllocationAttr, b.getUnitAttr());
489   if (skipDealloc)
490     allocaOp->setAttr(kSkipDeallocAttr, b.getUnitAttr());
491   return allocaOp.getResult();
492 }
493 
494 /// Create an allocation after `shapedValue.getDefiningOp` (or at the top of the
495 /// block in case of a bbArg).
496 FailureOr<Value> BufferizationState::createAlloc(OpBuilder &b, Location loc,
497                                                  Value shapedValue,
498                                                  Optional<bool> dealloc) {
499   // Take a guard before anything else.
500   OpBuilder::InsertionGuard g(b);
501 
502   // Compute allocation memref type.
503   assert(shapedValue.getType().isa<ShapedType>());
504   SmallVector<Value> dynShape;
505   MemRefType allocMemRefType =
506       getAllocationTypeAndShape(b, loc, shapedValue, dynShape);
507 
508   // Should be the buffer be deallocated again or should we let it leak?
509   bool skipDealloc;
510   if (dealloc) {
511     skipDealloc = !dealloc.getValue();
512   } else {
513     assert(shapedValue.getType().isa<TensorType>() &&
514            "must specify `dealloc` if non-tensor value is passed");
515     // Buffer should be not be deallocated if deallocs are generally deactivated
516     // or if the tensor is yielded from a block.
517     skipDealloc = !getOptions().createDeallocs ||
518                   getAnalysisState().isTensorYielded(shapedValue);
519   }
520 
521   // Create the buffer allocation.
522   return createBufferAllocation(b, loc, allocMemRefType, dynShape, skipDealloc);
523 }
524 
525 /// Create a memory copy between two memref buffers.
526 LogicalResult bufferization::createMemCpy(OpBuilder &b, Location loc,
527                                           Value from, Value to,
528                                           const BufferizationOptions &options) {
529   if (options.memCpyFn)
530     return (*options.memCpyFn)(b, loc, from, to);
531 
532   b.create<memref::CopyOp>(loc, from, to);
533   return success();
534 }
535 
536 LogicalResult
537 bufferization::createAllocDeallocOps(Operation *op,
538                                      const BufferizationOptions &options,
539                                      bool onlyLeakingAllocs, bool *changed) {
540   IRRewriter rewriter(op->getContext());
541   if (changed)
542     *changed = false;
543 
544   // Bufferization creates memref.alloca ops. After bufferization, these must be
545   // rewritten to alloc/dealloc ops as specified in the bufferization options.
546   WalkResult status = op->walk([&](memref::AllocaOp allocaOp) {
547     // Ignore memref.alloca ops that were not created by the bufferization.
548     if (!allocaOp->hasAttr(kBufferAllocationAttr))
549       return WalkResult::skip();
550     // If `onlyLeakingAllocs`, process only ops that are marked as
551     // "skip dealloc".
552     bool skipDealloc = allocaOp->hasAttr(kSkipDeallocAttr);
553     if (onlyLeakingAllocs && !skipDealloc)
554       return WalkResult::skip();
555 
556     // Create alloc.
557     Block *block = allocaOp->getBlock();
558     rewriter.setInsertionPoint(allocaOp);
559     FailureOr<Value> alloc =
560         createAlloc(rewriter, allocaOp->getLoc(), allocaOp.getType(),
561                     allocaOp.dynamicSizes(), options);
562     if (failed(alloc))
563       return WalkResult::interrupt();
564     rewriter.replaceOp(allocaOp, *alloc);
565     if (changed)
566       *changed = true;
567 
568     // Stop here if the buffer should not be deallocated.
569     if (skipDealloc)
570       return WalkResult::advance();
571 
572     // Create dealloc.
573     rewriter.setInsertionPoint(block->getTerminator());
574     if (failed(createDealloc(rewriter, alloc->getLoc(), *alloc, options)))
575       return WalkResult::interrupt();
576 
577     return WalkResult::advance();
578   });
579 
580   return success(!status.wasInterrupted());
581 }
582 
583 /// Try to hoist all new buffer allocations until the next hoisting barrier.
584 // TODO: Consolidate this function with the existing buffer hoisting pass.
585 LogicalResult
586 bufferization::hoistBufferAllocations(Operation *op,
587                                       const BufferizationOptions &options) {
588   // Nothing to do if allocation hoisting is deactivated.
589   if (!options.hoistAllocations)
590     return success();
591 
592   // Gather all buffer allocations that were created by the bufferization.
593   SmallVector<Operation *> allocaOps;
594   op->walk([&](memref::AllocaOp allocaOp) {
595     if (allocaOp->hasAttr(kBufferAllocationAttr))
596       allocaOps.push_back(allocaOp);
597   });
598 
599   for (Operation *allocaOp : allocaOps) {
600     // TODO: Hoisting of allocs with dynamic shape not implemented.
601     if (!allocaOp->getOpOperands().empty())
602       continue;
603 
604     Operation *op = allocaOp->getParentOp();
605     while (op) {
606       if (auto bufferizableOp = dyn_cast<BufferizableOpInterface>(op)) {
607         if (bufferizableOp.isAllocationHoistingBarrier()) {
608           break;
609         }
610       } else {
611         // Op is not bufferizable: It may not be safe to hoist across this op.
612         break;
613       }
614       op = op->getParentOp();
615     }
616 
617     // FuncOp is an allocation hoisting barrier, so this should never happen.
618     assert(op && "allocation hoisting barrier not found");
619 
620     // Nothing to do if the insertion point is in the same block.
621     if (op == allocaOp->getParentOp())
622       continue;
623 
624     // `op` may have multiple blocks. Make sure that we insert in the right one.
625     SmallVector<Block *> blocks;
626     for (Region &r : op->getRegions())
627       for (Block &b : r.getBlocks())
628         blocks.push_back(&b);
629     auto *insertionBlock = llvm::find_if(
630         blocks, [&](Block *b) { return b->findAncestorOpInBlock(*allocaOp); });
631     assert(insertionBlock != blocks.end() && "owning block not found");
632 
633     // Move to the beginning of the block.
634     allocaOp->moveBefore(&(*insertionBlock)->front());
635   }
636 
637   return success();
638 }
639 
640 //===----------------------------------------------------------------------===//
641 // Bufferization-specific BlockAndValueMapping support with debugging.
642 //===----------------------------------------------------------------------===//
643 
644 bool bufferization::isFunctionArgument(Value value) {
645   auto bbArg = value.dyn_cast<BlockArgument>();
646   if (!bbArg)
647     return false;
648   return isa<func::FuncOp>(bbArg.getOwner()->getParentOp());
649 }
650 
651 MemRefType bufferization::getContiguousMemRefType(ShapedType shapedType,
652                                                   Attribute memorySpace) {
653   MemRefLayoutAttrInterface layout = {};
654   return MemRefType::get(shapedType.getShape(), shapedType.getElementType(),
655                          layout, memorySpace);
656 }
657 
658 BaseMemRefType bufferization::getMemRefType(TensorType tensorType,
659                                             const BufferizationOptions &options,
660                                             MemRefLayoutAttrInterface layout,
661                                             Attribute memorySpace) {
662   // Case 1: Unranked memref type.
663   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
664     assert(!layout && "UnrankedTensorType cannot have a layout map");
665     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
666                                    memorySpace);
667   }
668 
669   // Case 2: Ranked memref type with specified layout. If fully dynamic layout
670   // maps are not requested, generate a type with `layout`, which is empty (no
671   // layout map) by default.
672   auto rankedTensorType = tensorType.cast<RankedTensorType>();
673   if (layout || !options.fullyDynamicLayoutMaps) {
674     return MemRefType::get(rankedTensorType.getShape(),
675                            rankedTensorType.getElementType(), layout,
676                            memorySpace);
677   }
678 
679   // Case 3: Ranked memref type with unspecified layout. Choose the most dynamic
680   // one.
681   // TODO: address space decisions to connect with the actual alloc.
682   int64_t dynamicOffset = ShapedType::kDynamicStrideOrOffset;
683   SmallVector<int64_t> dynamicStrides(rankedTensorType.getRank(),
684                                       ShapedType::kDynamicStrideOrOffset);
685   AffineMap stridedLayout = makeStridedLinearLayoutMap(
686       dynamicStrides, dynamicOffset, rankedTensorType.getContext());
687   return MemRefType::get(rankedTensorType.getShape(),
688                          rankedTensorType.getElementType(), stridedLayout,
689                          memorySpace);
690 }
691