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
40 #include "main/imports.h"
41 #include "compiler/glsl/ir.h"
42 #include "compiler/glsl/ir_rvalue_visitor.h"
43 #include "compiler/glsl_types.h"
44 #include "util/hash_table.h"
46 static bool debug
= false;
48 class variable_entry
: public exec_node
51 variable_entry(ir_variable
*var
)
54 this->whole_vector_access
= 0;
58 ir_variable
*var
; /* The key: the variable's pointer. */
60 /** Number of times the variable is referenced, including assignments. */
61 unsigned whole_vector_access
;
63 ir_variable
*components
[4];
65 /** ralloc_parent(this->var) -- the shader's ralloc context. */
69 class ir_vector_reference_visitor
: public ir_hierarchical_visitor
{
71 ir_vector_reference_visitor(void)
73 this->mem_ctx
= ralloc_context(NULL
);
74 this->ht
= _mesa_hash_table_create(mem_ctx
, _mesa_hash_pointer
,
75 _mesa_key_pointer_equal
);
78 ~ir_vector_reference_visitor(void)
83 virtual ir_visitor_status
visit(ir_variable
*);
84 virtual ir_visitor_status
visit(ir_dereference_variable
*);
85 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
86 virtual ir_visitor_status
visit_enter(ir_assignment
*);
87 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
89 variable_entry
*get_variable_entry(ir_variable
*var
);
91 /* List of variable_entry */
92 struct hash_table
*ht
;
98 ir_vector_reference_visitor::get_variable_entry(ir_variable
*var
)
102 if (!var
->type
->is_vector())
105 switch (var
->data
.mode
) {
107 case ir_var_shader_storage
:
108 case ir_var_shader_shared
:
109 case ir_var_shader_in
:
110 case ir_var_shader_out
:
111 case ir_var_system_value
:
112 case ir_var_function_in
:
113 case ir_var_function_out
:
114 case ir_var_function_inout
:
115 /* Can't split varyings or uniforms. Function in/outs won't get split
120 case ir_var_temporary
:
124 struct hash_entry
*hte
= _mesa_hash_table_search(ht
, var
);
126 return (struct variable_entry
*) hte
->data
;
128 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
129 _mesa_hash_table_insert(ht
, var
, entry
);
135 ir_vector_reference_visitor::visit(ir_variable
*ir
)
137 /* Make sure splitting looks at splitting this variable */
138 (void)this->get_variable_entry(ir
);
140 return visit_continue
;
144 ir_vector_reference_visitor::visit(ir_dereference_variable
*ir
)
146 ir_variable
*const var
= ir
->var
;
147 variable_entry
*entry
= this->get_variable_entry(var
);
150 entry
->whole_vector_access
++;
152 return visit_continue
;
156 ir_vector_reference_visitor::visit_enter(ir_swizzle
*ir
)
158 /* Don't descend into a vector ir_dereference_variable below. */
159 if (ir
->val
->as_dereference_variable() && ir
->type
->is_scalar())
160 return visit_continue_with_parent
;
162 return visit_continue
;
166 ir_vector_reference_visitor::visit_enter(ir_assignment
*ir
)
168 if (ir
->lhs
->as_dereference_variable() &&
169 ir
->rhs
->as_dereference_variable() &&
171 /* We'll split copies of a vector to copies of channels, so don't
172 * descend to the ir_dereference_variables.
174 return visit_continue_with_parent
;
176 if (ir
->lhs
->as_dereference_variable() &&
177 _mesa_is_pow_two(ir
->write_mask
) &&
179 /* If we're writing just a channel, then channel-splitting the LHS is OK.
181 ir
->rhs
->accept(this);
182 return visit_continue_with_parent
;
184 return visit_continue
;
188 ir_vector_reference_visitor::visit_enter(ir_function_signature
*ir
)
190 /* We don't want to descend into the function parameters and
191 * split them, so just accept the body here.
193 visit_list_elements(this, &ir
->body
);
194 return visit_continue_with_parent
;
197 class ir_vector_splitting_visitor
: public ir_rvalue_visitor
{
199 ir_vector_splitting_visitor(struct hash_table
*vars
)
204 virtual ir_visitor_status
visit_leave(ir_assignment
*);
206 void handle_rvalue(ir_rvalue
**rvalue
);
207 variable_entry
*get_splitting_entry(ir_variable
*var
);
209 struct hash_table
*ht
;
213 ir_vector_splitting_visitor::get_splitting_entry(ir_variable
*var
)
217 if (!var
->type
->is_vector())
220 struct hash_entry
*hte
= _mesa_hash_table_search(ht
, var
);
221 return hte
? (struct variable_entry
*) hte
->data
: NULL
;
225 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
230 ir_swizzle
*swiz
= (*rvalue
)->as_swizzle();
231 if (!swiz
|| !swiz
->type
->is_scalar())
234 ir_dereference_variable
*deref_var
= swiz
->val
->as_dereference_variable();
238 variable_entry
*entry
= get_splitting_entry(deref_var
->var
);
242 ir_variable
*var
= entry
->components
[swiz
->mask
.x
];
243 *rvalue
= new(entry
->mem_ctx
) ir_dereference_variable(var
);
247 ir_vector_splitting_visitor::visit_leave(ir_assignment
*ir
)
249 ir_dereference_variable
*lhs_deref
= ir
->lhs
->as_dereference_variable();
250 ir_dereference_variable
*rhs_deref
= ir
->rhs
->as_dereference_variable();
251 variable_entry
*lhs
= lhs_deref
? get_splitting_entry(lhs_deref
->var
) : NULL
;
252 variable_entry
*rhs
= rhs_deref
? get_splitting_entry(rhs_deref
->var
) : NULL
;
254 if (lhs_deref
&& rhs_deref
&& (lhs
|| rhs
) && !ir
->condition
) {
255 unsigned int rhs_chan
= 0;
257 /* Straight assignment of vector variables. */
258 for (unsigned int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
259 ir_dereference
*new_lhs
;
261 void *mem_ctx
= lhs
? lhs
->mem_ctx
: rhs
->mem_ctx
;
262 unsigned int writemask
;
264 if (!(ir
->write_mask
& (1 << i
)))
268 new_lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[i
]);
271 new_lhs
= ir
->lhs
->clone(mem_ctx
, NULL
);
277 new(mem_ctx
) ir_dereference_variable(rhs
->components
[rhs_chan
]);
279 new_rhs
= new(mem_ctx
) ir_swizzle(ir
->rhs
->clone(mem_ctx
, NULL
),
280 rhs_chan
, 0, 0, 0, 1);
283 ir
->insert_before(new(mem_ctx
) ir_assignment(new_lhs
,
291 void *mem_ctx
= lhs
->mem_ctx
;
294 switch (ir
->write_mask
) {
309 unreachable("not reached: non-channelwise dereference of LHS.");
312 ir
->lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[elem
]);
313 ir
->write_mask
= (1 << 0);
315 handle_rvalue(&ir
->rhs
);
317 handle_rvalue(&ir
->rhs
);
320 handle_rvalue(&ir
->condition
);
322 return visit_continue
;
326 brw_do_vector_splitting(exec_list
*instructions
)
328 struct hash_entry
*hte
;
330 ir_vector_reference_visitor refs
;
332 visit_list_elements(&refs
, instructions
);
334 /* Trim out variables we can't split. */
335 hash_table_foreach(refs
.ht
, hte
) {
336 struct variable_entry
*entry
= (struct variable_entry
*) hte
->data
;
338 fprintf(stderr
, "vector %s@%p: whole_access %d\n",
339 entry
->var
->name
, (void *) entry
->var
,
340 entry
->whole_vector_access
);
343 if (entry
->whole_vector_access
) {
344 _mesa_hash_table_remove(refs
.ht
, hte
);
348 if (refs
.ht
->entries
== 0)
351 void *mem_ctx
= ralloc_context(NULL
);
353 /* Replace the decls of the vectors to be split with their split
356 hash_table_foreach(refs
.ht
, hte
) {
357 struct variable_entry
*entry
= (struct variable_entry
*) hte
->data
;
358 const struct glsl_type
*type
;
359 type
= glsl_type::get_instance(entry
->var
->type
->base_type
, 1, 1);
361 entry
->mem_ctx
= ralloc_parent(entry
->var
);
363 for (unsigned int i
= 0; i
< entry
->var
->type
->vector_elements
; i
++) {
364 char *const name
= ir_variable::temporaries_allocate_names
365 ? ralloc_asprintf(mem_ctx
, "%s_%c",
370 entry
->components
[i
] = new(entry
->mem_ctx
) ir_variable(type
, name
,
375 entry
->var
->insert_before(entry
->components
[i
]);
378 entry
->var
->remove();
381 ir_vector_splitting_visitor
split(refs
.ht
);
382 visit_list_elements(&split
, instructions
);
384 ralloc_free(mem_ctx
);