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 // Module Bufferization implements the following calling convention.
28e07a7fd5SMatthias Springer //
29e07a7fd5SMatthias Springer // * In the absence of conflicts within a FuncOp, the FuncOp's bbArgs may always
30e07a7fd5SMatthias Springer //   be written to in-place.
31e07a7fd5SMatthias Springer // * If a tensor operand of a CallOp is read after the CallOp, the operand of
32e07a7fd5SMatthias Springer //   the CallOp must bufferize out-of-place.
33e07a7fd5SMatthias Springer //
34e07a7fd5SMatthias Springer // Example: The tensor.insert op bufferizes in-place because it is allowed to
35e07a7fd5SMatthias Springer // modify the buffer of `%t1` directly. The CallOp in `caller` must bufferize
36e07a7fd5SMatthias Springer // out-of-place because `%t0` is modified by the callee but read by the
37e07a7fd5SMatthias Springer // tensor.extract op. The analysis of CallOps decides whether an OpOperand must
38e07a7fd5SMatthias Springer // bufferize out-of-place based on results of `funcOpBbArgReadWriteAnalysis`.
39e07a7fd5SMatthias Springer // ```
40e07a7fd5SMatthias Springer // func @callee(%t1 : tensor<?xf32>) -> tensor<?xf32> {
41e07a7fd5SMatthias Springer //   %f = ... : f32
42e07a7fd5SMatthias Springer //   %0 = tensor.insert %f into %t1[...] : tensor<?xf32>
43e07a7fd5SMatthias Springer //   return %0 : tensor<?xf32>
44e07a7fd5SMatthias Springer // }
45e07a7fd5SMatthias Springer //
46e07a7fd5SMatthias Springer // func @caller() -> () {
47e07a7fd5SMatthias Springer //   %t0 = ... : tensor<?xf32>
48e07a7fd5SMatthias Springer //   %1 = call @callee(%t0) : (tensor<?xf32>) -> (tensor<?xf32>)
49e07a7fd5SMatthias Springer //   %2 = tensor.extract %1[...]  : tensor<?xf32>
50e07a7fd5SMatthias Springer // }
51e07a7fd5SMatthias Springer // ```
52e07a7fd5SMatthias Springer //
53e07a7fd5SMatthias Springer // Note: If a function is external, `funcOpBbArgReadWriteAnalysis` cannot
54e07a7fd5SMatthias Springer // analyze the function body. In such a case, the CallOp analysis conservatively
55e07a7fd5SMatthias Springer // assumes that each tensor OpOperand is both read and written.
56e07a7fd5SMatthias Springer //
57e07a7fd5SMatthias Springer // TODO: Add FuncOp attributes so that bbArgs of external FuncOps can be marked
58e07a7fd5SMatthias Springer // as "not reading" and/or "not writing".
59e07a7fd5SMatthias Springer 
60e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h"
61e07a7fd5SMatthias Springer 
62e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
63e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/IR/Bufferization.h"
64e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"
65e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.h"
66e07a7fd5SMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
67b3ebe3beSMatthias Springer #include "mlir/Dialect/Bufferization/Transforms/TensorCopyInsertion.h"
68e07a7fd5SMatthias Springer #include "mlir/Dialect/Func/IR/FuncOps.h"
69e07a7fd5SMatthias Springer #include "mlir/Dialect/MemRef/IR/MemRef.h"
70e07a7fd5SMatthias Springer #include "mlir/IR/Operation.h"
71e07a7fd5SMatthias Springer 
72e07a7fd5SMatthias Springer using namespace mlir;
73e07a7fd5SMatthias Springer using namespace mlir::bufferization;
74e07a7fd5SMatthias Springer using namespace mlir::bufferization::func_ext;
75e07a7fd5SMatthias Springer 
76e07a7fd5SMatthias Springer /// A mapping of FuncOps to their callers.
77e07a7fd5SMatthias Springer using FuncCallerMap = DenseMap<func::FuncOp, DenseSet<Operation *>>;
78e07a7fd5SMatthias Springer 
79e07a7fd5SMatthias Springer /// Get FuncAnalysisState.
80e07a7fd5SMatthias Springer static const FuncAnalysisState &
getFuncAnalysisState(const AnalysisState & state)81e07a7fd5SMatthias Springer getFuncAnalysisState(const AnalysisState &state) {
82e07a7fd5SMatthias Springer   Optional<const FuncAnalysisState *> maybeState =
83e07a7fd5SMatthias Springer       state.getDialectState<FuncAnalysisState>(
84e07a7fd5SMatthias Springer           func::FuncDialect::getDialectNamespace());
855413bf1bSKazu Hirata   assert(maybeState && "FuncAnalysisState does not exist");
86e07a7fd5SMatthias Springer   return **maybeState;
87e07a7fd5SMatthias Springer }
88e07a7fd5SMatthias Springer 
89e07a7fd5SMatthias Springer /// Get or create FuncAnalysisState.
getFuncAnalysisState(AnalysisState & state)90e07a7fd5SMatthias Springer static FuncAnalysisState &getFuncAnalysisState(AnalysisState &state) {
91e07a7fd5SMatthias Springer   return state.getOrCreateDialectState<FuncAnalysisState>(
92e07a7fd5SMatthias Springer       func::FuncDialect::getDialectNamespace());
93e07a7fd5SMatthias Springer }
94e07a7fd5SMatthias Springer 
95e07a7fd5SMatthias Springer /// Return the state (phase) of analysis of the FuncOp.
9602d3499aSStella Laurenzo /// Used for debug modes.
9702d3499aSStella Laurenzo LLVM_ATTRIBUTE_UNUSED
getFuncOpAnalysisState(const AnalysisState & state,func::FuncOp funcOp)98e07a7fd5SMatthias Springer static FuncOpAnalysisState getFuncOpAnalysisState(const AnalysisState &state,
99e07a7fd5SMatthias Springer                                                   func::FuncOp funcOp) {
100e07a7fd5SMatthias Springer   const FuncAnalysisState &funcState = getFuncAnalysisState(state);
101e07a7fd5SMatthias Springer   auto it = funcState.analyzedFuncOps.find(funcOp);
102e07a7fd5SMatthias Springer   if (it == funcState.analyzedFuncOps.end())
103e07a7fd5SMatthias Springer     return FuncOpAnalysisState::NotAnalyzed;
104e07a7fd5SMatthias Springer   return it->second;
105e07a7fd5SMatthias Springer }
106e07a7fd5SMatthias Springer 
107e07a7fd5SMatthias Springer /// Return the unique ReturnOp that terminates `funcOp`.
108e07a7fd5SMatthias Springer /// Return nullptr if there is no such unique ReturnOp.
getAssumedUniqueReturnOp(func::FuncOp funcOp)109e07a7fd5SMatthias Springer static func::ReturnOp getAssumedUniqueReturnOp(func::FuncOp funcOp) {
110e07a7fd5SMatthias Springer   func::ReturnOp returnOp;
111e07a7fd5SMatthias Springer   for (Block &b : funcOp.getBody()) {
112e07a7fd5SMatthias Springer     if (auto candidateOp = dyn_cast<func::ReturnOp>(b.getTerminator())) {
113e07a7fd5SMatthias Springer       if (returnOp)
114e07a7fd5SMatthias Springer         return nullptr;
115e07a7fd5SMatthias Springer       returnOp = candidateOp;
116e07a7fd5SMatthias Springer     }
117e07a7fd5SMatthias Springer   }
118e07a7fd5SMatthias Springer   return returnOp;
119e07a7fd5SMatthias Springer }
120e07a7fd5SMatthias Springer 
121e07a7fd5SMatthias Springer namespace {
122e07a7fd5SMatthias Springer 
123e07a7fd5SMatthias Springer /// Annotate IR with the results of the analysis. For testing purposes only.
annotateEquivalentReturnBbArg(OpOperand & returnVal,BlockArgument bbArg)124e07a7fd5SMatthias Springer static void annotateEquivalentReturnBbArg(OpOperand &returnVal,
125e07a7fd5SMatthias Springer                                           BlockArgument bbArg) {
126e07a7fd5SMatthias Springer   const char *kEquivalentArgsAttr = "__equivalent_func_args__";
127e07a7fd5SMatthias Springer   Operation *op = returnVal.getOwner();
128e07a7fd5SMatthias Springer 
129e07a7fd5SMatthias Springer   SmallVector<int64_t> equivBbArgs;
130e07a7fd5SMatthias Springer   if (op->hasAttr(kEquivalentArgsAttr)) {
131e07a7fd5SMatthias Springer     auto attr = op->getAttr(kEquivalentArgsAttr).cast<ArrayAttr>();
132e07a7fd5SMatthias Springer     equivBbArgs = llvm::to_vector<4>(llvm::map_range(attr, [](Attribute a) {
133e07a7fd5SMatthias Springer       return a.cast<IntegerAttr>().getValue().getSExtValue();
134e07a7fd5SMatthias Springer     }));
135e07a7fd5SMatthias Springer   } else {
136e07a7fd5SMatthias Springer     equivBbArgs.append(op->getNumOperands(), -1);
137e07a7fd5SMatthias Springer   }
138e07a7fd5SMatthias Springer   equivBbArgs[returnVal.getOperandNumber()] = bbArg.getArgNumber();
139e07a7fd5SMatthias Springer 
140e07a7fd5SMatthias Springer   OpBuilder b(op->getContext());
141e07a7fd5SMatthias Springer   op->setAttr(kEquivalentArgsAttr, b.getI64ArrayAttr(equivBbArgs));
142e07a7fd5SMatthias Springer }
143e07a7fd5SMatthias Springer 
144e07a7fd5SMatthias Springer /// Store function BlockArguments that are equivalent to/aliasing a returned
145e07a7fd5SMatthias Springer /// value in FuncAnalysisState.
aliasingFuncOpBBArgsAnalysis(FuncOp funcOp,OneShotAnalysisState & state)1463490aadfSMatthias Springer static LogicalResult aliasingFuncOpBBArgsAnalysis(FuncOp funcOp,
1473490aadfSMatthias Springer                                                   OneShotAnalysisState &state) {
148e07a7fd5SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
149e07a7fd5SMatthias Springer 
150e07a7fd5SMatthias Springer   // Support only single return-terminated block in the function.
151e07a7fd5SMatthias Springer   func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
152e07a7fd5SMatthias Springer   assert(returnOp && "expected func with single return op");
153e07a7fd5SMatthias Springer 
154e07a7fd5SMatthias Springer   for (OpOperand &returnVal : returnOp->getOpOperands())
155e07a7fd5SMatthias Springer     if (returnVal.get().getType().isa<RankedTensorType>())
156e07a7fd5SMatthias Springer       for (BlockArgument bbArg : funcOp.getArguments())
157e07a7fd5SMatthias Springer         if (bbArg.getType().isa<RankedTensorType>()) {
158e07a7fd5SMatthias Springer           int64_t returnIdx = returnVal.getOperandNumber();
159e07a7fd5SMatthias Springer           int64_t bbArgIdx = bbArg.getArgNumber();
1603490aadfSMatthias Springer           if (state.areEquivalentBufferizedValues(returnVal.get(), bbArg)) {
161e07a7fd5SMatthias Springer             funcState.equivalentFuncArgs[funcOp][returnIdx] = bbArgIdx;
162e07a7fd5SMatthias Springer             if (state.getOptions().testAnalysisOnly)
163e07a7fd5SMatthias Springer               annotateEquivalentReturnBbArg(returnVal, bbArg);
164e07a7fd5SMatthias Springer           }
1653490aadfSMatthias Springer           if (state.areAliasingBufferizedValues(returnVal.get(), bbArg)) {
166e07a7fd5SMatthias Springer             funcState.aliasingFuncArgs[funcOp][returnIdx].push_back(bbArgIdx);
167e07a7fd5SMatthias Springer             funcState.aliasingReturnVals[funcOp][bbArgIdx].push_back(returnIdx);
168e07a7fd5SMatthias Springer           }
169e07a7fd5SMatthias Springer         }
170e07a7fd5SMatthias Springer 
171e07a7fd5SMatthias Springer   return success();
172e07a7fd5SMatthias Springer }
173e07a7fd5SMatthias Springer 
annotateFuncArgAccess(func::FuncOp funcOp,BlockArgument bbArg,bool isRead,bool isWritten)174e07a7fd5SMatthias Springer static void annotateFuncArgAccess(func::FuncOp funcOp, BlockArgument bbArg,
175e07a7fd5SMatthias Springer                                   bool isRead, bool isWritten) {
176e07a7fd5SMatthias Springer   OpBuilder b(funcOp.getContext());
177e07a7fd5SMatthias Springer   Attribute accessType;
178e07a7fd5SMatthias Springer   if (isRead && isWritten) {
179e07a7fd5SMatthias Springer     accessType = b.getStringAttr("read-write");
180e07a7fd5SMatthias Springer   } else if (isRead) {
181e07a7fd5SMatthias Springer     accessType = b.getStringAttr("read");
182e07a7fd5SMatthias Springer   } else if (isWritten) {
183e07a7fd5SMatthias Springer     accessType = b.getStringAttr("write");
184e07a7fd5SMatthias Springer   } else {
185e07a7fd5SMatthias Springer     accessType = b.getStringAttr("none");
186e07a7fd5SMatthias Springer   }
187e07a7fd5SMatthias Springer   funcOp.setArgAttr(bbArg.getArgNumber(), "bufferization.access", accessType);
188e07a7fd5SMatthias Springer }
189e07a7fd5SMatthias Springer 
1903490aadfSMatthias Springer /// Determine which FuncOp bbArgs are read and which are written. When run on a
1913490aadfSMatthias Springer /// function with unknown ops, we conservatively assume that such ops bufferize
1923490aadfSMatthias Springer /// to a read + write.
funcOpBbArgReadWriteAnalysis(FuncOp funcOp,OneShotAnalysisState & state)1933490aadfSMatthias Springer static LogicalResult funcOpBbArgReadWriteAnalysis(FuncOp funcOp,
1943490aadfSMatthias Springer                                                   OneShotAnalysisState &state) {
195e07a7fd5SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
196e07a7fd5SMatthias Springer 
197e07a7fd5SMatthias Springer   // If the function has no body, conservatively assume that all args are
198e07a7fd5SMatthias Springer   // read + written.
199e07a7fd5SMatthias Springer   if (funcOp.getBody().empty()) {
200e07a7fd5SMatthias Springer     for (BlockArgument bbArg : funcOp.getArguments()) {
201e07a7fd5SMatthias Springer       funcState.readBbArgs[funcOp].insert(bbArg.getArgNumber());
202e07a7fd5SMatthias Springer       funcState.writtenBbArgs[funcOp].insert(bbArg.getArgNumber());
203e07a7fd5SMatthias Springer     }
204e07a7fd5SMatthias Springer 
205e07a7fd5SMatthias Springer     return success();
206e07a7fd5SMatthias Springer   }
207e07a7fd5SMatthias Springer 
208e07a7fd5SMatthias Springer   for (BlockArgument bbArg : funcOp.getArguments()) {
209e07a7fd5SMatthias Springer     if (!bbArg.getType().isa<TensorType>())
210e07a7fd5SMatthias Springer       continue;
211e07a7fd5SMatthias Springer     bool isRead = state.isValueRead(bbArg);
2123490aadfSMatthias Springer     bool isWritten = state.isValueWritten(bbArg);
213e07a7fd5SMatthias Springer     if (state.getOptions().testAnalysisOnly)
214e07a7fd5SMatthias Springer       annotateFuncArgAccess(funcOp, bbArg, isRead, isWritten);
215e07a7fd5SMatthias Springer     if (isRead)
216e07a7fd5SMatthias Springer       funcState.readBbArgs[funcOp].insert(bbArg.getArgNumber());
217e07a7fd5SMatthias Springer     if (isWritten)
218e07a7fd5SMatthias Springer       funcState.writtenBbArgs[funcOp].insert(bbArg.getArgNumber());
219e07a7fd5SMatthias Springer   }
220e07a7fd5SMatthias Springer 
221e07a7fd5SMatthias Springer   return success();
222e07a7fd5SMatthias Springer }
223e07a7fd5SMatthias Springer } // namespace
224e07a7fd5SMatthias Springer 
225e07a7fd5SMatthias Springer /// Remove bufferization attributes on FuncOp arguments.
removeBufferizationAttributes(BlockArgument bbArg)226e07a7fd5SMatthias Springer static void removeBufferizationAttributes(BlockArgument bbArg) {
227e07a7fd5SMatthias Springer   auto funcOp = cast<func::FuncOp>(bbArg.getOwner()->getParentOp());
228e07a7fd5SMatthias Springer   funcOp.removeArgAttr(bbArg.getArgNumber(),
229e07a7fd5SMatthias Springer                        BufferizationDialect::kBufferLayoutAttrName);
230e07a7fd5SMatthias Springer   funcOp.removeArgAttr(bbArg.getArgNumber(),
231e07a7fd5SMatthias Springer                        BufferizationDialect::kWritableAttrName);
232e07a7fd5SMatthias Springer }
233e07a7fd5SMatthias Springer 
234e07a7fd5SMatthias Springer /// Return the func::FuncOp called by `callOp`.
getCalledFunction(CallOpInterface callOp)235e07a7fd5SMatthias Springer static func::FuncOp getCalledFunction(CallOpInterface callOp) {
236e07a7fd5SMatthias Springer   SymbolRefAttr sym = callOp.getCallableForCallee().dyn_cast<SymbolRefAttr>();
237e07a7fd5SMatthias Springer   if (!sym)
238e07a7fd5SMatthias Springer     return nullptr;
239e07a7fd5SMatthias Springer   return dyn_cast_or_null<func::FuncOp>(
240e07a7fd5SMatthias Springer       SymbolTable::lookupNearestSymbolFrom(callOp, sym));
241e07a7fd5SMatthias Springer }
242e07a7fd5SMatthias Springer 
243e07a7fd5SMatthias Springer /// Gather equivalence info of CallOps.
244e07a7fd5SMatthias Springer /// Note: This only adds new equivalence info if the called function was already
245e07a7fd5SMatthias Springer /// analyzed.
246e07a7fd5SMatthias Springer // TODO: This does not handle cyclic function call graphs etc.
equivalenceAnalysis(func::FuncOp funcOp,BufferizationAliasInfo & aliasInfo,OneShotAnalysisState & state)247e07a7fd5SMatthias Springer static void equivalenceAnalysis(func::FuncOp funcOp,
248e07a7fd5SMatthias Springer                                 BufferizationAliasInfo &aliasInfo,
249bf582569SMatthias Springer                                 OneShotAnalysisState &state) {
250bf582569SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
251e07a7fd5SMatthias Springer   funcOp->walk([&](func::CallOp callOp) {
252e07a7fd5SMatthias Springer     func::FuncOp calledFunction = getCalledFunction(callOp);
253e07a7fd5SMatthias Springer     assert(calledFunction && "could not retrieved called func::FuncOp");
254e07a7fd5SMatthias Springer 
255e07a7fd5SMatthias Springer     // No equivalence info available for the called function.
256e07a7fd5SMatthias Springer     if (!funcState.equivalentFuncArgs.count(calledFunction))
257e07a7fd5SMatthias Springer       return WalkResult::skip();
258e07a7fd5SMatthias Springer 
259e07a7fd5SMatthias Springer     for (auto it : funcState.equivalentFuncArgs[calledFunction]) {
260e07a7fd5SMatthias Springer       int64_t returnIdx = it.first;
261e07a7fd5SMatthias Springer       int64_t bbargIdx = it.second;
262bf582569SMatthias Springer       if (!state.isInPlace(callOp->getOpOperand(bbargIdx)))
263bf582569SMatthias Springer         continue;
264e07a7fd5SMatthias Springer       Value returnVal = callOp.getResult(returnIdx);
265e07a7fd5SMatthias Springer       Value argVal = callOp->getOperand(bbargIdx);
266e07a7fd5SMatthias Springer       aliasInfo.unionEquivalenceClasses(returnVal, argVal);
267e07a7fd5SMatthias Springer     }
268e07a7fd5SMatthias Springer 
269e07a7fd5SMatthias Springer     return WalkResult::advance();
270e07a7fd5SMatthias Springer   });
271e07a7fd5SMatthias Springer }
272e07a7fd5SMatthias Springer 
273e07a7fd5SMatthias Springer /// Store all functions of the `moduleOp` in `orderedFuncOps`, sorted by
274e07a7fd5SMatthias Springer /// callee-caller order (i.e. callees without callers first).
275e07a7fd5SMatthias Springer /// Store the map of FuncOp to all its callers in `callerMap`.
276e07a7fd5SMatthias Springer /// Return `failure()` if a cycle of calls is detected or if we are unable to
277e07a7fd5SMatthias Springer /// retrieve the called FuncOp from any CallOpInterface.
278e07a7fd5SMatthias Springer static LogicalResult
getFuncOpsOrderedByCalls(ModuleOp moduleOp,SmallVectorImpl<func::FuncOp> & orderedFuncOps,FuncCallerMap & callerMap)279e07a7fd5SMatthias Springer getFuncOpsOrderedByCalls(ModuleOp moduleOp,
280e07a7fd5SMatthias Springer                          SmallVectorImpl<func::FuncOp> &orderedFuncOps,
281e07a7fd5SMatthias Springer                          FuncCallerMap &callerMap) {
282e07a7fd5SMatthias Springer   // For each FuncOp, the set of functions called by it (i.e. the union of
283e07a7fd5SMatthias Springer   // symbols of all nested CallOpInterfaceOp).
284e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, DenseSet<func::FuncOp>> calledBy;
285e07a7fd5SMatthias Springer   // For each FuncOp, the number of CallOpInterface it contains.
286e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, unsigned> numberCallOpsContainedInFuncOp;
287e07a7fd5SMatthias Springer   WalkResult res = moduleOp.walk([&](func::FuncOp funcOp) -> WalkResult {
288e07a7fd5SMatthias Springer     if (!funcOp.getBody().empty()) {
289e07a7fd5SMatthias Springer       func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
290e07a7fd5SMatthias Springer       if (!returnOp)
291e07a7fd5SMatthias Springer         return funcOp->emitError()
292e07a7fd5SMatthias Springer                << "cannot bufferize a FuncOp with tensors and "
293e07a7fd5SMatthias Springer                   "without a unique ReturnOp";
294e07a7fd5SMatthias Springer     }
295e07a7fd5SMatthias Springer 
296e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp[funcOp] = 0;
297e07a7fd5SMatthias Springer     return funcOp.walk([&](CallOpInterface callOp) -> WalkResult {
298e07a7fd5SMatthias Springer       // Only support CallOp for now.
299e07a7fd5SMatthias Springer       if (!isa<func::CallOp>(callOp.getOperation()))
300e07a7fd5SMatthias Springer         return callOp->emitError() << "expected a CallOp";
301e07a7fd5SMatthias Springer       func::FuncOp calledFunction = getCalledFunction(callOp);
302e07a7fd5SMatthias Springer       assert(calledFunction && "could not retrieved called func::FuncOp");
30386fd1c13SBenjamin Kramer       callerMap[calledFunction].insert(callOp);
30486fd1c13SBenjamin Kramer       if (calledBy[calledFunction].insert(funcOp).second) {
305e07a7fd5SMatthias Springer         numberCallOpsContainedInFuncOp[funcOp]++;
306e07a7fd5SMatthias Springer       }
307e07a7fd5SMatthias Springer       return WalkResult::advance();
308e07a7fd5SMatthias Springer     });
309e07a7fd5SMatthias Springer   });
310e07a7fd5SMatthias Springer   if (res.wasInterrupted())
311e07a7fd5SMatthias Springer     return failure();
312e07a7fd5SMatthias Springer   // Iteratively remove function operation that do not call any of the
313e07a7fd5SMatthias Springer   // functions remaining in the callCounter map and add them to the worklist.
314e07a7fd5SMatthias Springer   while (!numberCallOpsContainedInFuncOp.empty()) {
315e07a7fd5SMatthias Springer     auto it = llvm::find_if(numberCallOpsContainedInFuncOp,
316e07a7fd5SMatthias Springer                             [](auto entry) { return entry.getSecond() == 0; });
317e07a7fd5SMatthias Springer     if (it == numberCallOpsContainedInFuncOp.end())
318e07a7fd5SMatthias Springer       return moduleOp.emitOpError(
319e07a7fd5SMatthias Springer           "expected callgraph to be free of circular dependencies.");
320e07a7fd5SMatthias Springer     orderedFuncOps.push_back(it->getFirst());
321e07a7fd5SMatthias Springer     for (auto callee : calledBy[it->getFirst()])
322e07a7fd5SMatthias Springer       numberCallOpsContainedInFuncOp[callee]--;
323e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp.erase(it);
324e07a7fd5SMatthias Springer   }
325e07a7fd5SMatthias Springer   return success();
326e07a7fd5SMatthias Springer }
327e07a7fd5SMatthias Springer 
328e07a7fd5SMatthias Springer /// Fold return values that are memref casts and update function return types.
329e07a7fd5SMatthias Springer ///
330e07a7fd5SMatthias Springer /// During FuncOp bufferization, the exact type of the returned memrefs (if any)
331e07a7fd5SMatthias Springer /// is not known yet. Therefore, the bufferization uses memref types with the
332e07a7fd5SMatthias Springer /// most generic layout map as function return types. After bufferizing the
333e07a7fd5SMatthias Springer /// entire function body, a more concise memref type can potentially be used for
334e07a7fd5SMatthias Springer /// the return type of the function.
foldMemRefCasts(func::FuncOp funcOp)335e07a7fd5SMatthias Springer static void foldMemRefCasts(func::FuncOp funcOp) {
336e07a7fd5SMatthias Springer   if (funcOp.getBody().empty())
337e07a7fd5SMatthias Springer     return;
338e07a7fd5SMatthias Springer 
339e07a7fd5SMatthias Springer   func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
340e07a7fd5SMatthias Springer   SmallVector<Type> resultTypes;
341e07a7fd5SMatthias Springer 
342e07a7fd5SMatthias Springer   for (OpOperand &operand : returnOp->getOpOperands()) {
343e07a7fd5SMatthias Springer     if (auto castOp = operand.get().getDefiningOp<memref::CastOp>()) {
344*136d746eSJacques Pienaar       operand.set(castOp.getSource());
345*136d746eSJacques Pienaar       resultTypes.push_back(castOp.getSource().getType());
346e07a7fd5SMatthias Springer     } else {
347e07a7fd5SMatthias Springer       resultTypes.push_back(operand.get().getType());
348e07a7fd5SMatthias Springer     }
349e07a7fd5SMatthias Springer   }
350e07a7fd5SMatthias Springer 
351e07a7fd5SMatthias Springer   auto newFuncType = FunctionType::get(
352e07a7fd5SMatthias Springer       funcOp.getContext(), funcOp.getFunctionType().getInputs(), resultTypes);
353e07a7fd5SMatthias Springer   funcOp.setType(newFuncType);
354e07a7fd5SMatthias Springer }
355e07a7fd5SMatthias Springer 
356f470f8cbSMatthias Springer LogicalResult
analyzeModuleOp(ModuleOp moduleOp,OneShotAnalysisState & state)357f470f8cbSMatthias Springer mlir::bufferization::analyzeModuleOp(ModuleOp moduleOp,
358f470f8cbSMatthias Springer                                      OneShotAnalysisState &state) {
359f470f8cbSMatthias Springer   OneShotBufferizationOptions options =
360f470f8cbSMatthias Springer       static_cast<const OneShotBufferizationOptions &>(state.getOptions());
361d6dab38aSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
362d6dab38aSMatthias Springer          "expected that function boundary bufferization is activated");
363f470f8cbSMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
364f470f8cbSMatthias Springer   BufferizationAliasInfo &aliasInfo = state.getAliasInfo();
365e07a7fd5SMatthias Springer 
366e07a7fd5SMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
367e07a7fd5SMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
368e07a7fd5SMatthias Springer 
369e07a7fd5SMatthias Springer   // A mapping of FuncOps to their callers.
370e07a7fd5SMatthias Springer   FuncCallerMap callerMap;
371e07a7fd5SMatthias Springer 
372e07a7fd5SMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
373e07a7fd5SMatthias Springer     return failure();
374e07a7fd5SMatthias Springer 
375e07a7fd5SMatthias Springer   // Analyze ops.
376e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
377e07a7fd5SMatthias Springer     // No body => no analysis.
378e07a7fd5SMatthias Springer     if (funcOp.getBody().empty())
379e07a7fd5SMatthias Springer       continue;
380e07a7fd5SMatthias Springer 
381e07a7fd5SMatthias Springer     // Now analyzing function.
382e07a7fd5SMatthias Springer     funcState.startFunctionAnalysis(funcOp);
383e07a7fd5SMatthias Springer 
384e07a7fd5SMatthias Springer     // Gather equivalence info for CallOps.
385bf582569SMatthias Springer     equivalenceAnalysis(funcOp, aliasInfo, state);
386e07a7fd5SMatthias Springer 
387e07a7fd5SMatthias Springer     // Analyze funcOp.
388f470f8cbSMatthias Springer     if (failed(analyzeOp(funcOp, state)))
389e07a7fd5SMatthias Springer       return failure();
390e07a7fd5SMatthias Springer 
3913490aadfSMatthias Springer     // Run some extra function analyses.
392f470f8cbSMatthias Springer     if (failed(aliasingFuncOpBBArgsAnalysis(funcOp, state)) ||
393f470f8cbSMatthias Springer         failed(funcOpBbArgReadWriteAnalysis(funcOp, state)))
3943490aadfSMatthias Springer       return failure();
3953490aadfSMatthias Springer 
396e07a7fd5SMatthias Springer     // Mark op as fully analyzed.
397e07a7fd5SMatthias Springer     funcState.analyzedFuncOps[funcOp] = FuncOpAnalysisState::Analyzed;
398e07a7fd5SMatthias Springer   }
399e07a7fd5SMatthias Springer 
400e07a7fd5SMatthias Springer   return success();
401f470f8cbSMatthias Springer }
402f470f8cbSMatthias Springer 
bufferizeModuleOp(ModuleOp moduleOp,const OneShotAnalysisState & analysisState)403f470f8cbSMatthias Springer LogicalResult mlir::bufferization::bufferizeModuleOp(
404f470f8cbSMatthias Springer     ModuleOp moduleOp, const OneShotAnalysisState &analysisState) {
405f470f8cbSMatthias Springer   auto const &options = static_cast<const OneShotBufferizationOptions &>(
406f470f8cbSMatthias Springer       analysisState.getOptions());
407f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
408f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
409f470f8cbSMatthias Springer   IRRewriter rewriter(moduleOp.getContext());
410f470f8cbSMatthias Springer 
411f470f8cbSMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
412f470f8cbSMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
413f470f8cbSMatthias Springer 
414f470f8cbSMatthias Springer   // A mapping of FuncOps to their callers.
415f470f8cbSMatthias Springer   FuncCallerMap callerMap;
416f470f8cbSMatthias Springer 
417f470f8cbSMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
418f470f8cbSMatthias Springer     return failure();
419e07a7fd5SMatthias Springer 
420e07a7fd5SMatthias Springer   // Bufferize functions.
421e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
422e07a7fd5SMatthias Springer     // Note: It would be good to apply cleanups here but we cannot as aliasInfo
423e07a7fd5SMatthias Springer     // would be invalidated.
424b3ebe3beSMatthias Springer     if (failed(bufferizeOp(funcOp, options, /*copyBeforeWrite=*/false)))
425e07a7fd5SMatthias Springer       return failure();
426f287da8aSMatthias Springer     // Change buffer return types to more precise layout maps.
427f287da8aSMatthias Springer     if (options.functionBoundaryTypeConversion ==
428f287da8aSMatthias Springer         BufferizationOptions::LayoutMapOption::InferLayoutMap)
429e07a7fd5SMatthias Springer       foldMemRefCasts(funcOp);
430e07a7fd5SMatthias Springer   }
431e07a7fd5SMatthias Springer 
432e07a7fd5SMatthias Springer   // Post-pass cleanup of function argument attributes.
433e07a7fd5SMatthias Springer   moduleOp.walk([&](func::FuncOp op) {
434e07a7fd5SMatthias Springer     for (BlockArgument bbArg : op.getArguments())
435e07a7fd5SMatthias Springer       removeBufferizationAttributes(bbArg);
436e07a7fd5SMatthias Springer   });
437e07a7fd5SMatthias Springer 
438e07a7fd5SMatthias Springer   return success();
439e07a7fd5SMatthias Springer }
440f470f8cbSMatthias Springer 
runOneShotModuleBufferize(ModuleOp moduleOp,const OneShotBufferizationOptions & options)441f470f8cbSMatthias Springer LogicalResult mlir::bufferization::runOneShotModuleBufferize(
442940e2908SMehdi Amini     ModuleOp moduleOp, const OneShotBufferizationOptions &options) {
443f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
444f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
445f470f8cbSMatthias Springer   OneShotAnalysisState analysisState(moduleOp, options);
446b3ebe3beSMatthias Springer   if (failed(insertTensorCopies(moduleOp, options)))
447f470f8cbSMatthias Springer     return failure();
448f470f8cbSMatthias Springer   if (options.testAnalysisOnly)
449f470f8cbSMatthias Springer     return success();
450f470f8cbSMatthias Springer   if (failed(bufferizeModuleOp(moduleOp, analysisState)))
451f470f8cbSMatthias Springer     return failure();
452f470f8cbSMatthias Springer   return success();
453f470f8cbSMatthias Springer }
454