Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_vector_splitting.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 brw_wm_vector_splitting.cpp
26 *
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.
30 *
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
38 */
39
40 #include "main/core.h"
41 #include "brw_context.h"
42 #include "glsl/ir.h"
43 #include "glsl/ir_visitor.h"
44 #include "glsl/ir_rvalue_visitor.h"
45 #include "glsl/nir/glsl_types.h"
46 #include "util/hash_table.h"
47
48 static bool debug = false;
49
50 class variable_entry : public exec_node
51 {
52 public:
53 variable_entry(ir_variable *var)
54 {
55 this->var = var;
56 this->whole_vector_access = 0;
57 this->mem_ctx = NULL;
58 }
59
60 ir_variable *var; /* The key: the variable's pointer. */
61
62 /** Number of times the variable is referenced, including assignments. */
63 unsigned whole_vector_access;
64
65 ir_variable *components[4];
66
67 /** ralloc_parent(this->var) -- the shader's ralloc context. */
68 void *mem_ctx;
69 };
70
71 class ir_vector_reference_visitor : public ir_hierarchical_visitor {
72 public:
73 ir_vector_reference_visitor(void)
74 {
75 this->mem_ctx = ralloc_context(NULL);
76 this->ht = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
77 _mesa_key_pointer_equal);
78 }
79
80 ~ir_vector_reference_visitor(void)
81 {
82 ralloc_free(mem_ctx);
83 }
84
85 virtual ir_visitor_status visit(ir_variable *);
86 virtual ir_visitor_status visit(ir_dereference_variable *);
87 virtual ir_visitor_status visit_enter(ir_swizzle *);
88 virtual ir_visitor_status visit_enter(ir_assignment *);
89 virtual ir_visitor_status visit_enter(ir_function_signature *);
90
91 variable_entry *get_variable_entry(ir_variable *var);
92
93 /* List of variable_entry */
94 struct hash_table *ht;
95
96 void *mem_ctx;
97 };
98
99 variable_entry *
100 ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
101 {
102 assert(var);
103
104 if (!var->type->is_vector())
105 return NULL;
106
107 switch (var->data.mode) {
108 case ir_var_uniform:
109 case ir_var_shader_storage:
110 case ir_var_shader_in:
111 case ir_var_shader_out:
112 case ir_var_system_value:
113 case ir_var_function_in:
114 case ir_var_function_out:
115 case ir_var_function_inout:
116 /* Can't split varyings or uniforms. Function in/outs won't get split
117 * either.
118 */
119 return NULL;
120 case ir_var_auto:
121 case ir_var_temporary:
122 break;
123 }
124
125 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
126 if (hte)
127 return (struct variable_entry *) hte->data;
128
129 variable_entry *entry = new(mem_ctx) variable_entry(var);
130 _mesa_hash_table_insert(ht, var, entry);
131 return entry;
132 }
133
134
135 ir_visitor_status
136 ir_vector_reference_visitor::visit(ir_variable *ir)
137 {
138 /* Make sure splitting looks at splitting this variable */
139 (void)this->get_variable_entry(ir);
140
141 return visit_continue;
142 }
143
144 ir_visitor_status
145 ir_vector_reference_visitor::visit(ir_dereference_variable *ir)
146 {
147 ir_variable *const var = ir->var;
148 variable_entry *entry = this->get_variable_entry(var);
149
150 if (entry)
151 entry->whole_vector_access++;
152
153 return visit_continue;
154 }
155
156 ir_visitor_status
157 ir_vector_reference_visitor::visit_enter(ir_swizzle *ir)
158 {
159 /* Don't descend into a vector ir_dereference_variable below. */
160 if (ir->val->as_dereference_variable() && ir->type->is_scalar())
161 return visit_continue_with_parent;
162
163 return visit_continue;
164 }
165
166 ir_visitor_status
167 ir_vector_reference_visitor::visit_enter(ir_assignment *ir)
168 {
169 if (ir->lhs->as_dereference_variable() &&
170 ir->rhs->as_dereference_variable() &&
171 !ir->condition) {
172 /* We'll split copies of a vector to copies of channels, so don't
173 * descend to the ir_dereference_variables.
174 */
175 return visit_continue_with_parent;
176 }
177 if (ir->lhs->as_dereference_variable() &&
178 _mesa_is_pow_two(ir->write_mask) &&
179 !ir->condition) {
180 /* If we're writing just a channel, then channel-splitting the LHS is OK.
181 */
182 ir->rhs->accept(this);
183 return visit_continue_with_parent;
184 }
185 return visit_continue;
186 }
187
188 ir_visitor_status
189 ir_vector_reference_visitor::visit_enter(ir_function_signature *ir)
190 {
191 /* We don't want to descend into the function parameters and
192 * split them, so just accept the body here.
193 */
194 visit_list_elements(this, &ir->body);
195 return visit_continue_with_parent;
196 }
197
198 class ir_vector_splitting_visitor : public ir_rvalue_visitor {
199 public:
200 ir_vector_splitting_visitor(struct hash_table *vars)
201 {
202 this->ht = vars;
203 }
204
205 virtual ir_visitor_status visit_leave(ir_assignment *);
206
207 void handle_rvalue(ir_rvalue **rvalue);
208 variable_entry *get_splitting_entry(ir_variable *var);
209
210 struct hash_table *ht;
211 };
212
213 variable_entry *
214 ir_vector_splitting_visitor::get_splitting_entry(ir_variable *var)
215 {
216 assert(var);
217
218 if (!var->type->is_vector())
219 return NULL;
220
221 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
222 return hte ? (struct variable_entry *) hte->data : NULL;
223 }
224
225 void
226 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
227 {
228 if (!*rvalue)
229 return;
230
231 ir_swizzle *swiz = (*rvalue)->as_swizzle();
232 if (!swiz || !swiz->type->is_scalar())
233 return;
234
235 ir_dereference_variable *deref_var = swiz->val->as_dereference_variable();
236 if (!deref_var)
237 return;
238
239 variable_entry *entry = get_splitting_entry(deref_var->var);
240 if (!entry)
241 return;
242
243 ir_variable *var = entry->components[swiz->mask.x];
244 *rvalue = new(entry->mem_ctx) ir_dereference_variable(var);
245 }
246
247 ir_visitor_status
248 ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
249 {
250 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
251 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
252 variable_entry *lhs = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
253 variable_entry *rhs = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
254
255 if (lhs_deref && rhs_deref && (lhs || rhs) && !ir->condition) {
256 unsigned int rhs_chan = 0;
257
258 /* Straight assignment of vector variables. */
259 for (unsigned int i = 0; i < ir->lhs->type->vector_elements; i++) {
260 ir_dereference *new_lhs;
261 ir_rvalue *new_rhs;
262 void *mem_ctx = lhs ? lhs->mem_ctx : rhs->mem_ctx;
263 unsigned int writemask;
264
265 if (!(ir->write_mask & (1 << i)))
266 continue;
267
268 if (lhs) {
269 new_lhs = new(mem_ctx) ir_dereference_variable(lhs->components[i]);
270 writemask = 1;
271 } else {
272 new_lhs = ir->lhs->clone(mem_ctx, NULL);
273 writemask = 1 << i;
274 }
275
276 if (rhs) {
277 new_rhs =
278 new(mem_ctx) ir_dereference_variable(rhs->components[rhs_chan]);
279 } else {
280 new_rhs = new(mem_ctx) ir_swizzle(ir->rhs->clone(mem_ctx, NULL),
281 rhs_chan, 0, 0, 0, 1);
282 }
283
284 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
285 new_rhs,
286 NULL, writemask));
287
288 rhs_chan++;
289 }
290 ir->remove();
291 } else if (lhs) {
292 void *mem_ctx = lhs->mem_ctx;
293 int elem = -1;
294
295 switch (ir->write_mask) {
296 case (1 << 0):
297 elem = 0;
298 break;
299 case (1 << 1):
300 elem = 1;
301 break;
302 case (1 << 2):
303 elem = 2;
304 break;
305 case (1 << 3):
306 elem = 3;
307 break;
308 default:
309 ir->fprint(stderr);
310 unreachable("not reached: non-channelwise dereference of LHS.");
311 }
312
313 ir->lhs = new(mem_ctx) ir_dereference_variable(lhs->components[elem]);
314 ir->write_mask = (1 << 0);
315
316 handle_rvalue(&ir->rhs);
317 } else {
318 handle_rvalue(&ir->rhs);
319 }
320
321 handle_rvalue(&ir->condition);
322
323 return visit_continue;
324 }
325
326 bool
327 brw_do_vector_splitting(exec_list *instructions)
328 {
329 struct hash_entry *hte;
330
331 ir_vector_reference_visitor refs;
332
333 visit_list_elements(&refs, instructions);
334
335 /* Trim out variables we can't split. */
336 hash_table_foreach(refs.ht, hte) {
337 struct variable_entry *entry = (struct variable_entry *) hte->data;
338 if (debug) {
339 fprintf(stderr, "vector %s@%p: whole_access %d\n",
340 entry->var->name, (void *) entry->var,
341 entry->whole_vector_access);
342 }
343
344 if (entry->whole_vector_access) {
345 _mesa_hash_table_remove(refs.ht, hte);
346 }
347 }
348
349 if (refs.ht->entries == 0)
350 return false;
351
352 void *mem_ctx = ralloc_context(NULL);
353
354 /* Replace the decls of the vectors to be split with their split
355 * components.
356 */
357 hash_table_foreach(refs.ht, hte) {
358 struct variable_entry *entry = (struct variable_entry *) hte->data;
359 const struct glsl_type *type;
360 type = glsl_type::get_instance(entry->var->type->base_type, 1, 1);
361
362 entry->mem_ctx = ralloc_parent(entry->var);
363
364 for (unsigned int i = 0; i < entry->var->type->vector_elements; i++) {
365 char *const name = ir_variable::temporaries_allocate_names
366 ? ralloc_asprintf(mem_ctx, "%s_%c",
367 entry->var->name,
368 "xyzw"[i])
369 : NULL;
370
371 entry->components[i] = new(entry->mem_ctx) ir_variable(type, name,
372 ir_var_temporary);
373
374 ralloc_free(name);
375
376 entry->var->insert_before(entry->components[i]);
377 }
378
379 entry->var->remove();
380 }
381
382 ir_vector_splitting_visitor split(refs.ht);
383 visit_list_elements(&split, instructions);
384
385 ralloc_free(mem_ctx);
386
387 return true;
388 }