MLIR  21.0.0git
Transforms.h
Go to the documentation of this file.
1 //===- Transforms.h - MemRef Dialect transformations ------------*- 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 /// This header declares functions that assist transformations in the MemRef
10 /// dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_MEMREF_TRANSFORMS_TRANSFORMS_H
15 #define MLIR_DIALECT_MEMREF_TRANSFORMS_TRANSFORMS_H
16 
17 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/STLFunctionalExtras.h"
19 
20 namespace mlir {
21 class OpBuilder;
22 class RewritePatternSet;
23 class RewriterBase;
24 class Value;
25 class ValueRange;
26 
27 namespace arith {
28 class WideIntEmulationConverter;
29 class NarrowTypeEmulationConverter;
30 } // namespace arith
31 
32 namespace memref {
33 class AllocOp;
34 class AllocaOp;
35 class DeallocOp;
36 
37 //===----------------------------------------------------------------------===//
38 // Patterns
39 //===----------------------------------------------------------------------===//
40 
41 /// Collects a set of patterns to rewrite ops within the memref dialect.
42 void populateExpandOpsPatterns(RewritePatternSet &patterns);
43 
44 /// Appends patterns for folding memref aliasing ops into consumer load/store
45 /// ops into `patterns`.
46 void populateFoldMemRefAliasOpPatterns(RewritePatternSet &patterns);
47 
48 /// Appends patterns that resolve `memref.dim` operations with values that are
49 /// defined by operations that implement the
50 /// `ReifyRankedShapedTypeOpInterface`, in terms of shapes of its input
51 /// operands.
53  RewritePatternSet &patterns);
54 
55 /// Appends patterns that resolve `memref.dim` operations with values that are
56 /// defined by operations that implement the `InferShapedTypeOpInterface`, in
57 /// terms of shapes of its input operands.
59 
60 /// Appends patterns for expanding memref operations that modify the metadata
61 /// (sizes, offset, strides) of a memref into easier to analyze constructs.
62 void populateExpandStridedMetadataPatterns(RewritePatternSet &patterns);
63 
64 /// Appends patterns for resolving `memref.extract_strided_metadata` into
65 /// `memref.extract_strided_metadata` of its source.
67 
68 /// Appends patterns for expanding `memref.realloc` operations.
69 void populateExpandReallocPatterns(RewritePatternSet &patterns,
70  bool emitDeallocs = true);
71 
72 /// Appends patterns for emulating wide integer memref operations with ops over
73 /// narrower integer types.
75  const arith::WideIntEmulationConverter &typeConverter,
76  RewritePatternSet &patterns);
77 
78 /// Appends type conversions for emulating wide integer memref operations with
79 /// ops over narrowe integer types.
81  arith::WideIntEmulationConverter &typeConverter);
82 
83 /// Appends patterns for emulating memref operations over narrow types with ops
84 /// over wider types.
86  const arith::NarrowTypeEmulationConverter &typeConverter,
87  RewritePatternSet &patterns);
88 
89 /// Appends type conversions for emulating memref operations over narrow types
90 /// with ops over wider types.
92  arith::NarrowTypeEmulationConverter &typeConverter);
93 
94 /// Transformation to do multi-buffering/array expansion to remove dependencies
95 /// on the temporary allocation between consecutive loop iterations.
96 /// It returns the new allocation if the original allocation was multi-buffered
97 /// and returns failure() otherwise.
98 /// When `skipOverrideAnalysis`, the pass will apply the transformation
99 /// without checking thwt the buffer is overrided at the beginning of each
100 /// iteration. This implies that user knows that there is no data carried across
101 /// loop iterations. Example:
102 /// ```
103 /// %0 = memref.alloc() : memref<4x128xf32>
104 /// scf.for %iv = %c1 to %c1024 step %c3 {
105 /// memref.copy %1, %0 : memref<4x128xf32> to memref<4x128xf32>
106 /// "some_use"(%0) : (memref<4x128xf32>) -> ()
107 /// }
108 /// ```
109 /// into:
110 /// ```
111 /// %0 = memref.alloc() : memref<5x4x128xf32>
112 /// scf.for %iv = %c1 to %c1024 step %c3 {
113 /// %s = arith.subi %iv, %c1 : index
114 /// %d = arith.divsi %s, %c3 : index
115 /// %i = arith.remsi %d, %c5 : index
116 /// %sv = memref.subview %0[%i, 0, 0] [1, 4, 128] [1, 1, 1] :
117 /// memref<5x4x128xf32> to memref<4x128xf32, strided<[128, 1], offset: ?>>
118 /// memref.copy %1, %sv : memref<4x128xf32> to memref<4x128xf32, strided<...>>
119 /// "some_use"(%sv) : (memref<4x128xf32, strided<...>) -> ()
120 /// }
121 /// ```
122 FailureOr<memref::AllocOp> multiBuffer(RewriterBase &rewriter,
123  memref::AllocOp allocOp,
124  unsigned multiplier,
125  bool skipOverrideAnalysis = false);
126 /// Call into `multiBuffer` with locally constructed IRRewriter.
127 FailureOr<memref::AllocOp> multiBuffer(memref::AllocOp allocOp,
128  unsigned multiplier,
129  bool skipOverrideAnalysis = false);
130 
131 /// Appends patterns for extracting address computations from the instructions
132 /// with memory accesses such that these memory accesses use only a base
133 /// pointer.
134 ///
135 /// For instance,
136 /// ```mlir
137 /// memref.load %base[%off0, ...]
138 /// ```
139 ///
140 /// Will be rewritten in:
141 /// ```mlir
142 /// %new_base = memref.subview %base[%off0,...][1,...][1,...]
143 /// memref.load %new_base[%c0,...]
144 /// ```
146 
147 void populateFlattenMemrefsPatterns(RewritePatternSet &patterns);
148 
149 /// Build a new memref::AllocaOp whose dynamic sizes are independent of all
150 /// given independencies. If the op is already independent of all
151 /// independencies, the same AllocaOp result is returned.
152 ///
153 /// Failure indicates the no suitable upper bound for the dynamic sizes could be
154 /// found.
155 FailureOr<Value> buildIndependentOp(OpBuilder &b, AllocaOp allocaOp,
156  ValueRange independencies);
157 
158 /// Build a new memref::AllocaOp whose dynamic sizes are independent of all
159 /// given independencies. If the op is already independent of all
160 /// independencies, the same AllocaOp result is returned.
161 ///
162 /// The original AllocaOp is replaced with the new one, wrapped in a SubviewOp.
163 /// The result type of the replacement is different from the original allocation
164 /// type: it has the same shape, but a different layout map. This function
165 /// updates all users that do not have a memref result or memref region block
166 /// argument, and some frequently used memref dialect ops (such as
167 /// memref.subview). It does not update other uses such as the init_arg of an
168 /// scf.for op. Such uses are wrapped in unrealized_conversion_cast.
169 ///
170 /// Failure indicates the no suitable upper bound for the dynamic sizes could be
171 /// found.
172 ///
173 /// Example (make independent of %iv):
174 /// ```
175 /// scf.for %iv = %c0 to %sz step %c1 {
176 /// %0 = memref.alloca(%iv) : memref<?xf32>
177 /// %1 = memref.subview %0[0][5][1] : ...
178 /// linalg.generic outs(%1 : ...) ...
179 /// %2 = scf.for ... iter_arg(%arg0 = %0) ...
180 /// ...
181 /// }
182 /// ```
183 ///
184 /// The above IR is rewritten to:
185 ///
186 /// ```
187 /// scf.for %iv = %c0 to %sz step %c1 {
188 /// %0 = memref.alloca(%sz - 1) : memref<?xf32>
189 /// %0_subview = memref.subview %0[0][%iv][1]
190 /// : memref<?xf32> to memref<?xf32, #map>
191 /// %1 = memref.subview %0_subview[0][5][1] : ...
192 /// linalg.generic outs(%1 : ...) ...
193 /// %cast = unrealized_conversion_cast %0_subview
194 /// : memref<?xf32, #map> to memref<?xf32>
195 /// %2 = scf.for ... iter_arg(%arg0 = %cast) ...
196 /// ...
197 /// }
198 /// ```
199 FailureOr<Value> replaceWithIndependentOp(RewriterBase &rewriter,
200  memref::AllocaOp allocaOp,
201  ValueRange independencies);
202 
203 /// Replaces the given `alloc` with the corresponding `alloca` and returns it if
204 /// the following conditions are met:
205 /// - the corresponding dealloc is available in the same block as the alloc;
206 /// - the filter, if provided, succeeds on the alloc/dealloc pair.
207 /// Otherwise returns nullptr and leaves the IR unchanged.
208 memref::AllocaOp allocToAlloca(
209  RewriterBase &rewriter, memref::AllocOp alloc,
210  function_ref<bool(memref::AllocOp, memref::DeallocOp)> filter = nullptr);
211 
212 } // namespace memref
213 } // namespace mlir
214 
215 #endif
This class helps build Operations.
Definition: Builders.h:205
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:358
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
void populateFoldMemRefAliasOpPatterns(RewritePatternSet &patterns)
Appends patterns for folding memref aliasing ops into consumer load/store ops into patterns.
void populateMemRefWideIntEmulationPatterns(const arith::WideIntEmulationConverter &typeConverter, RewritePatternSet &patterns)
Appends patterns for emulating wide integer memref operations with ops over narrower integer types.
FailureOr< Value > buildIndependentOp(OpBuilder &b, AllocaOp allocaOp, ValueRange independencies)
Build a new memref::AllocaOp whose dynamic sizes are independent of all given independencies.
void populateResolveRankedShapedTypeResultDimsPatterns(RewritePatternSet &patterns)
Appends patterns that resolve memref.dim operations with values that are defined by operations that i...
FailureOr< Value > replaceWithIndependentOp(RewriterBase &rewriter, memref::AllocaOp allocaOp, ValueRange independencies)
Build a new memref::AllocaOp whose dynamic sizes are independent of all given independencies.
void populateMemRefNarrowTypeEmulationConversions(arith::NarrowTypeEmulationConverter &typeConverter)
Appends type conversions for emulating memref operations over narrow types with ops over wider types.
FailureOr< memref::AllocOp > multiBuffer(RewriterBase &rewriter, memref::AllocOp allocOp, unsigned multiplier, bool skipOverrideAnalysis=false)
Transformation to do multi-buffering/array expansion to remove dependencies on the temporary allocati...
Definition: MultiBuffer.cpp:98
void populateMemRefWideIntEmulationConversions(arith::WideIntEmulationConverter &typeConverter)
Appends type conversions for emulating wide integer memref operations with ops over narrowe integer t...
void populateFlattenMemrefsPatterns(RewritePatternSet &patterns)
void populateResolveExtractStridedMetadataPatterns(RewritePatternSet &patterns)
Appends patterns for resolving memref.extract_strided_metadata into memref.extract_strided_metadata o...
void populateExpandOpsPatterns(RewritePatternSet &patterns)
Collects a set of patterns to rewrite ops within the memref dialect.
Definition: ExpandOps.cpp:109
void populateResolveShapedTypeResultDimsPatterns(RewritePatternSet &patterns)
Appends patterns that resolve memref.dim operations with values that are defined by operations that i...
void populateMemRefNarrowTypeEmulationPatterns(const arith::NarrowTypeEmulationConverter &typeConverter, RewritePatternSet &patterns)
Appends patterns for emulating memref operations over narrow types with ops over wider types.
void populateExtractAddressComputationsPatterns(RewritePatternSet &patterns)
Appends patterns for extracting address computations from the instructions with memory accesses such ...
void populateExpandReallocPatterns(RewritePatternSet &patterns, bool emitDeallocs=true)
Appends patterns for expanding memref.realloc operations.
memref::AllocaOp allocToAlloca(RewriterBase &rewriter, memref::AllocOp alloc, function_ref< bool(memref::AllocOp, memref::DeallocOp)> filter=nullptr)
Replaces the given alloc with the corresponding alloca and returns it if the following conditions are...
void populateExpandStridedMetadataPatterns(RewritePatternSet &patterns)
Appends patterns for expanding memref operations that modify the metadata (sizes, offset,...
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns