xref: /llvm-project/mlir/include/mlir/Dialect/Bufferization/Transforms/Bufferize.h (revision 2ff2e871f5e632ea493efaf4f2192f8b18a54ab1)
1 //===- Bufferize.h - Bufferization Utilities --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // We use the term "bufferize" to mean conversion from tensor types to
10 // memref types.
11 //
12 // Generally speaking, for each op that operates on tensor types, the
13 // `BufferizableOpInterface` needs to be implemented. This file contains the
14 // bufferization driver that is responsible for bufferizing the ops in the right
15 // order, etc.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
20 #define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
21 
22 #include "mlir/Transforms/DialectConversion.h"
23 
24 namespace mlir {
25 namespace bufferization {
26 
27 class AnalysisState;
28 struct BufferizationOptions;
29 class OpFilter;
30 
31 /// Bufferization statistics for debugging. These can be printed after running
32 /// the OneShotBufferizePass with `-mlir-pass-statistics`. See the pass
33 /// definition for more details.
34 struct BufferizationStatistics {
35   int64_t numBufferAlloc = 0;
36   int64_t numBufferDealloc = 0;
37   int64_t numTensorInPlace = 0;
38   int64_t numTensorOutOfPlace = 0;
39 };
40 
41 /// Bufferize `op` and its nested ops that implement `BufferizableOpInterface`.
42 ///
43 /// Note: This function does not resolve read-after-write conflicts. Use this
44 /// function only if it is guaranteed that the input IR can bufferize without
45 /// additional buffer copies or set "options.copyBeforeWrite = true". The
46 /// general bufferization entry point is `runOneShotBufferize`.
47 LogicalResult bufferizeOp(Operation *op, const BufferizationOptions &options,
48                           BufferizationStatistics *statistics = nullptr);
49 
50 /// Bufferize the signature of `block` and its callers (i.e., ops that have the
51 /// given block as a successor). All block argument types are changed to memref
52 /// types. All corresponding operands of all callers  are wrapped in
53 /// bufferization.to_memref ops. All uses of bufferized tensor block arguments
54 /// are wrapped in bufferization.to_tensor ops.
55 ///
56 /// It is expected that all callers implement the `BranchOpInterface`.
57 /// Otherwise, this function will fail. The `BranchOpInterface` is used to query
58 /// the range of operands that are forwarded to this block.
59 ///
60 /// It is expected that the parent op of this block implements the
61 /// `BufferizableOpInterface`. The buffer types of tensor block arguments are
62 /// computed with `BufferizableOpIntercace::getBufferType`.
63 LogicalResult bufferizeBlockSignature(Block *block, RewriterBase &rewriter,
64                                       const BufferizationOptions &options);
65 
66 } // namespace bufferization
67 } // namespace mlir
68 
69 #endif // MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
70