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