MLIR 24.0.0git
FuncBufferizableOpInterfaceImpl.cpp
Go to the documentation of this file.
1//===- BufferizableOpInterfaceImpl.cpp - Impl. of BufferizableOpInterface -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
16#include "mlir/IR/Dialect.h"
17#include "mlir/IR/Operation.h"
18#include <optional>
19
20namespace mlir {
21/// Return all func.return ops in the given function.
24 for (Block &b : funcOp.getBody())
25 if (auto returnOp = dyn_cast<func::ReturnOp>(b.getTerminator()))
26 result.push_back(returnOp);
27 return result;
28}
29
30namespace bufferization {
31namespace func_ext {
32
35 auto createdEquiv = equivalentFuncArgs.try_emplace(funcOp, IndexMapping());
36 auto createdAliasingResults =
37 aliasingReturnVals.try_emplace(funcOp, IndexToIndexListMapping());
38 auto createdRead = readBbArgs.try_emplace(funcOp, BbArgIndexSet());
39 auto createdWritten = writtenBbArgs.try_emplace(funcOp, BbArgIndexSet());
40 auto createdWritable = writableReturnVals.try_emplace(funcOp);
41 (void)createdEquiv;
42 (void)createdAliasingResults;
43 (void)createdRead;
44 (void)createdWritten;
45 (void)createdWritable;
46#ifndef NDEBUG
47 assert(createdEquiv.second && "equivalence info exists already");
48 assert(createdAliasingResults.second && "aliasing info exists already");
49 assert(createdRead.second && "bbarg access info exists already");
50 assert(createdWritten.second && "bbarg access info exists already");
51 assert(createdWritable.second &&
52 "return value writability info exists already");
53#endif // NDEBUG
54}
55
56/// Return the memory space for the index-th function result. Emits an error on
57/// `funcOp` and returns failure if `options.defaultMemorySpaceFn` returns
58/// std::nullopt.
59static FailureOr<Attribute>
62 auto type =
63 dyn_cast<TensorLikeType>(funcOp.getFunctionType().getResult(index));
64 assert(type && "expected TensorLikeType");
65
66 auto memorySpace = options.defaultMemorySpaceFn(type);
67 if (!memorySpace)
68 return funcOp.emitError("could not infer memory space for result #")
69 << index;
70 return *memorySpace;
71}
72
73/// Return the index-th bufferized function argument type. This assumes that the
74/// specified argument is a tensor. If the tensor is ranked, a layout map may be
75/// specified by the user (as per `options.functionArgTypeConverterFn`). Emits
76/// an error on `funcOp` and returns failure if `options.defaultMemorySpaceFn`
77/// returns std::nullopt.
78static FailureOr<BufferLikeType>
81 auto type =
82 dyn_cast<TensorLikeType>(funcOp.getFunctionType().getInput(index));
83 assert(type && "expected TensorLikeType");
84
85 auto memorySpace = options.defaultMemorySpaceFn(type);
86 if (!memorySpace)
87 return funcOp.emitError("could not infer memory space for argument #")
88 << index;
89 BufferLikeType bufferType =
90 options.functionArgTypeConverterFn(type, *memorySpace, funcOp, options);
91
92 // Note: For builtin tensors there is additional logic related to layout.
93 if (isa<TensorType>(type)) {
94 auto layoutAttr = funcOp.getArgAttrOfType<MemRefLayoutAttrInterface>(
95 index, BufferizationDialect::kBufferLayoutAttrName);
96 if (!layoutAttr)
97 return bufferType;
98
99 auto rankedMemrefType = dyn_cast<MemRefType>(bufferType);
100 assert(rankedMemrefType &&
101 "buffer layout not supported on unranked tensors");
102 return cast<BufferLikeType>(MemRefType::get(
103 rankedMemrefType.getShape(), rankedMemrefType.getElementType(),
104 layoutAttr, rankedMemrefType.getMemorySpace()));
105 }
106
107 return bufferType;
108}
109
110/// Return the FuncOp called by `callOp`.
112 SymbolTableCollection &symbolTables) {
113 return dyn_cast_or_null<FuncOp>(callOp.resolveCallableInTable(&symbolTables));
114}
115
116/// Return the FuncOp called by `callOp`.
118 const AnalysisState &state) {
119 if (isa<OneShotAnalysisState>(state)) {
120 auto &oneShotAnalysisState =
121 static_cast<const OneShotAnalysisState &>(state);
122 if (auto *funcAnalysisState =
123 oneShotAnalysisState.getExtension<FuncAnalysisState>()) {
124 // Use the cached symbol tables.
125 return getCalledFunction(callOp, funcAnalysisState->symbolTables);
126 }
127 }
128
129 SymbolTableCollection symbolTables;
130 return getCalledFunction(callOp, symbolTables);
131}
132
133/// Get FuncAnalysisState.
134static const FuncAnalysisState &
136 assert(isa<OneShotAnalysisState>(state) && "expected OneShotAnalysisState");
137 auto *result = static_cast<const OneShotAnalysisState &>(state)
138 .getExtension<FuncAnalysisState>();
139 assert(result && "FuncAnalysisState does not exist");
140 return *result;
141}
142
143/// Return the state (phase) of analysis of the FuncOp.
145 FuncOp funcOp) {
146 if (!isa<OneShotAnalysisState>(state))
148 auto *funcState = static_cast<const OneShotAnalysisState &>(state)
149 .getExtension<FuncAnalysisState>();
150 if (!funcState)
152 const auto &analyzedFuncOps = funcState->analyzedFuncOps;
153 auto it = analyzedFuncOps.find(funcOp);
154 if (it == analyzedFuncOps.end())
156 return it->second;
157}
158
159/// Return the index of the bbArg in the given FuncOp that is equivalent to the
160/// specified return value (if any).
161static std::optional<int64_t>
162getEquivalentFuncArgIdx(FuncOp funcOp, const FuncAnalysisState &state,
163 int64_t returnValIdx) {
164 auto funcOpIt = state.equivalentFuncArgs.find(funcOp);
165 if (funcOpIt == state.equivalentFuncArgs.end())
166 // No equivalence info stores for funcOp.
167 return std::nullopt;
168
169 auto retValIt = funcOpIt->getSecond().find(returnValIdx);
170 if (retValIt == funcOpIt->getSecond().end())
171 // Return value has no equivalent bbArg.
172 return std::nullopt;
173
174 return retValIt->getSecond();
175}
176
178 : public BufferizableOpInterface::ExternalModel<CallOpInterface,
179 func::CallOp> {
181 const AnalysisState &state) const {
182 func::CallOp callOp = cast<func::CallOp>(op);
183 FuncOp funcOp = getCalledFunction(callOp, state);
184 assert(funcOp && "expected CallOp to a FuncOp");
185
187 // FuncOp not analyzed yet. Assume that OpOperand is read.
188 return true;
189
190 const FuncAnalysisState &funcState = getFuncAnalysisState(state);
191 return funcState.readBbArgs.lookup(funcOp).contains(
192 opOperand.getOperandNumber());
193 }
194
196 const AnalysisState &state) const {
197 func::CallOp callOp = cast<func::CallOp>(op);
198 FuncOp funcOp = getCalledFunction(callOp, state);
199 assert(funcOp && "expected CallOp to a FuncOp");
200
202 // FuncOp not analyzed yet. Assume that OpOperand is written.
203 return true;
204
205 const FuncAnalysisState &funcState = getFuncAnalysisState(state);
206 return funcState.writtenBbArgs.lookup(funcOp).contains(
207 opOperand.getOperandNumber());
208 }
209
210 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
211 const AnalysisState &state) const {
212 func::CallOp callOp = cast<func::CallOp>(op);
213 FuncOp funcOp = getCalledFunction(callOp, state);
214 assert(funcOp && "expected CallOp to a FuncOp");
216 // FuncOp not analyzed yet. Any OpResult may be aliasing.
217 return detail::unknownGetAliasingValues(opOperand);
218
219 // Get aliasing results from state.
220 const FuncAnalysisState &funcState = getFuncAnalysisState(state);
221 auto aliasingReturnVals =
222 funcState.aliasingReturnVals.lookup(funcOp).lookup(
223 opOperand.getOperandNumber());
224
225 // Check if the aliasing OpResult is equivalent to the OpOperand.
226 std::optional<int64_t> equivalent = {};
227 if (aliasingReturnVals.size() == 1) {
228 equivalent = getEquivalentFuncArgIdx(funcOp, funcState,
229 aliasingReturnVals.front());
230 assert((!equivalent.has_value() ||
231 *equivalent == opOperand.getOperandNumber()) &&
232 "inconsistent analysis state");
233 }
234 AliasingValueList result;
235 for (int64_t resultIdx : aliasingReturnVals)
236 result.addAlias({callOp->getOpResult(resultIdx),
237 equivalent.has_value() ? BufferRelation::Equivalent
238 : BufferRelation::Unknown,
239 /*isDefinite=*/equivalent.has_value()});
240 return result;
241 }
242
243 bool isWritable(Operation *op, Value value,
244 const AnalysisState &state) const {
245 auto callOp = cast<func::CallOp>(op);
246 FuncOp funcOp = getCalledFunction(callOp, state);
247 assert(funcOp && "expected CallOp to a FuncOp");
248
249 // Unknown or not yet analyzed functions may return read-only buffers.
251 return false;
252
253 const FuncAnalysisState &funcState = getFuncAnalysisState(state);
254 return funcState.writableReturnVals.lookup(funcOp).contains(
255 cast<OpResult>(value).getResultNumber());
256 }
257
258 FailureOr<BufferLikeType>
260 const BufferizationState &state,
261 SmallVector<Value> &invocationStack) const {
262 auto callOp = cast<func::CallOp>(op);
263
264 // Reuse the cached symbol tables from the bufferization state.
265 FuncOp funcOp = getCalledFunction(callOp, state.getSymbolTables());
266 assert(funcOp && "expected CallOp to a FuncOp");
267 int64_t resultIdx = cast<OpResult>(value).getResultNumber();
268
269 // If the callee was already bufferized, we can directly take the type from
270 // its signature.
271 FunctionType funcType = funcOp.getFunctionType();
272 Type resultType = funcType.getResult(resultIdx);
273 if (auto bufferizedType = dyn_cast<BufferLikeType>(resultType))
274 return bufferizedType;
275
276 // Otherwise, call the type converter to compute the bufferized type.
277 auto tensorType = cast<TensorLikeType>(resultType);
278 auto memorySpace = getFunctionResultMemorySpace(funcOp, resultIdx, options);
279 if (failed(memorySpace))
280 return failure();
281 return options.functionArgTypeConverterFn(tensorType, *memorySpace, funcOp,
282 options);
283 }
284
285 /// All function arguments are writable. It is the responsibility of the
286 /// CallOp to insert buffer copies where necessary.
287 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
289 BufferizationState &state) const {
290 func::CallOp callOp = cast<func::CallOp>(op);
291
292 // 1. Compute the result types of the new CallOp.
293 SmallVector<Type> resultTypes;
294 for (Value result : callOp.getResults()) {
295 Type returnType = result.getType();
296 if (!isa<TensorLikeType>(returnType)) {
297 // Non-tensor values are returned.
298 resultTypes.push_back(returnType);
299 continue;
300 }
301
302 // Returning a memref.
303 FailureOr<BufferLikeType> resultType =
304 bufferization::getBufferType(result, options, state);
305 if (failed(resultType))
306 return failure();
307 resultTypes.push_back(*resultType);
308 }
309
310 // 2. Rewrite tensor operands as memrefs based on type of the already
311 // bufferized callee.
312 SmallVector<Value> newOperands;
313
314 FuncOp funcOp = getCalledFunction(callOp, state.getSymbolTables());
315 assert(funcOp && "expected CallOp to a FuncOp");
316 FunctionType funcType = funcOp.getFunctionType();
317
318 for (OpOperand &opOperand : callOp->getOpOperands()) {
319 // Non-tensor operands are just copied.
320 if (!isa<TensorLikeType>(opOperand.get().getType())) {
321 newOperands.push_back(opOperand.get());
322 continue;
323 }
324
325 // Retrieve buffers for tensor operands.
326 FailureOr<Value> maybeBuffer =
327 getBuffer(rewriter, opOperand.get(), options, state);
328 if (failed(maybeBuffer))
329 return failure();
330 Value buffer = *maybeBuffer;
331
332 // Caller / callee type mismatch is handled with castOrReallocMemRefValue.
333 auto bufferType = funcType.getInput(opOperand.getOperandNumber());
334 if (!isa<BufferLikeType>(bufferType)) {
335 // The called function was not bufferized yet. This can happen when
336 // there cycles in the function call graph. Compute the bufferized
337 // result type.
338 FailureOr<BufferLikeType> maybeBufferType =
339 bufferization::getBufferType(
340 funcOp.getArgument(opOperand.getOperandNumber()), options,
341 state);
342 if (failed(maybeBufferType))
343 return failure();
344 bufferType = *maybeBufferType;
345 }
346
347 // Since we don't yet have a clear layout story, to_buffer may
348 // conservatively turn tensors into more dynamic memref than necessary.
349 // If the memref type of the callee fails, introduce an extra memref.cast
350 // that will either canonicalize away or fail compilation until we can do
351 // something better. Insert a reallocation + copy if it cannot be
352 // statically guaranteed that a direct cast would be valid.
353 if (buffer.getType() != bufferType) {
354 auto memrefDstType = dyn_cast<MemRefType>(bufferType);
355 assert(memrefDstType &&
356 "buffer layout not supported on unranked tensors");
357 FailureOr<Value> replacement =
359 memrefDstType, options);
360 if (failed(replacement))
361 return failure();
362 buffer = *replacement;
363 }
364 newOperands.push_back(buffer);
365 }
366
367 // 3. Create the new CallOp.
368 func::CallOp newCallOp =
369 func::CallOp::create(rewriter, callOp.getLoc(), resultTypes,
370 newOperands, callOp.getProperties(),
371 callOp->getDiscardableAttrDictionary().getValue());
372
373 // 4. Replace the old op with the new op.
374 replaceOpWithBufferizedValues(rewriter, callOp, newCallOp->getResults());
375
376 return success();
377 }
378};
379
381 : public BufferizableOpInterface::ExternalModel<ReturnOpInterface,
382 func::ReturnOp> {
384 const AnalysisState &state) const {
385 return true;
386 }
387
389 const AnalysisState &state) const {
390 return false;
391 }
392
393 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
394 const AnalysisState &state) const {
395 return {};
396 }
397
398 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
400 BufferizationState &state) const {
401#ifndef NDEBUG
402 auto returnOp = cast<func::ReturnOp>(op);
403 assert(isa<FuncOp>(returnOp->getParentOp()) &&
404 "only support FuncOp parent for ReturnOp");
405#endif // NDEBUG
406
407 // ReturnOps are bufferized as part of FuncOps.
408 return success();
409 }
410};
411
414 FuncOpInterface, FuncOp> {
415
416 static bool supportsUnstructuredControlFlow() { return true; }
417
419 auto isaTensor = llvm::IsaPred<TensorLikeType>;
420
421 // A function has tensor semantics if it has tensor arguments/results.
422 auto funcOp = cast<FuncOp>(op);
423 bool hasTensorArg = any_of(funcOp.getArgumentTypes(), isaTensor);
424 bool hasTensorResult = any_of(funcOp.getResultTypes(), isaTensor);
425 if (hasTensorArg || hasTensorResult)
426 return true;
427
428 // It also has tensor semantics if it has tensor block arguments.
429 // TODO: Decouple bufferization of unstructured control flow from
430 // BufferizableOpInterface implementations. We should only care about
431 // region entry block arguments here (which are already covered by the
432 // argument types of the function).
433 for (Block &block : funcOp.getBody())
434 if (any_of(block.getArgumentTypes(), isaTensor))
435 return true;
436
437 return false;
438 }
439
440 AliasingOpOperandList
442 const AnalysisState &state) const {
443 return getAliasingBranchOpOperands(op, cast<BlockArgument>(value), state);
444 }
445
446 FailureOr<BufferLikeType>
448 const BufferizationState &state,
449 SmallVector<Value> &invocationStack) const {
450 auto funcOp = cast<FuncOp>(op);
451 auto bbArg = cast<BlockArgument>(value);
452
453 // Function arguments are special.
454 if (bbArg.getOwner() == &funcOp.getBody().front())
455 return getBufferizedFunctionArgType(funcOp, bbArg.getArgNumber(),
456 options);
457
459 getBufferType(op, value, options, state, invocationStack);
460 }
461
462 /// Rewrite function bbArgs and return values into buffer form. This function
463 /// bufferizes the function signature and the ReturnOp. When the entire
464 /// function body has been bufferized, function return types can be switched
465 /// to more concise memref types as part of `foldMemRefCasts`.
466 ///
467 /// All function bbArgs are writable unless they are explicitly marked as
468 /// read-only. Callers must insert copies when needed.
469 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
471 BufferizationState &state) const {
472 auto funcOp = cast<FuncOp>(op);
473 FunctionType funcType = funcOp.getFunctionType();
474
475 // Compute the argument types.
476 SmallVector<Type> argTypes;
477 for (const auto &it : llvm::enumerate(funcType.getInputs())) {
478 Type argType = it.value();
479 if (isa<TensorLikeType>(argType)) {
480 auto bufferizedArgType =
481 getBufferizedFunctionArgType(funcOp, it.index(), options);
482 if (failed(bufferizedArgType))
483 return failure();
484 argTypes.push_back(*bufferizedArgType);
485 continue;
486 }
487 argTypes.push_back(argType);
488 }
489
490 // Compute the result types.
491 SmallVector<Type> retTypes;
492 for (auto [index, resultType] : llvm::enumerate(funcType.getResults())) {
493 if (auto tensorType = dyn_cast<TensorLikeType>(resultType)) {
494 auto memorySpace = getFunctionResultMemorySpace(funcOp, index, options);
495 if (failed(memorySpace))
496 return failure();
497 BufferLikeType resultType = options.functionArgTypeConverterFn(
498 tensorType, *memorySpace, funcOp, options);
499 retTypes.push_back(resultType);
500 continue;
501 }
502 retTypes.push_back(resultType);
503 }
504
505 // Compute the new function type.
506 auto newFuncType = FunctionType::get(op->getContext(), argTypes, retTypes);
507
508 // If the function has no body, set the new function type and we are done.
509 if (funcOp.isExternal()) {
510 funcOp.setType(newFuncType);
511 return success();
512 }
513
514 // 1. Bufferize every block.
515 for (Block &block : funcOp.getBody())
516 if (failed(bufferization::bufferizeBlockSignature(&block, rewriter,
517 options, state)))
518 return failure();
519
520 // 2. Bufferize the operands of the all return op.
521 for (func::ReturnOp returnOp : getReturnOps(funcOp)) {
522 assert(returnOp->getNumOperands() == retTypes.size() &&
523 "incorrect number of return values");
524 SmallVector<Value> returnValues;
525 for (auto [returnVal, bufferizedType] :
526 llvm::zip_equal(returnOp->getOperands(), retTypes)) {
527 auto tensorType = dyn_cast<TensorLikeType>(returnVal.getType());
528 rewriter.setInsertionPoint(returnOp);
529
530 // If not a tensor type just forward it.
531 if (!tensorType) {
532 returnValues.push_back(returnVal);
533 continue;
534 }
535
536 // Note: If `inferFunctionResultLayout = true`, casts are later folded
537 // away.
538 Value toBufferOp = bufferization::ToBufferOp::create(
539 rewriter, returnOp.getLoc(), bufferizedType, returnVal);
540 returnValues.push_back(toBufferOp);
541 }
542
543 returnOp.getOperandsMutable().assign(returnValues);
544 }
545
546 // 3. Set the new function type.
547 funcOp.setType(newFuncType);
548 return success();
549 }
550
551 /// Return `true` if the given function argument is writable.
552 bool isWritable(Operation *op, Value value,
553 const AnalysisState &state) const {
554 auto funcOp = cast<FuncOp>(op);
555 BlockArgument bbArg = dyn_cast<BlockArgument>(value);
556 assert(bbArg && "expected BlockArgument");
557
558 // Non-entry block arguments are always writable. (They may alias with
559 // values that are not writable, which will turn them into read-only.)
560 if (bbArg.getOwner() != &funcOp.getBody().front())
561 return true;
562
563 // "bufferization.writable" overrides other writability decisions. This is
564 // currently used for testing only.
565 if (BoolAttr writable = funcOp.getArgAttrOfType<BoolAttr>(
566 bbArg.getArgNumber(), BufferizationDialect::kWritableAttrName))
567 return writable.getValue();
568
569 // All function arguments are writable by default.
570 return true;
571 }
572};
573
574} // namespace func_ext
575} // namespace bufferization
576} // namespace mlir
577
580 registry.addExtension(+[](MLIRContext *ctx, func::FuncDialect *dialect) {
581 func::CallOp::attachInterface<func_ext::CallOpInterface>(*ctx);
582 func::FuncOp::attachInterface<func_ext::FuncOpInterface>(*ctx);
583 func::ReturnOp::attachInterface<func_ext::ReturnOpInterface>(*ctx);
584 });
585}
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
static bool isaTensor(Type t)
static llvm::ManagedStatic< PassManagerOptions > options
Base class for generic analysis states.
This class represents an argument of a Block.
Definition Value.h:308
unsigned getArgNumber() const
Returns the number of this argument.
Definition Value.h:320
Block * getOwner() const
Returns the block that owns this argument.
Definition Value.h:317
Block represents an ordered list of Operations.
Definition Block.h:34
Operation & front()
Definition Block.h:178
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:401
This class represents an operand of an operation.
Definition Value.h:256
unsigned getOperandNumber() const
Return which operand this is in the OpOperand list of the Operation.
Definition Value.cpp:231
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
This class represents a collection of SymbolTables.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
State for analysis-enabled bufferization.
static FailureOr< Attribute > getFunctionResultMemorySpace(FuncOp funcOp, int64_t index, const BufferizationOptions &options)
Return the memory space for the index-th function result.
FuncOpAnalysisState
The state of analysis of a FuncOp.
void registerBufferizableOpInterfaceExternalModels(DialectRegistry &registry)
static FuncOp getCalledFunction(CallOpInterface callOp, SymbolTableCollection &symbolTables)
Return the FuncOp called by callOp.
static std::optional< int64_t > getEquivalentFuncArgIdx(FuncOp funcOp, const FuncAnalysisState &state, int64_t returnValIdx)
Return the index of the bbArg in the given FuncOp that is equivalent to the specified return value (i...
static FuncOpAnalysisState getFuncOpAnalysisState(const AnalysisState &state, FuncOp funcOp)
Return the state (phase) of analysis of the FuncOp.
static const FuncAnalysisState & getFuncAnalysisState(const AnalysisState &state)
Get FuncAnalysisState.
static FailureOr< BufferLikeType > getBufferizedFunctionArgType(FuncOp funcOp, int64_t index, const BufferizationOptions &options)
Return the index-th bufferized function argument type.
FailureOr< Value > castOrReallocMemRefValue(OpBuilder &b, Value value, MemRefType type, const BufferizationOptions &options)
Try to cast the given ranked MemRef-typed value to the given ranked MemRef type.
SmallVector< func::ReturnOp > getReturnOps(func::FuncOp funcOp)
Helper function that returns all func.return ops in the given function.
LogicalResult bufferizeBlockSignature(Block *block, RewriterBase &rewriter, const BufferizationOptions &options, BufferizationState &state)
Bufferize the signature of block and its callers (i.e., ops that have the given block as a successor)...
Include the generated interface declarations.
A template that provides a default implementation of getAliasingOpOperands for ops that support unstr...
AliasingOpOperandList getAliasingBranchOpOperands(Operation *op, BlockArgument bbArg, const AnalysisState &state) const
FailureOr< BufferLikeType > getBufferType(Operation *op, Value value, const BufferizationOptions &options, const BufferizationState &state, SmallVector< Value > &invocationStack) const
FailureOr< BufferLikeType > getBufferType(Operation *op, Value value, const BufferizationOptions &options, const BufferizationState &state, SmallVector< Value > &invocationStack) const
AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, const BufferizationOptions &options, BufferizationState &state) const
All function arguments are writable.
bool isWritable(Operation *op, Value value, const AnalysisState &state) const
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
Extra analysis state that is required for bufferization of function boundaries.
DenseMap< FuncOp, IndexMapping > equivalentFuncArgs
A mapping of ReturnOp OpOperand indices to equivalent FuncOp BBArg indices.
DenseMap< int64_t, SmallVector< int64_t > > IndexToIndexListMapping
A mapping of indices to a list of indices.
DenseMap< FuncOp, IndexToIndexListMapping > aliasingReturnVals
A mapping of FuncOp BBArg indices to aliasing ReturnOp OpOperand indices.
DenseMap< FuncOp, BbArgIndexSet > readBbArgs
A set of all read BlockArguments of FuncOps.
DenseMap< FuncOp, DenseSet< int64_t > > writableReturnVals
Indices of tensor return values whose buffers are writable at every func.return, including all aliase...
DenseSet< int64_t > BbArgIndexSet
A set of block argument indices.
DenseMap< FuncOp, BbArgIndexSet > writtenBbArgs
A set of all written-to BlockArguments of FuncOps.
DenseMap< FuncOp, FuncOpAnalysisState > analyzedFuncOps
Keep track of which FuncOps are fully analyzed or currently being analyzed.
void startFunctionAnalysis(FuncOp funcOp)
This function is called right before analyzing the given FuncOp.
DenseMap< int64_t, int64_t > IndexMapping
A mapping of indices to indices.
FailureOr< BufferLikeType > getBufferType(Operation *op, Value value, const BufferizationOptions &options, const BufferizationState &state, SmallVector< Value > &invocationStack) const
AliasingOpOperandList getAliasingOpOperands(Operation *op, Value value, const AnalysisState &state) const
bool isWritable(Operation *op, Value value, const AnalysisState &state) const
Return true if the given function argument is writable.
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, const BufferizationOptions &options, BufferizationState &state) const
Rewrite function bbArgs and return values into buffer form.
AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const AnalysisState &state) const
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, const BufferizationOptions &options, BufferizationState &state) const