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 brw_wm_vector_splitting.cpp
27 * If a vector is only ever referenced by its components, then
28 * split those components out to individual variables so they can be
29 * handled normally by other optimization passes.
31 * This skips vectors in uniforms and varyings, which need to be
32 * accessible as vectors for their access by the GL. Also, vector
33 * results of non-variable-derefs in assignments aren't handled
34 * because to do so we would have to store the vector result to a
35 * temporary in order to unload each channel, and to do so would just
36 * loop us back to where we started. For the 965, this is exactly the
37 * behavior we want for the results of texture lookups, but probably not for
41 #include "main/core.h"
42 #include "intel_context.h"
44 #include "../glsl/ir.h"
45 #include "../glsl/ir_visitor.h"
46 #include "../glsl/ir_print_visitor.h"
47 #include "../glsl/ir_rvalue_visitor.h"
48 #include "../glsl/glsl_types.h"
50 static bool debug
= false;
52 class variable_entry
: public exec_node
55 variable_entry(ir_variable
*var
)
58 this->whole_vector_access
= 0;
59 this->declaration
= false;
63 ir_variable
*var
; /* The key: the variable's pointer. */
65 /** Number of times the variable is referenced, including assignments. */
66 unsigned whole_vector_access
;
68 bool declaration
; /* If the variable had a decl in the instruction stream */
70 ir_variable
*components
[4];
72 /** talloc_parent(this->var) -- the shader's talloc context. */
76 class ir_vector_reference_visitor
: public ir_hierarchical_visitor
{
78 ir_vector_reference_visitor(void)
80 this->mem_ctx
= talloc_new(NULL
);
81 this->variable_list
.make_empty();
84 ~ir_vector_reference_visitor(void)
89 virtual ir_visitor_status
visit(ir_variable
*);
90 virtual ir_visitor_status
visit(ir_dereference_variable
*);
91 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
92 virtual ir_visitor_status
visit_enter(ir_assignment
*);
93 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
95 variable_entry
*get_variable_entry(ir_variable
*var
);
97 /* List of variable_entry */
98 exec_list variable_list
;
104 ir_vector_reference_visitor::get_variable_entry(ir_variable
*var
)
108 if (!var
->type
->is_vector())
116 /* Can't split varyings or uniforms. Function in/outs won't get split
117 * either, so don't care about the ambiguity.
121 case ir_var_temporary
:
125 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
126 variable_entry
*entry
= (variable_entry
*)iter
.get();
127 if (entry
->var
== var
)
131 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
132 this->variable_list
.push_tail(entry
);
138 ir_vector_reference_visitor::visit(ir_variable
*ir
)
140 variable_entry
*entry
= this->get_variable_entry(ir
);
143 entry
->declaration
= true;
145 return visit_continue
;
149 ir_vector_reference_visitor::visit(ir_dereference_variable
*ir
)
151 ir_variable
*const var
= ir
->var
;
152 variable_entry
*entry
= this->get_variable_entry(var
);
155 entry
->whole_vector_access
++;
157 return visit_continue
;
161 ir_vector_reference_visitor::visit_enter(ir_swizzle
*ir
)
163 /* Don't descend into a vector ir_dereference_variable below. */
164 if (ir
->val
->as_dereference_variable() && ir
->type
->is_scalar())
165 return visit_continue_with_parent
;
167 return visit_continue
;
171 ir_vector_reference_visitor::visit_enter(ir_assignment
*ir
)
173 if (ir
->lhs
->as_dereference_variable() &&
174 ir
->rhs
->as_dereference_variable() &&
176 /* We'll split copies of a vector to copies of channels, so don't
177 * descend to the ir_dereference_variables.
179 return visit_continue_with_parent
;
181 if (ir
->lhs
->as_dereference_variable() &&
182 is_power_of_two(ir
->write_mask
) &&
184 /* If we're writing just a channel, then channel-splitting the LHS is OK.
186 ir
->rhs
->accept(this);
187 return visit_continue_with_parent
;
189 return visit_continue
;
193 ir_vector_reference_visitor::visit_enter(ir_function_signature
*ir
)
195 /* We don't want to descend into the function parameters and
196 * split them, so just accept the body here.
198 visit_list_elements(this, &ir
->body
);
199 return visit_continue_with_parent
;
202 class ir_vector_splitting_visitor
: public ir_rvalue_visitor
{
204 ir_vector_splitting_visitor(exec_list
*vars
)
206 this->variable_list
= vars
;
209 virtual ir_visitor_status
visit_leave(ir_assignment
*);
211 void handle_rvalue(ir_rvalue
**rvalue
);
212 struct variable_entry
*get_splitting_entry(ir_variable
*var
);
214 exec_list
*variable_list
;
218 struct variable_entry
*
219 ir_vector_splitting_visitor::get_splitting_entry(ir_variable
*var
)
223 if (!var
->type
->is_vector())
226 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
227 variable_entry
*entry
= (variable_entry
*)iter
.get();
228 if (entry
->var
== var
) {
237 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
242 ir_swizzle
*swiz
= (*rvalue
)->as_swizzle();
243 if (!swiz
|| !swiz
->type
->is_scalar())
246 ir_dereference_variable
*deref_var
= swiz
->val
->as_dereference_variable();
250 variable_entry
*entry
= get_splitting_entry(deref_var
->var
);
254 ir_variable
*var
= entry
->components
[swiz
->mask
.x
];
255 *rvalue
= new(entry
->mem_ctx
) ir_dereference_variable(var
);
259 ir_vector_splitting_visitor::visit_leave(ir_assignment
*ir
)
261 ir_dereference_variable
*lhs_deref
= ir
->lhs
->as_dereference_variable();
262 ir_dereference_variable
*rhs_deref
= ir
->rhs
->as_dereference_variable();
263 variable_entry
*lhs
= lhs_deref
? get_splitting_entry(lhs_deref
->var
) : NULL
;
264 variable_entry
*rhs
= rhs_deref
? get_splitting_entry(rhs_deref
->var
) : NULL
;
266 if (lhs_deref
&& rhs_deref
&& (lhs
|| rhs
) && !ir
->condition
) {
267 /* Straight assignment of vector variables. */
268 for (unsigned int i
= 0; i
< ir
->rhs
->type
->vector_elements
; i
++) {
269 ir_dereference
*new_lhs
;
271 void *mem_ctx
= lhs
? lhs
->mem_ctx
: rhs
->mem_ctx
;
272 unsigned int writemask
;
275 new_lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[i
]);
276 writemask
= (ir
->write_mask
>> i
) & 1;
278 new_lhs
= ir
->lhs
->clone(mem_ctx
, NULL
);
279 writemask
= ir
->write_mask
& (1 << i
);
283 new_rhs
= new(mem_ctx
) ir_dereference_variable(rhs
->components
[i
]);
284 /* If we're writing into a writemask, smear it out to that channel. */
286 new_rhs
= new(mem_ctx
) ir_swizzle(new_rhs
, i
, i
, i
, i
, i
+ 1);
288 new_rhs
= new(mem_ctx
) ir_swizzle(ir
->rhs
->clone(mem_ctx
, NULL
),
292 ir
->insert_before(new(mem_ctx
) ir_assignment(new_lhs
,
300 switch (ir
->write_mask
) {
315 assert(!"not reached: non-channelwise dereference of LHS.");
318 ir
->lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[elem
]);
319 ir
->write_mask
= (1 << 0);
321 handle_rvalue(&ir
->rhs
);
322 ir
->rhs
= new(mem_ctx
) ir_swizzle(ir
->rhs
,
323 elem
, elem
, elem
, elem
, 1);
325 handle_rvalue(&ir
->rhs
);
328 handle_rvalue(&ir
->condition
);
330 return visit_continue
;
335 brw_do_vector_splitting(exec_list
*instructions
)
337 ir_vector_reference_visitor refs
;
339 visit_list_elements(&refs
, instructions
);
341 /* Trim out variables we can't split. */
342 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
343 variable_entry
*entry
= (variable_entry
*)iter
.get();
346 printf("vector %s@%p: decl %d, whole_access %d\n",
347 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
348 entry
->whole_vector_access
);
351 if (!entry
->declaration
|| entry
->whole_vector_access
) {
356 if (refs
.variable_list
.is_empty())
359 void *mem_ctx
= talloc_new(NULL
);
361 /* Replace the decls of the vectors to be split with their split
364 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
365 variable_entry
*entry
= (variable_entry
*)iter
.get();
366 const struct glsl_type
*type
;
367 type
= glsl_type::get_instance(entry
->var
->type
->base_type
, 1, 1);
369 entry
->mem_ctx
= talloc_parent(entry
->var
);
371 for (unsigned int i
= 0; i
< entry
->var
->type
->vector_elements
; i
++) {
372 const char *name
= talloc_asprintf(mem_ctx
, "%s_%c",
376 entry
->components
[i
] = new(entry
->mem_ctx
) ir_variable(type
, name
,
378 entry
->var
->insert_before(entry
->components
[i
]);
381 entry
->var
->remove();
384 ir_vector_splitting_visitor
split(&refs
.variable_list
);
385 visit_list_elements(&split
, instructions
);
387 talloc_free(mem_ctx
);