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