[clang-tidy][NFC]remove deps of clang in clang tidy test (#116588)
[llvm-project.git] / mlir / test / CAPI / rewrite.c
blobb33d225767046c1e9d4318550eb53634eb4934cc
1 //===- rewrite.c - Test of the rewriting C API ----------------------------===//
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 //===----------------------------------------------------------------------===//
10 // RUN: mlir-capi-rewrite-test 2>&1 | FileCheck %s
12 #include "mlir-c/Rewrite.h"
13 #include "mlir-c/BuiltinTypes.h"
14 #include "mlir-c/IR.h"
16 #include <assert.h>
17 #include <stdio.h>
19 MlirOperation createOperationWithName(MlirContext ctx, const char *name) {
20 MlirStringRef nameRef = mlirStringRefCreateFromCString(name);
21 MlirLocation loc = mlirLocationUnknownGet(ctx);
22 MlirOperationState state = mlirOperationStateGet(nameRef, loc);
23 MlirType indexType = mlirIndexTypeGet(ctx);
24 mlirOperationStateAddResults(&state, 1, &indexType);
25 return mlirOperationCreate(&state);
28 void testInsertionPoint(MlirContext ctx) {
29 // CHECK-LABEL: @testInsertionPoint
30 fprintf(stderr, "@testInsertionPoint\n");
32 const char *moduleString = "\"dialect.op1\"() : () -> ()\n";
33 MlirModule module =
34 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
35 MlirOperation op = mlirModuleGetOperation(module);
36 MlirBlock body = mlirModuleGetBody(module);
37 MlirOperation op1 = mlirBlockGetFirstOperation(body);
39 // IRRewriter create
40 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
42 // Insert before op
43 mlirRewriterBaseSetInsertionPointBefore(rewriter, op1);
44 MlirOperation op2 = createOperationWithName(ctx, "dialect.op2");
45 mlirRewriterBaseInsert(rewriter, op2);
47 // Insert after op
48 mlirRewriterBaseSetInsertionPointAfter(rewriter, op2);
49 MlirOperation op3 = createOperationWithName(ctx, "dialect.op3");
50 mlirRewriterBaseInsert(rewriter, op3);
51 MlirValue op3Res = mlirOperationGetResult(op3, 0);
53 // Insert after value
54 mlirRewriterBaseSetInsertionPointAfterValue(rewriter, op3Res);
55 MlirOperation op4 = createOperationWithName(ctx, "dialect.op4");
56 mlirRewriterBaseInsert(rewriter, op4);
58 // Insert at beginning of block
59 mlirRewriterBaseSetInsertionPointToStart(rewriter, body);
60 MlirOperation op5 = createOperationWithName(ctx, "dialect.op5");
61 mlirRewriterBaseInsert(rewriter, op5);
63 // Insert at end of block
64 mlirRewriterBaseSetInsertionPointToEnd(rewriter, body);
65 MlirOperation op6 = createOperationWithName(ctx, "dialect.op6");
66 mlirRewriterBaseInsert(rewriter, op6);
68 // Get insertion blocks
69 MlirBlock block1 = mlirRewriterBaseGetBlock(rewriter);
70 MlirBlock block2 = mlirRewriterBaseGetInsertionBlock(rewriter);
71 (void)block1;
72 (void)block2;
73 assert(body.ptr == block1.ptr);
74 assert(body.ptr == block2.ptr);
76 // clang-format off
77 // CHECK-NEXT: module {
78 // CHECK-NEXT: %{{.*}} = "dialect.op5"() : () -> index
79 // CHECK-NEXT: %{{.*}} = "dialect.op2"() : () -> index
80 // CHECK-NEXT: %{{.*}} = "dialect.op3"() : () -> index
81 // CHECK-NEXT: %{{.*}} = "dialect.op4"() : () -> index
82 // CHECK-NEXT: "dialect.op1"() : () -> ()
83 // CHECK-NEXT: %{{.*}} = "dialect.op6"() : () -> index
84 // CHECK-NEXT: }
85 // clang-format on
86 mlirOperationDump(op);
88 mlirIRRewriterDestroy(rewriter);
89 mlirModuleDestroy(module);
92 void testCreateBlock(MlirContext ctx) {
93 // CHECK-LABEL: @testCreateBlock
94 fprintf(stderr, "@testCreateBlock\n");
96 const char *moduleString = "\"dialect.op1\"() ({^bb0:}) : () -> ()\n"
97 "\"dialect.op2\"() ({^bb0:}) : () -> ()\n";
98 MlirModule module =
99 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
100 MlirOperation op = mlirModuleGetOperation(module);
101 MlirBlock body = mlirModuleGetBody(module);
103 MlirOperation op1 = mlirBlockGetFirstOperation(body);
104 MlirRegion region1 = mlirOperationGetRegion(op1, 0);
105 MlirBlock block1 = mlirRegionGetFirstBlock(region1);
107 MlirOperation op2 = mlirOperationGetNextInBlock(op1);
108 MlirRegion region2 = mlirOperationGetRegion(op2, 0);
109 MlirBlock block2 = mlirRegionGetFirstBlock(region2);
111 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
113 // Create block before
114 MlirType indexType = mlirIndexTypeGet(ctx);
115 MlirLocation unknown = mlirLocationUnknownGet(ctx);
116 mlirRewriterBaseCreateBlockBefore(rewriter, block1, 1, &indexType, &unknown);
118 mlirRewriterBaseSetInsertionPointToEnd(rewriter, body);
120 // Clone operation
121 mlirRewriterBaseClone(rewriter, op1);
123 // Clone without regions
124 mlirRewriterBaseCloneWithoutRegions(rewriter, op1);
126 // Clone region before
127 mlirRewriterBaseCloneRegionBefore(rewriter, region1, block2);
129 mlirOperationDump(op);
130 // clang-format off
131 // CHECK-NEXT: "builtin.module"() ({
132 // CHECK-NEXT: "dialect.op1"() ({
133 // CHECK-NEXT: ^{{.*}}(%{{.*}}: index):
134 // CHECK-NEXT: ^{{.*}}:
135 // CHECK-NEXT: }) : () -> ()
136 // CHECK-NEXT: "dialect.op2"() ({
137 // CHECK-NEXT: ^{{.*}}(%{{.*}}: index):
138 // CHECK-NEXT: ^{{.*}}:
139 // CHECK-NEXT: ^{{.*}}:
140 // CHECK-NEXT: }) : () -> ()
141 // CHECK-NEXT: "dialect.op1"() ({
142 // CHECK-NEXT: ^{{.*}}(%{{.*}}: index):
143 // CHECK-NEXT: ^{{.*}}:
144 // CHECK-NEXT: }) : () -> ()
145 // CHECK-NEXT: "dialect.op1"() ({
146 // CHECK-NEXT: }) : () -> ()
147 // CHECK-NEXT: }) : () -> ()
148 // clang-format on
150 mlirIRRewriterDestroy(rewriter);
151 mlirModuleDestroy(module);
154 void testInlineRegionBlock(MlirContext ctx) {
155 // CHECK-LABEL: @testInlineRegionBlock
156 fprintf(stderr, "@testInlineRegionBlock\n");
158 const char *moduleString =
159 "\"dialect.op1\"() ({\n"
160 " ^bb0(%arg0: index):\n"
161 " \"dialect.op1_in1\"(%arg0) [^bb1] : (index) -> ()\n"
162 " ^bb1():\n"
163 " \"dialect.op1_in2\"() : () -> ()\n"
164 "}) : () -> ()\n"
165 "\"dialect.op2\"() ({^bb0:}) : () -> ()\n"
166 "\"dialect.op3\"() ({\n"
167 " ^bb0(%arg0: index):\n"
168 " \"dialect.op3_in1\"(%arg0) : (index) -> ()\n"
169 " ^bb1():\n"
170 " %x = \"dialect.op3_in2\"() : () -> index\n"
171 " %y = \"dialect.op3_in3\"() : () -> index\n"
172 "}) : () -> ()\n"
173 "\"dialect.op4\"() ({\n"
174 " ^bb0():\n"
175 " \"dialect.op4_in1\"() : () -> index\n"
176 " ^bb1(%arg0: index):\n"
177 " \"dialect.op4_in2\"(%arg0) : (index) -> ()\n"
178 "}) : () -> ()\n";
179 MlirModule module =
180 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
181 MlirOperation op = mlirModuleGetOperation(module);
182 MlirBlock body = mlirModuleGetBody(module);
184 MlirOperation op1 = mlirBlockGetFirstOperation(body);
185 MlirRegion region1 = mlirOperationGetRegion(op1, 0);
187 MlirOperation op2 = mlirOperationGetNextInBlock(op1);
188 MlirRegion region2 = mlirOperationGetRegion(op2, 0);
189 MlirBlock block2 = mlirRegionGetFirstBlock(region2);
191 MlirOperation op3 = mlirOperationGetNextInBlock(op2);
192 MlirRegion region3 = mlirOperationGetRegion(op3, 0);
193 MlirBlock block3_1 = mlirRegionGetFirstBlock(region3);
194 MlirBlock block3_2 = mlirBlockGetNextInRegion(block3_1);
195 MlirOperation op3_in2 = mlirBlockGetFirstOperation(block3_2);
196 MlirValue op3_in2_res = mlirOperationGetResult(op3_in2, 0);
197 MlirOperation op3_in3 = mlirOperationGetNextInBlock(op3_in2);
199 MlirOperation op4 = mlirOperationGetNextInBlock(op3);
200 MlirRegion region4 = mlirOperationGetRegion(op4, 0);
201 MlirBlock block4_1 = mlirRegionGetFirstBlock(region4);
202 MlirOperation op4_in1 = mlirBlockGetFirstOperation(block4_1);
203 MlirValue op4_in1_res = mlirOperationGetResult(op4_in1, 0);
204 MlirBlock block4_2 = mlirBlockGetNextInRegion(block4_1);
206 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
208 // Test these three functions
209 mlirRewriterBaseInlineRegionBefore(rewriter, region1, block2);
210 mlirRewriterBaseInlineBlockBefore(rewriter, block3_1, op3_in3, 1,
211 &op3_in2_res);
212 mlirRewriterBaseMergeBlocks(rewriter, block4_2, block4_1, 1, &op4_in1_res);
214 mlirOperationDump(op);
215 // clang-format off
216 // CHECK-NEXT: "builtin.module"() ({
217 // CHECK-NEXT: "dialect.op1"() ({
218 // CHECK-NEXT: }) : () -> ()
219 // CHECK-NEXT: "dialect.op2"() ({
220 // CHECK-NEXT: ^{{.*}}(%{{.*}}: index):
221 // CHECK-NEXT: "dialect.op1_in1"(%{{.*}})[^[[bb:.*]]] : (index) -> ()
222 // CHECK-NEXT: ^[[bb]]:
223 // CHECK-NEXT: "dialect.op1_in2"() : () -> ()
224 // CHECK-NEXT: ^{{.*}}: // no predecessors
225 // CHECK-NEXT: }) : () -> ()
226 // CHECK-NEXT: "dialect.op3"() ({
227 // CHECK-NEXT: %{{.*}} = "dialect.op3_in2"() : () -> index
228 // CHECK-NEXT: "dialect.op3_in1"(%{{.*}}) : (index) -> ()
229 // CHECK-NEXT: %{{.*}} = "dialect.op3_in3"() : () -> index
230 // CHECK-NEXT: }) : () -> ()
231 // CHECK-NEXT: "dialect.op4"() ({
232 // CHECK-NEXT: %{{.*}} = "dialect.op4_in1"() : () -> index
233 // CHECK-NEXT: "dialect.op4_in2"(%{{.*}}) : (index) -> ()
234 // CHECK-NEXT: }) : () -> ()
235 // CHECK-NEXT: }) : () -> ()
236 // clang-format on
238 mlirIRRewriterDestroy(rewriter);
239 mlirModuleDestroy(module);
242 void testReplaceOp(MlirContext ctx) {
243 // CHECK-LABEL: @testReplaceOp
244 fprintf(stderr, "@testReplaceOp\n");
246 const char *moduleString =
247 "%x, %y, %z = \"dialect.create_values\"() : () -> (index, index, index)\n"
248 "%x_1, %y_1 = \"dialect.op1\"() : () -> (index, index)\n"
249 "\"dialect.use_op1\"(%x_1, %y_1) : (index, index) -> ()\n"
250 "%x_2, %y_2 = \"dialect.op2\"() : () -> (index, index)\n"
251 "%x_3, %y_3 = \"dialect.op3\"() : () -> (index, index)\n"
252 "\"dialect.use_op2\"(%x_2, %y_2) : (index, index) -> ()\n";
253 MlirModule module =
254 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
255 MlirOperation op = mlirModuleGetOperation(module);
256 MlirBlock body = mlirModuleGetBody(module);
258 // get a handle to all operations/values
259 MlirOperation createValues = mlirBlockGetFirstOperation(body);
260 MlirValue x = mlirOperationGetResult(createValues, 0);
261 MlirValue z = mlirOperationGetResult(createValues, 2);
262 MlirOperation op1 = mlirOperationGetNextInBlock(createValues);
263 MlirOperation useOp1 = mlirOperationGetNextInBlock(op1);
264 MlirOperation op2 = mlirOperationGetNextInBlock(useOp1);
265 MlirOperation op3 = mlirOperationGetNextInBlock(op2);
267 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
269 // Test replace op with values
270 MlirValue xz[2] = {x, z};
271 mlirRewriterBaseReplaceOpWithValues(rewriter, op1, 2, xz);
273 // Test replace op with op
274 mlirRewriterBaseReplaceOpWithOperation(rewriter, op2, op3);
276 mlirOperationDump(op);
277 // clang-format off
278 // CHECK-NEXT: module {
279 // CHECK-NEXT: %[[res:.*]]:3 = "dialect.create_values"() : () -> (index, index, index)
280 // CHECK-NEXT: "dialect.use_op1"(%[[res]]#0, %[[res]]#2) : (index, index) -> ()
281 // CHECK-NEXT: %[[res2:.*]]:2 = "dialect.op3"() : () -> (index, index)
282 // CHECK-NEXT: "dialect.use_op2"(%[[res2]]#0, %[[res2]]#1) : (index, index) -> ()
283 // CHECK-NEXT: }
284 // clang-format on
286 mlirIRRewriterDestroy(rewriter);
287 mlirModuleDestroy(module);
290 void testErase(MlirContext ctx) {
291 // CHECK-LABEL: @testErase
292 fprintf(stderr, "@testErase\n");
294 const char *moduleString = "\"dialect.op_to_erase\"() : () -> ()\n"
295 "\"dialect.op2\"() ({\n"
296 "^bb0():\n"
297 " \"dialect.op2_nested\"() : () -> ()"
298 "^block_to_erase():\n"
299 " \"dialect.op2_nested\"() : () -> ()"
300 "^bb1():\n"
301 " \"dialect.op2_nested\"() : () -> ()"
302 "}) : () -> ()\n";
303 MlirModule module =
304 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
305 MlirOperation op = mlirModuleGetOperation(module);
306 MlirBlock body = mlirModuleGetBody(module);
308 // get a handle to all operations/values
309 MlirOperation opToErase = mlirBlockGetFirstOperation(body);
310 MlirOperation op2 = mlirOperationGetNextInBlock(opToErase);
311 MlirRegion op2Region = mlirOperationGetRegion(op2, 0);
312 MlirBlock bb0 = mlirRegionGetFirstBlock(op2Region);
313 MlirBlock blockToErase = mlirBlockGetNextInRegion(bb0);
315 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
316 mlirRewriterBaseEraseOp(rewriter, opToErase);
317 mlirRewriterBaseEraseBlock(rewriter, blockToErase);
319 mlirOperationDump(op);
320 // CHECK-NEXT: module {
321 // CHECK-NEXT: "dialect.op2"() ({
322 // CHECK-NEXT: "dialect.op2_nested"() : () -> ()
323 // CHECK-NEXT: ^{{.*}}:
324 // CHECK-NEXT: "dialect.op2_nested"() : () -> ()
325 // CHECK-NEXT: }) : () -> ()
326 // CHECK-NEXT: }
328 mlirIRRewriterDestroy(rewriter);
329 mlirModuleDestroy(module);
332 void testMove(MlirContext ctx) {
333 // CHECK-LABEL: @testMove
334 fprintf(stderr, "@testMove\n");
336 const char *moduleString = "\"dialect.op1\"() : () -> ()\n"
337 "\"dialect.op2\"() ({\n"
338 "^bb0(%arg0: index):\n"
339 " \"dialect.op2_1\"(%arg0) : (index) -> ()"
340 "^bb1(%arg1: index):\n"
341 " \"dialect.op2_2\"(%arg1) : (index) -> ()"
342 "}) : () -> ()\n"
343 "\"dialect.op3\"() : () -> ()\n"
344 "\"dialect.op4\"() : () -> ()\n";
346 MlirModule module =
347 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
348 MlirOperation op = mlirModuleGetOperation(module);
349 MlirBlock body = mlirModuleGetBody(module);
351 // get a handle to all operations/values
352 MlirOperation op1 = mlirBlockGetFirstOperation(body);
353 MlirOperation op2 = mlirOperationGetNextInBlock(op1);
354 MlirOperation op3 = mlirOperationGetNextInBlock(op2);
355 MlirOperation op4 = mlirOperationGetNextInBlock(op3);
357 MlirRegion region2 = mlirOperationGetRegion(op2, 0);
358 MlirBlock block0 = mlirRegionGetFirstBlock(region2);
359 MlirBlock block1 = mlirBlockGetNextInRegion(block0);
361 // Test move operations.
362 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
363 mlirRewriterBaseMoveOpBefore(rewriter, op3, op1);
364 mlirRewriterBaseMoveOpAfter(rewriter, op4, op1);
365 mlirRewriterBaseMoveBlockBefore(rewriter, block1, block0);
367 mlirOperationDump(op);
368 // CHECK-NEXT: module {
369 // CHECK-NEXT: "dialect.op3"() : () -> ()
370 // CHECK-NEXT: "dialect.op1"() : () -> ()
371 // CHECK-NEXT: "dialect.op4"() : () -> ()
372 // CHECK-NEXT: "dialect.op2"() ({
373 // CHECK-NEXT: ^{{.*}}(%[[arg0:.*]]: index):
374 // CHECK-NEXT: "dialect.op2_2"(%[[arg0]]) : (index) -> ()
375 // CHECK-NEXT: ^{{.*}}(%[[arg1:.*]]: index): // no predecessors
376 // CHECK-NEXT: "dialect.op2_1"(%[[arg1]]) : (index) -> ()
377 // CHECK-NEXT: }) : () -> ()
378 // CHECK-NEXT: }
380 mlirIRRewriterDestroy(rewriter);
381 mlirModuleDestroy(module);
384 void testOpModification(MlirContext ctx) {
385 // CHECK-LABEL: @testOpModification
386 fprintf(stderr, "@testOpModification\n");
388 const char *moduleString =
389 "%x, %y = \"dialect.op1\"() : () -> (index, index)\n"
390 "\"dialect.op2\"(%x) : (index) -> ()\n";
392 MlirModule module =
393 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
394 MlirOperation op = mlirModuleGetOperation(module);
395 MlirBlock body = mlirModuleGetBody(module);
397 // get a handle to all operations/values
398 MlirOperation op1 = mlirBlockGetFirstOperation(body);
399 MlirValue y = mlirOperationGetResult(op1, 1);
400 MlirOperation op2 = mlirOperationGetNextInBlock(op1);
402 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
403 mlirRewriterBaseStartOpModification(rewriter, op1);
404 mlirRewriterBaseCancelOpModification(rewriter, op1);
406 mlirRewriterBaseStartOpModification(rewriter, op2);
407 mlirOperationSetOperand(op2, 0, y);
408 mlirRewriterBaseFinalizeOpModification(rewriter, op2);
410 mlirOperationDump(op);
411 // CHECK-NEXT: module {
412 // CHECK-NEXT: %[[xy:.*]]:2 = "dialect.op1"() : () -> (index, index)
413 // CHECK-NEXT: "dialect.op2"(%[[xy]]#1) : (index) -> ()
414 // CHECK-NEXT: }
416 mlirIRRewriterDestroy(rewriter);
417 mlirModuleDestroy(module);
420 void testReplaceUses(MlirContext ctx) {
421 // CHECK-LABEL: @testReplaceUses
422 fprintf(stderr, "@testReplaceUses\n");
424 const char *moduleString =
425 // Replace values with values
426 "%x1, %y1, %z1 = \"dialect.op1\"() : () -> (index, index, index)\n"
427 "%x2, %y2, %z2 = \"dialect.op2\"() : () -> (index, index, index)\n"
428 "\"dialect.op1_uses\"(%x1, %y1, %z1) : (index, index, index) -> ()\n"
429 // Replace op with values
430 "%x3 = \"dialect.op3\"() : () -> index\n"
431 "%x4 = \"dialect.op4\"() : () -> index\n"
432 "\"dialect.op3_uses\"(%x3) : (index) -> ()\n"
433 // Replace op with op
434 "%x5 = \"dialect.op5\"() : () -> index\n"
435 "%x6 = \"dialect.op6\"() : () -> index\n"
436 "\"dialect.op5_uses\"(%x5) : (index) -> ()\n"
437 // Replace op in block;
438 "%x7 = \"dialect.op7\"() : () -> index\n"
439 "%x8 = \"dialect.op8\"() : () -> index\n"
440 "\"dialect.op9\"() ({\n"
441 "^bb0:\n"
442 " \"dialect.op7_uses\"(%x7) : (index) -> ()\n"
443 "}): () -> ()\n"
444 "\"dialect.op7_uses\"(%x7) : (index) -> ()\n"
445 // Replace value with value except in op
446 "%x10 = \"dialect.op10\"() : () -> index\n"
447 "%x11 = \"dialect.op11\"() : () -> index\n"
448 "\"dialect.op10_uses\"(%x10) : (index) -> ()\n"
449 "\"dialect.op10_uses\"(%x10) : (index) -> ()\n";
451 MlirModule module =
452 mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
453 MlirOperation op = mlirModuleGetOperation(module);
454 MlirBlock body = mlirModuleGetBody(module);
456 // get a handle to all operations/values
457 MlirOperation op1 = mlirBlockGetFirstOperation(body);
458 MlirValue x1 = mlirOperationGetResult(op1, 0);
459 MlirValue y1 = mlirOperationGetResult(op1, 1);
460 MlirValue z1 = mlirOperationGetResult(op1, 2);
461 MlirOperation op2 = mlirOperationGetNextInBlock(op1);
462 MlirValue x2 = mlirOperationGetResult(op2, 0);
463 MlirValue y2 = mlirOperationGetResult(op2, 1);
464 MlirValue z2 = mlirOperationGetResult(op2, 2);
465 MlirOperation op1Uses = mlirOperationGetNextInBlock(op2);
467 MlirOperation op3 = mlirOperationGetNextInBlock(op1Uses);
468 MlirOperation op4 = mlirOperationGetNextInBlock(op3);
469 MlirValue x4 = mlirOperationGetResult(op4, 0);
470 MlirOperation op3Uses = mlirOperationGetNextInBlock(op4);
472 MlirOperation op5 = mlirOperationGetNextInBlock(op3Uses);
473 MlirOperation op6 = mlirOperationGetNextInBlock(op5);
474 MlirOperation op5Uses = mlirOperationGetNextInBlock(op6);
476 MlirOperation op7 = mlirOperationGetNextInBlock(op5Uses);
477 MlirOperation op8 = mlirOperationGetNextInBlock(op7);
478 MlirValue x8 = mlirOperationGetResult(op8, 0);
479 MlirOperation op9 = mlirOperationGetNextInBlock(op8);
480 MlirRegion region9 = mlirOperationGetRegion(op9, 0);
481 MlirBlock block9 = mlirRegionGetFirstBlock(region9);
482 MlirOperation op7Uses = mlirOperationGetNextInBlock(op9);
484 MlirOperation op10 = mlirOperationGetNextInBlock(op7Uses);
485 MlirValue x10 = mlirOperationGetResult(op10, 0);
486 MlirOperation op11 = mlirOperationGetNextInBlock(op10);
487 MlirValue x11 = mlirOperationGetResult(op11, 0);
488 MlirOperation op10Uses1 = mlirOperationGetNextInBlock(op11);
490 MlirRewriterBase rewriter = mlirIRRewriterCreate(ctx);
492 // Replace values
493 mlirRewriterBaseReplaceAllUsesWith(rewriter, x1, x2);
494 MlirValue y1z1[2] = {y1, z1};
495 MlirValue y2z2[2] = {y2, z2};
496 mlirRewriterBaseReplaceAllValueRangeUsesWith(rewriter, 2, y1z1, y2z2);
498 // Replace op with values
499 mlirRewriterBaseReplaceOpWithValues(rewriter, op3, 1, &x4);
501 // Replace op with op
502 mlirRewriterBaseReplaceOpWithOperation(rewriter, op5, op6);
504 // Replace op with op in block
505 mlirRewriterBaseReplaceOpUsesWithinBlock(rewriter, op7, 1, &x8, block9);
507 // Replace value with value except in op
508 mlirRewriterBaseReplaceAllUsesExcept(rewriter, x10, x11, op10Uses1);
510 mlirOperationDump(op);
511 // clang-format off
512 // CHECK-NEXT: module {
513 // CHECK-NEXT: %{{.*}}:3 = "dialect.op1"() : () -> (index, index, index)
514 // CHECK-NEXT: %[[res2:.*]]:3 = "dialect.op2"() : () -> (index, index, index)
515 // CHECK-NEXT: "dialect.op1_uses"(%[[res2]]#0, %[[res2]]#1, %[[res2]]#2) : (index, index, index) -> ()
516 // CHECK-NEXT: %[[res4:.*]] = "dialect.op4"() : () -> index
517 // CHECK-NEXT: "dialect.op3_uses"(%[[res4]]) : (index) -> ()
518 // CHECK-NEXT: %[[res6:.*]] = "dialect.op6"() : () -> index
519 // CHECK-NEXT: "dialect.op5_uses"(%[[res6]]) : (index) -> ()
520 // CHECK-NEXT: %[[res7:.*]] = "dialect.op7"() : () -> index
521 // CHECK-NEXT: %[[res8:.*]] = "dialect.op8"() : () -> index
522 // CHECK-NEXT: "dialect.op9"() ({
523 // CHECK-NEXT: "dialect.op7_uses"(%[[res8]]) : (index) -> ()
524 // CHECK-NEXT: }) : () -> ()
525 // CHECK-NEXT: "dialect.op7_uses"(%[[res7]]) : (index) -> ()
526 // CHECK-NEXT: %[[res10:.*]] = "dialect.op10"() : () -> index
527 // CHECK-NEXT: %[[res11:.*]] = "dialect.op11"() : () -> index
528 // CHECK-NEXT: "dialect.op10_uses"(%[[res10]]) : (index) -> ()
529 // CHECK-NEXT: "dialect.op10_uses"(%[[res11]]) : (index) -> ()
530 // CHECK-NEXT: }
531 // clang-format on
533 mlirIRRewriterDestroy(rewriter);
534 mlirModuleDestroy(module);
537 int main(void) {
538 MlirContext ctx = mlirContextCreate();
539 mlirContextSetAllowUnregisteredDialects(ctx, true);
540 mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("builtin"));
542 testInsertionPoint(ctx);
543 testCreateBlock(ctx);
544 testInlineRegionBlock(ctx);
545 testReplaceOp(ctx);
546 testErase(ctx);
547 testMove(ctx);
548 testOpModification(ctx);
549 testReplaceUses(ctx);
551 mlirContextDestroy(ctx);
552 return 0;