1// RUN: mlir-opt %s -test-loop-unrolling='unroll-factor=2' | FileCheck %s --check-prefix UNROLL-BY-2
2// RUN: mlir-opt %s -test-loop-unrolling='unroll-factor=3' | FileCheck %s --check-prefix UNROLL-BY-3
3// RUN: mlir-opt %s -test-loop-unrolling='unroll-factor=2 loop-depth=0' | FileCheck %s --check-prefix UNROLL-OUTER-BY-2
4// RUN: mlir-opt %s -test-loop-unrolling='unroll-factor=2 loop-depth=1' | FileCheck %s --check-prefix UNROLL-INNER-BY-2
5// RUN: mlir-opt %s -test-loop-unrolling='unroll-factor=2 annotate=true' | FileCheck %s --check-prefix UNROLL-BY-2-ANNOTATE
6// RUN: mlir-opt %s --affine-loop-unroll='unroll-factor=6 unroll-up-to-factor=true' | FileCheck %s --check-prefix UNROLL-UP-TO
7
8func.func @dynamic_loop_unroll(%arg0 : index, %arg1 : index, %arg2 : index,
9                          %arg3: memref<?xf32>) {
10  %0 = arith.constant 7.0 : f32
11  scf.for %i0 = %arg0 to %arg1 step %arg2 {
12    memref.store %0, %arg3[%i0] : memref<?xf32>
13  }
14  return
15}
16// UNROLL-BY-2-LABEL: func @dynamic_loop_unroll
17//  UNROLL-BY-2-SAME:  %[[LB:.*0]]: index,
18//  UNROLL-BY-2-SAME:  %[[UB:.*1]]: index,
19//  UNROLL-BY-2-SAME:  %[[STEP:.*2]]: index,
20//  UNROLL-BY-2-SAME:  %[[MEM:.*3]]: memref<?xf32>
21//
22//   UNROLL-BY-2-DAG:  %[[V0:.*]] = arith.subi %[[UB]], %[[LB]] : index
23//   UNROLL-BY-2-DAG:  %[[C1:.*]] = arith.constant 1 : index
24//   UNROLL-BY-2-DAG:  %[[V1:.*]] = arith.subi %[[STEP]], %[[C1]] : index
25//   UNROLL-BY-2-DAG:  %[[V2:.*]] = arith.addi %[[V0]], %[[V1]] : index
26//       Compute trip count in V3.
27//   UNROLL-BY-2-DAG:  %[[V3:.*]] = arith.divsi %[[V2]], %[[STEP]] : index
28//       Store unroll factor in C2.
29//   UNROLL-BY-2-DAG:  %[[C2:.*]] = arith.constant 2 : index
30//   UNROLL-BY-2-DAG:  %[[V4:.*]] = arith.remsi %[[V3]], %[[C2]] : index
31//   UNROLL-BY-2-DAG:  %[[V5:.*]] = arith.subi %[[V3]], %[[V4]] : index
32//   UNROLL-BY-2-DAG:  %[[V6:.*]] = arith.muli %[[V5]], %[[STEP]] : index
33//       Compute upper bound of unrolled loop in V7.
34//   UNROLL-BY-2-DAG:  %[[V7:.*]] = arith.addi %[[LB]], %[[V6]] : index
35//       Compute step of unrolled loop in V8.
36//   UNROLL-BY-2-DAG:  %[[V8:.*]] = arith.muli %[[STEP]], %[[C2]] : index
37//       UNROLL-BY-2:  scf.for %[[IV:.*]] = %[[LB]] to %[[V7]] step %[[V8]] {
38//  UNROLL-BY-2-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
39//  UNROLL-BY-2-NEXT:    %[[C1_IV:.*]] = arith.constant 1 : index
40//  UNROLL-BY-2-NEXT:    %[[V9:.*]] = arith.muli %[[STEP]], %[[C1_IV]] : index
41//  UNROLL-BY-2-NEXT:    %[[V10:.*]] = arith.addi %[[IV]], %[[V9]] : index
42//  UNROLL-BY-2-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V10]]] : memref<?xf32>
43//  UNROLL-BY-2-NEXT:  }
44//  UNROLL-BY-2-NEXT:  scf.for %[[IV:.*]] = %[[V7]] to %[[UB]] step %[[STEP]] {
45//  UNROLL-BY-2-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
46//  UNROLL-BY-2-NEXT:  }
47//  UNROLL-BY-2-NEXT:  return
48
49// UNROLL-BY-3-LABEL: func @dynamic_loop_unroll
50//  UNROLL-BY-3-SAME:  %[[LB:.*0]]: index,
51//  UNROLL-BY-3-SAME:  %[[UB:.*1]]: index,
52//  UNROLL-BY-3-SAME:  %[[STEP:.*2]]: index,
53//  UNROLL-BY-3-SAME:  %[[MEM:.*3]]: memref<?xf32>
54//
55//   UNROLL-BY-3-DAG:  %[[V0:.*]] = arith.subi %[[UB]], %[[LB]] : index
56//   UNROLL-BY-3-DAG:  %[[C1:.*]] = arith.constant 1 : index
57//   UNROLL-BY-3-DAG:  %[[V1:.*]] = arith.subi %[[STEP]], %[[C1]] : index
58//   UNROLL-BY-3-DAG:  %[[V2:.*]] = arith.addi %[[V0]], %[[V1]] : index
59//       Compute trip count in V3.
60//   UNROLL-BY-3-DAG:  %[[V3:.*]] = arith.divsi %[[V2]], %[[STEP]] : index
61//       Store unroll factor in C3.
62//   UNROLL-BY-3-DAG:  %[[C3:.*]] = arith.constant 3 : index
63//   UNROLL-BY-3-DAG:  %[[V4:.*]] = arith.remsi %[[V3]], %[[C3]] : index
64//   UNROLL-BY-3-DAG:  %[[V5:.*]] = arith.subi %[[V3]], %[[V4]] : index
65//   UNROLL-BY-3-DAG:  %[[V6:.*]] = arith.muli %[[V5]], %[[STEP]] : index
66//       Compute upper bound of unrolled loop in V7.
67//   UNROLL-BY-3-DAG:  %[[V7:.*]] = arith.addi %[[LB]], %[[V6]] : index
68//       Compute step of unrolled loop in V8.
69//   UNROLL-BY-3-DAG:  %[[V8:.*]] = arith.muli %[[STEP]], %[[C3]] : index
70//       UNROLL-BY-3:  scf.for %[[IV:.*]] = %[[LB]] to %[[V7]] step %[[V8]] {
71//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
72//  UNROLL-BY-3-NEXT:    %[[C1_IV:.*]] = arith.constant 1 : index
73//  UNROLL-BY-3-NEXT:    %[[V9:.*]] = arith.muli %[[STEP]], %[[C1_IV]] : index
74//  UNROLL-BY-3-NEXT:    %[[V10:.*]] = arith.addi %[[IV]], %[[V9]] : index
75//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V10]]] : memref<?xf32>
76//  UNROLL-BY-3-NEXT:    %[[C2_IV:.*]] = arith.constant 2 : index
77//  UNROLL-BY-3-NEXT:    %[[V11:.*]] = arith.muli %[[STEP]], %[[C2_IV]] : index
78//  UNROLL-BY-3-NEXT:    %[[V12:.*]] = arith.addi %[[IV]], %[[V11]] : index
79//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V12]]] : memref<?xf32>
80//  UNROLL-BY-3-NEXT:  }
81//  UNROLL-BY-3-NEXT:  scf.for %[[IV:.*]] = %[[V7]] to %[[UB]] step %[[STEP]] {
82//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
83//  UNROLL-BY-3-NEXT:  }
84//  UNROLL-BY-3-NEXT:  return
85
86func.func @dynamic_loop_unroll_outer_by_2(
87  %arg0 : index, %arg1 : index, %arg2 : index, %arg3 : index, %arg4 : index,
88  %arg5 : index, %arg6: memref<?xf32>) {
89  %0 = arith.constant 7.0 : f32
90  scf.for %i0 = %arg0 to %arg1 step %arg2 {
91    scf.for %i1 = %arg3 to %arg4 step %arg5 {
92     memref.store %0, %arg6[%i1] : memref<?xf32>
93    }
94  }
95  return
96}
97// UNROLL-OUTER-BY-2-LABEL: func @dynamic_loop_unroll_outer_by_2
98//  UNROLL-OUTER-BY-2-SAME:  %[[LB0:.*0]]: index,
99//  UNROLL-OUTER-BY-2-SAME:  %[[UB0:.*1]]: index,
100//  UNROLL-OUTER-BY-2-SAME:  %[[STEP0:.*2]]: index,
101//  UNROLL-OUTER-BY-2-SAME:  %[[LB1:.*3]]: index,
102//  UNROLL-OUTER-BY-2-SAME:  %[[UB1:.*4]]: index,
103//  UNROLL-OUTER-BY-2-SAME:  %[[STEP1:.*5]]: index,
104//  UNROLL-OUTER-BY-2-SAME:  %[[MEM:.*6]]: memref<?xf32>
105//
106//       UNROLL-OUTER-BY-2:  scf.for %[[IV0:.*]] = %[[LB0]] to %{{.*}} step %{{.*}} {
107//  UNROLL-OUTER-BY-2-NEXT:    scf.for %[[IV1:.*]] = %[[LB1]] to %[[UB1]] step %[[STEP1]] {
108//  UNROLL-OUTER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[IV1]]] : memref<?xf32>
109//  UNROLL-OUTER-BY-2-NEXT:    }
110//  UNROLL-OUTER-BY-2-NEXT:    scf.for %[[IV1:.*]] = %[[LB1]] to %[[UB1]] step %[[STEP1]] {
111//  UNROLL-OUTER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[IV1]]] : memref<?xf32>
112//  UNROLL-OUTER-BY-2-NEXT:    }
113//  UNROLL-OUTER-BY-2-NEXT:  }
114//  UNROLL-OUTER-BY-2-NEXT:  scf.for %[[IV0:.*]] = %{{.*}} to %[[UB0]] step %[[STEP0]] {
115//  UNROLL-OUTER-BY-2-NEXT:    scf.for %[[IV1:.*]] = %[[LB1]] to %[[UB1]] step %[[STEP1]] {
116//  UNROLL-OUTER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[IV1]]] : memref<?xf32>
117//  UNROLL-OUTER-BY-2-NEXT:    }
118//  UNROLL-OUTER-BY-2-NEXT:  }
119//  UNROLL-OUTER-BY-2-NEXT:  return
120
121func.func @dynamic_loop_unroll_inner_by_2(
122  %arg0 : index, %arg1 : index, %arg2 : index, %arg3 : index, %arg4 : index,
123  %arg5 : index, %arg6: memref<?xf32>) {
124  %0 = arith.constant 7.0 : f32
125  scf.for %i0 = %arg0 to %arg1 step %arg2 {
126    scf.for %i1 = %arg3 to %arg4 step %arg5 {
127     memref.store %0, %arg6[%i1] : memref<?xf32>
128    }
129  }
130  return
131}
132// UNROLL-INNER-BY-2-LABEL: func @dynamic_loop_unroll_inner_by_2
133//  UNROLL-INNER-BY-2-SAME:  %[[LB0:.*0]]: index,
134//  UNROLL-INNER-BY-2-SAME:  %[[UB0:.*1]]: index,
135//  UNROLL-INNER-BY-2-SAME:  %[[STEP0:.*2]]: index,
136//  UNROLL-INNER-BY-2-SAME:  %[[LB1:.*3]]: index,
137//  UNROLL-INNER-BY-2-SAME:  %[[UB1:.*4]]: index,
138//  UNROLL-INNER-BY-2-SAME:  %[[STEP1:.*5]]: index,
139//  UNROLL-INNER-BY-2-SAME:  %[[MEM:.*6]]: memref<?xf32>
140//
141//       UNROLL-INNER-BY-2:  scf.for %[[IV0:.*]] = %[[LB0]] to %[[UB0]] step %[[STEP0]] {
142//       UNROLL-INNER-BY-2:    scf.for %[[IV1:.*]] = %[[LB1]] to %{{.*}} step %{{.*}} {
143//  UNROLL-INNER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[IV1]]] : memref<?xf32>
144//  UNROLL-INNER-BY-2-NEXT:      %[[C1_IV:.*]] = arith.constant 1 : index
145//  UNROLL-INNER-BY-2-NEXT:      %[[V0:.*]] = arith.muli %[[STEP1]], %[[C1_IV]] : index
146//  UNROLL-INNER-BY-2-NEXT:      %[[V1:.*]] = arith.addi %[[IV1]], %[[V0]] : index
147//  UNROLL-INNER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[V1]]] : memref<?xf32>
148//  UNROLL-INNER-BY-2-NEXT:    }
149//  UNROLL-INNER-BY-2-NEXT:    scf.for %[[IV1:.*]] = %{{.*}} to %[[UB1]] step %[[STEP1]] {
150//  UNROLL-INNER-BY-2-NEXT:      memref.store %{{.*}}, %[[MEM]][%[[IV1]]] : memref<?xf32>
151//  UNROLL-INNER-BY-2-NEXT:    }
152//  UNROLL-INNER-BY-2-NEXT:  }
153//  UNROLL-INNER-BY-2-NEXT:  return
154
155// Test that no epilogue clean-up loop is generated because the trip count is
156// a multiple of the unroll factor.
157func.func @static_loop_unroll_by_2(%arg0 : memref<?xf32>) {
158  %0 = arith.constant 7.0 : f32
159  %lb = arith.constant 0 : index
160  %ub = arith.constant 20 : index
161  %step = arith.constant 1 : index
162  scf.for %i0 = %lb to %ub step %step {
163    memref.store %0, %arg0[%i0] : memref<?xf32>
164  }
165  return
166}
167// UNROLL-BY-2-LABEL: func @static_loop_unroll_by_2
168//  UNROLL-BY-2-SAME:  %[[MEM:.*0]]: memref<?xf32>
169//
170//   UNROLL-BY-2-DAG:  %[[C0:.*]] = arith.constant 0 : index
171//   UNROLL-BY-2-DAG:  %[[C1:.*]] = arith.constant 1 : index
172//   UNROLL-BY-2-DAG:  %[[C20:.*]] = arith.constant 20 : index
173//   UNROLL-BY-2-DAG:  %[[C2:.*]] = arith.constant 2 : index
174//   UNROLL-BY-2:  scf.for %[[IV:.*]] = %[[C0]] to %[[C20]] step %[[C2]] {
175//  UNROLL-BY-2-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
176//  UNROLL-BY-2-NEXT:    %[[C1_IV:.*]] = arith.constant 1 : index
177//  UNROLL-BY-2-NEXT:    %[[V0:.*]] = arith.muli %[[C1]], %[[C1_IV]] : index
178//  UNROLL-BY-2-NEXT:    %[[V1:.*]] = arith.addi %[[IV]], %[[V0]] : index
179//  UNROLL-BY-2-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V1]]] : memref<?xf32>
180//  UNROLL-BY-2-NEXT:  }
181//  UNROLL-BY-2-NEXT:  return
182
183// UNROLL-BY-2-ANNOTATE-LABEL: func @static_loop_unroll_by_2
184// UNROLL-BY-2-ANNOTATE:    memref.store %{{.*}}, %[[MEM:.*0]][%{{.*}}] {unrolled_iteration = 0 : ui32} : memref<?xf32>
185// UNROLL-BY-2-ANNOTATE:    memref.store %{{.*}}, %[[MEM]][%{{.*}}] {unrolled_iteration = 1 : ui32} : memref<?xf32>
186
187// Test that epilogue clean up loop is generated (trip count is not
188// a multiple of unroll factor).
189func.func @static_loop_unroll_by_3(%arg0 : memref<?xf32>) {
190  %0 = arith.constant 7.0 : f32
191  %lb = arith.constant 0 : index
192  %ub = arith.constant 20 : index
193  %step = arith.constant 1 : index
194  scf.for %i0 = %lb to %ub step %step {
195    memref.store %0, %arg0[%i0] : memref<?xf32>
196  }
197  return
198}
199
200// UNROLL-BY-3-LABEL: func @static_loop_unroll_by_3
201//  UNROLL-BY-3-SAME:  %[[MEM:.*0]]: memref<?xf32>
202//
203//   UNROLL-BY-3-DAG:  %[[C0:.*]] = arith.constant 0 : index
204//   UNROLL-BY-3-DAG:  %[[C1:.*]] = arith.constant 1 : index
205//   UNROLL-BY-3-DAG:  %[[C20:.*]] = arith.constant 20 : index
206//   UNROLL-BY-3-DAG:  %[[C18:.*]] = arith.constant 18 : index
207//   UNROLL-BY-3-DAG:  %[[C3:.*]] = arith.constant 3 : index
208//       UNROLL-BY-3: scf.for %[[IV:.*]] = %[[C0]] to %[[C18]] step %[[C3]] {
209//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
210//  UNROLL-BY-3-NEXT:    %[[C1_IV:.*]] = arith.constant 1 : index
211//  UNROLL-BY-3-NEXT:    %[[V0:.*]] = arith.muli %[[C1]], %[[C1_IV]] : index
212//  UNROLL-BY-3-NEXT:    %[[V1:.*]] = arith.addi %[[IV]], %[[V0]] : index
213//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V1]]] : memref<?xf32>
214//  UNROLL-BY-3-NEXT:    %[[C2_IV:.*]] = arith.constant 2 : index
215//  UNROLL-BY-3-NEXT:    %[[V2:.*]] = arith.muli %[[C1]], %[[C2_IV]] : index
216//  UNROLL-BY-3-NEXT:    %[[V3:.*]] = arith.addi %[[IV]], %[[V2]] : index
217//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V3]]] : memref<?xf32>
218//  UNROLL-BY-3-NEXT:  }
219//  UNROLL-BY-3-NEXT:  scf.for %[[IV:.*]] = %[[C18]] to %[[C20]] step %[[C1]] {
220//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
221//  UNROLL-BY-3-NEXT:  }
222//  UNROLL-BY-3-NEXT:  return
223
224// Test that the single iteration epilogue loop body is promoted to the loops
225// containing block.
226func.func @static_loop_unroll_by_3_promote_epilogue(%arg0 : memref<?xf32>) {
227  %0 = arith.constant 7.0 : f32
228  %lb = arith.constant 0 : index
229  %ub = arith.constant 10 : index
230  %step = arith.constant 1 : index
231  scf.for %i0 = %lb to %ub step %step {
232    memref.store %0, %arg0[%i0] : memref<?xf32>
233  }
234  return
235}
236// UNROLL-BY-3-LABEL: func @static_loop_unroll_by_3_promote_epilogue
237//  UNROLL-BY-3-SAME:  %[[MEM:.*0]]: memref<?xf32>
238//
239//   UNROLL-BY-3-DAG:  %[[C0:.*]] = arith.constant 0 : index
240//   UNROLL-BY-3-DAG:  %[[C1:.*]] = arith.constant 1 : index
241//   UNROLL-BY-3-DAG:  %[[C10:.*]] = arith.constant 10 : index
242//   UNROLL-BY-3-DAG:  %[[C9:.*]] = arith.constant 9 : index
243//   UNROLL-BY-3-DAG:  %[[C3:.*]] = arith.constant 3 : index
244//       UNROLL-BY-3: scf.for %[[IV:.*]] = %[[C0]] to %[[C9]] step %[[C3]] {
245//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[IV]]] : memref<?xf32>
246//  UNROLL-BY-3-NEXT:    %[[C1_IV:.*]] = arith.constant 1 : index
247//  UNROLL-BY-3-NEXT:    %[[V0:.*]] = arith.muli %[[C1]], %[[C1_IV]] : index
248//  UNROLL-BY-3-NEXT:    %[[V1:.*]] = arith.addi %[[IV]], %[[V0]] : index
249//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V1]]] : memref<?xf32>
250//  UNROLL-BY-3-NEXT:    %[[C2_IV:.*]] = arith.constant 2 : index
251//  UNROLL-BY-3-NEXT:    %[[V2:.*]] = arith.muli %[[C1]], %[[C2_IV]] : index
252//  UNROLL-BY-3-NEXT:    %[[V3:.*]] = arith.addi %[[IV]], %[[V2]] : index
253//  UNROLL-BY-3-NEXT:    memref.store %{{.*}}, %[[MEM]][%[[V3]]] : memref<?xf32>
254//  UNROLL-BY-3-NEXT:  }
255//  UNROLL-BY-3-NEXT:  memref.store %{{.*}}, %[[MEM]][%[[C9]]] : memref<?xf32>
256//  UNROLL-BY-3-NEXT:  return
257
258// Test unroll-up-to functionality.
259func.func @static_loop_unroll_up_to_factor(%arg0 : memref<?xf32>) {
260  %0 = arith.constant 7.0 : f32
261  %lb = arith.constant 0 : index
262  %ub = arith.constant 2 : index
263  affine.for %i0 = %lb to %ub {
264    affine.store %0, %arg0[%i0] : memref<?xf32>
265  }
266  return
267}
268// UNROLL-UP-TO-LABEL: func @static_loop_unroll_up_to_factor
269//  UNROLL-UP-TO-SAME:  %[[MEM:.*0]]: memref<?xf32>
270//
271//   UNROLL-UP-TO-DAG:  %[[C0:.*]] = arith.constant 0 : index
272//   UNROLL-UP-TO-DAG:  %[[C2:.*]] = arith.constant 2 : index
273//   UNROLL-UP-TO-NEXT: %[[V0:.*]] = affine.apply {{.*}}
274//   UNROLL-UP-TO-NEXT: store %{{.*}}, %[[MEM]][%[[V0]]] : memref<?xf32>
275//   UNROLL-UP-TO-NEXT: %[[V1:.*]] = affine.apply {{.*}}
276//   UNROLL-UP-TO-NEXT: affine.store %{{.*}}, %[[MEM]][%[[V1]]] : memref<?xf32>
277//   UNROLL-UP-TO-NEXT: return
278
279