xref: /llvm-project/mlir/test/Interfaces/LoopLikeInterface/test-block-loop.mlir (revision 81a79ee446fe499e217144ae1cd505ede9021712)
1*81a79ee4STom Eccles// RUN: mlir-opt %s --mlir-disable-threading -test-block-is-in-loop 2>&1 | FileCheck %s
2*81a79ee4STom Eccles
3*81a79ee4STom Ecclesmodule {
4*81a79ee4STom Eccles  // Test function with only one bb
5*81a79ee4STom Eccles  func.func @simple() {
6*81a79ee4STom Eccles    func.return
7*81a79ee4STom Eccles  }
8*81a79ee4STom Eccles// CHECK: Block is not in a loop
9*81a79ee4STom Eccles// CHECK-NEXT: ^bb0:
10*81a79ee4STom Eccles
11*81a79ee4STom Eccles  // Test simple loop bb0 -> bb0
12*81a79ee4STom Eccles  func.func @loopForever() {
13*81a79ee4STom Eccles  ^bb0:
14*81a79ee4STom Eccles    cf.br ^bb1
15*81a79ee4STom Eccles  ^bb1:
16*81a79ee4STom Eccles    cf.br ^bb1
17*81a79ee4STom Eccles  }
18*81a79ee4STom Eccles// CHECK: Block is not in a loop
19*81a79ee4STom Eccles// CHECK-NEXT: ^bb0:
20*81a79ee4STom Eccles// CHECK: Block is in a loop
21*81a79ee4STom Eccles// CHECK-NEXT: ^bb1:
22*81a79ee4STom Eccles
23*81a79ee4STom Eccles  // Test bb0 -> bb1 -> bb2 -> bb1
24*81a79ee4STom Eccles  func.func @loopForever2() {
25*81a79ee4STom Eccles  ^bb0:
26*81a79ee4STom Eccles    cf.br ^bb1
27*81a79ee4STom Eccles  ^bb1:
28*81a79ee4STom Eccles    cf.br ^bb2
29*81a79ee4STom Eccles  ^bb2:
30*81a79ee4STom Eccles    cf.br ^bb1
31*81a79ee4STom Eccles  }
32*81a79ee4STom Eccles// CHECK: Block is not in a loop
33*81a79ee4STom Eccles// CHECK-NEXT: ^bb0:
34*81a79ee4STom Eccles// CHECK: Block is in a loop
35*81a79ee4STom Eccles// CHECK-NEXT: ^bb1:
36*81a79ee4STom Eccles// CHECK: Block is in a loop
37*81a79ee4STom Eccles// CHECK-NEXT: ^bb2:
38*81a79ee4STom Eccles
39*81a79ee4STom Eccles  // Test conditional branch without loop
40*81a79ee4STom Eccles  // bb0 -> bb1 -> {bb2, bb3}
41*81a79ee4STom Eccles  func.func @noLoop(%arg0: i1) {
42*81a79ee4STom Eccles    cf.br ^bb1
43*81a79ee4STom Eccles  ^bb1:
44*81a79ee4STom Eccles    cf.cond_br %arg0, ^bb2, ^bb3
45*81a79ee4STom Eccles  ^bb2:
46*81a79ee4STom Eccles    func.return
47*81a79ee4STom Eccles  ^bb3:
48*81a79ee4STom Eccles    func.return
49*81a79ee4STom Eccles  }
50*81a79ee4STom Eccles// CHECK: Block is not in a loop
51*81a79ee4STom Eccles// CHECK-NEXT: ^bb0(%arg0: i1)
52*81a79ee4STom Eccles// CHECK: Block is not in a loop
53*81a79ee4STom Eccles// CHECK-NEXT: ^bb1:
54*81a79ee4STom Eccles// CHECK: Block is not in a loop
55*81a79ee4STom Eccles// CHECK-NEXT: ^bb2:
56*81a79ee4STom Eccles// CHECK: Block is not in a loop
57*81a79ee4STom Eccles// CHECK-NEXT: ^bb3:
58*81a79ee4STom Eccles
59*81a79ee4STom Eccles  // test multiple loops
60*81a79ee4STom Eccles  // bb0 -> bb1 -> bb2 -> bb3 { -> bb2} -> bb4 { -> bb1 } -> bb5
61*81a79ee4STom Eccles  func.func @multipleLoops(%arg0: i1, %arg1: i1) {
62*81a79ee4STom Eccles    cf.br ^bb1
63*81a79ee4STom Eccles  ^bb1:
64*81a79ee4STom Eccles    cf.br ^bb2
65*81a79ee4STom Eccles  ^bb2:
66*81a79ee4STom Eccles    cf.br ^bb3
67*81a79ee4STom Eccles  ^bb3:
68*81a79ee4STom Eccles    cf.cond_br %arg0, ^bb4, ^bb2
69*81a79ee4STom Eccles  ^bb4:
70*81a79ee4STom Eccles    cf.cond_br %arg1, ^bb1, ^bb5
71*81a79ee4STom Eccles  ^bb5:
72*81a79ee4STom Eccles    return
73*81a79ee4STom Eccles  }
74*81a79ee4STom Eccles// CHECK: Block is not in a loop
75*81a79ee4STom Eccles// CHECK-NEXT: ^bb0(%arg0: i1, %arg1: i1)
76*81a79ee4STom Eccles// CHECK: Block is in a loop
77*81a79ee4STom Eccles// CHECK-NEXT: ^bb1:
78*81a79ee4STom Eccles// CHECK: Block is in a loop
79*81a79ee4STom Eccles// CHECK-NEXT: ^bb2:
80*81a79ee4STom Eccles// CHECK: Block is in a loop
81*81a79ee4STom Eccles// CHECK-NEXT: ^bb3:
82*81a79ee4STom Eccles// CHECK: Block is in a loop
83*81a79ee4STom Eccles// CHECK-NEXT: ^bb4:
84*81a79ee4STom Eccles// CHECK: Block is not in a loop
85*81a79ee4STom Eccles// CHECK-NEXT: ^bb5:
86*81a79ee4STom Eccles
87*81a79ee4STom Eccles  // test derived from real Flang output
88*81a79ee4STom Eccles  func.func @_QPblockTest0(%arg0: i1, %arg1: i1) {
89*81a79ee4STom Eccles    cf.br ^bb1
90*81a79ee4STom Eccles  ^bb1:  // 2 preds: ^bb0, ^bb4
91*81a79ee4STom Eccles    cf.cond_br %arg0, ^bb2, ^bb5
92*81a79ee4STom Eccles  ^bb2:  // pred: ^bb1
93*81a79ee4STom Eccles    cf.cond_br %arg1, ^bb3, ^bb4
94*81a79ee4STom Eccles  ^bb3:  // pred: ^bb2
95*81a79ee4STom Eccles    return
96*81a79ee4STom Eccles  ^bb4:  // pred: ^bb2
97*81a79ee4STom Eccles    cf.br ^bb1
98*81a79ee4STom Eccles  ^bb5:  // pred: ^bb1
99*81a79ee4STom Eccles    return
100*81a79ee4STom Eccles  }
101*81a79ee4STom Eccles// CHECK: Block is not in a loop
102*81a79ee4STom Eccles// CHECK-NEXT: ^bb0(%arg0: i1, %arg1: i1)
103*81a79ee4STom Eccles// CHECK: Block is in a loop
104*81a79ee4STom Eccles// CHECK-NEXT: ^bb1:
105*81a79ee4STom Eccles// CHECK: Block is in a loop
106*81a79ee4STom Eccles// CHECK-NEXT: ^bb2:
107*81a79ee4STom Eccles// CHECK: Block is not in a loop
108*81a79ee4STom Eccles// CHECK-NEXT: ^bb3:
109*81a79ee4STom Eccles// CHECK: Block is in a loop
110*81a79ee4STom Eccles// CHECK-NEXT: ^bb4:
111*81a79ee4STom Eccles// CHECK: Block is not in a loop
112*81a79ee4STom Eccles// CHECK-NEXT: ^bb5:
113*81a79ee4STom Eccles
114*81a79ee4STom Eccles// check nested blocks
115*81a79ee4STom Eccles  func.func @check_alloc_in_loop(%counter : i64) {
116*81a79ee4STom Eccles    cf.br ^bb1(%counter: i64)
117*81a79ee4STom Eccles    ^bb1(%lv : i64):
118*81a79ee4STom Eccles      %cm1 = arith.constant -1 : i64
119*81a79ee4STom Eccles      %rem = arith.addi %lv, %cm1 : i64
120*81a79ee4STom Eccles      %zero = arith.constant 0 : i64
121*81a79ee4STom Eccles      %p = arith.cmpi eq, %rem, %zero : i64
122*81a79ee4STom Eccles      cf.cond_br %p, ^bb3, ^bb2
123*81a79ee4STom Eccles    ^bb2:
124*81a79ee4STom Eccles      scf.execute_region -> () {
125*81a79ee4STom Eccles        %c1 = arith.constant 1 : i64
126*81a79ee4STom Eccles        scf.yield
127*81a79ee4STom Eccles      }
128*81a79ee4STom Eccles      cf.br ^bb1(%rem: i64)
129*81a79ee4STom Eccles    ^bb3:
130*81a79ee4STom Eccles      return
131*81a79ee4STom Eccles  }
132*81a79ee4STom Eccles// CHECK: Block is not in a loop
133*81a79ee4STom Eccles// CHECK-NEXT: ^bb0(%arg0: i64):
134*81a79ee4STom Eccles// CHECK: Block is in a loop
135*81a79ee4STom Eccles// CHECK-NEXT: ^bb1(%0: i64)
136*81a79ee4STom Eccles// CHECK: Block is in a loop
137*81a79ee4STom Eccles// CHECK-NEXT: ^bb0:
138*81a79ee4STom Eccles// CHECK-NEXT: %c1_i64
139*81a79ee4STom Eccles// CHECK: Block is in a loop
140*81a79ee4STom Eccles// CHECK-NEXT: ^bb2:
141*81a79ee4STom Eccles// CHECK: Block is not in a loop
142*81a79ee4STom Eccles// CHECK-NEXT: ^bb3:
143*81a79ee4STom Eccles}
144