2 * Copyright © 2010 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 * \file lower_if_to_cond_assign.cpp
27 * This attempts to flatten if-statements to conditional assignments for
28 * GPUs with limited or no flow control support.
30 * It can't handle other control flow being inside of its block, such
31 * as calls or loops. Hopefully loop unrolling and inlining will take
34 * Drivers for GPUs with no control flow support should simply call
36 * lower_if_to_cond_assign(instructions)
38 * to attempt to flatten all if-statements.
40 * Some GPUs (such as i965 prior to gen6) do support control flow, but have a
41 * maximum nesting depth N. Drivers for such hardware can call
43 * lower_if_to_cond_assign(instructions, N)
45 * to attempt to flatten any if-statements appearing at depth > N.
48 #include "compiler/glsl_types.h"
51 #include "util/hash_table.h" /* Needed for the hashing functions */
55 class ir_if_to_cond_assign_visitor
: public ir_hierarchical_visitor
{
57 ir_if_to_cond_assign_visitor(gl_shader_stage stage
,
60 this->progress
= false;
62 this->max_depth
= max_depth
;
65 this->condition_variables
=
66 _mesa_set_create(NULL
, _mesa_hash_pointer
,
67 _mesa_key_pointer_equal
);
70 ~ir_if_to_cond_assign_visitor()
72 _mesa_set_destroy(this->condition_variables
, NULL
);
75 ir_visitor_status
visit_enter(ir_if
*);
76 ir_visitor_status
visit_leave(ir_if
*);
78 bool found_unsupported_op
;
80 gl_shader_stage stage
;
84 struct set
*condition_variables
;
87 } /* anonymous namespace */
90 lower_if_to_cond_assign(gl_shader_stage stage
, exec_list
*instructions
,
93 if (max_depth
== UINT_MAX
)
96 ir_if_to_cond_assign_visitor
v(stage
, max_depth
);
98 visit_list_elements(&v
, instructions
);
104 check_ir_node(ir_instruction
*ir
, void *data
)
106 ir_if_to_cond_assign_visitor
*v
= (ir_if_to_cond_assign_visitor
*)data
;
108 switch (ir
->ir_type
) {
110 case ir_type_discard
:
112 case ir_type_loop_jump
:
114 case ir_type_emit_vertex
:
115 case ir_type_end_primitive
:
116 case ir_type_barrier
:
117 v
->found_unsupported_op
= true;
120 case ir_type_dereference_variable
: {
121 ir_variable
*var
= ir
->as_dereference_variable()->variable_referenced();
123 /* Lowering branches with TCS output accesses breaks many piglit tests,
124 * so don't touch them for now.
126 if (v
->stage
== MESA_SHADER_TESS_CTRL
&&
127 var
->data
.mode
== ir_var_shader_out
)
128 v
->found_unsupported_op
= true;
138 move_block_to_cond_assign(void *mem_ctx
,
139 ir_if
*if_ir
, ir_rvalue
*cond_expr
,
140 exec_list
*instructions
,
143 foreach_in_list_safe(ir_instruction
, ir
, instructions
) {
144 if (ir
->ir_type
== ir_type_assignment
) {
145 ir_assignment
*assign
= (ir_assignment
*)ir
;
147 if (_mesa_set_search(set
, assign
) == NULL
) {
148 _mesa_set_add(set
, assign
);
150 /* If the LHS of the assignment is a condition variable that was
151 * previously added, insert an additional assignment of false to
154 const bool assign_to_cv
=
156 set
, assign
->lhs
->variable_referenced()) != NULL
;
158 if (!assign
->condition
) {
161 new(mem_ctx
) ir_expression(ir_binop_logic_and
,
162 glsl_type::bool_type
,
163 cond_expr
->clone(mem_ctx
, NULL
),
166 assign
->condition
= cond_expr
->clone(mem_ctx
, NULL
);
170 new(mem_ctx
) ir_expression(ir_binop_logic_and
,
171 glsl_type::bool_type
,
172 cond_expr
->clone(mem_ctx
, NULL
),
178 /* Now, move from the if block to the block surrounding it. */
180 if_ir
->insert_before(ir
);
185 ir_if_to_cond_assign_visitor::visit_enter(ir_if
*ir
)
190 return visit_continue
;
194 ir_if_to_cond_assign_visitor::visit_leave(ir_if
*ir
)
196 /* Only flatten when beyond the GPU's maximum supported nesting depth. */
197 if (this->depth
-- <= this->max_depth
)
198 return visit_continue
;
200 this->found_unsupported_op
= false;
202 ir_assignment
*assign
;
204 /* Check that both blocks don't contain anything we can't support. */
205 foreach_in_list(ir_instruction
, then_ir
, &ir
->then_instructions
) {
206 visit_tree(then_ir
, check_ir_node
, this);
208 foreach_in_list(ir_instruction
, else_ir
, &ir
->else_instructions
) {
209 visit_tree(else_ir
, check_ir_node
, this);
211 if (this->found_unsupported_op
)
212 return visit_continue
; /* can't handle inner unsupported opcodes */
214 void *mem_ctx
= ralloc_parent(ir
);
216 /* Store the condition to a variable. Move all of the instructions from
217 * the then-clause of the if-statement. Use the condition variable as a
218 * condition for all assignments.
220 ir_variable
*const then_var
=
221 new(mem_ctx
) ir_variable(glsl_type::bool_type
,
222 "if_to_cond_assign_then",
224 ir
->insert_before(then_var
);
226 ir_dereference_variable
*then_cond
=
227 new(mem_ctx
) ir_dereference_variable(then_var
);
229 assign
= new(mem_ctx
) ir_assignment(then_cond
, ir
->condition
);
230 ir
->insert_before(assign
);
232 move_block_to_cond_assign(mem_ctx
, ir
, then_cond
,
233 &ir
->then_instructions
,
234 this->condition_variables
);
236 /* Add the new condition variable to the hash table. This allows us to
237 * find this variable when lowering other (enclosing) if-statements.
239 _mesa_set_add(this->condition_variables
, then_var
);
241 /* If there are instructions in the else-clause, store the inverse of the
242 * condition to a variable. Move all of the instructions from the
243 * else-clause if the if-statement. Use the (inverse) condition variable
244 * as a condition for all assignments.
246 if (!ir
->else_instructions
.is_empty()) {
247 ir_variable
*const else_var
=
248 new(mem_ctx
) ir_variable(glsl_type::bool_type
,
249 "if_to_cond_assign_else",
251 ir
->insert_before(else_var
);
253 ir_dereference_variable
*else_cond
=
254 new(mem_ctx
) ir_dereference_variable(else_var
);
257 new(mem_ctx
) ir_expression(ir_unop_logic_not
,
258 then_cond
->clone(mem_ctx
, NULL
));
260 assign
= new(mem_ctx
) ir_assignment(else_cond
, inverse
);
261 ir
->insert_before(assign
);
263 move_block_to_cond_assign(mem_ctx
, ir
, else_cond
,
264 &ir
->else_instructions
,
265 this->condition_variables
);
267 /* Add the new condition variable to the hash table. This allows us to
268 * find this variable when lowering other (enclosing) if-statements.
270 _mesa_set_add(this->condition_variables
, else_var
);
275 this->progress
= true;
277 return visit_continue
;