1e07a7fd5SMatthias Springer //===- ModuleBufferization.cpp - Bufferization across Func. Boundaries ----===//
2e07a7fd5SMatthias Springer //
3e07a7fd5SMatthias Springer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e07a7fd5SMatthias Springer // See https://llvm.org/LICENSE.txt for license information.
5e07a7fd5SMatthias Springer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e07a7fd5SMatthias Springer //
7e07a7fd5SMatthias Springer //===----------------------------------------------------------------------===//
8e07a7fd5SMatthias Springer //
9e07a7fd5SMatthias Springer // Module Bufferization is an extension of One-Shot Bufferize that
10e07a7fd5SMatthias Springer // bufferizes function boundaries. It provides `BufferizableOpInterface`
11e07a7fd5SMatthias Springer // implementations for FuncOp, CallOp and ReturnOp.
12e07a7fd5SMatthias Springer //
13e07a7fd5SMatthias Springer // Module Bufferization is run via `runOneShotModuleBufferize(ModuleOp, ...)`.
14e07a7fd5SMatthias Springer // This function analyzes the given module and determines the order of analysis
15e07a7fd5SMatthias Springer // and bufferization: Functions that are called are processed before their
16e07a7fd5SMatthias Springer // respective callers.
17e07a7fd5SMatthias Springer //
18e07a7fd5SMatthias Springer // After analyzing a FuncOp, additional information about its bbArgs is
193490aadfSMatthias Springer // gathered and stored in `FuncAnalysisState`.
20e07a7fd5SMatthias Springer //
21e07a7fd5SMatthias Springer // * `aliasingFuncOpBBArgsAnalysis` determines the equivalent/aliasing bbArgs
22e07a7fd5SMatthias Springer // for
23e07a7fd5SMatthias Springer //   each tensor return value (if any).
24e07a7fd5SMatthias Springer // * `funcOpBbArgReadWriteAnalysis` determines whether or not a tensor bbArg is
25e07a7fd5SMatthias Springer //   read/written.
26e07a7fd5SMatthias Springer //
27e07a7fd5SMatthias Springer // Only tensors that are equivalent to some FuncOp bbArg may be returned.
28e07a7fd5SMatthias Springer // Bufferization currently fails if other tensors (in particular tensors that
29e07a7fd5SMatthias Springer // bufferize out-of-place and result in a new buffer allocation) are returned.
30e07a7fd5SMatthias Springer // In the future, such allocations could be hoisted to the caller.
31e07a7fd5SMatthias Springer //
32e07a7fd5SMatthias Springer // Example: `foo` fails bufferization because %0 is not equivalent to any bbArg.
33e07a7fd5SMatthias Springer // ```
34e07a7fd5SMatthias Springer // func @foo() -> tensor<?xf32> {
35ec55f0bdSMatthias Springer //   %0 = bufferization.alloc_tensor(...) : tensor<?xf32>
36e07a7fd5SMatthias Springer //   return %0 : tensor<?xf32>
37e07a7fd5SMatthias Springer // }
38e07a7fd5SMatthias Springer // ```
39e07a7fd5SMatthias Springer //
40e07a7fd5SMatthias Springer // Module Bufferization implements the following calling convention.
41e07a7fd5SMatthias Springer //
42e07a7fd5SMatthias Springer // * In the absence of conflicts within a FuncOp, the FuncOp's bbArgs may always
43e07a7fd5SMatthias Springer //   be written to in-place.
44e07a7fd5SMatthias Springer // * If a tensor operand of a CallOp is read after the CallOp, the operand of
45e07a7fd5SMatthias Springer //   the CallOp must bufferize out-of-place.
46e07a7fd5SMatthias Springer //
47e07a7fd5SMatthias Springer // Example: The tensor.insert op bufferizes in-place because it is allowed to
48e07a7fd5SMatthias Springer // modify the buffer of `%t1` directly. The CallOp in `caller` must bufferize
49e07a7fd5SMatthias Springer // out-of-place because `%t0` is modified by the callee but read by the
50e07a7fd5SMatthias Springer // tensor.extract op. The analysis of CallOps decides whether an OpOperand must
51e07a7fd5SMatthias Springer // bufferize out-of-place based on results of `funcOpBbArgReadWriteAnalysis`.
52e07a7fd5SMatthias Springer // ```
53e07a7fd5SMatthias Springer // func @callee(%t1 : tensor<?xf32>) -> tensor<?xf32> {
54e07a7fd5SMatthias Springer //   %f = ... : f32
55e07a7fd5SMatthias Springer //   %0 = tensor.insert %f into %t1[...] : tensor<?xf32>
56e07a7fd5SMatthias Springer //   return %0 : tensor<?xf32>
57e07a7fd5SMatthias Springer // }
58e07a7fd5SMatthias Springer //
59e07a7fd5SMatthias Springer // func @caller() -> () {
60e07a7fd5SMatthias Springer //   %t0 = ... : tensor<?xf32>
61e07a7fd5SMatthias Springer //   %1 = call @callee(%t0) : (tensor<?xf32>) -> (tensor<?xf32>)
62e07a7fd5SMatthias Springer //   %2 = tensor.extract %1[...]  : tensor<?xf32>
63e07a7fd5SMatthias Springer // }
64e07a7fd5SMatthias Springer // ```
65e07a7fd5SMatthias Springer //
66e07a7fd5SMatthias Springer // Note: If a function is external, `funcOpBbArgReadWriteAnalysis` cannot
67e07a7fd5SMatthias Springer // analyze the function body. In such a case, the CallOp analysis conservatively
68e07a7fd5SMatthias Springer // assumes that each tensor OpOperand is both read and written.
69e07a7fd5SMatthias Springer //
70e07a7fd5SMatthias Springer // TODO: Add FuncOp attributes so that bbArgs of external FuncOps can be marked
71e07a7fd5SMatthias Springer // as "not reading" and/or "not writing".
72e07a7fd5SMatthias Springer 
73e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h"
74e07a7fd5SMatthias Springer 
75e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
76e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/IR/Bufferization.h"
77e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"
78e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.h"
79e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
80e07a7fd5SMatthias Springer #include "mlir/Dialect/Func/IR/FuncOps.h"
81e07a7fd5SMatthias Springer #include "mlir/Dialect/MemRef/IR/MemRef.h"
82e07a7fd5SMatthias Springer #include "mlir/IR/Operation.h"
83e07a7fd5SMatthias Springer 
84e07a7fd5SMatthias Springer using namespace mlir;
85e07a7fd5SMatthias Springer using namespace mlir::bufferization;
86e07a7fd5SMatthias Springer using namespace mlir::bufferization::func_ext;
87e07a7fd5SMatthias Springer 
88e07a7fd5SMatthias Springer /// A mapping of FuncOps to their callers.
89e07a7fd5SMatthias Springer using FuncCallerMap = DenseMap<func::FuncOp, DenseSet<Operation *>>;
90e07a7fd5SMatthias Springer 
91e07a7fd5SMatthias Springer /// Get FuncAnalysisState.
92e07a7fd5SMatthias Springer static const FuncAnalysisState &
93e07a7fd5SMatthias Springer getFuncAnalysisState(const AnalysisState &state) {
94e07a7fd5SMatthias Springer   Optional<const FuncAnalysisState *> maybeState =
95e07a7fd5SMatthias Springer       state.getDialectState<FuncAnalysisState>(
96e07a7fd5SMatthias Springer           func::FuncDialect::getDialectNamespace());
97e07a7fd5SMatthias Springer   assert(maybeState.hasValue() && "FuncAnalysisState does not exist");
98e07a7fd5SMatthias Springer   return **maybeState;
99e07a7fd5SMatthias Springer }
100e07a7fd5SMatthias Springer 
101e07a7fd5SMatthias Springer /// Get or create FuncAnalysisState.
102e07a7fd5SMatthias Springer static FuncAnalysisState &getFuncAnalysisState(AnalysisState &state) {
103e07a7fd5SMatthias Springer   return state.getOrCreateDialectState<FuncAnalysisState>(
104e07a7fd5SMatthias Springer       func::FuncDialect::getDialectNamespace());
105e07a7fd5SMatthias Springer }
106e07a7fd5SMatthias Springer 
107e07a7fd5SMatthias Springer /// Return the state (phase) of analysis of the FuncOp.
10802d3499aSStella Laurenzo /// Used for debug modes.
10902d3499aSStella Laurenzo LLVM_ATTRIBUTE_UNUSED
110e07a7fd5SMatthias Springer static FuncOpAnalysisState getFuncOpAnalysisState(const AnalysisState &state,
111e07a7fd5SMatthias Springer                                                   func::FuncOp funcOp) {
112e07a7fd5SMatthias Springer   const FuncAnalysisState &funcState = getFuncAnalysisState(state);
113e07a7fd5SMatthias Springer   auto it = funcState.analyzedFuncOps.find(funcOp);
114e07a7fd5SMatthias Springer   if (it == funcState.analyzedFuncOps.end())
115e07a7fd5SMatthias Springer     return FuncOpAnalysisState::NotAnalyzed;
116e07a7fd5SMatthias Springer   return it->second;
117e07a7fd5SMatthias Springer }
118e07a7fd5SMatthias Springer 
119e07a7fd5SMatthias Springer /// Return the unique ReturnOp that terminates `funcOp`.
120e07a7fd5SMatthias Springer /// Return nullptr if there is no such unique ReturnOp.
121e07a7fd5SMatthias Springer static func::ReturnOp getAssumedUniqueReturnOp(func::FuncOp funcOp) {
122e07a7fd5SMatthias Springer   func::ReturnOp returnOp;
123e07a7fd5SMatthias Springer   for (Block &b : funcOp.getBody()) {
124e07a7fd5SMatthias Springer     if (auto candidateOp = dyn_cast<func::ReturnOp>(b.getTerminator())) {
125e07a7fd5SMatthias Springer       if (returnOp)
126e07a7fd5SMatthias Springer         return nullptr;
127e07a7fd5SMatthias Springer       returnOp = candidateOp;
128e07a7fd5SMatthias Springer     }
129e07a7fd5SMatthias Springer   }
130e07a7fd5SMatthias Springer   return returnOp;
131e07a7fd5SMatthias Springer }
132e07a7fd5SMatthias Springer 
133e07a7fd5SMatthias Springer namespace {
134e07a7fd5SMatthias Springer 
135e07a7fd5SMatthias Springer /// Annotate IR with the results of the analysis. For testing purposes only.
136e07a7fd5SMatthias Springer static void annotateEquivalentReturnBbArg(OpOperand &returnVal,
137e07a7fd5SMatthias Springer                                           BlockArgument bbArg) {
138e07a7fd5SMatthias Springer   const char *kEquivalentArgsAttr = "__equivalent_func_args__";
139e07a7fd5SMatthias Springer   Operation *op = returnVal.getOwner();
140e07a7fd5SMatthias Springer 
141e07a7fd5SMatthias Springer   SmallVector<int64_t> equivBbArgs;
142e07a7fd5SMatthias Springer   if (op->hasAttr(kEquivalentArgsAttr)) {
143e07a7fd5SMatthias Springer     auto attr = op->getAttr(kEquivalentArgsAttr).cast<ArrayAttr>();
144e07a7fd5SMatthias Springer     equivBbArgs = llvm::to_vector<4>(llvm::map_range(attr, [](Attribute a) {
145e07a7fd5SMatthias Springer       return a.cast<IntegerAttr>().getValue().getSExtValue();
146e07a7fd5SMatthias Springer     }));
147e07a7fd5SMatthias Springer   } else {
148e07a7fd5SMatthias Springer     equivBbArgs.append(op->getNumOperands(), -1);
149e07a7fd5SMatthias Springer   }
150e07a7fd5SMatthias Springer   equivBbArgs[returnVal.getOperandNumber()] = bbArg.getArgNumber();
151e07a7fd5SMatthias Springer 
152e07a7fd5SMatthias Springer   OpBuilder b(op->getContext());
153e07a7fd5SMatthias Springer   op->setAttr(kEquivalentArgsAttr, b.getI64ArrayAttr(equivBbArgs));
154e07a7fd5SMatthias Springer }
155e07a7fd5SMatthias Springer 
156e07a7fd5SMatthias Springer /// Store function BlockArguments that are equivalent to/aliasing a returned
157e07a7fd5SMatthias Springer /// value in FuncAnalysisState.
1583490aadfSMatthias Springer static LogicalResult aliasingFuncOpBBArgsAnalysis(FuncOp funcOp,
1593490aadfSMatthias Springer                                                   OneShotAnalysisState &state) {
160e07a7fd5SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
161e07a7fd5SMatthias Springer 
162e07a7fd5SMatthias Springer   // Support only single return-terminated block in the function.
163e07a7fd5SMatthias Springer   func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
164e07a7fd5SMatthias Springer   assert(returnOp && "expected func with single return op");
165e07a7fd5SMatthias Springer 
166e07a7fd5SMatthias Springer   for (OpOperand &returnVal : returnOp->getOpOperands())
167e07a7fd5SMatthias Springer     if (returnVal.get().getType().isa<RankedTensorType>())
168e07a7fd5SMatthias Springer       for (BlockArgument bbArg : funcOp.getArguments())
169e07a7fd5SMatthias Springer         if (bbArg.getType().isa<RankedTensorType>()) {
170e07a7fd5SMatthias Springer           int64_t returnIdx = returnVal.getOperandNumber();
171e07a7fd5SMatthias Springer           int64_t bbArgIdx = bbArg.getArgNumber();
1723490aadfSMatthias Springer           if (state.areEquivalentBufferizedValues(returnVal.get(), bbArg)) {
173e07a7fd5SMatthias Springer             funcState.equivalentFuncArgs[funcOp][returnIdx] = bbArgIdx;
174e07a7fd5SMatthias Springer             if (state.getOptions().testAnalysisOnly)
175e07a7fd5SMatthias Springer               annotateEquivalentReturnBbArg(returnVal, bbArg);
176e07a7fd5SMatthias Springer           }
1773490aadfSMatthias Springer           if (state.areAliasingBufferizedValues(returnVal.get(), bbArg)) {
178e07a7fd5SMatthias Springer             funcState.aliasingFuncArgs[funcOp][returnIdx].push_back(bbArgIdx);
179e07a7fd5SMatthias Springer             funcState.aliasingReturnVals[funcOp][bbArgIdx].push_back(returnIdx);
180e07a7fd5SMatthias Springer           }
181e07a7fd5SMatthias Springer         }
182e07a7fd5SMatthias Springer 
183e07a7fd5SMatthias Springer   return success();
184e07a7fd5SMatthias Springer }
185e07a7fd5SMatthias Springer 
186e07a7fd5SMatthias Springer static void annotateFuncArgAccess(func::FuncOp funcOp, BlockArgument bbArg,
187e07a7fd5SMatthias Springer                                   bool isRead, bool isWritten) {
188e07a7fd5SMatthias Springer   OpBuilder b(funcOp.getContext());
189e07a7fd5SMatthias Springer   Attribute accessType;
190e07a7fd5SMatthias Springer   if (isRead && isWritten) {
191e07a7fd5SMatthias Springer     accessType = b.getStringAttr("read-write");
192e07a7fd5SMatthias Springer   } else if (isRead) {
193e07a7fd5SMatthias Springer     accessType = b.getStringAttr("read");
194e07a7fd5SMatthias Springer   } else if (isWritten) {
195e07a7fd5SMatthias Springer     accessType = b.getStringAttr("write");
196e07a7fd5SMatthias Springer   } else {
197e07a7fd5SMatthias Springer     accessType = b.getStringAttr("none");
198e07a7fd5SMatthias Springer   }
199e07a7fd5SMatthias Springer   funcOp.setArgAttr(bbArg.getArgNumber(), "bufferization.access", accessType);
200e07a7fd5SMatthias Springer }
201e07a7fd5SMatthias Springer 
2023490aadfSMatthias Springer /// Determine which FuncOp bbArgs are read and which are written. When run on a
2033490aadfSMatthias Springer /// function with unknown ops, we conservatively assume that such ops bufferize
2043490aadfSMatthias Springer /// to a read + write.
2053490aadfSMatthias Springer static LogicalResult funcOpBbArgReadWriteAnalysis(FuncOp funcOp,
2063490aadfSMatthias Springer                                                   OneShotAnalysisState &state) {
207e07a7fd5SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
208e07a7fd5SMatthias Springer 
209e07a7fd5SMatthias Springer   // If the function has no body, conservatively assume that all args are
210e07a7fd5SMatthias Springer   // read + written.
211e07a7fd5SMatthias Springer   if (funcOp.getBody().empty()) {
212e07a7fd5SMatthias Springer     for (BlockArgument bbArg : funcOp.getArguments()) {
213e07a7fd5SMatthias Springer       funcState.readBbArgs[funcOp].insert(bbArg.getArgNumber());
214e07a7fd5SMatthias Springer       funcState.writtenBbArgs[funcOp].insert(bbArg.getArgNumber());
215e07a7fd5SMatthias Springer     }
216e07a7fd5SMatthias Springer 
217e07a7fd5SMatthias Springer     return success();
218e07a7fd5SMatthias Springer   }
219e07a7fd5SMatthias Springer 
220e07a7fd5SMatthias Springer   for (BlockArgument bbArg : funcOp.getArguments()) {
221e07a7fd5SMatthias Springer     if (!bbArg.getType().isa<TensorType>())
222e07a7fd5SMatthias Springer       continue;
223e07a7fd5SMatthias Springer     bool isRead = state.isValueRead(bbArg);
2243490aadfSMatthias Springer     bool isWritten = state.isValueWritten(bbArg);
225e07a7fd5SMatthias Springer     if (state.getOptions().testAnalysisOnly)
226e07a7fd5SMatthias Springer       annotateFuncArgAccess(funcOp, bbArg, isRead, isWritten);
227e07a7fd5SMatthias Springer     if (isRead)
228e07a7fd5SMatthias Springer       funcState.readBbArgs[funcOp].insert(bbArg.getArgNumber());
229e07a7fd5SMatthias Springer     if (isWritten)
230e07a7fd5SMatthias Springer       funcState.writtenBbArgs[funcOp].insert(bbArg.getArgNumber());
231e07a7fd5SMatthias Springer   }
232e07a7fd5SMatthias Springer 
233e07a7fd5SMatthias Springer   return success();
234e07a7fd5SMatthias Springer }
235e07a7fd5SMatthias Springer } // namespace
236e07a7fd5SMatthias Springer 
237e07a7fd5SMatthias Springer /// Remove bufferization attributes on FuncOp arguments.
238e07a7fd5SMatthias Springer static void removeBufferizationAttributes(BlockArgument bbArg) {
239e07a7fd5SMatthias Springer   auto funcOp = cast<func::FuncOp>(bbArg.getOwner()->getParentOp());
240e07a7fd5SMatthias Springer   funcOp.removeArgAttr(bbArg.getArgNumber(),
241e07a7fd5SMatthias Springer                        BufferizationDialect::kBufferLayoutAttrName);
242e07a7fd5SMatthias Springer   funcOp.removeArgAttr(bbArg.getArgNumber(),
243e07a7fd5SMatthias Springer                        BufferizationDialect::kWritableAttrName);
244e07a7fd5SMatthias Springer }
245e07a7fd5SMatthias Springer 
246e07a7fd5SMatthias Springer /// Return the func::FuncOp called by `callOp`.
247e07a7fd5SMatthias Springer static func::FuncOp getCalledFunction(CallOpInterface callOp) {
248e07a7fd5SMatthias Springer   SymbolRefAttr sym = callOp.getCallableForCallee().dyn_cast<SymbolRefAttr>();
249e07a7fd5SMatthias Springer   if (!sym)
250e07a7fd5SMatthias Springer     return nullptr;
251e07a7fd5SMatthias Springer   return dyn_cast_or_null<func::FuncOp>(
252e07a7fd5SMatthias Springer       SymbolTable::lookupNearestSymbolFrom(callOp, sym));
253e07a7fd5SMatthias Springer }
254e07a7fd5SMatthias Springer 
255e07a7fd5SMatthias Springer /// Gather equivalence info of CallOps.
256e07a7fd5SMatthias Springer /// Note: This only adds new equivalence info if the called function was already
257e07a7fd5SMatthias Springer /// analyzed.
258e07a7fd5SMatthias Springer // TODO: This does not handle cyclic function call graphs etc.
259e07a7fd5SMatthias Springer static void equivalenceAnalysis(func::FuncOp funcOp,
260e07a7fd5SMatthias Springer                                 BufferizationAliasInfo &aliasInfo,
261e07a7fd5SMatthias Springer                                 FuncAnalysisState &funcState) {
262e07a7fd5SMatthias Springer   funcOp->walk([&](func::CallOp callOp) {
263e07a7fd5SMatthias Springer     func::FuncOp calledFunction = getCalledFunction(callOp);
264e07a7fd5SMatthias Springer     assert(calledFunction && "could not retrieved called func::FuncOp");
265e07a7fd5SMatthias Springer 
266e07a7fd5SMatthias Springer     // No equivalence info available for the called function.
267e07a7fd5SMatthias Springer     if (!funcState.equivalentFuncArgs.count(calledFunction))
268e07a7fd5SMatthias Springer       return WalkResult::skip();
269e07a7fd5SMatthias Springer 
270e07a7fd5SMatthias Springer     for (auto it : funcState.equivalentFuncArgs[calledFunction]) {
271e07a7fd5SMatthias Springer       int64_t returnIdx = it.first;
272e07a7fd5SMatthias Springer       int64_t bbargIdx = it.second;
273e07a7fd5SMatthias Springer       Value returnVal = callOp.getResult(returnIdx);
274e07a7fd5SMatthias Springer       Value argVal = callOp->getOperand(bbargIdx);
275e07a7fd5SMatthias Springer       aliasInfo.unionEquivalenceClasses(returnVal, argVal);
276e07a7fd5SMatthias Springer     }
277e07a7fd5SMatthias Springer 
278e07a7fd5SMatthias Springer     return WalkResult::advance();
279e07a7fd5SMatthias Springer   });
280e07a7fd5SMatthias Springer }
281e07a7fd5SMatthias Springer 
282e07a7fd5SMatthias Springer /// Store all functions of the `moduleOp` in `orderedFuncOps`, sorted by
283e07a7fd5SMatthias Springer /// callee-caller order (i.e. callees without callers first).
284e07a7fd5SMatthias Springer /// Store the map of FuncOp to all its callers in `callerMap`.
285e07a7fd5SMatthias Springer /// Return `failure()` if a cycle of calls is detected or if we are unable to
286e07a7fd5SMatthias Springer /// retrieve the called FuncOp from any CallOpInterface.
287e07a7fd5SMatthias Springer static LogicalResult
288e07a7fd5SMatthias Springer getFuncOpsOrderedByCalls(ModuleOp moduleOp,
289e07a7fd5SMatthias Springer                          SmallVectorImpl<func::FuncOp> &orderedFuncOps,
290e07a7fd5SMatthias Springer                          FuncCallerMap &callerMap) {
291e07a7fd5SMatthias Springer   // For each FuncOp, the set of functions called by it (i.e. the union of
292e07a7fd5SMatthias Springer   // symbols of all nested CallOpInterfaceOp).
293e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, DenseSet<func::FuncOp>> calledBy;
294e07a7fd5SMatthias Springer   // For each FuncOp, the number of CallOpInterface it contains.
295e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, unsigned> numberCallOpsContainedInFuncOp;
296e07a7fd5SMatthias Springer   WalkResult res = moduleOp.walk([&](func::FuncOp funcOp) -> WalkResult {
297e07a7fd5SMatthias Springer     if (!funcOp.getBody().empty()) {
298e07a7fd5SMatthias Springer       func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
299e07a7fd5SMatthias Springer       if (!returnOp)
300e07a7fd5SMatthias Springer         return funcOp->emitError()
301e07a7fd5SMatthias Springer                << "cannot bufferize a FuncOp with tensors and "
302e07a7fd5SMatthias Springer                   "without a unique ReturnOp";
303e07a7fd5SMatthias Springer     }
304e07a7fd5SMatthias Springer 
305e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp[funcOp] = 0;
306e07a7fd5SMatthias Springer     return funcOp.walk([&](CallOpInterface callOp) -> WalkResult {
307e07a7fd5SMatthias Springer       // Only support CallOp for now.
308e07a7fd5SMatthias Springer       if (!isa<func::CallOp>(callOp.getOperation()))
309e07a7fd5SMatthias Springer         return callOp->emitError() << "expected a CallOp";
310e07a7fd5SMatthias Springer       func::FuncOp calledFunction = getCalledFunction(callOp);
311e07a7fd5SMatthias Springer       assert(calledFunction && "could not retrieved called func::FuncOp");
31286fd1c13SBenjamin Kramer       callerMap[calledFunction].insert(callOp);
31386fd1c13SBenjamin Kramer       if (calledBy[calledFunction].insert(funcOp).second) {
314e07a7fd5SMatthias Springer         numberCallOpsContainedInFuncOp[funcOp]++;
315e07a7fd5SMatthias Springer       }
316e07a7fd5SMatthias Springer       return WalkResult::advance();
317e07a7fd5SMatthias Springer     });
318e07a7fd5SMatthias Springer   });
319e07a7fd5SMatthias Springer   if (res.wasInterrupted())
320e07a7fd5SMatthias Springer     return failure();
321e07a7fd5SMatthias Springer   // Iteratively remove function operation that do not call any of the
322e07a7fd5SMatthias Springer   // functions remaining in the callCounter map and add them to the worklist.
323e07a7fd5SMatthias Springer   while (!numberCallOpsContainedInFuncOp.empty()) {
324e07a7fd5SMatthias Springer     auto it = llvm::find_if(numberCallOpsContainedInFuncOp,
325e07a7fd5SMatthias Springer                             [](auto entry) { return entry.getSecond() == 0; });
326e07a7fd5SMatthias Springer     if (it == numberCallOpsContainedInFuncOp.end())
327e07a7fd5SMatthias Springer       return moduleOp.emitOpError(
328e07a7fd5SMatthias Springer           "expected callgraph to be free of circular dependencies.");
329e07a7fd5SMatthias Springer     orderedFuncOps.push_back(it->getFirst());
330e07a7fd5SMatthias Springer     for (auto callee : calledBy[it->getFirst()])
331e07a7fd5SMatthias Springer       numberCallOpsContainedInFuncOp[callee]--;
332e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp.erase(it);
333e07a7fd5SMatthias Springer   }
334e07a7fd5SMatthias Springer   return success();
335e07a7fd5SMatthias Springer }
336e07a7fd5SMatthias Springer 
337e07a7fd5SMatthias Springer /// Set the attribute that triggers inplace bufferization on a FuncOp argument
338e07a7fd5SMatthias Springer /// `bbArg`.
339e07a7fd5SMatthias Springer static void setInPlaceFuncArgument(BlockArgument bbArg, bool inPlace) {
340e07a7fd5SMatthias Springer   auto funcOp = cast<func::FuncOp>(bbArg.getOwner()->getParentOp());
341e07a7fd5SMatthias Springer   funcOp.setArgAttr(bbArg.getArgNumber(),
342e07a7fd5SMatthias Springer                     BufferizableOpInterface::kInplaceableAttrName,
343e07a7fd5SMatthias Springer                     BoolAttr::get(bbArg.getContext(), inPlace));
344e07a7fd5SMatthias Springer }
345e07a7fd5SMatthias Springer 
346e07a7fd5SMatthias Springer /// Annotate the IR with the result of the analysis. For testing/debugging only.
347e07a7fd5SMatthias Springer static void annotateOpsWithBufferizationMarkers(func::FuncOp funcOp,
348e07a7fd5SMatthias Springer                                                 const AnalysisState &state) {
349e07a7fd5SMatthias Springer   auto bufferizableOp = cast<BufferizableOpInterface>(funcOp.getOperation());
350e07a7fd5SMatthias Springer   for (BlockArgument bbArg : funcOp.getArguments())
351e07a7fd5SMatthias Springer     if (bbArg.getType().isa<TensorType>())
352e07a7fd5SMatthias Springer       setInPlaceFuncArgument(bbArg, bufferizableOp.isWritable(bbArg, state));
353e07a7fd5SMatthias Springer }
354e07a7fd5SMatthias Springer 
355e07a7fd5SMatthias Springer /// Fold return values that are memref casts and update function return types.
356e07a7fd5SMatthias Springer ///
357e07a7fd5SMatthias Springer /// During FuncOp bufferization, the exact type of the returned memrefs (if any)
358e07a7fd5SMatthias Springer /// is not known yet. Therefore, the bufferization uses memref types with the
359e07a7fd5SMatthias Springer /// most generic layout map as function return types. After bufferizing the
360e07a7fd5SMatthias Springer /// entire function body, a more concise memref type can potentially be used for
361e07a7fd5SMatthias Springer /// the return type of the function.
362e07a7fd5SMatthias Springer static void foldMemRefCasts(func::FuncOp funcOp) {
363e07a7fd5SMatthias Springer   if (funcOp.getBody().empty())
364e07a7fd5SMatthias Springer     return;
365e07a7fd5SMatthias Springer 
366e07a7fd5SMatthias Springer   func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
367e07a7fd5SMatthias Springer   SmallVector<Type> resultTypes;
368e07a7fd5SMatthias Springer 
369e07a7fd5SMatthias Springer   for (OpOperand &operand : returnOp->getOpOperands()) {
370e07a7fd5SMatthias Springer     if (auto castOp = operand.get().getDefiningOp<memref::CastOp>()) {
371e07a7fd5SMatthias Springer       operand.set(castOp.source());
372e07a7fd5SMatthias Springer       resultTypes.push_back(castOp.source().getType());
373e07a7fd5SMatthias Springer     } else {
374e07a7fd5SMatthias Springer       resultTypes.push_back(operand.get().getType());
375e07a7fd5SMatthias Springer     }
376e07a7fd5SMatthias Springer   }
377e07a7fd5SMatthias Springer 
378e07a7fd5SMatthias Springer   auto newFuncType = FunctionType::get(
379e07a7fd5SMatthias Springer       funcOp.getContext(), funcOp.getFunctionType().getInputs(), resultTypes);
380e07a7fd5SMatthias Springer   funcOp.setType(newFuncType);
381e07a7fd5SMatthias Springer }
382e07a7fd5SMatthias Springer 
383*f470f8cbSMatthias Springer LogicalResult
384*f470f8cbSMatthias Springer mlir::bufferization::analyzeModuleOp(ModuleOp moduleOp,
385*f470f8cbSMatthias Springer                                      OneShotAnalysisState &state) {
386*f470f8cbSMatthias Springer   OneShotBufferizationOptions options =
387*f470f8cbSMatthias Springer       static_cast<const OneShotBufferizationOptions &>(state.getOptions());
388d6dab38aSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
389d6dab38aSMatthias Springer          "expected that function boundary bufferization is activated");
390*f470f8cbSMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
391*f470f8cbSMatthias Springer   BufferizationAliasInfo &aliasInfo = state.getAliasInfo();
392e07a7fd5SMatthias Springer 
393e07a7fd5SMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
394e07a7fd5SMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
395e07a7fd5SMatthias Springer 
396e07a7fd5SMatthias Springer   // A mapping of FuncOps to their callers.
397e07a7fd5SMatthias Springer   FuncCallerMap callerMap;
398e07a7fd5SMatthias Springer 
399e07a7fd5SMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
400e07a7fd5SMatthias Springer     return failure();
401e07a7fd5SMatthias Springer 
402e07a7fd5SMatthias Springer   // Analyze ops.
403e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
404e07a7fd5SMatthias Springer     // No body => no analysis.
405e07a7fd5SMatthias Springer     if (funcOp.getBody().empty())
406e07a7fd5SMatthias Springer       continue;
407e07a7fd5SMatthias Springer 
408e07a7fd5SMatthias Springer     // Now analyzing function.
409e07a7fd5SMatthias Springer     funcState.startFunctionAnalysis(funcOp);
410e07a7fd5SMatthias Springer 
411e07a7fd5SMatthias Springer     // Gather equivalence info for CallOps.
412e07a7fd5SMatthias Springer     equivalenceAnalysis(funcOp, aliasInfo, funcState);
413e07a7fd5SMatthias Springer 
414e07a7fd5SMatthias Springer     // Analyze funcOp.
415*f470f8cbSMatthias Springer     if (failed(analyzeOp(funcOp, state)))
416e07a7fd5SMatthias Springer       return failure();
417e07a7fd5SMatthias Springer 
4183490aadfSMatthias Springer     // Run some extra function analyses.
419*f470f8cbSMatthias Springer     if (failed(aliasingFuncOpBBArgsAnalysis(funcOp, state)) ||
420*f470f8cbSMatthias Springer         failed(funcOpBbArgReadWriteAnalysis(funcOp, state)))
4213490aadfSMatthias Springer       return failure();
4223490aadfSMatthias Springer 
423e07a7fd5SMatthias Springer     // Mark op as fully analyzed.
424e07a7fd5SMatthias Springer     funcState.analyzedFuncOps[funcOp] = FuncOpAnalysisState::Analyzed;
425e07a7fd5SMatthias Springer 
426e07a7fd5SMatthias Springer     // Add annotations to function arguments.
427e07a7fd5SMatthias Springer     if (options.testAnalysisOnly)
428*f470f8cbSMatthias Springer       annotateOpsWithBufferizationMarkers(funcOp, state);
429e07a7fd5SMatthias Springer   }
430e07a7fd5SMatthias Springer 
431e07a7fd5SMatthias Springer   return success();
432*f470f8cbSMatthias Springer }
433*f470f8cbSMatthias Springer 
434*f470f8cbSMatthias Springer LogicalResult mlir::bufferization::bufferizeModuleOp(
435*f470f8cbSMatthias Springer     ModuleOp moduleOp, const OneShotAnalysisState &analysisState) {
436*f470f8cbSMatthias Springer   auto const &options = static_cast<const OneShotBufferizationOptions &>(
437*f470f8cbSMatthias Springer       analysisState.getOptions());
438*f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
439*f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
440*f470f8cbSMatthias Springer   IRRewriter rewriter(moduleOp.getContext());
441*f470f8cbSMatthias Springer   BufferizationState bufferizationState(analysisState);
442*f470f8cbSMatthias Springer 
443*f470f8cbSMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
444*f470f8cbSMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
445*f470f8cbSMatthias Springer 
446*f470f8cbSMatthias Springer   // A mapping of FuncOps to their callers.
447*f470f8cbSMatthias Springer   FuncCallerMap callerMap;
448*f470f8cbSMatthias Springer 
449*f470f8cbSMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
450*f470f8cbSMatthias Springer     return failure();
451e07a7fd5SMatthias Springer 
452e07a7fd5SMatthias Springer   // Bufferize functions.
453e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
454e07a7fd5SMatthias Springer     // Note: It would be good to apply cleanups here but we cannot as aliasInfo
455e07a7fd5SMatthias Springer     // would be invalidated.
456e07a7fd5SMatthias Springer     if (failed(bufferizeOp(funcOp, bufferizationState)))
457e07a7fd5SMatthias Springer       return failure();
458f287da8aSMatthias Springer     // Change buffer return types to more precise layout maps.
459f287da8aSMatthias Springer     if (options.functionBoundaryTypeConversion ==
460f287da8aSMatthias Springer         BufferizationOptions::LayoutMapOption::InferLayoutMap)
461e07a7fd5SMatthias Springer       foldMemRefCasts(funcOp);
462e07a7fd5SMatthias Springer   }
463e07a7fd5SMatthias Springer 
464e07a7fd5SMatthias Springer   // Check result.
465e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
466e07a7fd5SMatthias Springer     if (!options.allowReturnAllocs &&
467e07a7fd5SMatthias Springer         llvm::any_of(funcOp.getFunctionType().getResults(), [](Type t) {
468e07a7fd5SMatthias Springer           return t.isa<MemRefType, UnrankedMemRefType>();
469e07a7fd5SMatthias Springer         })) {
470e07a7fd5SMatthias Springer       funcOp->emitError("memref return type is unsupported");
471e07a7fd5SMatthias Springer       return failure();
472e07a7fd5SMatthias Springer     }
473e07a7fd5SMatthias Springer   }
474e07a7fd5SMatthias Springer 
475e07a7fd5SMatthias Springer   // Finalize all buffers.
476e07a7fd5SMatthias Springer   if (failed(finalizeBuffers(moduleOp, options)))
477e07a7fd5SMatthias Springer     return failure();
478e07a7fd5SMatthias Springer 
479e07a7fd5SMatthias Springer   // Post-pass cleanup of function argument attributes.
480e07a7fd5SMatthias Springer   moduleOp.walk([&](func::FuncOp op) {
481e07a7fd5SMatthias Springer     for (BlockArgument bbArg : op.getArguments())
482e07a7fd5SMatthias Springer       removeBufferizationAttributes(bbArg);
483e07a7fd5SMatthias Springer   });
484e07a7fd5SMatthias Springer 
485e07a7fd5SMatthias Springer   return success();
486e07a7fd5SMatthias Springer }
487*f470f8cbSMatthias Springer 
488*f470f8cbSMatthias Springer LogicalResult mlir::bufferization::runOneShotModuleBufferize(
489*f470f8cbSMatthias Springer     ModuleOp moduleOp, OneShotBufferizationOptions options) {
490*f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
491*f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
492*f470f8cbSMatthias Springer   OneShotAnalysisState analysisState(moduleOp, options);
493*f470f8cbSMatthias Springer   if (failed(analyzeModuleOp(moduleOp, analysisState)))
494*f470f8cbSMatthias Springer     return failure();
495*f470f8cbSMatthias Springer   if (options.testAnalysisOnly)
496*f470f8cbSMatthias Springer     return success();
497*f470f8cbSMatthias Springer   if (failed(bufferizeModuleOp(moduleOp, analysisState)))
498*f470f8cbSMatthias Springer     return failure();
499*f470f8cbSMatthias Springer   return success();
500*f470f8cbSMatthias Springer }
501