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,
261*bf582569SMatthias Springer                                 OneShotAnalysisState &state) {
262*bf582569SMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
263e07a7fd5SMatthias Springer   funcOp->walk([&](func::CallOp callOp) {
264e07a7fd5SMatthias Springer     func::FuncOp calledFunction = getCalledFunction(callOp);
265e07a7fd5SMatthias Springer     assert(calledFunction && "could not retrieved called func::FuncOp");
266e07a7fd5SMatthias Springer 
267e07a7fd5SMatthias Springer     // No equivalence info available for the called function.
268e07a7fd5SMatthias Springer     if (!funcState.equivalentFuncArgs.count(calledFunction))
269e07a7fd5SMatthias Springer       return WalkResult::skip();
270e07a7fd5SMatthias Springer 
271e07a7fd5SMatthias Springer     for (auto it : funcState.equivalentFuncArgs[calledFunction]) {
272e07a7fd5SMatthias Springer       int64_t returnIdx = it.first;
273e07a7fd5SMatthias Springer       int64_t bbargIdx = it.second;
274*bf582569SMatthias Springer       if (!state.isInPlace(callOp->getOpOperand(bbargIdx)))
275*bf582569SMatthias Springer         continue;
276e07a7fd5SMatthias Springer       Value returnVal = callOp.getResult(returnIdx);
277e07a7fd5SMatthias Springer       Value argVal = callOp->getOperand(bbargIdx);
278e07a7fd5SMatthias Springer       aliasInfo.unionEquivalenceClasses(returnVal, argVal);
279e07a7fd5SMatthias Springer     }
280e07a7fd5SMatthias Springer 
281e07a7fd5SMatthias Springer     return WalkResult::advance();
282e07a7fd5SMatthias Springer   });
283e07a7fd5SMatthias Springer }
284e07a7fd5SMatthias Springer 
285e07a7fd5SMatthias Springer /// Store all functions of the `moduleOp` in `orderedFuncOps`, sorted by
286e07a7fd5SMatthias Springer /// callee-caller order (i.e. callees without callers first).
287e07a7fd5SMatthias Springer /// Store the map of FuncOp to all its callers in `callerMap`.
288e07a7fd5SMatthias Springer /// Return `failure()` if a cycle of calls is detected or if we are unable to
289e07a7fd5SMatthias Springer /// retrieve the called FuncOp from any CallOpInterface.
290e07a7fd5SMatthias Springer static LogicalResult
291e07a7fd5SMatthias Springer getFuncOpsOrderedByCalls(ModuleOp moduleOp,
292e07a7fd5SMatthias Springer                          SmallVectorImpl<func::FuncOp> &orderedFuncOps,
293e07a7fd5SMatthias Springer                          FuncCallerMap &callerMap) {
294e07a7fd5SMatthias Springer   // For each FuncOp, the set of functions called by it (i.e. the union of
295e07a7fd5SMatthias Springer   // symbols of all nested CallOpInterfaceOp).
296e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, DenseSet<func::FuncOp>> calledBy;
297e07a7fd5SMatthias Springer   // For each FuncOp, the number of CallOpInterface it contains.
298e07a7fd5SMatthias Springer   DenseMap<func::FuncOp, unsigned> numberCallOpsContainedInFuncOp;
299e07a7fd5SMatthias Springer   WalkResult res = moduleOp.walk([&](func::FuncOp funcOp) -> WalkResult {
300e07a7fd5SMatthias Springer     if (!funcOp.getBody().empty()) {
301e07a7fd5SMatthias Springer       func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
302e07a7fd5SMatthias Springer       if (!returnOp)
303e07a7fd5SMatthias Springer         return funcOp->emitError()
304e07a7fd5SMatthias Springer                << "cannot bufferize a FuncOp with tensors and "
305e07a7fd5SMatthias Springer                   "without a unique ReturnOp";
306e07a7fd5SMatthias Springer     }
307e07a7fd5SMatthias Springer 
308e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp[funcOp] = 0;
309e07a7fd5SMatthias Springer     return funcOp.walk([&](CallOpInterface callOp) -> WalkResult {
310e07a7fd5SMatthias Springer       // Only support CallOp for now.
311e07a7fd5SMatthias Springer       if (!isa<func::CallOp>(callOp.getOperation()))
312e07a7fd5SMatthias Springer         return callOp->emitError() << "expected a CallOp";
313e07a7fd5SMatthias Springer       func::FuncOp calledFunction = getCalledFunction(callOp);
314e07a7fd5SMatthias Springer       assert(calledFunction && "could not retrieved called func::FuncOp");
31586fd1c13SBenjamin Kramer       callerMap[calledFunction].insert(callOp);
31686fd1c13SBenjamin Kramer       if (calledBy[calledFunction].insert(funcOp).second) {
317e07a7fd5SMatthias Springer         numberCallOpsContainedInFuncOp[funcOp]++;
318e07a7fd5SMatthias Springer       }
319e07a7fd5SMatthias Springer       return WalkResult::advance();
320e07a7fd5SMatthias Springer     });
321e07a7fd5SMatthias Springer   });
322e07a7fd5SMatthias Springer   if (res.wasInterrupted())
323e07a7fd5SMatthias Springer     return failure();
324e07a7fd5SMatthias Springer   // Iteratively remove function operation that do not call any of the
325e07a7fd5SMatthias Springer   // functions remaining in the callCounter map and add them to the worklist.
326e07a7fd5SMatthias Springer   while (!numberCallOpsContainedInFuncOp.empty()) {
327e07a7fd5SMatthias Springer     auto it = llvm::find_if(numberCallOpsContainedInFuncOp,
328e07a7fd5SMatthias Springer                             [](auto entry) { return entry.getSecond() == 0; });
329e07a7fd5SMatthias Springer     if (it == numberCallOpsContainedInFuncOp.end())
330e07a7fd5SMatthias Springer       return moduleOp.emitOpError(
331e07a7fd5SMatthias Springer           "expected callgraph to be free of circular dependencies.");
332e07a7fd5SMatthias Springer     orderedFuncOps.push_back(it->getFirst());
333e07a7fd5SMatthias Springer     for (auto callee : calledBy[it->getFirst()])
334e07a7fd5SMatthias Springer       numberCallOpsContainedInFuncOp[callee]--;
335e07a7fd5SMatthias Springer     numberCallOpsContainedInFuncOp.erase(it);
336e07a7fd5SMatthias Springer   }
337e07a7fd5SMatthias Springer   return success();
338e07a7fd5SMatthias Springer }
339e07a7fd5SMatthias Springer 
340e07a7fd5SMatthias Springer /// Set the attribute that triggers inplace bufferization on a FuncOp argument
341e07a7fd5SMatthias Springer /// `bbArg`.
342e07a7fd5SMatthias Springer static void setInPlaceFuncArgument(BlockArgument bbArg, bool inPlace) {
343e07a7fd5SMatthias Springer   auto funcOp = cast<func::FuncOp>(bbArg.getOwner()->getParentOp());
344e07a7fd5SMatthias Springer   funcOp.setArgAttr(bbArg.getArgNumber(),
345e07a7fd5SMatthias Springer                     BufferizableOpInterface::kInplaceableAttrName,
346e07a7fd5SMatthias Springer                     BoolAttr::get(bbArg.getContext(), inPlace));
347e07a7fd5SMatthias Springer }
348e07a7fd5SMatthias Springer 
349e07a7fd5SMatthias Springer /// Annotate the IR with the result of the analysis. For testing/debugging only.
350e07a7fd5SMatthias Springer static void annotateOpsWithBufferizationMarkers(func::FuncOp funcOp,
351e07a7fd5SMatthias Springer                                                 const AnalysisState &state) {
352e07a7fd5SMatthias Springer   auto bufferizableOp = cast<BufferizableOpInterface>(funcOp.getOperation());
353e07a7fd5SMatthias Springer   for (BlockArgument bbArg : funcOp.getArguments())
354e07a7fd5SMatthias Springer     if (bbArg.getType().isa<TensorType>())
355e07a7fd5SMatthias Springer       setInPlaceFuncArgument(bbArg, bufferizableOp.isWritable(bbArg, state));
356e07a7fd5SMatthias Springer }
357e07a7fd5SMatthias Springer 
358e07a7fd5SMatthias Springer /// Fold return values that are memref casts and update function return types.
359e07a7fd5SMatthias Springer ///
360e07a7fd5SMatthias Springer /// During FuncOp bufferization, the exact type of the returned memrefs (if any)
361e07a7fd5SMatthias Springer /// is not known yet. Therefore, the bufferization uses memref types with the
362e07a7fd5SMatthias Springer /// most generic layout map as function return types. After bufferizing the
363e07a7fd5SMatthias Springer /// entire function body, a more concise memref type can potentially be used for
364e07a7fd5SMatthias Springer /// the return type of the function.
365e07a7fd5SMatthias Springer static void foldMemRefCasts(func::FuncOp funcOp) {
366e07a7fd5SMatthias Springer   if (funcOp.getBody().empty())
367e07a7fd5SMatthias Springer     return;
368e07a7fd5SMatthias Springer 
369e07a7fd5SMatthias Springer   func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
370e07a7fd5SMatthias Springer   SmallVector<Type> resultTypes;
371e07a7fd5SMatthias Springer 
372e07a7fd5SMatthias Springer   for (OpOperand &operand : returnOp->getOpOperands()) {
373e07a7fd5SMatthias Springer     if (auto castOp = operand.get().getDefiningOp<memref::CastOp>()) {
374e07a7fd5SMatthias Springer       operand.set(castOp.source());
375e07a7fd5SMatthias Springer       resultTypes.push_back(castOp.source().getType());
376e07a7fd5SMatthias Springer     } else {
377e07a7fd5SMatthias Springer       resultTypes.push_back(operand.get().getType());
378e07a7fd5SMatthias Springer     }
379e07a7fd5SMatthias Springer   }
380e07a7fd5SMatthias Springer 
381e07a7fd5SMatthias Springer   auto newFuncType = FunctionType::get(
382e07a7fd5SMatthias Springer       funcOp.getContext(), funcOp.getFunctionType().getInputs(), resultTypes);
383e07a7fd5SMatthias Springer   funcOp.setType(newFuncType);
384e07a7fd5SMatthias Springer }
385e07a7fd5SMatthias Springer 
386f470f8cbSMatthias Springer LogicalResult
387f470f8cbSMatthias Springer mlir::bufferization::analyzeModuleOp(ModuleOp moduleOp,
388f470f8cbSMatthias Springer                                      OneShotAnalysisState &state) {
389f470f8cbSMatthias Springer   OneShotBufferizationOptions options =
390f470f8cbSMatthias Springer       static_cast<const OneShotBufferizationOptions &>(state.getOptions());
391d6dab38aSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
392d6dab38aSMatthias Springer          "expected that function boundary bufferization is activated");
393f470f8cbSMatthias Springer   FuncAnalysisState &funcState = getFuncAnalysisState(state);
394f470f8cbSMatthias Springer   BufferizationAliasInfo &aliasInfo = state.getAliasInfo();
395e07a7fd5SMatthias Springer 
396e07a7fd5SMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
397e07a7fd5SMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
398e07a7fd5SMatthias Springer 
399e07a7fd5SMatthias Springer   // A mapping of FuncOps to their callers.
400e07a7fd5SMatthias Springer   FuncCallerMap callerMap;
401e07a7fd5SMatthias Springer 
402e07a7fd5SMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
403e07a7fd5SMatthias Springer     return failure();
404e07a7fd5SMatthias Springer 
405e07a7fd5SMatthias Springer   // Analyze ops.
406e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
407e07a7fd5SMatthias Springer     // No body => no analysis.
408e07a7fd5SMatthias Springer     if (funcOp.getBody().empty())
409e07a7fd5SMatthias Springer       continue;
410e07a7fd5SMatthias Springer 
411e07a7fd5SMatthias Springer     // Now analyzing function.
412e07a7fd5SMatthias Springer     funcState.startFunctionAnalysis(funcOp);
413e07a7fd5SMatthias Springer 
414e07a7fd5SMatthias Springer     // Gather equivalence info for CallOps.
415*bf582569SMatthias Springer     equivalenceAnalysis(funcOp, aliasInfo, state);
416e07a7fd5SMatthias Springer 
417e07a7fd5SMatthias Springer     // Analyze funcOp.
418f470f8cbSMatthias Springer     if (failed(analyzeOp(funcOp, state)))
419e07a7fd5SMatthias Springer       return failure();
420e07a7fd5SMatthias Springer 
4213490aadfSMatthias Springer     // Run some extra function analyses.
422f470f8cbSMatthias Springer     if (failed(aliasingFuncOpBBArgsAnalysis(funcOp, state)) ||
423f470f8cbSMatthias Springer         failed(funcOpBbArgReadWriteAnalysis(funcOp, state)))
4243490aadfSMatthias Springer       return failure();
4253490aadfSMatthias Springer 
426e07a7fd5SMatthias Springer     // Mark op as fully analyzed.
427e07a7fd5SMatthias Springer     funcState.analyzedFuncOps[funcOp] = FuncOpAnalysisState::Analyzed;
428e07a7fd5SMatthias Springer 
429e07a7fd5SMatthias Springer     // Add annotations to function arguments.
430e07a7fd5SMatthias Springer     if (options.testAnalysisOnly)
431f470f8cbSMatthias Springer       annotateOpsWithBufferizationMarkers(funcOp, state);
432e07a7fd5SMatthias Springer   }
433e07a7fd5SMatthias Springer 
434e07a7fd5SMatthias Springer   return success();
435f470f8cbSMatthias Springer }
436f470f8cbSMatthias Springer 
437f470f8cbSMatthias Springer LogicalResult mlir::bufferization::bufferizeModuleOp(
438f470f8cbSMatthias Springer     ModuleOp moduleOp, const OneShotAnalysisState &analysisState) {
439f470f8cbSMatthias Springer   auto const &options = static_cast<const OneShotBufferizationOptions &>(
440f470f8cbSMatthias Springer       analysisState.getOptions());
441f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
442f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
443f470f8cbSMatthias Springer   IRRewriter rewriter(moduleOp.getContext());
444f470f8cbSMatthias Springer   BufferizationState bufferizationState(analysisState);
445f470f8cbSMatthias Springer 
446f470f8cbSMatthias Springer   // A list of functions in the order in which they are analyzed + bufferized.
447f470f8cbSMatthias Springer   SmallVector<func::FuncOp> orderedFuncOps;
448f470f8cbSMatthias Springer 
449f470f8cbSMatthias Springer   // A mapping of FuncOps to their callers.
450f470f8cbSMatthias Springer   FuncCallerMap callerMap;
451f470f8cbSMatthias Springer 
452f470f8cbSMatthias Springer   if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
453f470f8cbSMatthias Springer     return failure();
454e07a7fd5SMatthias Springer 
455e07a7fd5SMatthias Springer   // Bufferize functions.
456e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
457e07a7fd5SMatthias Springer     // Note: It would be good to apply cleanups here but we cannot as aliasInfo
458e07a7fd5SMatthias Springer     // would be invalidated.
459e07a7fd5SMatthias Springer     if (failed(bufferizeOp(funcOp, bufferizationState)))
460e07a7fd5SMatthias Springer       return failure();
461f287da8aSMatthias Springer     // Change buffer return types to more precise layout maps.
462f287da8aSMatthias Springer     if (options.functionBoundaryTypeConversion ==
463f287da8aSMatthias Springer         BufferizationOptions::LayoutMapOption::InferLayoutMap)
464e07a7fd5SMatthias Springer       foldMemRefCasts(funcOp);
465e07a7fd5SMatthias Springer   }
466e07a7fd5SMatthias Springer 
467e07a7fd5SMatthias Springer   // Check result.
468e07a7fd5SMatthias Springer   for (func::FuncOp funcOp : orderedFuncOps) {
469e07a7fd5SMatthias Springer     if (!options.allowReturnAllocs &&
470e07a7fd5SMatthias Springer         llvm::any_of(funcOp.getFunctionType().getResults(), [](Type t) {
471e07a7fd5SMatthias Springer           return t.isa<MemRefType, UnrankedMemRefType>();
472e07a7fd5SMatthias Springer         })) {
473e07a7fd5SMatthias Springer       funcOp->emitError("memref return type is unsupported");
474e07a7fd5SMatthias Springer       return failure();
475e07a7fd5SMatthias Springer     }
476e07a7fd5SMatthias Springer   }
477e07a7fd5SMatthias Springer 
478e07a7fd5SMatthias Springer   // Post-pass cleanup of function argument attributes.
479e07a7fd5SMatthias Springer   moduleOp.walk([&](func::FuncOp op) {
480e07a7fd5SMatthias Springer     for (BlockArgument bbArg : op.getArguments())
481e07a7fd5SMatthias Springer       removeBufferizationAttributes(bbArg);
482e07a7fd5SMatthias Springer   });
483e07a7fd5SMatthias Springer 
484e07a7fd5SMatthias Springer   return success();
485e07a7fd5SMatthias Springer }
486f470f8cbSMatthias Springer 
487f470f8cbSMatthias Springer LogicalResult mlir::bufferization::runOneShotModuleBufferize(
488940e2908SMehdi Amini     ModuleOp moduleOp, const OneShotBufferizationOptions &options) {
489f470f8cbSMatthias Springer   assert(options.bufferizeFunctionBoundaries &&
490f470f8cbSMatthias Springer          "expected that function boundary bufferization is activated");
491f470f8cbSMatthias Springer   OneShotAnalysisState analysisState(moduleOp, options);
492f470f8cbSMatthias Springer   if (failed(analyzeModuleOp(moduleOp, analysisState)))
493f470f8cbSMatthias Springer     return failure();
494f470f8cbSMatthias Springer   if (options.testAnalysisOnly)
495f470f8cbSMatthias Springer     return success();
496f470f8cbSMatthias Springer   if (failed(bufferizeModuleOp(moduleOp, analysisState)))
497f470f8cbSMatthias Springer     return failure();
498f470f8cbSMatthias Springer   return success();
499f470f8cbSMatthias Springer }
500