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 //===----------------------------------------------------------------------===//
378 // Bufferization-specific scoped alloc/dealloc insertion support.
379 //===----------------------------------------------------------------------===//
380 
381 /// Create a memref allocation with the given type and dynamic extents.
382 FailureOr<Value> BufferizationOptions::createAlloc(OpBuilder &b, Location loc,
383                                                    MemRefType type,
384                                                    ValueRange dynShape) const {
385   if (allocationFn)
386     return (*allocationFn)(b, loc, type, dynShape, bufferAlignment);
387 
388   // Default bufferallocation via AllocOp.
389   Value allocated = b.create<memref::AllocOp>(
390       loc, type, dynShape, b.getI64IntegerAttr(bufferAlignment));
391   return allocated;
392 }
393 
394 /// Creates a memref deallocation. The given memref buffer must have been
395 /// allocated using `createAlloc`.
396 LogicalResult BufferizationOptions::createDealloc(OpBuilder &b, Location loc,
397                                                   Value allocatedBuffer) const {
398   if (deallocationFn)
399     return (*deallocationFn)(b, loc, allocatedBuffer);
400 
401   // Default buffer deallocation via DeallocOp.
402   b.create<memref::DeallocOp>(loc, allocatedBuffer);
403   return success();
404 }
405 
406 static MemRefType getContiguousMemRefType(ShapedType shapedType,
407                                           Attribute memorySpace = {}) {
408   MemRefLayoutAttrInterface layout = {};
409   return MemRefType::get(shapedType.getShape(), shapedType.getElementType(),
410                          layout, memorySpace);
411 }
412 
413 /// Compute the type of the `memref` to use for allocating the buffer for
414 /// `shapedValue`. Also returns (by reference in `dynShape`), the value for the
415 /// dynamic dimensions in the returned `memref` type.
416 static MemRefType getAllocationTypeAndShape(OpBuilder &b, Location loc,
417                                             Value shapedValue,
418                                             SmallVectorImpl<Value> &dynShape) {
419   MemRefType allocMemRefType =
420       getContiguousMemRefType(shapedValue.getType().cast<ShapedType>());
421 
422   // Compute the dynamic part of the shape.
423   bool reifiedShapes = false;
424   if (auto rankedOp = dyn_cast_or_null<ReifyRankedShapedTypeOpInterface>(
425           shapedValue.getDefiningOp())) {
426     ReifiedRankedShapedTypeDims resultDims;
427     if (succeeded(rankedOp.reifyResultShapes(b, resultDims))) {
428       reifiedShapes = true;
429       OpResult resultValue = shapedValue.dyn_cast<OpResult>();
430       auto &shape = resultDims[resultValue.getResultNumber()];
431       for (const auto &dim : enumerate(allocMemRefType.getShape()))
432         if (ShapedType::isDynamic(dim.value()))
433           dynShape.push_back(shape[dim.index()]);
434     }
435   }
436 
437   if (!reifiedShapes) {
438     for (const auto &dim : enumerate(allocMemRefType.getShape()))
439       if (ShapedType::isDynamic(dim.value())) {
440         assert((shapedValue.getType().isa<UnrankedMemRefType>() ||
441                 shapedValue.getType().isa<MemRefType>()) &&
442                "expected MemRef type");
443         dynShape.push_back(
444             b.create<memref::DimOp>(loc, shapedValue, dim.index()));
445       }
446   }
447 
448   return allocMemRefType;
449 }
450 
451 static Value createBufferAllocation(OpBuilder &b, Location loc, MemRefType type,
452                                     ValueRange dynShape, bool skipDealloc) {
453   auto allocaOp = b.create<memref::AllocaOp>(loc, type, dynShape);
454   allocaOp->setAttr(kBufferAllocationAttr, b.getUnitAttr());
455   if (skipDealloc)
456     allocaOp->setAttr(kSkipDeallocAttr, b.getUnitAttr());
457   return allocaOp.getResult();
458 }
459 
460 /// Create an allocation after `shapedValue.getDefiningOp` (or at the top of the
461 /// block in case of a bbArg).
462 FailureOr<Value> BufferizationState::createAlloc(OpBuilder &b, Location loc,
463                                                  Value shapedValue,
464                                                  Optional<bool> dealloc) {
465   // Take a guard before anything else.
466   OpBuilder::InsertionGuard g(b);
467 
468   // Compute allocation memref type.
469   assert(shapedValue.getType().isa<ShapedType>());
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   return createBufferAllocation(b, loc, allocMemRefType, dynShape, skipDealloc);
489 }
490 
491 /// Create a memory copy between two memref buffers.
492 LogicalResult BufferizationOptions::createMemCpy(OpBuilder &b, Location loc,
493                                                  Value from, Value to) const {
494   if (memCpyFn)
495     return (*memCpyFn)(b, loc, from, to);
496 
497   b.create<memref::CopyOp>(loc, from, to);
498   return success();
499 }
500 
501 LogicalResult
502 bufferization::createAllocDeallocOps(Operation *op,
503                                      const BufferizationOptions &options,
504                                      bool onlyLeakingAllocs, bool *changed) {
505   IRRewriter rewriter(op->getContext());
506   if (changed)
507     *changed = false;
508 
509   // Bufferization creates memref.alloca ops. After bufferization, these must be
510   // rewritten to alloc/dealloc ops as specified in the bufferization options.
511   WalkResult status = op->walk([&](memref::AllocaOp allocaOp) {
512     // Ignore memref.alloca ops that were not created by the bufferization.
513     if (!allocaOp->hasAttr(kBufferAllocationAttr))
514       return WalkResult::skip();
515     // If `onlyLeakingAllocs`, process only ops that are marked as
516     // "skip dealloc".
517     bool skipDealloc = allocaOp->hasAttr(kSkipDeallocAttr);
518     if (onlyLeakingAllocs && !skipDealloc)
519       return WalkResult::skip();
520 
521     // Create alloc.
522     Block *block = allocaOp->getBlock();
523     rewriter.setInsertionPoint(allocaOp);
524     FailureOr<Value> alloc =
525         options.createAlloc(rewriter, allocaOp->getLoc(), allocaOp.getType(),
526                             allocaOp.dynamicSizes());
527     if (failed(alloc))
528       return WalkResult::interrupt();
529     rewriter.replaceOp(allocaOp, *alloc);
530     if (changed)
531       *changed = true;
532 
533     // Stop here if the buffer should not be deallocated.
534     if (skipDealloc)
535       return WalkResult::advance();
536 
537     // Create dealloc.
538     rewriter.setInsertionPoint(block->getTerminator());
539     if (failed(options.createDealloc(rewriter, alloc->getLoc(), *alloc)))
540       return WalkResult::interrupt();
541 
542     return WalkResult::advance();
543   });
544 
545   return success(!status.wasInterrupted());
546 }
547 
548 //===----------------------------------------------------------------------===//
549 // Bufferization-specific BlockAndValueMapping support with debugging.
550 //===----------------------------------------------------------------------===//
551 
552 bool bufferization::isFunctionArgument(Value value) {
553   auto bbArg = value.dyn_cast<BlockArgument>();
554   if (!bbArg)
555     return false;
556   return isa<func::FuncOp>(bbArg.getOwner()->getParentOp());
557 }
558 
559 BaseMemRefType bufferization::getMemRefType(TensorType tensorType,
560                                             const BufferizationOptions &options,
561                                             MemRefLayoutAttrInterface layout,
562                                             Attribute memorySpace) {
563   // Case 1: Unranked memref type.
564   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
565     assert(!layout && "UnrankedTensorType cannot have a layout map");
566     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
567                                    memorySpace);
568   }
569 
570   // Case 2: Ranked memref type with specified layout.
571   auto rankedTensorType = tensorType.cast<RankedTensorType>();
572   if (layout) {
573     return MemRefType::get(rankedTensorType.getShape(),
574                            rankedTensorType.getElementType(), layout,
575                            memorySpace);
576   }
577 
578   // Case 3: Configured with "fully dynamic layout maps".
579   if (options.unknownTypeConversion ==
580       BufferizationOptions::LayoutMapOption::FullyDynamicLayoutMap)
581     return getMemRefTypeWithFullyDynamicLayout(tensorType, memorySpace);
582 
583   // Case 4: Configured with "static identity layout maps".
584   if (options.unknownTypeConversion ==
585       BufferizationOptions::LayoutMapOption::IdentityLayoutMap)
586     return getMemRefTypeWithStaticIdentityLayout(tensorType, memorySpace);
587 
588   llvm_unreachable("InferLayoutMap is an invalid option");
589 }
590 
591 BaseMemRefType
592 bufferization::getMemRefTypeWithFullyDynamicLayout(TensorType tensorType,
593                                                    Attribute memorySpace) {
594   // Case 1: Unranked memref type.
595   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
596     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
597                                    memorySpace);
598   }
599 
600   // Case 2: Ranked memref type.
601   auto rankedTensorType = tensorType.cast<RankedTensorType>();
602   int64_t dynamicOffset = ShapedType::kDynamicStrideOrOffset;
603   SmallVector<int64_t> dynamicStrides(rankedTensorType.getRank(),
604                                       ShapedType::kDynamicStrideOrOffset);
605   AffineMap stridedLayout = makeStridedLinearLayoutMap(
606       dynamicStrides, dynamicOffset, rankedTensorType.getContext());
607   return MemRefType::get(rankedTensorType.getShape(),
608                          rankedTensorType.getElementType(), stridedLayout,
609                          memorySpace);
610 }
611 
612 /// Return a MemRef type with a static identity layout (i.e., no layout map). If
613 /// the given tensor type is unranked, return an unranked MemRef type.
614 BaseMemRefType
615 bufferization::getMemRefTypeWithStaticIdentityLayout(TensorType tensorType,
616                                                      Attribute memorySpace) {
617   // Case 1: Unranked memref type.
618   if (auto unrankedTensorType = tensorType.dyn_cast<UnrankedTensorType>()) {
619     return UnrankedMemRefType::get(unrankedTensorType.getElementType(),
620                                    memorySpace);
621   }
622 
623   // Case 2: Ranked memref type.
624   auto rankedTensorType = tensorType.cast<RankedTensorType>();
625   MemRefLayoutAttrInterface layout = {};
626   return MemRefType::get(rankedTensorType.getShape(),
627                          rankedTensorType.getElementType(), layout,
628                          memorySpace);
629 }
630