MLIR  20.0.0git
Passes.h
Go to the documentation of this file.
1 #ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H
2 #define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H
3 
5 #include "mlir/Pass/Pass.h"
6 
7 namespace mlir {
8 class FunctionOpInterface;
9 class ModuleOp;
10 class RewritePatternSet;
11 class OpBuilder;
12 class SymbolTable;
13 
14 namespace func {
15 class FuncOp;
16 } // namespace func
17 
18 namespace bufferization {
19 struct OneShotBufferizationOptions;
20 
21 /// Maps from symbol table to its corresponding dealloc helper function.
23 
24 //===----------------------------------------------------------------------===//
25 // Passes
26 //===----------------------------------------------------------------------===//
27 
28 #define GEN_PASS_DECL
29 #include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
30 
31 /// Creates an instance of the BufferDeallocation pass to free all allocated
32 /// buffers.
33 std::unique_ptr<Pass> createBufferDeallocationPass();
34 
35 /// Creates an instance of the OwnershipBasedBufferDeallocation pass to free all
36 /// allocated buffers.
37 std::unique_ptr<Pass> createOwnershipBasedBufferDeallocationPass(
38  DeallocationOptions options = DeallocationOptions());
39 
40 /// Creates a pass that finds all temporary allocations
41 /// and attempts to move the deallocation after the last user/dependency
42 /// of the allocation, thereby optimizing allocation liveness.
43 std::unique_ptr<Pass> createOptimizeAllocationLivenessPass();
44 
45 /// Creates a pass that optimizes `bufferization.dealloc` operations. For
46 /// example, it reduces the number of alias checks needed at runtime using
47 /// static alias analysis.
48 std::unique_ptr<Pass> createBufferDeallocationSimplificationPass();
49 
50 /// Creates an instance of the LowerDeallocations pass to lower
51 /// `bufferization.dealloc` operations to the `memref` dialect.
52 std::unique_ptr<Pass> createLowerDeallocationsPass();
53 
54 /// Adds the conversion pattern of the `bufferization.dealloc` operation to the
55 /// given pattern set for use in other transformation passes.
57  RewritePatternSet &patterns, const DeallocHelperMap &deallocHelperFuncMap);
58 
59 /// Construct the library function needed for the fully generic
60 /// `bufferization.dealloc` lowering implemented in the LowerDeallocations pass.
61 /// The function can then be called at bufferization dealloc sites to determine
62 /// aliasing and ownership.
63 ///
64 /// The generated function takes two memrefs of indices and three memrefs of
65 /// booleans as arguments:
66 /// * The first argument A should contain the result of the
67 /// extract_aligned_pointer_as_index operation applied to the memrefs to be
68 /// deallocated
69 /// * The second argument B should contain the result of the
70 /// extract_aligned_pointer_as_index operation applied to the memrefs to be
71 /// retained
72 /// * The third argument C should contain the conditions as passed directly
73 /// to the deallocation operation.
74 /// * The fourth argument D is used to pass results to the caller. Those
75 /// represent the condition under which the memref at the corresponding
76 /// position in A should be deallocated.
77 /// * The fifth argument E is used to pass results to the caller. It
78 /// provides the ownership value corresponding the the memref at the same
79 /// position in B
80 ///
81 /// This helper function is supposed to be called once for each
82 /// `bufferization.dealloc` operation to determine the deallocation need and new
83 /// ownership indicator for the retained values, but does not perform the
84 /// deallocation itself.
85 ///
86 /// Generated code:
87 /// ```
88 /// func.func @dealloc_helper(
89 /// %dyn_dealloc_base_pointer_list: memref<?xindex>,
90 /// %dyn_retain_base_pointer_list: memref<?xindex>,
91 /// %dyn_cond_list: memref<?xi1>,
92 /// %dyn_dealloc_cond_out: memref<?xi1>,
93 /// %dyn_ownership_out: memref<?xi1>) {
94 /// %c0 = arith.constant 0 : index
95 /// %c1 = arith.constant 1 : index
96 /// %true = arith.constant true
97 /// %false = arith.constant false
98 /// %num_dealloc_memrefs = memref.dim %dyn_dealloc_base_pointer_list, %c0
99 /// %num_retain_memrefs = memref.dim %dyn_retain_base_pointer_list, %c0
100 /// // Zero initialize result buffer.
101 /// scf.for %i = %c0 to %num_retain_memrefs step %c1 {
102 /// memref.store %false, %dyn_ownership_out[%i] : memref<?xi1>
103 /// }
104 /// scf.for %i = %c0 to %num_dealloc_memrefs step %c1 {
105 /// %dealloc_bp = memref.load %dyn_dealloc_base_pointer_list[%i]
106 /// %cond = memref.load %dyn_cond_list[%i]
107 /// // Check for aliasing with retained memrefs.
108 /// %does_not_alias_retained = scf.for %j = %c0 to %num_retain_memrefs
109 /// step %c1 iter_args(%does_not_alias_aggregated = %true) -> (i1) {
110 /// %retain_bp = memref.load %dyn_retain_base_pointer_list[%j]
111 /// %does_alias = arith.cmpi eq, %retain_bp, %dealloc_bp : index
112 /// scf.if %does_alias {
113 /// %curr_ownership = memref.load %dyn_ownership_out[%j]
114 /// %updated_ownership = arith.ori %curr_ownership, %cond : i1
115 /// memref.store %updated_ownership, %dyn_ownership_out[%j]
116 /// }
117 /// %does_not_alias = arith.cmpi ne, %retain_bp, %dealloc_bp : index
118 /// %updated_aggregate = arith.andi %does_not_alias_aggregated,
119 /// %does_not_alias : i1
120 /// scf.yield %updated_aggregate : i1
121 /// }
122 /// // Check for aliasing with dealloc memrefs in the list before the
123 /// // current one, i.e.,
124 /// // `fix i, forall j < i: check_aliasing(%dyn_dealloc_base_pointer[j],
125 /// // %dyn_dealloc_base_pointer[i])`
126 /// %does_not_alias_any = scf.for %j = %c0 to %i step %c1
127 /// iter_args(%does_not_alias_agg = %does_not_alias_retained) -> (i1) {
128 /// %prev_dealloc_bp = memref.load %dyn_dealloc_base_pointer_list[%j]
129 /// %does_not_alias = arith.cmpi ne, %prev_dealloc_bp, %dealloc_bp
130 /// %updated_alias_agg = arith.andi %does_not_alias_agg, %does_not_alias
131 /// scf.yield %updated_alias_agg : i1
132 /// }
133 /// %dealloc_cond = arith.andi %does_not_alias_any, %cond : i1
134 /// memref.store %dealloc_cond, %dyn_dealloc_cond_out[%i] : memref<?xi1>
135 /// }
136 /// return
137 /// }
138 /// ```
139 func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc,
140  SymbolTable &symbolTable);
141 
142 /// Run buffer deallocation.
143 LogicalResult deallocateBuffers(Operation *op);
144 
145 /// Run the ownership-based buffer deallocation.
146 LogicalResult deallocateBuffersOwnershipBased(FunctionOpInterface op,
147  DeallocationOptions options);
148 
149 /// Creates a pass that moves allocations upwards to reduce the number of
150 /// required copies that are inserted during the BufferDeallocation pass.
151 std::unique_ptr<Pass> createBufferHoistingPass();
152 
153 /// Creates a pass that moves allocations upwards out of loops. This avoids
154 /// reallocations inside of loops.
155 std::unique_ptr<Pass> createBufferLoopHoistingPass();
156 
157 // Options struct for BufferResultsToOutParams pass.
158 // Note: defined only here, not in tablegen.
160  /// Memcpy function: Generate a memcpy between two memrefs.
161  using MemCpyFn =
162  std::function<LogicalResult(OpBuilder &, Location, Value, Value)>;
163 
164  // Filter function; returns true if the function should be converted.
165  // Defaults to true, i.e. all functions are converted.
166  std::function<bool(func::FuncOp *)> filterFn = [](func::FuncOp *func) {
167  return true;
168  };
169 
170  /// Memcpy function; used to create a copy between two memrefs.
171  /// If this is empty, memref.copy is used.
172  std::optional<MemCpyFn> memCpyFn;
173 
174  /// If true, the pass adds a "bufferize.result" attribute to each output
175  /// parameter.
176  bool addResultAttribute = false;
177 
178  /// If true, the pass eliminates the memref.alloc and memcpy if the returned
179  /// memref is allocated in the current function.
180  bool hoistStaticAllocs = false;
181 };
182 
183 /// Creates a pass that converts memref function results to out-params.
184 std::unique_ptr<Pass> createBufferResultsToOutParamsPass(
186 
187 /// Replace buffers that are returned from a function with an out parameter.
188 /// Also update all call sites.
189 LogicalResult
190 promoteBufferResultsToOutParams(ModuleOp module,
191  const BufferResultsToOutParamsOpts &options);
192 
193 /// Creates a pass that drops memref function results that are equivalent to a
194 /// function argument.
195 std::unique_ptr<Pass> createDropEquivalentBufferResultsPass();
196 
197 /// Create a pass that rewrites tensor.empty to bufferization.alloc_tensor.
198 std::unique_ptr<Pass> createEmptyTensorToAllocTensorPass();
199 
200 /// Drop all memref function results that are equivalent to a function argument.
201 LogicalResult dropEquivalentBufferResults(ModuleOp module);
202 
203 /// Creates a pass that finalizes a partial bufferization by removing remaining
204 /// bufferization.to_tensor and bufferization.to_memref operations.
205 std::unique_ptr<OperationPass<func::FuncOp>> createFinalizingBufferizePass();
206 
207 /// Create a pass that bufferizes all ops that implement BufferizableOpInterface
208 /// with One-Shot Bufferize.
209 std::unique_ptr<Pass> createOneShotBufferizePass();
210 
211 /// Create a pass that bufferizes all ops that implement BufferizableOpInterface
212 /// with One-Shot Bufferize and the specified bufferization options.
213 std::unique_ptr<Pass>
214 createOneShotBufferizePass(const OneShotBufferizationOptions &options);
215 
216 /// Creates a pass that promotes heap-based allocations to stack-based ones.
217 /// Only buffers smaller than the provided size are promoted.
218 /// Dynamic shaped buffers are promoted up to the given rank.
219 std::unique_ptr<Pass>
220 createPromoteBuffersToStackPass(unsigned maxAllocSizeInBytes = 1024,
221  unsigned maxRankOfAllocatedMemRef = 1);
222 
223 /// Creates a pass that promotes heap-based allocations to stack-based ones.
224 /// Only buffers smaller with `isSmallAlloc(alloc) == true` are promoted.
225 std::unique_ptr<Pass>
226 createPromoteBuffersToStackPass(std::function<bool(Value)> isSmallAlloc);
227 
228 /// Create a pass that tries to eliminate tensor.empty ops that are anchored on
229 /// insert_slice ops.
230 std::unique_ptr<Pass> createEmptyTensorEliminationPass();
231 
232 //===----------------------------------------------------------------------===//
233 // Registration
234 //===----------------------------------------------------------------------===//
235 
236 /// Generate the code for registering passes.
237 #define GEN_PASS_REGISTRATION
238 #include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
239 
240 } // namespace bufferization
241 } // namespace mlir
242 
243 #endif // MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H
static llvm::ManagedStatic< PassManagerOptions > options
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class helps build Operations.
Definition: Builders.h:215
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
std::unique_ptr< Pass > createDropEquivalentBufferResultsPass()
Creates a pass that drops memref function results that are equivalent to a function argument.
std::unique_ptr< Pass > createOwnershipBasedBufferDeallocationPass(DeallocationOptions options=DeallocationOptions())
Creates an instance of the OwnershipBasedBufferDeallocation pass to free all allocated buffers.
std::unique_ptr< Pass > createBufferHoistingPass()
Creates a pass that moves allocations upwards to reduce the number of required copies that are insert...
LogicalResult promoteBufferResultsToOutParams(ModuleOp module, const BufferResultsToOutParamsOpts &options)
Replace buffers that are returned from a function with an out parameter.
std::unique_ptr< Pass > createBufferResultsToOutParamsPass(const BufferResultsToOutParamsOpts &options={})
Creates a pass that converts memref function results to out-params.
llvm::DenseMap< Operation *, func::FuncOp > DeallocHelperMap
Maps from symbol table to its corresponding dealloc helper function.
Definition: Passes.h:22
std::unique_ptr< Pass > createLowerDeallocationsPass()
Creates an instance of the LowerDeallocations pass to lower bufferization.dealloc operations to the m...
std::unique_ptr< OperationPass< func::FuncOp > > createFinalizingBufferizePass()
Creates a pass that finalizes a partial bufferization by removing remaining bufferization....
Definition: Bufferize.cpp:335
std::unique_ptr< Pass > createOneShotBufferizePass()
Create a pass that bufferizes all ops that implement BufferizableOpInterface with One-Shot Bufferize.
Definition: Bufferize.cpp:325
LogicalResult deallocateBuffers(Operation *op)
Run buffer deallocation.
std::unique_ptr< Pass > createBufferDeallocationSimplificationPass()
Creates a pass that optimizes bufferization.dealloc operations.
std::unique_ptr< Pass > createBufferLoopHoistingPass()
Creates a pass that moves allocations upwards out of loops.
std::unique_ptr< Pass > createEmptyTensorEliminationPass()
Create a pass that tries to eliminate tensor.empty ops that are anchored on insert_slice ops.
void populateBufferizationDeallocLoweringPattern(RewritePatternSet &patterns, const DeallocHelperMap &deallocHelperFuncMap)
Adds the conversion pattern of the bufferization.dealloc operation to the given pattern set for use i...
std::unique_ptr< Pass > createPromoteBuffersToStackPass(unsigned maxAllocSizeInBytes=1024, unsigned maxRankOfAllocatedMemRef=1)
Creates a pass that promotes heap-based allocations to stack-based ones.
LogicalResult deallocateBuffersOwnershipBased(FunctionOpInterface op, DeallocationOptions options)
Run the ownership-based buffer deallocation.
std::unique_ptr< Pass > createOptimizeAllocationLivenessPass()
Creates a pass that finds all temporary allocations and attempts to move the deallocation after the l...
std::unique_ptr< Pass > createBufferDeallocationPass()
Creates an instance of the BufferDeallocation pass to free all allocated buffers.
func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc, SymbolTable &symbolTable)
Construct the library function needed for the fully generic bufferization.dealloc lowering implemente...
LogicalResult dropEquivalentBufferResults(ModuleOp module)
Drop all memref function results that are equivalent to a function argument.
std::unique_ptr< Pass > createEmptyTensorToAllocTensorPass()
Create a pass that rewrites tensor.empty to bufferization.alloc_tensor.
Include the generated interface declarations.
std::function< LogicalResult(OpBuilder &, Location, Value, Value)> MemCpyFn
Memcpy function: Generate a memcpy between two memrefs.
Definition: Passes.h:162
bool hoistStaticAllocs
If true, the pass eliminates the memref.alloc and memcpy if the returned memref is allocated in the c...
Definition: Passes.h:180
bool addResultAttribute
If true, the pass adds a "bufferize.result" attribute to each output parameter.
Definition: Passes.h:176
std::function< bool(func::FuncOp *)> filterFn
Definition: Passes.h:166
std::optional< MemCpyFn > memCpyFn
Memcpy function; used to create a copy between two memrefs.
Definition: Passes.h:172