nir: Teach loop unrolling about 64-bit instruction lowering
[mesa.git] / src / compiler / nir / nir_opt_loop_unroll.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
27 #include "nir_loop_analyze.h"
28
29
30 /* This limit is chosen fairly arbitrarily. GLSL IR max iteration is 32
31 * instructions. (Multiply counting nodes and magic number 5.) But there is
32 * no 1:1 mapping between GLSL IR and NIR so 25 was picked because it seemed
33 * to give about the same results. Around 5 instructions per node. But some
34 * loops that would unroll with GLSL IR fail to unroll if we set this to 25 so
35 * we set it to 26.
36 */
37 #define LOOP_UNROLL_LIMIT 26
38
39 /* Prepare this loop for unrolling by first converting to lcssa and then
40 * converting the phis from the top level of the loop body to regs.
41 * Partially converting out of SSA allows us to unroll the loop without having
42 * to keep track of and update phis along the way which gets tricky and
43 * doesn't add much value over converting to regs.
44 *
45 * The loop may have a continue instruction at the end of the loop which does
46 * nothing. Once we're out of SSA, we can safely delete it so we don't have
47 * to deal with it later.
48 */
49 static void
50 loop_prepare_for_unroll(nir_loop *loop)
51 {
52 nir_rematerialize_derefs_in_use_blocks_impl(
53 nir_cf_node_get_function(&loop->cf_node));
54
55 nir_convert_loop_to_lcssa(loop);
56
57 /* Lower phis at the top level of the loop body */
58 foreach_list_typed_safe(nir_cf_node, node, node, &loop->body) {
59 if (nir_cf_node_block == node->type) {
60 nir_lower_phis_to_regs_block(nir_cf_node_as_block(node));
61 }
62 }
63
64 /* Lower phis after the loop */
65 nir_block *block_after_loop =
66 nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node));
67
68 nir_lower_phis_to_regs_block(block_after_loop);
69
70 /* Remove continue if its the last instruction in the loop */
71 nir_instr *last_instr = nir_block_last_instr(nir_loop_last_block(loop));
72 if (last_instr && last_instr->type == nir_instr_type_jump) {
73 nir_instr_remove(last_instr);
74 }
75 }
76
77 static void
78 get_first_blocks_in_terminator(nir_loop_terminator *term,
79 nir_block **first_break_block,
80 nir_block **first_continue_block)
81 {
82 if (term->continue_from_then) {
83 *first_continue_block = nir_if_first_then_block(term->nif);
84 *first_break_block = nir_if_first_else_block(term->nif);
85 } else {
86 *first_continue_block = nir_if_first_else_block(term->nif);
87 *first_break_block = nir_if_first_then_block(term->nif);
88 }
89 }
90
91 /**
92 * Unroll a loop where we know exactly how many iterations there are and there
93 * is only a single exit point. Note here we can unroll loops with multiple
94 * theoretical exits that only have a single terminating exit that we always
95 * know is the "real" exit.
96 *
97 * loop {
98 * ...instrs...
99 * }
100 *
101 * And the iteration count is 3, the output will be:
102 *
103 * ...instrs... ...instrs... ...instrs...
104 */
105 static void
106 simple_unroll(nir_loop *loop)
107 {
108 nir_loop_terminator *limiting_term = loop->info->limiting_terminator;
109 assert(nir_is_trivial_loop_if(limiting_term->nif,
110 limiting_term->break_block));
111
112 loop_prepare_for_unroll(loop);
113
114 /* Skip over loop terminator and get the loop body. */
115 list_for_each_entry(nir_loop_terminator, terminator,
116 &loop->info->loop_terminator_list,
117 loop_terminator_link) {
118
119 /* Remove all but the limiting terminator as we know the other exit
120 * conditions can never be met. Note we need to extract any instructions
121 * in the continue from branch and insert then into the loop body before
122 * removing it.
123 */
124 if (terminator->nif != limiting_term->nif) {
125 nir_block *first_break_block;
126 nir_block *first_continue_block;
127 get_first_blocks_in_terminator(terminator, &first_break_block,
128 &first_continue_block);
129
130 assert(nir_is_trivial_loop_if(terminator->nif,
131 terminator->break_block));
132
133 nir_cf_list continue_from_lst;
134 nir_cf_extract(&continue_from_lst,
135 nir_before_block(first_continue_block),
136 nir_after_block(terminator->continue_from_block));
137 nir_cf_reinsert(&continue_from_lst,
138 nir_after_cf_node(&terminator->nif->cf_node));
139
140 nir_cf_node_remove(&terminator->nif->cf_node);
141 }
142 }
143
144 nir_block *first_break_block;
145 nir_block *first_continue_block;
146 get_first_blocks_in_terminator(limiting_term, &first_break_block,
147 &first_continue_block);
148
149 /* Pluck out the loop header */
150 nir_block *header_blk = nir_loop_first_block(loop);
151 nir_cf_list lp_header;
152 nir_cf_extract(&lp_header, nir_before_block(header_blk),
153 nir_before_cf_node(&limiting_term->nif->cf_node));
154
155 /* Add the continue from block of the limiting terminator to the loop body
156 */
157 nir_cf_list continue_from_lst;
158 nir_cf_extract(&continue_from_lst, nir_before_block(first_continue_block),
159 nir_after_block(limiting_term->continue_from_block));
160 nir_cf_reinsert(&continue_from_lst,
161 nir_after_cf_node(&limiting_term->nif->cf_node));
162
163 /* Pluck out the loop body */
164 nir_cf_list loop_body;
165 nir_cf_extract(&loop_body, nir_after_cf_node(&limiting_term->nif->cf_node),
166 nir_after_block(nir_loop_last_block(loop)));
167
168 struct hash_table *remap_table = _mesa_pointer_hash_table_create(NULL);
169
170 /* Clone the loop header and insert before the loop */
171 nir_cf_list_clone_and_reinsert(&lp_header, loop->cf_node.parent,
172 nir_before_cf_node(&loop->cf_node),
173 remap_table);
174
175 for (unsigned i = 0; i < loop->info->max_trip_count; i++) {
176 /* Clone loop body and insert before the loop */
177 nir_cf_list_clone_and_reinsert(&loop_body, loop->cf_node.parent,
178 nir_before_cf_node(&loop->cf_node),
179 remap_table);
180
181 /* Clone loop header and insert after loop body */
182 nir_cf_list_clone_and_reinsert(&lp_header, loop->cf_node.parent,
183 nir_before_cf_node(&loop->cf_node),
184 remap_table);
185 }
186
187 /* Remove the break from the loop terminator and add instructions from
188 * the break block after the unrolled loop.
189 */
190 nir_instr *break_instr = nir_block_last_instr(limiting_term->break_block);
191 nir_instr_remove(break_instr);
192 nir_cf_list break_list;
193 nir_cf_extract(&break_list, nir_before_block(first_break_block),
194 nir_after_block(limiting_term->break_block));
195
196 /* Clone so things get properly remapped */
197 nir_cf_list_clone_and_reinsert(&break_list, loop->cf_node.parent,
198 nir_before_cf_node(&loop->cf_node),
199 remap_table);
200
201 /* Remove the loop */
202 nir_cf_node_remove(&loop->cf_node);
203
204 /* Delete the original loop body, break block & header */
205 nir_cf_delete(&lp_header);
206 nir_cf_delete(&loop_body);
207 nir_cf_delete(&break_list);
208
209 _mesa_hash_table_destroy(remap_table, NULL);
210 }
211
212 static void
213 move_cf_list_into_loop_term(nir_cf_list *lst, nir_loop_terminator *term)
214 {
215 /* Move the rest of the loop inside the continue-from-block */
216 nir_cf_reinsert(lst, nir_after_block(term->continue_from_block));
217
218 /* Remove the break */
219 nir_instr_remove(nir_block_last_instr(term->break_block));
220 }
221
222 static nir_cursor
223 get_complex_unroll_insert_location(nir_cf_node *node, bool continue_from_then)
224 {
225 if (node->type == nir_cf_node_loop) {
226 return nir_before_cf_node(node);
227 } else {
228 nir_if *if_stmt = nir_cf_node_as_if(node);
229 if (continue_from_then) {
230 return nir_after_block(nir_if_last_then_block(if_stmt));
231 } else {
232 return nir_after_block(nir_if_last_else_block(if_stmt));
233 }
234 }
235 }
236
237 static nir_cf_node *
238 complex_unroll_loop_body(nir_loop *loop, nir_loop_terminator *unlimit_term,
239 nir_cf_list *lp_header, nir_cf_list *lp_body,
240 struct hash_table *remap_table,
241 unsigned num_times_to_clone)
242 {
243 /* In the terminator that we have no trip count for move everything after
244 * the terminator into the continue from branch.
245 */
246 nir_cf_list loop_end;
247 nir_cf_extract(&loop_end, nir_after_cf_node(&unlimit_term->nif->cf_node),
248 nir_after_block(nir_loop_last_block(loop)));
249 move_cf_list_into_loop_term(&loop_end, unlimit_term);
250
251 /* Pluck out the loop body. */
252 nir_cf_extract(lp_body, nir_before_block(nir_loop_first_block(loop)),
253 nir_after_block(nir_loop_last_block(loop)));
254
255 /* Set unroll_loc to the loop as we will insert the unrolled loop before it
256 */
257 nir_cf_node *unroll_loc = &loop->cf_node;
258
259 /* Temp list to store the cloned loop as we unroll */
260 nir_cf_list unrolled_lp_body;
261
262 for (unsigned i = 0; i < num_times_to_clone; i++) {
263
264 nir_cursor cursor =
265 get_complex_unroll_insert_location(unroll_loc,
266 unlimit_term->continue_from_then);
267
268 /* Clone loop header and insert in if branch */
269 nir_cf_list_clone_and_reinsert(lp_header, loop->cf_node.parent,
270 cursor, remap_table);
271
272 cursor =
273 get_complex_unroll_insert_location(unroll_loc,
274 unlimit_term->continue_from_then);
275
276 /* Clone loop body */
277 nir_cf_list_clone(&unrolled_lp_body, lp_body, loop->cf_node.parent,
278 remap_table);
279
280 unroll_loc = exec_node_data(nir_cf_node,
281 exec_list_get_tail(&unrolled_lp_body.list),
282 node);
283 assert(unroll_loc->type == nir_cf_node_block &&
284 exec_list_is_empty(&nir_cf_node_as_block(unroll_loc)->instr_list));
285
286 /* Get the unrolled if node */
287 unroll_loc = nir_cf_node_prev(unroll_loc);
288
289 /* Insert unrolled loop body */
290 nir_cf_reinsert(&unrolled_lp_body, cursor);
291 }
292
293 return unroll_loc;
294 }
295
296 /**
297 * Unroll a loop with two exists when the trip count of one of the exits is
298 * unknown. If continue_from_then is true, the loop is repeated only when the
299 * "then" branch of the if is taken; otherwise it is repeated only
300 * when the "else" branch of the if is taken.
301 *
302 * For example, if the input is:
303 *
304 * loop {
305 * ...phis/condition...
306 * if condition {
307 * ...then instructions...
308 * } else {
309 * ...continue instructions...
310 * break
311 * }
312 * ...body...
313 * }
314 *
315 * And the iteration count is 3, and unlimit_term->continue_from_then is true,
316 * then the output will be:
317 *
318 * ...condition...
319 * if condition {
320 * ...then instructions...
321 * ...body...
322 * if condition {
323 * ...then instructions...
324 * ...body...
325 * if condition {
326 * ...then instructions...
327 * ...body...
328 * } else {
329 * ...continue instructions...
330 * }
331 * } else {
332 * ...continue instructions...
333 * }
334 * } else {
335 * ...continue instructions...
336 * }
337 */
338 static void
339 complex_unroll(nir_loop *loop, nir_loop_terminator *unlimit_term,
340 bool limiting_term_second)
341 {
342 assert(nir_is_trivial_loop_if(unlimit_term->nif,
343 unlimit_term->break_block));
344
345 nir_loop_terminator *limiting_term = loop->info->limiting_terminator;
346 assert(nir_is_trivial_loop_if(limiting_term->nif,
347 limiting_term->break_block));
348
349 loop_prepare_for_unroll(loop);
350
351 nir_block *header_blk = nir_loop_first_block(loop);
352
353 nir_cf_list lp_header;
354 nir_cf_list limit_break_list;
355 unsigned num_times_to_clone;
356 if (limiting_term_second) {
357 /* Pluck out the loop header */
358 nir_cf_extract(&lp_header, nir_before_block(header_blk),
359 nir_before_cf_node(&unlimit_term->nif->cf_node));
360
361 /* We need some special handling when its the second terminator causing
362 * us to exit the loop for example:
363 *
364 * for (int i = 0; i < uniform_lp_count; i++) {
365 * colour = vec4(0.0, 1.0, 0.0, 1.0);
366 *
367 * if (i == 1) {
368 * break;
369 * }
370 * ... any further code is unreachable after i == 1 ...
371 * }
372 */
373 nir_cf_list after_lt;
374 nir_if *limit_if = limiting_term->nif;
375 nir_cf_extract(&after_lt, nir_after_cf_node(&limit_if->cf_node),
376 nir_after_block(nir_loop_last_block(loop)));
377 move_cf_list_into_loop_term(&after_lt, limiting_term);
378
379 /* Because the trip count is the number of times we pass over the entire
380 * loop before hitting a break when the second terminator is the
381 * limiting terminator we can actually execute code inside the loop when
382 * trip count == 0 e.g. the code above the break. So we need to bump
383 * the trip_count in order for the code below to clone anything. When
384 * trip count == 1 we execute the code above the break twice and the
385 * code below it once so we need clone things twice and so on.
386 */
387 num_times_to_clone = loop->info->max_trip_count + 1;
388 } else {
389 /* Pluck out the loop header */
390 nir_cf_extract(&lp_header, nir_before_block(header_blk),
391 nir_before_cf_node(&limiting_term->nif->cf_node));
392
393 nir_block *first_break_block;
394 nir_block *first_continue_block;
395 get_first_blocks_in_terminator(limiting_term, &first_break_block,
396 &first_continue_block);
397
398 /* Remove the break then extract instructions from the break block so we
399 * can insert them in the innermost else of the unrolled loop.
400 */
401 nir_instr *break_instr = nir_block_last_instr(limiting_term->break_block);
402 nir_instr_remove(break_instr);
403 nir_cf_extract(&limit_break_list, nir_before_block(first_break_block),
404 nir_after_block(limiting_term->break_block));
405
406 nir_cf_list continue_list;
407 nir_cf_extract(&continue_list, nir_before_block(first_continue_block),
408 nir_after_block(limiting_term->continue_from_block));
409
410 nir_cf_reinsert(&continue_list,
411 nir_after_cf_node(&limiting_term->nif->cf_node));
412
413 nir_cf_node_remove(&limiting_term->nif->cf_node);
414
415 num_times_to_clone = loop->info->max_trip_count;
416 }
417
418 struct hash_table *remap_table = _mesa_pointer_hash_table_create(NULL);
419
420 nir_cf_list lp_body;
421 nir_cf_node *unroll_loc =
422 complex_unroll_loop_body(loop, unlimit_term, &lp_header, &lp_body,
423 remap_table, num_times_to_clone);
424
425 if (!limiting_term_second) {
426 assert(unroll_loc->type == nir_cf_node_if);
427
428 nir_cursor cursor =
429 get_complex_unroll_insert_location(unroll_loc,
430 unlimit_term->continue_from_then);
431
432 /* Clone loop header and insert in if branch */
433 nir_cf_list_clone_and_reinsert(&lp_header, loop->cf_node.parent,
434 cursor, remap_table);
435
436 cursor =
437 get_complex_unroll_insert_location(unroll_loc,
438 unlimit_term->continue_from_then);
439
440 /* Clone so things get properly remapped, and insert break block from
441 * the limiting terminator.
442 */
443 nir_cf_list_clone_and_reinsert(&limit_break_list, loop->cf_node.parent,
444 cursor, remap_table);
445
446 nir_cf_delete(&limit_break_list);
447 }
448
449 /* The loop has been unrolled so remove it. */
450 nir_cf_node_remove(&loop->cf_node);
451
452 /* Delete the original loop header and body */
453 nir_cf_delete(&lp_header);
454 nir_cf_delete(&lp_body);
455
456 _mesa_hash_table_destroy(remap_table, NULL);
457 }
458
459 /* Unrolls the classic wrapper loops e.g
460 *
461 * do {
462 * // ...
463 * } while (false)
464 */
465 static bool
466 wrapper_unroll(nir_loop *loop)
467 {
468 if (!list_empty(&loop->info->loop_terminator_list)) {
469
470 /* Unrolling a loop with a large number of exits can result in a
471 * large inrease in register pressure. For now we just skip
472 * unrolling if we have more than 3 exits (not including the break
473 * at the end of the loop).
474 *
475 * TODO: Most loops that fit this pattern are simply switch
476 * statements that are converted to a loop to take advantage of
477 * exiting jump instruction handling. In this case we could make
478 * use of a binary seach pattern like we do in
479 * nir_lower_indirect_derefs(), this should allow us to unroll the
480 * loops in an optimal way and should also avoid some of the
481 * register pressure that comes from simply nesting the
482 * terminators one after the other.
483 */
484 if (list_length(&loop->info->loop_terminator_list) > 3)
485 return false;
486
487 loop_prepare_for_unroll(loop);
488
489 nir_cursor loop_end = nir_after_block(nir_loop_last_block(loop));
490 list_for_each_entry(nir_loop_terminator, terminator,
491 &loop->info->loop_terminator_list,
492 loop_terminator_link) {
493
494 /* Remove break from the terminator */
495 nir_instr *break_instr =
496 nir_block_last_instr(terminator->break_block);
497 nir_instr_remove(break_instr);
498
499 /* Pluck out the loop body. */
500 nir_cf_list loop_body;
501 nir_cf_extract(&loop_body,
502 nir_after_cf_node(&terminator->nif->cf_node),
503 loop_end);
504
505 /* Reinsert loop body into continue from block */
506 nir_cf_reinsert(&loop_body,
507 nir_after_block(terminator->continue_from_block));
508
509 loop_end = terminator->continue_from_then ?
510 nir_after_block(nir_if_last_then_block(terminator->nif)) :
511 nir_after_block(nir_if_last_else_block(terminator->nif));
512 }
513 } else {
514 nir_block *blk_after_loop =
515 nir_cursor_current_block(nir_after_cf_node(&loop->cf_node));
516
517 /* There may still be some single src phis following the loop that
518 * have not yet been cleaned up by another pass. Tidy those up
519 * before unrolling the loop.
520 */
521 nir_foreach_instr_safe(instr, blk_after_loop) {
522 if (instr->type != nir_instr_type_phi)
523 break;
524
525 nir_phi_instr *phi = nir_instr_as_phi(instr);
526 assert(exec_list_length(&phi->srcs) == 1);
527
528 nir_phi_src *phi_src =
529 exec_node_data(nir_phi_src, exec_list_get_head(&phi->srcs), node);
530
531 nir_ssa_def_rewrite_uses(&phi->dest.ssa, phi_src->src);
532 nir_instr_remove(instr);
533 }
534
535 /* Remove break at end of the loop */
536 nir_block *last_loop_blk = nir_loop_last_block(loop);
537 nir_instr *break_instr = nir_block_last_instr(last_loop_blk);
538 nir_instr_remove(break_instr);
539 }
540
541 /* Pluck out the loop body. */
542 nir_cf_list loop_body;
543 nir_cf_extract(&loop_body, nir_before_block(nir_loop_first_block(loop)),
544 nir_after_block(nir_loop_last_block(loop)));
545
546 /* Reinsert loop body after the loop */
547 nir_cf_reinsert(&loop_body, nir_after_cf_node(&loop->cf_node));
548
549 /* The loop has been unrolled so remove it. */
550 nir_cf_node_remove(&loop->cf_node);
551
552 return true;
553 }
554
555 static bool
556 is_loop_small_enough_to_unroll(nir_shader *shader, nir_loop_info *li)
557 {
558 unsigned max_iter = shader->options->max_unroll_iterations;
559
560 if (li->max_trip_count > max_iter)
561 return false;
562
563 if (li->force_unroll)
564 return true;
565
566 bool loop_not_too_large =
567 li->instr_cost * li->max_trip_count <= max_iter * LOOP_UNROLL_LIMIT;
568
569 return loop_not_too_large;
570 }
571
572 static bool
573 process_loops(nir_shader *sh, nir_cf_node *cf_node, bool *has_nested_loop_out)
574 {
575 bool progress = false;
576 bool has_nested_loop = false;
577 nir_loop *loop;
578
579 switch (cf_node->type) {
580 case nir_cf_node_block:
581 return progress;
582 case nir_cf_node_if: {
583 nir_if *if_stmt = nir_cf_node_as_if(cf_node);
584 foreach_list_typed_safe(nir_cf_node, nested_node, node, &if_stmt->then_list)
585 progress |= process_loops(sh, nested_node, has_nested_loop_out);
586 foreach_list_typed_safe(nir_cf_node, nested_node, node, &if_stmt->else_list)
587 progress |= process_loops(sh, nested_node, has_nested_loop_out);
588 return progress;
589 }
590 case nir_cf_node_loop: {
591 loop = nir_cf_node_as_loop(cf_node);
592 foreach_list_typed_safe(nir_cf_node, nested_node, node, &loop->body)
593 progress |= process_loops(sh, nested_node, &has_nested_loop);
594
595 break;
596 }
597 default:
598 unreachable("unknown cf node type");
599 }
600
601 /* Don't attempt to unroll a second inner loop in this pass, wait until the
602 * next pass as we have altered the cf.
603 */
604 if (!progress) {
605
606 /* Check for the classic
607 *
608 * do {
609 * // ...
610 * } while (false)
611 *
612 * that is used to wrap multi-line macros. GLSL IR also wraps switch
613 * statements in a loop like this.
614 */
615 if (loop->info->limiting_terminator == NULL &&
616 !loop->info->complex_loop) {
617
618 nir_block *last_loop_blk = nir_loop_last_block(loop);
619 if (!nir_block_ends_in_break(last_loop_blk))
620 goto exit;
621
622 progress = wrapper_unroll(loop);
623
624 goto exit;
625 }
626
627 if (has_nested_loop || loop->info->limiting_terminator == NULL)
628 goto exit;
629
630 if (!is_loop_small_enough_to_unroll(sh, loop->info))
631 goto exit;
632
633 if (loop->info->exact_trip_count_known) {
634 simple_unroll(loop);
635 progress = true;
636 } else {
637 /* Attempt to unroll loops with two terminators. */
638 unsigned num_lt = list_length(&loop->info->loop_terminator_list);
639 if (num_lt == 2) {
640 bool limiting_term_second = true;
641 nir_loop_terminator *terminator =
642 list_first_entry(&loop->info->loop_terminator_list,
643 nir_loop_terminator, loop_terminator_link);
644
645
646 if (terminator->nif == loop->info->limiting_terminator->nif) {
647 limiting_term_second = false;
648 terminator =
649 list_last_entry(&loop->info->loop_terminator_list,
650 nir_loop_terminator, loop_terminator_link);
651 }
652
653 /* If the first terminator has a trip count of zero and is the
654 * limiting terminator just do a simple unroll as the second
655 * terminator can never be reached.
656 */
657 if (loop->info->max_trip_count == 0 && !limiting_term_second) {
658 simple_unroll(loop);
659 } else {
660 complex_unroll(loop, terminator, limiting_term_second);
661 }
662 progress = true;
663 }
664 }
665 }
666
667 exit:
668 *has_nested_loop_out = true;
669 return progress;
670 }
671
672 static bool
673 nir_opt_loop_unroll_impl(nir_function_impl *impl,
674 nir_variable_mode indirect_mask)
675 {
676 bool progress = false;
677 nir_metadata_require(impl, nir_metadata_loop_analysis, indirect_mask);
678 nir_metadata_require(impl, nir_metadata_block_index);
679
680 foreach_list_typed_safe(nir_cf_node, node, node, &impl->body) {
681 bool has_nested_loop = false;
682 progress |= process_loops(impl->function->shader, node,
683 &has_nested_loop);
684 }
685
686 if (progress)
687 nir_lower_regs_to_ssa_impl(impl);
688
689 return progress;
690 }
691
692 /**
693 * indirect_mask specifies which type of indirectly accessed variables
694 * should force loop unrolling.
695 */
696 bool
697 nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask)
698 {
699 bool progress = false;
700
701 nir_foreach_function(function, shader) {
702 if (function->impl) {
703 progress |= nir_opt_loop_unroll_impl(function->impl, indirect_mask);
704 }
705 }
706 return progress;
707 }