11// RUN: fir-opt %s --fir-to-scf --allow-unregistered-dialect | FileCheck %s
22
33// CHECK-LABEL: func.func @test_simple_iterate_while_1() -> (index, i1, i16, i32) {
4- // CHECK: %[[VAL_0:.*]] = arith.constant 11 : index
5- // CHECK: %[[VAL_1:.*]] = arith.constant 22 : index
6- // CHECK: %[[VAL_2:.*]] = arith.constant 2 : index
7- // CHECK: %[[VAL_3:.*]] = arith.constant true
8- // CHECK: %[[VAL_4:.*]] = arith.constant 123 : i16
9- // CHECK: %[[VAL_5:.*]] = arith.constant 456 : i32
10- // CHECK: %[[VAL_6:.*]]:4 = scf.while (%[[VAL_7:.*]] = %[[VAL_0]], %[[VAL_8:.*]] = %[[VAL_3]], %[[VAL_9:.*]] = %[[VAL_4]], %[[VAL_10:.*]] = %[[VAL_5]]) : (index, i1, i16, i32) -> (index, i1, i16, i32) {
11- // CHECK: %[[VAL_11:.*]] = arith.cmpi sle, %[[VAL_7]], %[[VAL_1]] : index
12- // CHECK: %[[VAL_12:.*]] = arith.andi %[[VAL_11]], %[[VAL_8]] : i1
13- // CHECK: scf.condition(%[[VAL_12]]) %[[VAL_7]], %[[VAL_8]], %[[VAL_9]], %[[VAL_10]] : index, i1, i16, i32
4+ // CHECK: %[[CONSTANT_0:.*]] = arith.constant 11 : index
5+ // CHECK: %[[CONSTANT_1:.*]] = arith.constant 22 : index
6+ // CHECK: %[[CONSTANT_2:.*]] = arith.constant 2 : index
7+ // CHECK: %[[CONSTANT_3:.*]] = arith.constant true
8+ // CHECK: %[[CONSTANT_4:.*]] = arith.constant 123 : i16
9+ // CHECK: %[[CONSTANT_5:.*]] = arith.constant 456 : i32
10+ // CHECK: %[[WHILE_0:.*]]:4 = scf.while (%[[VAL_0:.*]] = %[[CONSTANT_0]], %[[VAL_1:.*]] = %[[CONSTANT_3]], %[[VAL_2:.*]] = %[[CONSTANT_4]], %[[VAL_3:.*]] = %[[CONSTANT_5]]) : (index, i1, i16, i32) -> (index, i1, i16, i32) {
11+ // CHECK: %[[CONSTANT_6:.*]] = arith.constant 0 : index
12+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_6]], %[[CONSTANT_2]] : index
13+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[CONSTANT_1]] : index
14+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_2]], %[[CONSTANT_6]] : index
15+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[CONSTANT_1]] : index
16+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
17+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
18+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
19+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
20+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]] : index, i1, i16, i32
1421// CHECK: } do {
15- // CHECK: ^bb0(%[[VAL_13 :.*]]: index, %[[VAL_14 :.*]]: i1, %[[VAL_15 :.*]]: i16, %[[VAL_16 :.*]]: i32):
16- // CHECK: %[[VAL_17 :.*]] = arith.addi %[[VAL_13 ]], %[[VAL_2 ]] : index
17- // CHECK: %[[VAL_18 :.*]] = arith.constant true
18- // CHECK: %[[VAL_19 :.*]] = arith.constant 22 : i16
19- // CHECK: %[[VAL_20 :.*]] = arith.constant 33 : i32
20- // CHECK: scf.yield %[[VAL_17 ]], %[[VAL_18 ]], %[[VAL_19 ]], %[[VAL_20 ]] : index, i1, i16, i32
21- // CHECK: }
22- // CHECK: return %[[VAL_21 :.*]]#0, %[[VAL_21 ]]#1, %[[VAL_21 ]]#2, %[[VAL_21 ]]#3 : index, i1, i16, i32
22+ // CHECK: ^bb0(%[[VAL_4 :.*]]: index, %[[VAL_5 :.*]]: i1, %[[VAL_6 :.*]]: i16, %[[VAL_7 :.*]]: i32):
23+ // CHECK: %[[ADDI_0 :.*]] = arith.addi %[[VAL_4 ]], %[[CONSTANT_2 ]] : index
24+ // CHECK: %[[CONSTANT_7 :.*]] = arith.constant true
25+ // CHECK: %[[CONSTANT_8 :.*]] = arith.constant 22 : i16
26+ // CHECK: %[[CONSTANT_9 :.*]] = arith.constant 33 : i32
27+ // CHECK: scf.yield %[[ADDI_0 ]], %[[CONSTANT_7 ]], %[[CONSTANT_8 ]], %[[CONSTANT_9 ]] : index, i1, i16, i32
28+ // CHECK: } attributes {finalValue}
29+ // CHECK: return %[[VAL_8 :.*]]#0, %[[VAL_8 ]]#1, %[[VAL_8 ]]#2, %[[VAL_8 ]]#3 : index, i1, i16, i32
2330// CHECK: }
2431func.func @test_simple_iterate_while_1() -> (index, i1, i16, i32) {
2532 %lo = arith.constant 11 : index
@@ -41,19 +48,26 @@ func.func @test_simple_iterate_while_1() -> (index, i1, i16, i32) {
4148
4249// CHECK-LABEL: func.func @test_simple_iterate_while_2(
4350// CHECK-SAME: %[[ARG0:.*]]: index, %[[ARG1:.*]]: index, %[[ARG2:.*]]: i1, %[[ARG3:.*]]: i32) -> (index, i1, i32) {
44- // CHECK: %[[VAL_0:.*]] = arith.constant 1 : index
45- // CHECK: %[[VAL_1:.*]]:3 = scf.while (%[[VAL_2:.*]] = %[[ARG0]], %[[VAL_3:.*]] = %[[ARG2]], %[[VAL_4:.*]] = %[[ARG3]]) : (index, i1, i32) -> (index, i1, i32) {
46- // CHECK: %[[VAL_5:.*]] = arith.cmpi sle, %[[VAL_2]], %[[ARG1]] : index
47- // CHECK: %[[VAL_6:.*]] = arith.andi %[[VAL_5]], %[[VAL_3]] : i1
48- // CHECK: scf.condition(%[[VAL_6]]) %[[VAL_2]], %[[VAL_3]], %[[VAL_4]] : index, i1, i32
51+ // CHECK: %[[CONSTANT_0:.*]] = arith.constant 1 : index
52+ // CHECK: %[[WHILE_0:.*]]:3 = scf.while (%[[VAL_0:.*]] = %[[ARG0]], %[[VAL_1:.*]] = %[[ARG2]], %[[VAL_2:.*]] = %[[ARG3]]) : (index, i1, i32) -> (index, i1, i32) {
53+ // CHECK: %[[CONSTANT_1:.*]] = arith.constant 0 : index
54+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_1]], %[[CONSTANT_0]] : index
55+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[ARG1]] : index
56+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_0]], %[[CONSTANT_1]] : index
57+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[ARG1]] : index
58+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
59+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
60+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
61+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
62+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : index, i1, i32
4963// CHECK: } do {
50- // CHECK: ^bb0(%[[VAL_7 :.*]]: index, %[[VAL_8 :.*]]: i1, %[[VAL_9 :.*]]: i32):
51- // CHECK: %[[VAL_10 :.*]] = arith.addi %[[VAL_7 ]], %[[VAL_0 ]] : index
52- // CHECK: %[[VAL_11 :.*]] = arith.constant 123 : i32
53- // CHECK: %[[VAL_12 :.*]] = arith.constant true
54- // CHECK: scf.yield %[[VAL_10 ]], %[[VAL_12 ]], %[[VAL_11 ]] : index, i1, i32
55- // CHECK: }
56- // CHECK: return %[[VAL_13 :.*]]#0, %[[VAL_13 ]]#1, %[[VAL_13 ]]#2 : index, i1, i32
64+ // CHECK: ^bb0(%[[VAL_3 :.*]]: index, %[[VAL_4 :.*]]: i1, %[[VAL_5 :.*]]: i32):
65+ // CHECK: %[[ADDI_0 :.*]] = arith.addi %[[VAL_3 ]], %[[CONSTANT_0 ]] : index
66+ // CHECK: %[[CONSTANT_2 :.*]] = arith.constant 123 : i32
67+ // CHECK: %[[CONSTANT_3 :.*]] = arith.constant true
68+ // CHECK: scf.yield %[[ADDI_0 ]], %[[CONSTANT_3 ]], %[[CONSTANT_2 ]] : index, i1, i32
69+ // CHECK: } attributes {finalValue}
70+ // CHECK: return %[[VAL_6 :.*]]#0, %[[VAL_6 ]]#1, %[[VAL_6 ]]#2 : index, i1, i32
5771// CHECK: }
5872func.func @test_simple_iterate_while_2(%start: index, %stop : index, %cond: i1, %val: i32) -> (index, i1, i32) {
5973 %step = arith.constant 1 : index
@@ -67,22 +81,97 @@ func.func @test_simple_iterate_while_2(%start: index, %stop: index, %cond: i1, %
6781 return %res#0, %res#1, %res#2 : index, i1, i32
6882}
6983
84+ // CHECK-LABEL: func.func @loop_with_negtive_step(
85+ // CHECK-SAME: %[[ARG0:.*]]: index,
86+ // CHECK-SAME: %[[ARG1:.*]]: index) -> i1 {
87+ // CHECK: %[[CONSTANT_0:.*]] = arith.constant -1 : index
88+ // CHECK: %[[CONSTANT_1:.*]] = arith.constant true
89+ // CHECK: %[[WHILE_0:.*]]:2 = scf.while (%[[VAL_0:.*]] = %[[ARG0]], %[[VAL_1:.*]] = %[[CONSTANT_1]]) : (index, i1) -> (index, i1) {
90+ // CHECK: %[[CONSTANT_2:.*]] = arith.constant 0 : index
91+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_2]], %[[CONSTANT_0]] : index
92+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[ARG1]] : index
93+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_0]], %[[CONSTANT_2]] : index
94+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[ARG1]] : index
95+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
96+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
97+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
98+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
99+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]] : index, i1
100+ // CHECK: } do {
101+ // CHECK: ^bb0(%[[VAL_2:.*]]: index, %[[VAL_3:.*]]: i1):
102+ // CHECK: %[[ADDI_0:.*]] = arith.addi %[[VAL_2]], %[[CONSTANT_0]] : index
103+ // CHECK: %[[VAL_4:.*]] = "test.get_some_value"() : () -> i1
104+ // CHECK: scf.yield %[[ADDI_0]], %[[VAL_4]] : index, i1
105+ // CHECK: } attributes {finalValue}
106+ // CHECK: return %[[VAL_5:.*]]#1 : i1
107+ // CHECK: }
108+ func.func @loop_with_negtive_step(%lo : index, %up : index) -> i1 {
109+ %c-1 = arith.constant -1 : index
110+ %ok1 = arith.constant true
111+ %res:2 = fir.iterate_while (%i = %lo to %up step %c-1) and (%j = %ok1) -> (index, i1) {
112+ %ok = "test.get_some_value"() : () -> i1
113+ fir.result %i, %ok : index, i1
114+ }
115+ return %res#1 : i1
116+ }
117+
118+ // CHECK-LABEL: func.func @loop_with_zero_step(
119+ // CHECK-SAME: %[[ARG0:.*]]: index,
120+ // CHECK-SAME: %[[ARG1:.*]]: index) -> i1 {
121+ // CHECK: %[[CONSTANT_0:.*]] = arith.constant 0 : index
122+ // CHECK: %[[CONSTANT_1:.*]] = arith.constant true
123+ // CHECK: %[[WHILE_0:.*]]:2 = scf.while (%[[VAL_0:.*]] = %[[ARG0]], %[[VAL_1:.*]] = %[[CONSTANT_1]]) : (index, i1) -> (index, i1) {
124+ // CHECK: %[[CONSTANT_2:.*]] = arith.constant 0 : index
125+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_2]], %[[CONSTANT_0]] : index
126+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[ARG1]] : index
127+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_0]], %[[CONSTANT_2]] : index
128+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[ARG1]] : index
129+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
130+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
131+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
132+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
133+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]] : index, i1
134+ // CHECK: } do {
135+ // CHECK: ^bb0(%[[VAL_2:.*]]: index, %[[VAL_3:.*]]: i1):
136+ // CHECK: %[[ADDI_0:.*]] = arith.addi %[[VAL_2]], %[[CONSTANT_0]] : index
137+ // CHECK: %[[VAL_4:.*]] = "test.get_some_value"() : () -> i1
138+ // CHECK: scf.yield %[[ADDI_0]], %[[VAL_4]] : index, i1
139+ // CHECK: } attributes {finalValue}
140+ // CHECK: return %[[VAL_5:.*]]#1 : i1
141+ // CHECK: }
142+ func.func @loop_with_zero_step(%lo : index, %up : index) -> i1 {
143+ %c0 = arith.constant 0 : index
144+ %ok1 = arith.constant true
145+ %res:2 = fir.iterate_while (%i = %lo to %up step %c0) and (%j = %ok1) -> (index, i1) {
146+ %ok = "test.get_some_value"() : () -> i1
147+ fir.result %i, %ok : index, i1
148+ }
149+ return %res#1 : i1
150+ }
151+
70152// CHECK-LABEL: func.func @test_zero_iterations() -> (index, i1, i8) {
71- // CHECK: %[[VAL_0:.*]] = arith.constant 10 : index
72- // CHECK: %[[VAL_1:.*]] = arith.constant 5 : index
73- // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
74- // CHECK: %[[VAL_3:.*]] = arith.constant true
75- // CHECK: %[[VAL_4:.*]] = arith.constant 42 : i8
76- // CHECK: %[[VAL_5:.*]]:3 = scf.while (%[[VAL_6:.*]] = %[[VAL_0]], %[[VAL_7:.*]] = %[[VAL_3]], %[[VAL_8:.*]] = %[[VAL_4]]) : (index, i1, i8) -> (index, i1, i8) {
77- // CHECK: %[[VAL_9:.*]] = arith.cmpi sle, %[[VAL_6]], %[[VAL_1]] : index
78- // CHECK: %[[VAL_10:.*]] = arith.andi %[[VAL_9]], %[[VAL_7]] : i1
79- // CHECK: scf.condition(%[[VAL_10]]) %[[VAL_6]], %[[VAL_7]], %[[VAL_8]] : index, i1, i8
153+ // CHECK: %[[CONSTANT_0:.*]] = arith.constant 10 : index
154+ // CHECK: %[[CONSTANT_1:.*]] = arith.constant 5 : index
155+ // CHECK: %[[CONSTANT_2:.*]] = arith.constant 1 : index
156+ // CHECK: %[[CONSTANT_3:.*]] = arith.constant true
157+ // CHECK: %[[CONSTANT_4:.*]] = arith.constant 42 : i8
158+ // CHECK: %[[WHILE_0:.*]]:3 = scf.while (%[[VAL_0:.*]] = %[[CONSTANT_0]], %[[VAL_1:.*]] = %[[CONSTANT_3]], %[[VAL_2:.*]] = %[[CONSTANT_4]]) : (index, i1, i8) -> (index, i1, i8) {
159+ // CHECK: %[[CONSTANT_5:.*]] = arith.constant 0 : index
160+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_5]], %[[CONSTANT_2]] : index
161+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[CONSTANT_1]] : index
162+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_2]], %[[CONSTANT_5]] : index
163+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[CONSTANT_1]] : index
164+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
165+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
166+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
167+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
168+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : index, i1, i8
80169// CHECK: } do {
81- // CHECK: ^bb0(%[[VAL_11 :.*]]: index, %[[VAL_12 :.*]]: i1, %[[VAL_13 :.*]]: i8):
82- // CHECK: %[[VAL_14 :.*]] = arith.addi %[[VAL_11 ]], %[[VAL_2 ]] : index
83- // CHECK: scf.yield %[[VAL_14 ]], %[[VAL_12 ]], %[[VAL_13 ]] : index, i1, i8
84- // CHECK: }
85- // CHECK: return %[[VAL_15 :.*]]#0, %[[VAL_15 ]]#1, %[[VAL_15 ]]#2 : index, i1, i8
170+ // CHECK: ^bb0(%[[VAL_3 :.*]]: index, %[[VAL_4 :.*]]: i1, %[[VAL_5 :.*]]: i8):
171+ // CHECK: %[[ADDI_0 :.*]] = arith.addi %[[VAL_3 ]], %[[CONSTANT_2 ]] : index
172+ // CHECK: scf.yield %[[ADDI_0 ]], %[[VAL_4 ]], %[[VAL_5 ]] : index, i1, i8
173+ // CHECK: } attributes {finalValue}
174+ // CHECK: return %[[VAL_6 :.*]]#0, %[[VAL_6 ]]#1, %[[VAL_6 ]]#2 : index, i1, i8
86175// CHECK: }
87176func.func @test_zero_iterations() -> (index, i1, i8) {
88177 %lo = arith.constant 10 : index
@@ -104,9 +193,16 @@ func.func @test_zero_iterations() -> (index, i1, i8) {
104193// CHECK: %[[CONSTANT_0:.*]] = arith.constant 1 : index
105194// CHECK: %[[CONSTANT_1:.*]] = arith.constant true
106195// CHECK: %[[WHILE_0:.*]]:2 = scf.while (%[[VAL_0:.*]] = %[[ARG0]], %[[VAL_1:.*]] = %[[CONSTANT_1]]) : (index, i1) -> (index, i1) {
107- // CHECK: %[[CMPI_0:.*]] = arith.cmpi sle, %[[VAL_0]], %[[ARG1]] : index
108- // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[VAL_1]] : i1
109- // CHECK: scf.condition(%[[ANDI_0]]) %[[VAL_0]], %[[VAL_1]] : index, i1
196+ // CHECK: %[[CONSTANT_2:.*]] = arith.constant 0 : index
197+ // CHECK: %[[CMPI_0:.*]] = arith.cmpi slt, %[[CONSTANT_2]], %[[CONSTANT_0]] : index
198+ // CHECK: %[[CMPI_1:.*]] = arith.cmpi sle, %[[VAL_0]], %[[ARG1]] : index
199+ // CHECK: %[[CMPI_2:.*]] = arith.cmpi slt, %[[CONSTANT_0]], %[[CONSTANT_2]] : index
200+ // CHECK: %[[CMPI_3:.*]] = arith.cmpi sge, %[[VAL_0]], %[[ARG1]] : index
201+ // CHECK: %[[ANDI_0:.*]] = arith.andi %[[CMPI_0]], %[[CMPI_1]] : i1
202+ // CHECK: %[[ANDI_1:.*]] = arith.andi %[[CMPI_2]], %[[CMPI_3]] : i1
203+ // CHECK: %[[ORI_0:.*]] = arith.ori %[[ANDI_0]], %[[ANDI_1]] : i1
204+ // CHECK: %[[ANDI_2:.*]] = arith.andi %[[VAL_1]], %[[ORI_0]] : i1
205+ // CHECK: scf.condition(%[[ANDI_2]]) %[[VAL_0]], %[[VAL_1]] : index, i1
110206// CHECK: } do {
111207// CHECK: ^bb0(%[[VAL_2:.*]]: index, %[[VAL_3:.*]]: i1):
112208// CHECK: %[[ADDI_0:.*]] = arith.addi %[[VAL_2]], %[[CONSTANT_0]] : index
0 commit comments