MLIR  20.0.0git
Rewrite.h
Go to the documentation of this file.
1 //===-- mlir-c/Rewrite.h - Helpers for C API to Rewrites ----------*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4 // Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header declares the registration and creation method for
11 // rewrite patterns.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_C_REWRITE_H
16 #define MLIR_C_REWRITE_H
17 
18 #include "mlir-c/IR.h"
19 #include "mlir-c/Support.h"
20 #include "mlir/Config/mlir-config.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 //===----------------------------------------------------------------------===//
27 /// Opaque type declarations (see mlir-c/IR.h for more details).
28 //===----------------------------------------------------------------------===//
29 
30 #define DEFINE_C_API_STRUCT(name, storage) \
31  struct name { \
32  storage *ptr; \
33  }; \
34  typedef struct name name
35 
36 DEFINE_C_API_STRUCT(MlirRewriterBase, void);
37 DEFINE_C_API_STRUCT(MlirFrozenRewritePatternSet, void);
38 DEFINE_C_API_STRUCT(MlirGreedyRewriteDriverConfig, void);
39 DEFINE_C_API_STRUCT(MlirRewritePatternSet, void);
40 
41 //===----------------------------------------------------------------------===//
42 /// RewriterBase API inherited from OpBuilder
43 //===----------------------------------------------------------------------===//
44 
45 /// Get the MLIR context referenced by the rewriter.
46 MLIR_CAPI_EXPORTED MlirContext
47 mlirRewriterBaseGetContext(MlirRewriterBase rewriter);
48 
49 //===----------------------------------------------------------------------===//
50 /// Insertion points methods
51 
52 // These do not include functions using Block::iterator or Region::iterator, as
53 // they are not exposed by the C API yet. Similarly for methods using
54 // `InsertPoint` directly.
55 
56 /// Reset the insertion point to no location. Creating an operation without a
57 /// set insertion point is an error, but this can still be useful when the
58 /// current insertion point a builder refers to is being removed.
60 mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter);
61 
62 /// Sets the insertion point to the specified operation, which will cause
63 /// subsequent insertions to go right before it.
65 mlirRewriterBaseSetInsertionPointBefore(MlirRewriterBase rewriter,
66  MlirOperation op);
67 
68 /// Sets the insertion point to the node after the specified operation, which
69 /// will cause subsequent insertions to go right after it.
71 mlirRewriterBaseSetInsertionPointAfter(MlirRewriterBase rewriter,
72  MlirOperation op);
73 
74 /// Sets the insertion point to the node after the specified value. If value
75 /// has a defining operation, sets the insertion point to the node after such
76 /// defining operation. This will cause subsequent insertions to go right
77 /// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
78 /// the start of its block.
80 mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter,
81  MlirValue value);
82 
83 /// Sets the insertion point to the start of the specified block.
85 mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter,
86  MlirBlock block);
87 
88 /// Sets the insertion point to the end of the specified block.
90 mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter,
91  MlirBlock block);
92 
93 /// Return the block the current insertion point belongs to. Note that the
94 /// insertion point is not necessarily the end of the block.
95 MLIR_CAPI_EXPORTED MlirBlock
96 mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter);
97 
98 /// Returns the current block of the rewriter.
99 MLIR_CAPI_EXPORTED MlirBlock
100 mlirRewriterBaseGetBlock(MlirRewriterBase rewriter);
101 
102 //===----------------------------------------------------------------------===//
103 /// Block and operation creation/insertion/cloning
104 
105 // These functions do not include the IRMapper, as it is not yet exposed by the
106 // C API.
107 
108 /// Add new block with 'argTypes' arguments and set the insertion point to the
109 /// end of it. The block is placed before 'insertBefore'. `locs` contains the
110 /// locations of the inserted arguments, and should match the size of
111 /// `argTypes`.
113  MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes,
114  MlirType const *argTypes, MlirLocation const *locations);
115 
116 /// Insert the given operation at the current insertion point and return it.
117 MLIR_CAPI_EXPORTED MlirOperation
118 mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op);
119 
120 /// Creates a deep copy of the specified operation.
121 MLIR_CAPI_EXPORTED MlirOperation
122 mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op);
123 
124 /// Creates a deep copy of this operation but keep the operation regions
125 /// empty.
127  MlirRewriterBase rewriter, MlirOperation op);
128 
129 /// Clone the blocks that belong to "region" before the given position in
130 /// another region "parent".
132 mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
133  MlirBlock before);
134 
135 //===----------------------------------------------------------------------===//
136 /// RewriterBase API
137 //===----------------------------------------------------------------------===//
138 
139 /// Move the blocks that belong to "region" before the given position in
140 /// another region "parent". The two regions must be different. The caller
141 /// is responsible for creating or updating the operation transferring flow
142 /// of control to the region and passing it the correct block arguments.
144 mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region,
145  MlirBlock before);
146 
147 /// Replace the results of the given (original) operation with the specified
148 /// list of values (replacements). The result types of the given op and the
149 /// replacements must match. The original op is erased.
151 mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter, MlirOperation op,
152  intptr_t nValues, MlirValue const *values);
153 
154 /// Replace the results of the given (original) operation with the specified
155 /// new op (replacement). The result types of the two ops must match. The
156 /// original op is erased.
158 mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter,
159  MlirOperation op, MlirOperation newOp);
160 
161 /// Erases an operation that is known to have no uses.
162 MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter,
163  MlirOperation op);
164 
165 /// Erases a block along with all operations inside it.
166 MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter,
167  MlirBlock block);
168 
169 /// Inline the operations of block 'source' before the operation 'op'. The
170 /// source block will be deleted and must have no uses. 'argValues' is used to
171 /// replace the block arguments of 'source'
172 ///
173 /// The source block must have no successors. Otherwise, the resulting IR
174 /// would have unreachable operations.
176 mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source,
177  MlirOperation op, intptr_t nArgValues,
178  MlirValue const *argValues);
179 
180 /// Inline the operations of block 'source' into the end of block 'dest'. The
181 /// source block will be deleted and must have no uses. 'argValues' is used to
182 /// replace the block arguments of 'source'
183 ///
184 /// The dest block must have no successors. Otherwise, the resulting IR would
185 /// have unreachable operation.
186 MLIR_CAPI_EXPORTED void mlirRewriterBaseMergeBlocks(MlirRewriterBase rewriter,
187  MlirBlock source,
188  MlirBlock dest,
189  intptr_t nArgValues,
190  MlirValue const *argValues);
191 
192 /// Unlink this operation from its current block and insert it right before
193 /// `existingOp` which may be in the same or another block in the same
194 /// function.
195 MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpBefore(MlirRewriterBase rewriter,
196  MlirOperation op,
197  MlirOperation existingOp);
198 
199 /// Unlink this operation from its current block and insert it right after
200 /// `existingOp` which may be in the same or another block in the same
201 /// function.
202 MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpAfter(MlirRewriterBase rewriter,
203  MlirOperation op,
204  MlirOperation existingOp);
205 
206 /// Unlink this block and insert it right before `existingBlock`.
208 mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block,
209  MlirBlock existingBlock);
210 
211 /// This method is used to notify the rewriter that an in-place operation
212 /// modification is about to happen. A call to this function *must* be
213 /// followed by a call to either `finalizeOpModification` or
214 /// `cancelOpModification`. This is a minor efficiency win (it avoids creating
215 /// a new operation and removing the old one) but also often allows simpler
216 /// code in the client.
218 mlirRewriterBaseStartOpModification(MlirRewriterBase rewriter,
219  MlirOperation op);
220 
221 /// This method is used to signal the end of an in-place modification of the
222 /// given operation. This can only be called on operations that were provided
223 /// to a call to `startOpModification`.
225 mlirRewriterBaseFinalizeOpModification(MlirRewriterBase rewriter,
226  MlirOperation op);
227 
228 /// This method cancels a pending in-place modification. This can only be
229 /// called on operations that were provided to a call to
230 /// `startOpModification`.
232 mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter,
233  MlirOperation op);
234 
235 /// Find uses of `from` and replace them with `to`. Also notify the listener
236 /// about every in-place op modification (for every use that was replaced).
238 mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from,
239  MlirValue to);
240 
241 /// Find uses of `from` and replace them with `to`. Also notify the listener
242 /// about every in-place op modification (for every use that was replaced).
244  MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from,
245  MlirValue const *to);
246 
247 /// Find uses of `from` and replace them with `to`. Also notify the listener
248 /// about every in-place op modification (for every use that was replaced)
249 /// and that the `from` operation is about to be replaced.
251 mlirRewriterBaseReplaceAllOpUsesWithValueRange(MlirRewriterBase rewriter,
252  MlirOperation from, intptr_t nTo,
253  MlirValue const *to);
254 
255 /// Find uses of `from` and replace them with `to`. Also notify the listener
256 /// about every in-place op modification (for every use that was replaced)
257 /// and that the `from` operation is about to be replaced.
259  MlirRewriterBase rewriter, MlirOperation from, MlirOperation to);
260 
261 /// Find uses of `from` within `block` and replace them with `to`. Also notify
262 /// the listener about every in-place op modification (for every use that was
263 /// replaced). The optional `allUsesReplaced` flag is set to "true" if all
264 /// uses were replaced.
266  MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues,
267  MlirValue const *newValues, MlirBlock block);
268 
269 /// Find uses of `from` and replace them with `to` except if the user is
270 /// `exceptedUser`. Also notify the listener about every in-place op
271 /// modification (for every use that was replaced).
273 mlirRewriterBaseReplaceAllUsesExcept(MlirRewriterBase rewriter, MlirValue from,
274  MlirValue to, MlirOperation exceptedUser);
275 
276 //===----------------------------------------------------------------------===//
277 /// IRRewriter API
278 //===----------------------------------------------------------------------===//
279 
280 /// Create an IRRewriter and transfer ownership to the caller.
281 MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context);
282 
283 /// Create an IRRewriter and transfer ownership to the caller. Additionally
284 /// set the insertion point before the operation.
285 MLIR_CAPI_EXPORTED MlirRewriterBase
286 mlirIRRewriterCreateFromOp(MlirOperation op);
287 
288 /// Takes an IRRewriter owned by the caller and destroys it. It is the
289 /// responsibility of the user to only pass an IRRewriter class.
290 MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter);
291 
292 //===----------------------------------------------------------------------===//
293 /// FrozenRewritePatternSet API
294 //===----------------------------------------------------------------------===//
295 
296 MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet
297 mlirFreezeRewritePattern(MlirRewritePatternSet op);
298 
300 mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet op);
301 
303  MlirModule op, MlirFrozenRewritePatternSet patterns,
304  MlirGreedyRewriteDriverConfig);
305 
306 //===----------------------------------------------------------------------===//
307 /// PDLPatternModule API
308 //===----------------------------------------------------------------------===//
309 
310 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
311 DEFINE_C_API_STRUCT(MlirPDLPatternModule, void);
312 
313 MLIR_CAPI_EXPORTED MlirPDLPatternModule
315 
316 MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op);
317 
318 MLIR_CAPI_EXPORTED MlirRewritePatternSet
320 #endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
321 
322 #undef DEFINE_C_API_STRUCT
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 
328 #endif // MLIR_C_REWRITE_H
MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveBlockBefore(MlirRewriterBase rewriter, MlirBlock block, MlirBlock existingBlock)
Unlink this block and insert it right before existingBlock.
Definition: Rewrite.cpp:169
MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op)
MLIR_CAPI_EXPORTED void mlirRewriterBaseFinalizeOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method is used to signal the end of an in-place modification of the given operation.
Definition: Rewrite.cpp:179
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter, MlirOperation op, intptr_t nValues, MlirValue const *values)
Replace the results of the given (original) operation with the specified list of values (replacements...
Definition: Rewrite.cpp:118
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseBlock(MlirRewriterBase rewriter, MlirBlock block)
Erases a block along with all operations inside it.
Definition: Rewrite.cpp:136
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointBefore(MlirRewriterBase rewriter, MlirOperation op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
Definition: Rewrite.cpp:37
MLIR_CAPI_EXPORTED void mlirRewriterBaseCancelOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method cancels a pending in-place modification.
Definition: Rewrite.cpp:184
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointAfter(MlirRewriterBase rewriter, MlirOperation op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Rewrite.cpp:42
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceOpUsesWithinBlock(MlirRewriterBase rewriter, MlirOperation op, intptr_t nNewValues, MlirValue const *newValues, MlirBlock block)
Find uses of from within block and replace them with to.
Definition: Rewrite.cpp:220
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllOpUsesWithOperation(MlirRewriterBase rewriter, MlirOperation from, MlirOperation to)
Find uses of from and replace them with to.
Definition: Rewrite.cpp:214
MLIR_CAPI_EXPORTED MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op)
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseClone(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of the specified operation.
Definition: Rewrite.cpp:93
#define DEFINE_C_API_STRUCT(name, storage)
Opaque type declarations (see mlir-c/IR.h for more details).
Definition: Rewrite.h:30
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseInsert(MlirRewriterBase rewriter, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition: Rewrite.cpp:86
MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet op)
FrozenRewritePatternSet API.
Definition: Rewrite.cpp:277
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllValueRangeUsesWith(MlirRewriterBase rewriter, intptr_t nValues, MlirValue const *from, MlirValue const *to)
Find uses of from and replace them with to.
Definition: Rewrite.cpp:194
MLIR_CAPI_EXPORTED void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition: Rewrite.cpp:132
MLIR_CAPI_EXPORTED void mlirRewriterBaseInlineBlockBefore(MlirRewriterBase rewriter, MlirBlock source, MlirOperation op, intptr_t nArgValues, MlirValue const *argValues)
Inline the operations of block 'source' before the operation 'op'.
Definition: Rewrite.cpp:140
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreateFromOp(MlirOperation op)
Create an IRRewriter and transfer ownership to the caller.
Definition: Rewrite.cpp:246
MLIR_CAPI_EXPORTED void mlirRewriterBaseStartOpModification(MlirRewriterBase rewriter, MlirOperation op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
Definition: Rewrite.cpp:174
MLIR_CAPI_EXPORTED MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition: Rewrite.cpp:26
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter, MlirOperation op, MlirOperation newOp)
Replace the results of the given (original) operation with the specified new op (replacement).
Definition: Rewrite.cpp:126
MLIR_CAPI_EXPORTED MlirRewriterBase mlirIRRewriterCreate(MlirContext context)
IRRewriter API.
Definition: Rewrite.cpp:242
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition: Rewrite.cpp:289
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition: Rewrite.cpp:62
MLIR_CAPI_EXPORTED void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet op)
Definition: Rewrite.cpp:283
MLIR_CAPI_EXPORTED void mlirRewriterBaseClearInsertionPoint(MlirRewriterBase rewriter)
Insertion points methods.
Definition: Rewrite.cpp:33
MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpAfter(MlirRewriterBase rewriter, MlirOperation op, MlirOperation existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
Definition: Rewrite.cpp:164
MLIR_CAPI_EXPORTED void mlirRewriterBaseCloneRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
Clone the blocks that belong to "region" before the given position in another region "parent".
Definition: Rewrite.cpp:103
MLIR_CAPI_EXPORTED MlirRewritePatternSet mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op)
MLIR_CAPI_EXPORTED void mlirRewriterBaseInlineRegionBefore(MlirRewriterBase rewriter, MlirRegion region, MlirBlock before)
RewriterBase API.
Definition: Rewrite.cpp:113
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter)
Returns the current block of the rewriter.
Definition: Rewrite.cpp:66
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToEnd(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the end of the specified block.
Definition: Rewrite.cpp:57
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllUsesExcept(MlirRewriterBase rewriter, MlirValue from, MlirValue to, MlirOperation exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
Definition: Rewrite.cpp:231
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllOpUsesWithValueRange(MlirRewriterBase rewriter, MlirOperation from, intptr_t nTo, MlirValue const *to)
Find uses of from and replace them with to.
Definition: Rewrite.cpp:205
MLIR_CAPI_EXPORTED void mlirRewriterBaseMoveOpBefore(MlirRewriterBase rewriter, MlirOperation op, MlirOperation existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
Definition: Rewrite.cpp:159
MLIR_CAPI_EXPORTED void mlirRewriterBaseMergeBlocks(MlirRewriterBase rewriter, MlirBlock source, MlirBlock dest, intptr_t nArgValues, MlirValue const *argValues)
Inline the operations of block 'source' into the end of block 'dest'.
Definition: Rewrite.cpp:151
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointAfterValue(MlirRewriterBase rewriter, MlirValue value)
Sets the insertion point to the node after the specified value.
Definition: Rewrite.cpp:47
MLIR_CAPI_EXPORTED MlirOperation mlirRewriterBaseCloneWithoutRegions(MlirRewriterBase rewriter, MlirOperation op)
Creates a deep copy of this operation but keep the operation regions empty.
Definition: Rewrite.cpp:98
MLIR_CAPI_EXPORTED void mlirIRRewriterDestroy(MlirRewriterBase rewriter)
Takes an IRRewriter owned by the caller and destroys it.
Definition: Rewrite.cpp:250
MLIR_CAPI_EXPORTED MlirBlock mlirRewriterBaseCreateBlockBefore(MlirRewriterBase rewriter, MlirBlock insertBefore, intptr_t nArgTypes, MlirType const *argTypes, MlirLocation const *locations)
Block and operation creation/insertion/cloning.
Definition: Rewrite.cpp:73
MLIR_CAPI_EXPORTED void mlirRewriterBaseReplaceAllUsesWith(MlirRewriterBase rewriter, MlirValue from, MlirValue to)
Find uses of from and replace them with to.
Definition: Rewrite.cpp:189
MLIR_CAPI_EXPORTED void mlirRewriterBaseSetInsertionPointToStart(MlirRewriterBase rewriter, MlirBlock block)
Sets the insertion point to the start of the specified block.
Definition: Rewrite.cpp:52
#define MLIR_CAPI_EXPORTED
Definition: Support.h:46
A logical result value, essentially a boolean with named states.
Definition: Support.h:116