glsl/lower_if: don't lower branches touching tess control outputs
[mesa.git] / src / compiler / glsl / lower_if_to_cond_assign.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 /**
25 * \file lower_if_to_cond_assign.cpp
26 *
27 * This attempts to flatten if-statements to conditional assignments for
28 * GPUs with limited or no flow control support.
29 *
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
32 * care of those.
33 *
34 * Drivers for GPUs with no control flow support should simply call
35 *
36 * lower_if_to_cond_assign(instructions)
37 *
38 * to attempt to flatten all if-statements.
39 *
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
42 *
43 * lower_if_to_cond_assign(instructions, N)
44 *
45 * to attempt to flatten any if-statements appearing at depth > N.
46 */
47
48 #include "compiler/glsl_types.h"
49 #include "ir.h"
50 #include "util/set.h"
51 #include "util/hash_table.h" /* Needed for the hashing functions */
52
53 namespace {
54
55 class ir_if_to_cond_assign_visitor : public ir_hierarchical_visitor {
56 public:
57 ir_if_to_cond_assign_visitor(gl_shader_stage stage,
58 unsigned max_depth)
59 {
60 this->progress = false;
61 this->stage = stage;
62 this->max_depth = max_depth;
63 this->depth = 0;
64
65 this->condition_variables =
66 _mesa_set_create(NULL, _mesa_hash_pointer,
67 _mesa_key_pointer_equal);
68 }
69
70 ~ir_if_to_cond_assign_visitor()
71 {
72 _mesa_set_destroy(this->condition_variables, NULL);
73 }
74
75 ir_visitor_status visit_enter(ir_if *);
76 ir_visitor_status visit_leave(ir_if *);
77
78 bool found_unsupported_op;
79 bool progress;
80 gl_shader_stage stage;
81 unsigned max_depth;
82 unsigned depth;
83
84 struct set *condition_variables;
85 };
86
87 } /* anonymous namespace */
88
89 bool
90 lower_if_to_cond_assign(gl_shader_stage stage, exec_list *instructions,
91 unsigned max_depth)
92 {
93 if (max_depth == UINT_MAX)
94 return false;
95
96 ir_if_to_cond_assign_visitor v(stage, max_depth);
97
98 visit_list_elements(&v, instructions);
99
100 return v.progress;
101 }
102
103 void
104 check_ir_node(ir_instruction *ir, void *data)
105 {
106 ir_if_to_cond_assign_visitor *v = (ir_if_to_cond_assign_visitor *)data;
107
108 switch (ir->ir_type) {
109 case ir_type_call:
110 case ir_type_discard:
111 case ir_type_loop:
112 case ir_type_loop_jump:
113 case ir_type_return:
114 case ir_type_emit_vertex:
115 case ir_type_end_primitive:
116 case ir_type_barrier:
117 v->found_unsupported_op = true;
118 break;
119
120 case ir_type_dereference_variable: {
121 ir_variable *var = ir->as_dereference_variable()->variable_referenced();
122
123 /* Lowering branches with TCS output accesses breaks many piglit tests,
124 * so don't touch them for now.
125 */
126 if (v->stage == MESA_SHADER_TESS_CTRL &&
127 var->data.mode == ir_var_shader_out)
128 v->found_unsupported_op = true;
129 break;
130 }
131
132 default:
133 break;
134 }
135 }
136
137 void
138 move_block_to_cond_assign(void *mem_ctx,
139 ir_if *if_ir, ir_rvalue *cond_expr,
140 exec_list *instructions,
141 struct set *set)
142 {
143 foreach_in_list_safe(ir_instruction, ir, instructions) {
144 if (ir->ir_type == ir_type_assignment) {
145 ir_assignment *assign = (ir_assignment *)ir;
146
147 if (_mesa_set_search(set, assign) == NULL) {
148 _mesa_set_add(set, assign);
149
150 /* If the LHS of the assignment is a condition variable that was
151 * previously added, insert an additional assignment of false to
152 * the variable.
153 */
154 const bool assign_to_cv =
155 _mesa_set_search(
156 set, assign->lhs->variable_referenced()) != NULL;
157
158 if (!assign->condition) {
159 if (assign_to_cv) {
160 assign->rhs =
161 new(mem_ctx) ir_expression(ir_binop_logic_and,
162 glsl_type::bool_type,
163 cond_expr->clone(mem_ctx, NULL),
164 assign->rhs);
165 } else {
166 assign->condition = cond_expr->clone(mem_ctx, NULL);
167 }
168 } else {
169 assign->condition =
170 new(mem_ctx) ir_expression(ir_binop_logic_and,
171 glsl_type::bool_type,
172 cond_expr->clone(mem_ctx, NULL),
173 assign->condition);
174 }
175 }
176 }
177
178 /* Now, move from the if block to the block surrounding it. */
179 ir->remove();
180 if_ir->insert_before(ir);
181 }
182 }
183
184 ir_visitor_status
185 ir_if_to_cond_assign_visitor::visit_enter(ir_if *ir)
186 {
187 (void) ir;
188 this->depth++;
189
190 return visit_continue;
191 }
192
193 ir_visitor_status
194 ir_if_to_cond_assign_visitor::visit_leave(ir_if *ir)
195 {
196 /* Only flatten when beyond the GPU's maximum supported nesting depth. */
197 if (this->depth-- <= this->max_depth)
198 return visit_continue;
199
200 this->found_unsupported_op = false;
201
202 ir_assignment *assign;
203
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);
207 }
208 foreach_in_list(ir_instruction, else_ir, &ir->else_instructions) {
209 visit_tree(else_ir, check_ir_node, this);
210 }
211 if (this->found_unsupported_op)
212 return visit_continue; /* can't handle inner unsupported opcodes */
213
214 void *mem_ctx = ralloc_parent(ir);
215
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.
219 */
220 ir_variable *const then_var =
221 new(mem_ctx) ir_variable(glsl_type::bool_type,
222 "if_to_cond_assign_then",
223 ir_var_temporary);
224 ir->insert_before(then_var);
225
226 ir_dereference_variable *then_cond =
227 new(mem_ctx) ir_dereference_variable(then_var);
228
229 assign = new(mem_ctx) ir_assignment(then_cond, ir->condition);
230 ir->insert_before(assign);
231
232 move_block_to_cond_assign(mem_ctx, ir, then_cond,
233 &ir->then_instructions,
234 this->condition_variables);
235
236 /* Add the new condition variable to the hash table. This allows us to
237 * find this variable when lowering other (enclosing) if-statements.
238 */
239 _mesa_set_add(this->condition_variables, then_var);
240
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.
245 */
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",
250 ir_var_temporary);
251 ir->insert_before(else_var);
252
253 ir_dereference_variable *else_cond =
254 new(mem_ctx) ir_dereference_variable(else_var);
255
256 ir_rvalue *inverse =
257 new(mem_ctx) ir_expression(ir_unop_logic_not,
258 then_cond->clone(mem_ctx, NULL));
259
260 assign = new(mem_ctx) ir_assignment(else_cond, inverse);
261 ir->insert_before(assign);
262
263 move_block_to_cond_assign(mem_ctx, ir, else_cond,
264 &ir->else_instructions,
265 this->condition_variables);
266
267 /* Add the new condition variable to the hash table. This allows us to
268 * find this variable when lowering other (enclosing) if-statements.
269 */
270 _mesa_set_add(this->condition_variables, else_var);
271 }
272
273 ir->remove();
274
275 this->progress = true;
276
277 return visit_continue;
278 }