2 * Copyright © 2016 Intel Corporation
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:
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
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.
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
27 #include "nir_loop_analyze.h"
29 /* Prepare this loop for unrolling by first converting to lcssa and then
30 * converting the phis from the loops first block and the block that follows
31 * the loop into regs. Partially converting out of SSA allows us to unroll
32 * the loop without having to keep track of and update phis along the way
33 * which gets tricky and doesn't add much value over conveting to regs.
35 * The loop may have a continue instruction at the end of the loop which does
36 * nothing. Once we're out of SSA, we can safely delete it so we don't have
37 * to deal with it later.
40 loop_prepare_for_unroll(nir_loop
*loop
)
42 nir_convert_loop_to_lcssa(loop
);
44 nir_lower_phis_to_regs_block(nir_loop_first_block(loop
));
46 nir_block
*block_after_loop
=
47 nir_cf_node_as_block(nir_cf_node_next(&loop
->cf_node
));
49 nir_lower_phis_to_regs_block(block_after_loop
);
51 nir_instr
*last_instr
= nir_block_last_instr(nir_loop_last_block(loop
));
52 if (last_instr
&& last_instr
->type
== nir_instr_type_jump
) {
53 assert(nir_instr_as_jump(last_instr
)->type
== nir_jump_continue
);
54 nir_instr_remove(last_instr
);
59 get_first_blocks_in_terminator(nir_loop_terminator
*term
,
60 nir_block
**first_break_block
,
61 nir_block
**first_continue_block
)
63 if (term
->continue_from_then
) {
64 *first_continue_block
= nir_if_first_then_block(term
->nif
);
65 *first_break_block
= nir_if_first_else_block(term
->nif
);
67 *first_continue_block
= nir_if_first_else_block(term
->nif
);
68 *first_break_block
= nir_if_first_then_block(term
->nif
);
73 * Unroll a loop where we know exactly how many iterations there are and there
74 * is only a single exit point. Note here we can unroll loops with multiple
75 * theoretical exits that only have a single terminating exit that we always
76 * know is the "real" exit.
82 * And the iteration count is 3, the output will be:
84 * ...instrs... ...instrs... ...instrs...
87 simple_unroll(nir_loop
*loop
)
89 nir_loop_terminator
*limiting_term
= loop
->info
->limiting_terminator
;
90 assert(nir_is_trivial_loop_if(limiting_term
->nif
,
91 limiting_term
->break_block
));
93 loop_prepare_for_unroll(loop
);
95 /* Skip over loop terminator and get the loop body. */
96 list_for_each_entry(nir_loop_terminator
, terminator
,
97 &loop
->info
->loop_terminator_list
,
98 loop_terminator_link
) {
100 /* Remove all but the limiting terminator as we know the other exit
101 * conditions can never be met. Note we need to extract any instructions
102 * in the continue from branch and insert then into the loop body before
105 if (terminator
->nif
!= limiting_term
->nif
) {
106 nir_block
*first_break_block
;
107 nir_block
*first_continue_block
;
108 get_first_blocks_in_terminator(terminator
, &first_break_block
,
109 &first_continue_block
);
111 assert(nir_is_trivial_loop_if(terminator
->nif
,
112 terminator
->break_block
));
114 nir_cf_list continue_from_lst
;
115 nir_cf_extract(&continue_from_lst
,
116 nir_before_block(first_continue_block
),
117 nir_after_block(terminator
->continue_from_block
));
118 nir_cf_reinsert(&continue_from_lst
,
119 nir_after_cf_node(&terminator
->nif
->cf_node
));
121 nir_cf_node_remove(&terminator
->nif
->cf_node
);
125 nir_block
*first_break_block
;
126 nir_block
*first_continue_block
;
127 get_first_blocks_in_terminator(limiting_term
, &first_break_block
,
128 &first_continue_block
);
130 /* Pluck out the loop header */
131 nir_block
*header_blk
= nir_loop_first_block(loop
);
132 nir_cf_list lp_header
;
133 nir_cf_extract(&lp_header
, nir_before_block(header_blk
),
134 nir_before_cf_node(&limiting_term
->nif
->cf_node
));
136 /* Add the continue from block of the limiting terminator to the loop body
138 nir_cf_list continue_from_lst
;
139 nir_cf_extract(&continue_from_lst
, nir_before_block(first_continue_block
),
140 nir_after_block(limiting_term
->continue_from_block
));
141 nir_cf_reinsert(&continue_from_lst
,
142 nir_after_cf_node(&limiting_term
->nif
->cf_node
));
144 /* Pluck out the loop body */
145 nir_cf_list loop_body
;
146 nir_cf_extract(&loop_body
, nir_after_cf_node(&limiting_term
->nif
->cf_node
),
147 nir_after_block(nir_loop_last_block(loop
)));
149 struct hash_table
*remap_table
=
150 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
151 _mesa_key_pointer_equal
);
153 /* Clone the loop header */
154 nir_cf_list cloned_header
;
155 nir_cf_list_clone(&cloned_header
, &lp_header
, loop
->cf_node
.parent
,
158 /* Insert cloned loop header before the loop */
159 nir_cf_reinsert(&cloned_header
, nir_before_cf_node(&loop
->cf_node
));
161 /* Temp list to store the cloned loop body as we unroll */
162 nir_cf_list unrolled_lp_body
;
164 /* Clone loop header and append to the loop body */
165 for (unsigned i
= 0; i
< loop
->info
->trip_count
; i
++) {
166 /* Clone loop body */
167 nir_cf_list_clone(&unrolled_lp_body
, &loop_body
, loop
->cf_node
.parent
,
170 /* Insert unrolled loop body before the loop */
171 nir_cf_reinsert(&unrolled_lp_body
, nir_before_cf_node(&loop
->cf_node
));
173 /* Clone loop header */
174 nir_cf_list_clone(&cloned_header
, &lp_header
, loop
->cf_node
.parent
,
177 /* Insert loop header after loop body */
178 nir_cf_reinsert(&cloned_header
, nir_before_cf_node(&loop
->cf_node
));
181 /* Remove the break from the loop terminator and add instructions from
182 * the break block after the unrolled loop.
184 nir_instr
*break_instr
= nir_block_last_instr(limiting_term
->break_block
);
185 nir_instr_remove(break_instr
);
186 nir_cf_list break_list
;
187 nir_cf_extract(&break_list
, nir_before_block(first_break_block
),
188 nir_after_block(limiting_term
->break_block
));
190 /* Clone so things get properly remapped */
191 nir_cf_list cloned_break_list
;
192 nir_cf_list_clone(&cloned_break_list
, &break_list
, loop
->cf_node
.parent
,
195 nir_cf_reinsert(&cloned_break_list
, nir_before_cf_node(&loop
->cf_node
));
197 /* Remove the loop */
198 nir_cf_node_remove(&loop
->cf_node
);
200 /* Delete the original loop body, break block & header */
201 nir_cf_delete(&lp_header
);
202 nir_cf_delete(&loop_body
);
203 nir_cf_delete(&break_list
);
205 _mesa_hash_table_destroy(remap_table
, NULL
);
209 move_cf_list_into_loop_term(nir_cf_list
*lst
, nir_loop_terminator
*term
)
211 /* Move the rest of the loop inside the continue-from-block */
212 nir_cf_reinsert(lst
, nir_after_block(term
->continue_from_block
));
214 /* Remove the break */
215 nir_instr_remove(nir_block_last_instr(term
->break_block
));
219 get_complex_unroll_insert_location(nir_cf_node
*node
, bool continue_from_then
)
221 if (node
->type
== nir_cf_node_loop
) {
222 return nir_before_cf_node(node
);
224 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
225 if (continue_from_then
) {
226 return nir_after_block(nir_if_last_then_block(if_stmt
));
228 return nir_after_block(nir_if_last_else_block(if_stmt
));
234 * Unroll a loop with two exists when the trip count of one of the exits is
235 * unknown. If continue_from_then is true, the loop is repeated only when the
236 * "then" branch of the if is taken; otherwise it is repeated only
237 * when the "else" branch of the if is taken.
239 * For example, if the input is:
242 * ...phis/condition...
244 * ...then instructions...
246 * ...continue instructions...
252 * And the iteration count is 3, and unlimit_term->continue_from_then is true,
253 * then the output will be:
257 * ...then instructions...
260 * ...then instructions...
263 * ...then instructions...
266 * ...continue instructions...
269 * ...continue instructions...
272 * ...continue instructions...
276 complex_unroll(nir_loop
*loop
, nir_loop_terminator
*unlimit_term
,
277 bool limiting_term_second
)
279 assert(nir_is_trivial_loop_if(unlimit_term
->nif
,
280 unlimit_term
->break_block
));
282 nir_loop_terminator
*limiting_term
= loop
->info
->limiting_terminator
;
283 assert(nir_is_trivial_loop_if(limiting_term
->nif
,
284 limiting_term
->break_block
));
286 loop_prepare_for_unroll(loop
);
288 nir_block
*header_blk
= nir_loop_first_block(loop
);
290 nir_cf_list lp_header
;
291 nir_cf_list limit_break_list
;
292 unsigned num_times_to_clone
;
293 if (limiting_term_second
) {
294 /* Pluck out the loop header */
295 nir_cf_extract(&lp_header
, nir_before_block(header_blk
),
296 nir_before_cf_node(&unlimit_term
->nif
->cf_node
));
298 /* We need some special handling when its the second terminator causing
299 * us to exit the loop for example:
301 * for (int i = 0; i < uniform_lp_count; i++) {
302 * colour = vec4(0.0, 1.0, 0.0, 1.0);
307 * ... any further code is unreachable after i == 1 ...
310 nir_cf_list after_lt
;
311 nir_if
*limit_if
= limiting_term
->nif
;
312 nir_cf_extract(&after_lt
, nir_after_cf_node(&limit_if
->cf_node
),
313 nir_after_block(nir_loop_last_block(loop
)));
314 move_cf_list_into_loop_term(&after_lt
, limiting_term
);
316 /* Because the trip count is the number of times we pass over the entire
317 * loop before hitting a break when the second terminator is the
318 * limiting terminator we can actually execute code inside the loop when
319 * trip count == 0 e.g. the code above the break. So we need to bump
320 * the trip_count in order for the code below to clone anything. When
321 * trip count == 1 we execute the code above the break twice and the
322 * code below it once so we need clone things twice and so on.
324 num_times_to_clone
= loop
->info
->trip_count
+ 1;
326 /* Pluck out the loop header */
327 nir_cf_extract(&lp_header
, nir_before_block(header_blk
),
328 nir_before_cf_node(&limiting_term
->nif
->cf_node
));
330 nir_block
*first_break_block
;
331 nir_block
*first_continue_block
;
332 get_first_blocks_in_terminator(limiting_term
, &first_break_block
,
333 &first_continue_block
);
335 /* Remove the break then extract instructions from the break block so we
336 * can insert them in the innermost else of the unrolled loop.
338 nir_instr
*break_instr
= nir_block_last_instr(limiting_term
->break_block
);
339 nir_instr_remove(break_instr
);
340 nir_cf_extract(&limit_break_list
, nir_before_block(first_break_block
),
341 nir_after_block(limiting_term
->break_block
));
343 nir_cf_list continue_list
;
344 nir_cf_extract(&continue_list
, nir_before_block(first_continue_block
),
345 nir_after_block(limiting_term
->continue_from_block
));
347 nir_cf_reinsert(&continue_list
,
348 nir_after_cf_node(&limiting_term
->nif
->cf_node
));
350 nir_cf_node_remove(&limiting_term
->nif
->cf_node
);
352 num_times_to_clone
= loop
->info
->trip_count
;
355 /* In the terminator that we have no trip count for move everything after
356 * the terminator into the continue from branch.
358 nir_cf_list loop_end
;
359 nir_cf_extract(&loop_end
, nir_after_cf_node(&unlimit_term
->nif
->cf_node
),
360 nir_after_block(nir_loop_last_block(loop
)));
361 move_cf_list_into_loop_term(&loop_end
, unlimit_term
);
363 /* Pluck out the loop body. */
364 nir_cf_list loop_body
;
365 nir_cf_extract(&loop_body
, nir_before_block(nir_loop_first_block(loop
)),
366 nir_after_block(nir_loop_last_block(loop
)));
368 struct hash_table
*remap_table
=
369 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
370 _mesa_key_pointer_equal
);
372 /* Set unroll_loc to the loop as we will insert the unrolled loop before it
374 nir_cf_node
*unroll_loc
= &loop
->cf_node
;
376 /* Temp lists to store the cloned loop as we unroll */
377 nir_cf_list unrolled_lp_body
;
378 nir_cf_list cloned_header
;
380 for (unsigned i
= 0; i
< num_times_to_clone
; i
++) {
381 /* Clone loop header */
382 nir_cf_list_clone(&cloned_header
, &lp_header
, loop
->cf_node
.parent
,
386 get_complex_unroll_insert_location(unroll_loc
,
387 unlimit_term
->continue_from_then
);
389 /* Insert cloned loop header */
390 nir_cf_reinsert(&cloned_header
, cursor
);
393 get_complex_unroll_insert_location(unroll_loc
,
394 unlimit_term
->continue_from_then
);
396 /* Clone loop body */
397 nir_cf_list_clone(&unrolled_lp_body
, &loop_body
, loop
->cf_node
.parent
,
400 unroll_loc
= exec_node_data(nir_cf_node
,
401 exec_list_get_tail(&unrolled_lp_body
.list
),
403 assert(unroll_loc
->type
== nir_cf_node_block
&&
404 exec_list_is_empty(&nir_cf_node_as_block(unroll_loc
)->instr_list
));
406 /* Get the unrolled if node */
407 unroll_loc
= nir_cf_node_prev(unroll_loc
);
409 /* Insert unrolled loop body */
410 nir_cf_reinsert(&unrolled_lp_body
, cursor
);
413 if (!limiting_term_second
) {
414 assert(unroll_loc
->type
== nir_cf_node_if
);
416 nir_cf_list_clone(&cloned_header
, &lp_header
, loop
->cf_node
.parent
,
420 get_complex_unroll_insert_location(unroll_loc
,
421 unlimit_term
->continue_from_then
);
423 /* Insert cloned loop header */
424 nir_cf_reinsert(&cloned_header
, cursor
);
426 /* Clone so things get properly remapped, and insert break block from
427 * the limiting terminator.
429 nir_cf_list cloned_break_blk
;
430 nir_cf_list_clone(&cloned_break_blk
, &limit_break_list
,
431 loop
->cf_node
.parent
, remap_table
);
434 get_complex_unroll_insert_location(unroll_loc
,
435 unlimit_term
->continue_from_then
);
437 nir_cf_reinsert(&cloned_break_blk
, cursor
);
438 nir_cf_delete(&limit_break_list
);
441 /* The loop has been unrolled so remove it. */
442 nir_cf_node_remove(&loop
->cf_node
);
444 /* Delete the original loop header and body */
445 nir_cf_delete(&lp_header
);
446 nir_cf_delete(&loop_body
);
448 _mesa_hash_table_destroy(remap_table
, NULL
);
452 is_loop_small_enough_to_unroll(nir_shader
*shader
, nir_loop_info
*li
)
454 unsigned max_iter
= shader
->options
->max_unroll_iterations
;
456 if (li
->trip_count
> max_iter
)
459 if (li
->force_unroll
)
462 bool loop_not_too_large
=
463 li
->num_instructions
* li
->trip_count
<= max_iter
* 25;
465 return loop_not_too_large
;
469 process_loops(nir_shader
*sh
, nir_cf_node
*cf_node
, bool *innermost_loop
)
471 bool progress
= false;
474 switch (cf_node
->type
) {
475 case nir_cf_node_block
:
477 case nir_cf_node_if
: {
478 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
479 foreach_list_typed_safe(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
)
480 progress
|= process_loops(sh
, nested_node
, innermost_loop
);
481 foreach_list_typed_safe(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
)
482 progress
|= process_loops(sh
, nested_node
, innermost_loop
);
485 case nir_cf_node_loop
: {
486 loop
= nir_cf_node_as_loop(cf_node
);
487 foreach_list_typed_safe(nir_cf_node
, nested_node
, node
, &loop
->body
)
488 progress
|= process_loops(sh
, nested_node
, innermost_loop
);
492 unreachable("unknown cf node type");
495 if (*innermost_loop
) {
496 /* Don't attempt to unroll outer loops or a second inner loop in
497 * this pass wait until the next pass as we have altered the cf.
499 *innermost_loop
= false;
501 if (loop
->info
->limiting_terminator
== NULL
)
504 if (!is_loop_small_enough_to_unroll(sh
, loop
->info
))
507 if (loop
->info
->is_trip_count_known
) {
511 /* Attempt to unroll loops with two terminators. */
512 unsigned num_lt
= list_length(&loop
->info
->loop_terminator_list
);
514 bool limiting_term_second
= true;
515 nir_loop_terminator
*terminator
=
516 list_last_entry(&loop
->info
->loop_terminator_list
,
517 nir_loop_terminator
, loop_terminator_link
);
520 if (terminator
->nif
== loop
->info
->limiting_terminator
->nif
) {
521 limiting_term_second
= false;
523 list_first_entry(&loop
->info
->loop_terminator_list
,
524 nir_loop_terminator
, loop_terminator_link
);
527 /* If the first terminator has a trip count of zero and is the
528 * limiting terminator just do a simple unroll as the second
529 * terminator can never be reached.
531 if (loop
->info
->trip_count
== 0 && !limiting_term_second
) {
534 complex_unroll(loop
, terminator
, limiting_term_second
);
545 nir_opt_loop_unroll_impl(nir_function_impl
*impl
,
546 nir_variable_mode indirect_mask
)
548 bool progress
= false;
549 nir_metadata_require(impl
, nir_metadata_loop_analysis
, indirect_mask
);
550 nir_metadata_require(impl
, nir_metadata_block_index
);
552 foreach_list_typed_safe(nir_cf_node
, node
, node
, &impl
->body
) {
553 bool innermost_loop
= true;
554 progress
|= process_loops(impl
->function
->shader
, node
,
559 nir_lower_regs_to_ssa_impl(impl
);
565 nir_opt_loop_unroll(nir_shader
*shader
, nir_variable_mode indirect_mask
)
567 bool progress
= false;
569 nir_foreach_function(function
, shader
) {
570 if (function
->impl
) {
571 progress
|= nir_opt_loop_unroll_impl(function
->impl
, indirect_mask
);