radeonsi: statically declare resource and sampler arrays
[mesa.git] / src / glsl / loop_unroll.cpp
1 /*
2 * Copyright © 2010 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 "glsl_types.h"
25 #include "loop_analysis.h"
26 #include "ir_hierarchical_visitor.h"
27
28 #include "main/mtypes.h"
29
30 namespace {
31
32 class loop_unroll_visitor : public ir_hierarchical_visitor {
33 public:
34 loop_unroll_visitor(loop_state *state,
35 const struct gl_shader_compiler_options *options)
36 {
37 this->state = state;
38 this->progress = false;
39 this->options = options;
40 }
41
42 virtual ir_visitor_status visit_leave(ir_loop *ir);
43 void simple_unroll(ir_loop *ir, int iterations);
44 void complex_unroll(ir_loop *ir, int iterations,
45 bool continue_from_then_branch);
46 void splice_post_if_instructions(ir_if *ir_if, exec_list *splice_dest);
47
48 loop_state *state;
49
50 bool progress;
51 const struct gl_shader_compiler_options *options;
52 };
53
54 } /* anonymous namespace */
55
56 static bool
57 is_break(ir_instruction *ir)
58 {
59 return ir != NULL && ir->ir_type == ir_type_loop_jump
60 && ((ir_loop_jump *) ir)->is_break();
61 }
62
63 class loop_unroll_count : public ir_hierarchical_visitor {
64 public:
65 int nodes;
66 bool unsupported_variable_indexing;
67 /* If there are nested loops, the node count will be inaccurate. */
68 bool nested_loop;
69
70 loop_unroll_count(exec_list *list, loop_variable_state *ls,
71 const struct gl_shader_compiler_options *options)
72 : ls(ls), options(options)
73 {
74 nodes = 0;
75 nested_loop = false;
76 unsupported_variable_indexing = false;
77
78 run(list);
79 }
80
81 virtual ir_visitor_status visit_enter(ir_assignment *)
82 {
83 nodes++;
84 return visit_continue;
85 }
86
87 virtual ir_visitor_status visit_enter(ir_expression *)
88 {
89 nodes++;
90 return visit_continue;
91 }
92
93 virtual ir_visitor_status visit_enter(ir_loop *)
94 {
95 nested_loop = true;
96 return visit_continue;
97 }
98
99 virtual ir_visitor_status visit_enter(ir_dereference_array *ir)
100 {
101 /* Check for arrays variably-indexed by a loop induction variable.
102 * Unrolling the loop may convert that access into constant-indexing.
103 *
104 * Many drivers don't support particular kinds of variable indexing,
105 * and have to resort to using lower_variable_index_to_cond_assign to
106 * handle it. This results in huge amounts of horrible code, so we'd
107 * like to avoid that if possible. Here, we just note that it will
108 * happen.
109 */
110 if ((ir->array->type->is_array() || ir->array->type->is_matrix()) &&
111 !ir->array_index->as_constant()) {
112 ir_variable *array = ir->array->variable_referenced();
113 loop_variable *lv = ls->get(ir->array_index->variable_referenced());
114 if (array && lv && lv->is_induction_var()) {
115 switch (array->data.mode) {
116 case ir_var_auto:
117 case ir_var_temporary:
118 case ir_var_const_in:
119 case ir_var_function_in:
120 case ir_var_function_out:
121 case ir_var_function_inout:
122 if (options->EmitNoIndirectTemp)
123 unsupported_variable_indexing = true;
124 break;
125 case ir_var_uniform:
126 if (options->EmitNoIndirectUniform)
127 unsupported_variable_indexing = true;
128 break;
129 case ir_var_shader_in:
130 if (options->EmitNoIndirectInput)
131 unsupported_variable_indexing = true;
132 break;
133 case ir_var_shader_out:
134 if (options->EmitNoIndirectOutput)
135 unsupported_variable_indexing = true;
136 break;
137 }
138 }
139 }
140 return visit_continue;
141 }
142
143 private:
144 loop_variable_state *ls;
145 const struct gl_shader_compiler_options *options;
146 };
147
148
149 /**
150 * Unroll a loop which does not contain any jumps. For example, if the input
151 * is:
152 *
153 * (loop (...) ...instrs...)
154 *
155 * And the iteration count is 3, the output will be:
156 *
157 * ...instrs... ...instrs... ...instrs...
158 */
159 void
160 loop_unroll_visitor::simple_unroll(ir_loop *ir, int iterations)
161 {
162 void *const mem_ctx = ralloc_parent(ir);
163
164 for (int i = 0; i < iterations; i++) {
165 exec_list copy_list;
166
167 copy_list.make_empty();
168 clone_ir_list(mem_ctx, &copy_list, &ir->body_instructions);
169
170 ir->insert_before(&copy_list);
171 }
172
173 /* The loop has been replaced by the unrolled copies. Remove the original
174 * loop from the IR sequence.
175 */
176 ir->remove();
177
178 this->progress = true;
179 }
180
181
182 /**
183 * Unroll a loop whose last statement is an ir_if. If \c
184 * continue_from_then_branch is true, the loop is repeated only when the
185 * "then" branch of the if is taken; otherwise it is repeated only when the
186 * "else" branch of the if is taken.
187 *
188 * For example, if the input is:
189 *
190 * (loop (...)
191 * ...body...
192 * (if (cond)
193 * (...then_instrs...)
194 * (...else_instrs...)))
195 *
196 * And the iteration count is 3, and \c continue_from_then_branch is true,
197 * then the output will be:
198 *
199 * ...body...
200 * (if (cond)
201 * (...then_instrs...
202 * ...body...
203 * (if (cond)
204 * (...then_instrs...
205 * ...body...
206 * (if (cond)
207 * (...then_instrs...)
208 * (...else_instrs...)))
209 * (...else_instrs...)))
210 * (...else_instrs))
211 */
212 void
213 loop_unroll_visitor::complex_unroll(ir_loop *ir, int iterations,
214 bool continue_from_then_branch)
215 {
216 void *const mem_ctx = ralloc_parent(ir);
217 ir_instruction *ir_to_replace = ir;
218
219 for (int i = 0; i < iterations; i++) {
220 exec_list copy_list;
221
222 copy_list.make_empty();
223 clone_ir_list(mem_ctx, &copy_list, &ir->body_instructions);
224
225 ir_if *ir_if = ((ir_instruction *) copy_list.get_tail())->as_if();
226 assert(ir_if != NULL);
227
228 ir_to_replace->insert_before(&copy_list);
229 ir_to_replace->remove();
230
231 /* placeholder that will be removed in the next iteration */
232 ir_to_replace =
233 new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
234
235 exec_list *const list = (continue_from_then_branch)
236 ? &ir_if->then_instructions : &ir_if->else_instructions;
237
238 list->push_tail(ir_to_replace);
239 }
240
241 ir_to_replace->remove();
242
243 this->progress = true;
244 }
245
246
247 /**
248 * Move all of the instructions which follow \c ir_if to the end of
249 * \c splice_dest.
250 *
251 * For example, in the code snippet:
252 *
253 * (if (cond)
254 * (...then_instructions...
255 * break)
256 * (...else_instructions...))
257 * ...post_if_instructions...
258 *
259 * If \c ir_if points to the "if" instruction, and \c splice_dest points to
260 * (...else_instructions...), the code snippet is transformed into:
261 *
262 * (if (cond)
263 * (...then_instructions...
264 * break)
265 * (...else_instructions...
266 * ...post_if_instructions...))
267 */
268 void
269 loop_unroll_visitor::splice_post_if_instructions(ir_if *ir_if,
270 exec_list *splice_dest)
271 {
272 while (!ir_if->get_next()->is_tail_sentinel()) {
273 ir_instruction *move_ir = (ir_instruction *) ir_if->get_next();
274
275 move_ir->remove();
276 splice_dest->push_tail(move_ir);
277 }
278 }
279
280
281 ir_visitor_status
282 loop_unroll_visitor::visit_leave(ir_loop *ir)
283 {
284 loop_variable_state *const ls = this->state->get(ir);
285 int iterations;
286
287 /* If we've entered a loop that hasn't been analyzed, something really,
288 * really bad has happened.
289 */
290 if (ls == NULL) {
291 assert(ls != NULL);
292 return visit_continue;
293 }
294
295 /* Don't try to unroll loops where the number of iterations is not known
296 * at compile-time.
297 */
298 if (ls->limiting_terminator == NULL)
299 return visit_continue;
300
301 iterations = ls->limiting_terminator->iterations;
302
303 const int max_iterations = options->MaxUnrollIterations;
304
305 /* Don't try to unroll loops that have zillions of iterations either.
306 */
307 if (iterations > max_iterations)
308 return visit_continue;
309
310 /* Don't try to unroll nested loops and loops with a huge body.
311 */
312 loop_unroll_count count(&ir->body_instructions, ls, options);
313
314 bool loop_too_large =
315 count.nested_loop || count.nodes * iterations > max_iterations * 5;
316
317 if (loop_too_large && !count.unsupported_variable_indexing)
318 return visit_continue;
319
320 /* Note: the limiting terminator contributes 1 to ls->num_loop_jumps.
321 * We'll be removing the limiting terminator before we unroll.
322 */
323 assert(ls->num_loop_jumps > 0);
324 unsigned predicted_num_loop_jumps = ls->num_loop_jumps - 1;
325
326 if (predicted_num_loop_jumps > 1)
327 return visit_continue;
328
329 if (predicted_num_loop_jumps == 0) {
330 ls->limiting_terminator->ir->remove();
331 simple_unroll(ir, iterations);
332 return visit_continue;
333 }
334
335 ir_instruction *last_ir = (ir_instruction *) ir->body_instructions.get_tail();
336 assert(last_ir != NULL);
337
338 if (is_break(last_ir)) {
339 /* If the only loop-jump is a break at the end of the loop, the loop
340 * will execute exactly once. Remove the break and use the simple
341 * unroller with an iteration count of 1.
342 */
343 last_ir->remove();
344
345 ls->limiting_terminator->ir->remove();
346 simple_unroll(ir, 1);
347 return visit_continue;
348 }
349
350 /* recognize loops in the form produced by ir_lower_jumps */
351 foreach_in_list(ir_instruction, cur_ir, &ir->body_instructions) {
352 /* Skip the limiting terminator, since it will go away when we
353 * unroll.
354 */
355 if (cur_ir == ls->limiting_terminator->ir)
356 continue;
357
358 ir_if *ir_if = cur_ir->as_if();
359 if (ir_if != NULL) {
360 /* Determine which if-statement branch, if any, ends with a
361 * break. The branch that did *not* have the break will get a
362 * temporary continue inserted in each iteration of the loop
363 * unroll.
364 *
365 * Note that since ls->num_loop_jumps is <= 1, it is impossible
366 * for both branches to end with a break.
367 */
368 ir_instruction *ir_if_last =
369 (ir_instruction *) ir_if->then_instructions.get_tail();
370
371 if (is_break(ir_if_last)) {
372 ls->limiting_terminator->ir->remove();
373 splice_post_if_instructions(ir_if, &ir_if->else_instructions);
374 ir_if_last->remove();
375 complex_unroll(ir, iterations, false);
376 return visit_continue;
377 } else {
378 ir_if_last =
379 (ir_instruction *) ir_if->else_instructions.get_tail();
380
381 if (is_break(ir_if_last)) {
382 ls->limiting_terminator->ir->remove();
383 splice_post_if_instructions(ir_if, &ir_if->then_instructions);
384 ir_if_last->remove();
385 complex_unroll(ir, iterations, true);
386 return visit_continue;
387 }
388 }
389 }
390 }
391
392 /* Did not find the break statement. It must be in a complex if-nesting,
393 * so don't try to unroll.
394 */
395 return visit_continue;
396 }
397
398
399 bool
400 unroll_loops(exec_list *instructions, loop_state *ls,
401 const struct gl_shader_compiler_options *options)
402 {
403 loop_unroll_visitor v(ls, options);
404
405 v.run(instructions);
406
407 return v.progress;
408 }