1 // RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline='builtin.module(func.func(canonicalize{region-simplify=aggressive}))' -split-input-file | FileCheck %s
3 // Check the simple case of single operation blocks with a return.
5 // CHECK-LABEL: func @return_blocks(
6 func.func @return_blocks() {
7 // CHECK: "foo.cond_br"()[^bb1, ^bb1]
12 "foo.cond_br"() [^bb1, ^bb2] : () -> ()
20 // Check the case of identical blocks with matching arguments.
22 // CHECK-LABEL: func @matching_arguments(
23 func.func @matching_arguments() -> i32 {
24 // CHECK: "foo.cond_br"()[^bb1, ^bb1]
25 // CHECK: ^bb1(%{{.*}}: i32):
29 "foo.cond_br"() [^bb1, ^bb2] : () -> ()
37 // Check that no merging occurs if there is an operand mismatch and we can't
38 // update th predecessor.
40 // CHECK-LABEL: func @mismatch_unknown_terminator
41 func.func @mismatch_unknown_terminator(%arg0 : i32, %arg1 : i32) -> i32 {
42 // CHECK: "foo.cond_br"()[^bb1, ^bb2]
44 "foo.cond_br"() [^bb1, ^bb2] : () -> ()
52 // Check that merging does occurs if there is an operand mismatch and we can
53 // update th predecessor.
55 // CHECK-LABEL: func @mismatch_operands
56 // CHECK-SAME: %[[COND:.*]]: i1, %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
57 func.func @mismatch_operands(%cond : i1, %arg0 : i32, %arg1 : i32) -> i32 {
58 // CHECK: %[[RES:.*]] = arith.select %[[COND]], %[[ARG0]], %[[ARG1]]
59 // CHECK: return %[[RES]]
61 cf.cond_br %cond, ^bb1, ^bb2
69 // Check the same as above, but with pre-existing arguments.
71 // CHECK-LABEL: func @mismatch_operands_matching_arguments(
72 // CHECK-SAME: %[[COND:.*]]: i1, %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
73 func.func @mismatch_operands_matching_arguments(%cond : i1, %arg0 : i32, %arg1 : i32) -> (i32, i32) {
74 // CHECK: %[[RES0:.*]] = arith.select %[[COND]], %[[ARG1]], %[[ARG0]]
75 // CHECK: %[[RES1:.*]] = arith.select %[[COND]], %[[ARG0]], %[[ARG1]]
76 // CHECK: return %[[RES1]], %[[RES0]]
78 cf.cond_br %cond, ^bb1(%arg1 : i32), ^bb2(%arg0 : i32)
81 return %arg0, %arg2 : i32, i32
83 return %arg1, %arg3 : i32, i32
86 // Check that merging does not occur if the uses of the arguments differ.
88 // CHECK-LABEL: func @mismatch_argument_uses(
89 func.func @mismatch_argument_uses(%cond : i1, %arg0 : i32, %arg1 : i32) -> (i32, i32) {
90 // CHECK: return {{.*}}, {{.*}}
92 cf.cond_br %cond, ^bb1(%arg1 : i32), ^bb2(%arg0 : i32)
95 return %arg0, %arg2 : i32, i32
97 return %arg3, %arg1 : i32, i32
100 // Check that merging does not occur if the types of the arguments differ.
102 // CHECK-LABEL: func @mismatch_argument_types(
103 func.func @mismatch_argument_types(%cond : i1, %arg0 : i32, %arg1 : i16) {
104 // CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
106 cf.cond_br %cond, ^bb1(%arg0 : i32), ^bb2(%arg1 : i16)
109 "foo.return"(%arg2) : (i32) -> ()
111 "foo.return"(%arg3) : (i16) -> ()
114 // Check that merging does not occur if the number of the arguments differ.
116 // CHECK-LABEL: func @mismatch_argument_count(
117 func.func @mismatch_argument_count(%cond : i1, %arg0 : i32) {
118 // CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
120 cf.cond_br %cond, ^bb1(%arg0 : i32), ^bb2
123 "foo.return"(%arg2) : (i32) -> ()
125 "foo.return"() : () -> ()
128 // Check that merging does not occur if the operations differ.
130 // CHECK-LABEL: func @mismatch_operations(
131 func.func @mismatch_operations(%cond : i1) {
132 // CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
134 cf.cond_br %cond, ^bb1, ^bb2
137 "foo.return"() : () -> ()
142 // Check that merging does not occur if the number of operations differ.
144 // CHECK-LABEL: func @mismatch_operation_count(
145 func.func @mismatch_operation_count(%cond : i1) {
146 // CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
148 cf.cond_br %cond, ^bb1, ^bb2
151 "foo.op"() : () -> ()
157 // Check that merging does not occur if the blocks contain regions.
159 // CHECK-LABEL: func @contains_regions(
160 func.func @contains_regions(%cond : i1) {
161 // CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
163 cf.cond_br %cond, ^bb1, ^bb2
167 "foo.op"() : () -> ()
172 "foo.op"() : () -> ()
177 // Check that properly handles back edges.
179 // CHECK-LABEL: func @mismatch_loop(
180 // CHECK-SAME: %[[ARG:.*]]: i1, %[[ARG2:.*]]: i1
181 func.func @mismatch_loop(%cond : i1, %cond2 : i1) {
182 // CHECK-NEXT: %[[LOOP_CARRY:.*]] = "foo.op"
183 // CHECK: cf.cond_br %{{.*}}, ^bb1(%[[ARG2]] : i1), ^bb2
185 %cond3 = "foo.op"() : () -> (i1)
186 cf.cond_br %cond, ^bb2, ^bb3
189 // CHECK: ^bb1(%[[ARG3:.*]]: i1):
190 // CHECK-NEXT: cf.cond_br %[[ARG3]], ^bb1(%[[LOOP_CARRY]] : i1), ^bb2
192 cf.cond_br %cond3, ^bb1, ^bb3
195 cf.cond_br %cond2, ^bb1, ^bb3
199 // CHECK-NEXT: return
204 // Check that blocks are not merged if the types of the operands differ.
206 // CHECK-LABEL: func @mismatch_operand_types(
207 func.func @mismatch_operand_types(%arg0 : i1, %arg1 : memref<i32>, %arg2 : memref<i1>) {
208 %c0_i32 = arith.constant 0 : i32
209 %true = arith.constant true
213 cf.cond_br %arg0, ^bb2, ^bb3
216 // CHECK: memref.store %{{.*}}, %{{.*}} : memref<i32>
217 memref.store %c0_i32, %arg1[] : memref<i32>
221 // CHECK: memref.store %{{.*}}, %{{.*}} : memref<i1>
222 memref.store %true, %arg2[] : memref<i1>
226 // Check that it is illegal to merge blocks containing an operand
227 // with an external user. Incorrectly performing the optimization
228 // anyways will result in print(merged, merged) rather than
229 // distinct operands.
230 func.func private @print(%arg0: i32, %arg1: i32)
231 // CHECK-LABEL: @nomerge
232 func.func @nomerge(%arg0: i32, %i: i32) {
233 %c1_i32 = arith.constant 1 : i32
234 %icmp = arith.cmpi slt, %i, %arg0 : i32
235 cf.cond_br %icmp, ^bb2, ^bb3
238 %ip1 = arith.addi %i, %c1_i32 : i32
239 cf.br ^bb4(%ip1 : i32)
242 %jp1 = arith.addi %j, %c1_i32 : i32
243 cf.br ^bb4(%jp1 : i32)
245 ^bb4(%j: i32): // 2 preds: ^bb2, ^bb7
246 %jcmp = arith.cmpi slt, %j, %arg0 : i32
247 // CHECK-NOT: call @print(%[[arg1:.+]], %[[arg1]])
248 call @print(%j, %ip1) : (i32, i32) -> ()
249 cf.cond_br %jcmp, ^bb7, ^bb3
256 // CHECK-LABEL: func @mismatch_dominance(
257 func.func @mismatch_dominance() -> i32 {
258 // CHECK: %[[RES:.*]] = "test.producing_br"()
259 %0 = "test.producing_br"()[^bb1, ^bb2] {
260 operandSegmentSizes = array<i32: 0, 0>
264 // CHECK: "test.br"(%[[RES]])[^[[MERGE_BLOCK:.*]]]
265 "test.br"(%0)[^bb4] : (i32) -> ()
268 %1 = "foo.def"() : () -> i32
269 "test.br"()[^bb3] : () -> ()
272 // CHECK: "test.br"(%{{.*}})[^[[MERGE_BLOCK]]]
273 "test.br"(%1)[^bb4] : (i32) -> ()
279 // CHECK-LABEL: func @dead_dealloc_fold_multi_use
280 func.func @dead_dealloc_fold_multi_use(%cond : i1) {
281 // CHECK-NEXT: return
282 %a = memref.alloc() : memref<4xf32>
283 cf.cond_br %cond, ^bb1, ^bb2
286 memref.dealloc %a: memref<4xf32>
290 memref.dealloc %a: memref<4xf32>
294 // CHECK-LABEL: func @nested_loop
295 func.func @nested_loop(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32, %arg4: i32, %arg5: i1) {
296 // Irreducible control-flow: enter the middle of the loop in LoopBody_entry here.
297 "test.foo_br"(%arg0, %arg4)[^LoopBody_entry] : (i32, i32) -> ()
299 // Loop exit condition: jump to exit or LoobBody blocks
300 ^Loop_header: // 2 preds: ^bb2, ^bb3
301 // Consumes the block arg from LoopBody_entry
302 // Because of this use here, we can't merge the two blocks below.
303 "test.foo_br2"(%0)[^EXIT, ^LoopBody_entry, ^LoopBody_other] : (i32) -> ()
305 // LoopBody_entry is jumped in from the entry block (bb0) and Loop_header
306 // It **dominates** the Loop_header.
307 ^LoopBody_entry(%0: i32): // 2 preds: ^bb0, ^Loop_header
309 %1 = "test.bar"(%0) : (i32) -> i32
312 // Other block inside the loop, not dominating the header
313 ^LoopBody_other(%2: i32): // pred: ^Loop_header
315 %3 = "test.bar"(%2) : (i32) -> i32
318 ^EXIT: // pred: ^Loop_header