Rename nir_lower_constant_initializers to nir_lower_variable_initalizers
[mesa.git] / src / compiler / nir / nir_split_vars.c
1 /*
2 * Copyright © 2018 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27 #include "nir_vla.h"
28
29 #include "util/set.h"
30 #include "util/u_math.h"
31
32 static struct set *
33 get_complex_used_vars(nir_shader *shader, void *mem_ctx)
34 {
35 struct set *complex_vars = _mesa_pointer_set_create(mem_ctx);
36
37 nir_foreach_function(function, shader) {
38 if (!function->impl)
39 continue;
40
41 nir_foreach_block(block, function->impl) {
42 nir_foreach_instr(instr, block) {
43 if (instr->type != nir_instr_type_deref)
44 continue;
45
46 nir_deref_instr *deref = nir_instr_as_deref(instr);
47
48 /* We only need to consider var derefs because
49 * nir_deref_instr_has_complex_use is recursive.
50 */
51 if (deref->deref_type == nir_deref_type_var &&
52 nir_deref_instr_has_complex_use(deref))
53 _mesa_set_add(complex_vars, deref->var);
54 }
55 }
56 }
57
58 return complex_vars;
59 }
60
61 struct split_var_state {
62 void *mem_ctx;
63
64 nir_shader *shader;
65 nir_function_impl *impl;
66
67 nir_variable *base_var;
68 };
69
70 struct field {
71 struct field *parent;
72
73 const struct glsl_type *type;
74
75 unsigned num_fields;
76 struct field *fields;
77
78 nir_variable *var;
79 };
80
81 static const struct glsl_type *
82 wrap_type_in_array(const struct glsl_type *type,
83 const struct glsl_type *array_type)
84 {
85 if (!glsl_type_is_array(array_type))
86 return type;
87
88 const struct glsl_type *elem_type =
89 wrap_type_in_array(type, glsl_get_array_element(array_type));
90 assert(glsl_get_explicit_stride(array_type) == 0);
91 return glsl_array_type(elem_type, glsl_get_length(array_type), 0);
92 }
93
94 static int
95 num_array_levels_in_array_of_vector_type(const struct glsl_type *type)
96 {
97 int num_levels = 0;
98 while (true) {
99 if (glsl_type_is_array_or_matrix(type)) {
100 num_levels++;
101 type = glsl_get_array_element(type);
102 } else if (glsl_type_is_vector_or_scalar(type)) {
103 return num_levels;
104 } else {
105 /* Not an array of vectors */
106 return -1;
107 }
108 }
109 }
110
111 static void
112 init_field_for_type(struct field *field, struct field *parent,
113 const struct glsl_type *type,
114 const char *name,
115 struct split_var_state *state)
116 {
117 *field = (struct field) {
118 .parent = parent,
119 .type = type,
120 };
121
122 const struct glsl_type *struct_type = glsl_without_array(type);
123 if (glsl_type_is_struct_or_ifc(struct_type)) {
124 field->num_fields = glsl_get_length(struct_type),
125 field->fields = ralloc_array(state->mem_ctx, struct field,
126 field->num_fields);
127 for (unsigned i = 0; i < field->num_fields; i++) {
128 char *field_name = NULL;
129 if (name) {
130 field_name = ralloc_asprintf(state->mem_ctx, "%s_%s", name,
131 glsl_get_struct_elem_name(struct_type, i));
132 } else {
133 field_name = ralloc_asprintf(state->mem_ctx, "{unnamed %s}_%s",
134 glsl_get_type_name(struct_type),
135 glsl_get_struct_elem_name(struct_type, i));
136 }
137 init_field_for_type(&field->fields[i], field,
138 glsl_get_struct_field(struct_type, i),
139 field_name, state);
140 }
141 } else {
142 const struct glsl_type *var_type = type;
143 for (struct field *f = field->parent; f; f = f->parent)
144 var_type = wrap_type_in_array(var_type, f->type);
145
146 nir_variable_mode mode = state->base_var->data.mode;
147 if (mode == nir_var_function_temp) {
148 field->var = nir_local_variable_create(state->impl, var_type, name);
149 } else {
150 field->var = nir_variable_create(state->shader, mode, var_type, name);
151 }
152 }
153 }
154
155 static bool
156 split_var_list_structs(nir_shader *shader,
157 nir_function_impl *impl,
158 struct exec_list *vars,
159 struct hash_table *var_field_map,
160 struct set **complex_vars,
161 void *mem_ctx)
162 {
163 struct split_var_state state = {
164 .mem_ctx = mem_ctx,
165 .shader = shader,
166 .impl = impl,
167 };
168
169 struct exec_list split_vars;
170 exec_list_make_empty(&split_vars);
171
172 /* To avoid list confusion (we'll be adding things as we split variables),
173 * pull all of the variables we plan to split off of the list
174 */
175 nir_foreach_variable_safe(var, vars) {
176 if (!glsl_type_is_struct_or_ifc(glsl_without_array(var->type)))
177 continue;
178
179 if (*complex_vars == NULL)
180 *complex_vars = get_complex_used_vars(shader, mem_ctx);
181
182 /* We can't split a variable that's referenced with deref that has any
183 * sort of complex usage.
184 */
185 if (_mesa_set_search(*complex_vars, var))
186 continue;
187
188 exec_node_remove(&var->node);
189 exec_list_push_tail(&split_vars, &var->node);
190 }
191
192 nir_foreach_variable(var, &split_vars) {
193 state.base_var = var;
194
195 struct field *root_field = ralloc(mem_ctx, struct field);
196 init_field_for_type(root_field, NULL, var->type, var->name, &state);
197 _mesa_hash_table_insert(var_field_map, var, root_field);
198 }
199
200 return !exec_list_is_empty(&split_vars);
201 }
202
203 static void
204 split_struct_derefs_impl(nir_function_impl *impl,
205 struct hash_table *var_field_map,
206 nir_variable_mode modes,
207 void *mem_ctx)
208 {
209 nir_builder b;
210 nir_builder_init(&b, impl);
211
212 nir_foreach_block(block, impl) {
213 nir_foreach_instr_safe(instr, block) {
214 if (instr->type != nir_instr_type_deref)
215 continue;
216
217 nir_deref_instr *deref = nir_instr_as_deref(instr);
218 if (!(deref->mode & modes))
219 continue;
220
221 /* Clean up any dead derefs we find lying around. They may refer to
222 * variables we're planning to split.
223 */
224 if (nir_deref_instr_remove_if_unused(deref))
225 continue;
226
227 if (!glsl_type_is_vector_or_scalar(deref->type))
228 continue;
229
230 nir_variable *base_var = nir_deref_instr_get_variable(deref);
231 struct hash_entry *entry =
232 _mesa_hash_table_search(var_field_map, base_var);
233 if (!entry)
234 continue;
235
236 struct field *root_field = entry->data;
237
238 nir_deref_path path;
239 nir_deref_path_init(&path, deref, mem_ctx);
240
241 struct field *tail_field = root_field;
242 for (unsigned i = 0; path.path[i]; i++) {
243 if (path.path[i]->deref_type != nir_deref_type_struct)
244 continue;
245
246 assert(i > 0);
247 assert(glsl_type_is_struct_or_ifc(path.path[i - 1]->type));
248 assert(path.path[i - 1]->type ==
249 glsl_without_array(tail_field->type));
250
251 tail_field = &tail_field->fields[path.path[i]->strct.index];
252 }
253 nir_variable *split_var = tail_field->var;
254
255 nir_deref_instr *new_deref = NULL;
256 for (unsigned i = 0; path.path[i]; i++) {
257 nir_deref_instr *p = path.path[i];
258 b.cursor = nir_after_instr(&p->instr);
259
260 switch (p->deref_type) {
261 case nir_deref_type_var:
262 assert(new_deref == NULL);
263 new_deref = nir_build_deref_var(&b, split_var);
264 break;
265
266 case nir_deref_type_array:
267 case nir_deref_type_array_wildcard:
268 new_deref = nir_build_deref_follower(&b, new_deref, p);
269 break;
270
271 case nir_deref_type_struct:
272 /* Nothing to do; we're splitting structs */
273 break;
274
275 default:
276 unreachable("Invalid deref type in path");
277 }
278 }
279
280 assert(new_deref->type == deref->type);
281 nir_ssa_def_rewrite_uses(&deref->dest.ssa,
282 nir_src_for_ssa(&new_deref->dest.ssa));
283 nir_deref_instr_remove_if_unused(deref);
284 }
285 }
286 }
287
288 /** A pass for splitting structs into multiple variables
289 *
290 * This pass splits arrays of structs into multiple variables, one for each
291 * (possibly nested) structure member. After this pass completes, no
292 * variables of the given mode will contain a struct type.
293 */
294 bool
295 nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes)
296 {
297 void *mem_ctx = ralloc_context(NULL);
298 struct hash_table *var_field_map =
299 _mesa_pointer_hash_table_create(mem_ctx);
300 struct set *complex_vars = NULL;
301
302 assert((modes & (nir_var_shader_temp | nir_var_function_temp)) == modes);
303
304 bool has_global_splits = false;
305 if (modes & nir_var_shader_temp) {
306 has_global_splits = split_var_list_structs(shader, NULL,
307 &shader->globals,
308 var_field_map,
309 &complex_vars,
310 mem_ctx);
311 }
312
313 bool progress = false;
314 nir_foreach_function(function, shader) {
315 if (!function->impl)
316 continue;
317
318 bool has_local_splits = false;
319 if (modes & nir_var_function_temp) {
320 has_local_splits = split_var_list_structs(shader, function->impl,
321 &function->impl->locals,
322 var_field_map,
323 &complex_vars,
324 mem_ctx);
325 }
326
327 if (has_global_splits || has_local_splits) {
328 split_struct_derefs_impl(function->impl, var_field_map,
329 modes, mem_ctx);
330
331 nir_metadata_preserve(function->impl, nir_metadata_block_index |
332 nir_metadata_dominance);
333 progress = true;
334 }
335 }
336
337 ralloc_free(mem_ctx);
338
339 return progress;
340 }
341
342 struct array_level_info {
343 unsigned array_len;
344 bool split;
345 };
346
347 struct array_split {
348 /* Only set if this is the tail end of the splitting */
349 nir_variable *var;
350
351 unsigned num_splits;
352 struct array_split *splits;
353 };
354
355 struct array_var_info {
356 nir_variable *base_var;
357
358 const struct glsl_type *split_var_type;
359
360 bool split_var;
361 struct array_split root_split;
362
363 unsigned num_levels;
364 struct array_level_info levels[0];
365 };
366
367 static bool
368 init_var_list_array_infos(nir_shader *shader,
369 struct exec_list *vars,
370 struct hash_table *var_info_map,
371 struct set **complex_vars,
372 void *mem_ctx)
373 {
374 bool has_array = false;
375
376 nir_foreach_variable(var, vars) {
377 int num_levels = num_array_levels_in_array_of_vector_type(var->type);
378 if (num_levels <= 0)
379 continue;
380
381 if (*complex_vars == NULL)
382 *complex_vars = get_complex_used_vars(shader, mem_ctx);
383
384 /* We can't split a variable that's referenced with deref that has any
385 * sort of complex usage.
386 */
387 if (_mesa_set_search(*complex_vars, var))
388 continue;
389
390 struct array_var_info *info =
391 rzalloc_size(mem_ctx, sizeof(*info) +
392 num_levels * sizeof(info->levels[0]));
393
394 info->base_var = var;
395 info->num_levels = num_levels;
396
397 const struct glsl_type *type = var->type;
398 for (int i = 0; i < num_levels; i++) {
399 info->levels[i].array_len = glsl_get_length(type);
400 type = glsl_get_array_element(type);
401
402 /* All levels start out initially as split */
403 info->levels[i].split = true;
404 }
405
406 _mesa_hash_table_insert(var_info_map, var, info);
407 has_array = true;
408 }
409
410 return has_array;
411 }
412
413 static struct array_var_info *
414 get_array_var_info(nir_variable *var,
415 struct hash_table *var_info_map)
416 {
417 struct hash_entry *entry =
418 _mesa_hash_table_search(var_info_map, var);
419 return entry ? entry->data : NULL;
420 }
421
422 static struct array_var_info *
423 get_array_deref_info(nir_deref_instr *deref,
424 struct hash_table *var_info_map,
425 nir_variable_mode modes)
426 {
427 if (!(deref->mode & modes))
428 return NULL;
429
430 nir_variable *var = nir_deref_instr_get_variable(deref);
431 if (var == NULL)
432 return NULL;
433
434 return get_array_var_info(var, var_info_map);
435 }
436
437 static void
438 mark_array_deref_used(nir_deref_instr *deref,
439 struct hash_table *var_info_map,
440 nir_variable_mode modes,
441 void *mem_ctx)
442 {
443 struct array_var_info *info =
444 get_array_deref_info(deref, var_info_map, modes);
445 if (!info)
446 return;
447
448 nir_deref_path path;
449 nir_deref_path_init(&path, deref, mem_ctx);
450
451 /* Walk the path and look for indirects. If we have an array deref with an
452 * indirect, mark the given level as not being split.
453 */
454 for (unsigned i = 0; i < info->num_levels; i++) {
455 nir_deref_instr *p = path.path[i + 1];
456 if (p->deref_type == nir_deref_type_array &&
457 !nir_src_is_const(p->arr.index))
458 info->levels[i].split = false;
459 }
460 }
461
462 static void
463 mark_array_usage_impl(nir_function_impl *impl,
464 struct hash_table *var_info_map,
465 nir_variable_mode modes,
466 void *mem_ctx)
467 {
468 nir_foreach_block(block, impl) {
469 nir_foreach_instr(instr, block) {
470 if (instr->type != nir_instr_type_intrinsic)
471 continue;
472
473 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
474 switch (intrin->intrinsic) {
475 case nir_intrinsic_copy_deref:
476 mark_array_deref_used(nir_src_as_deref(intrin->src[1]),
477 var_info_map, modes, mem_ctx);
478 /* Fall Through */
479
480 case nir_intrinsic_load_deref:
481 case nir_intrinsic_store_deref:
482 mark_array_deref_used(nir_src_as_deref(intrin->src[0]),
483 var_info_map, modes, mem_ctx);
484 break;
485
486 default:
487 break;
488 }
489 }
490 }
491 }
492
493 static void
494 create_split_array_vars(struct array_var_info *var_info,
495 unsigned level,
496 struct array_split *split,
497 const char *name,
498 nir_shader *shader,
499 nir_function_impl *impl,
500 void *mem_ctx)
501 {
502 while (level < var_info->num_levels && !var_info->levels[level].split) {
503 name = ralloc_asprintf(mem_ctx, "%s[*]", name);
504 level++;
505 }
506
507 if (level == var_info->num_levels) {
508 /* We add parens to the variable name so it looks like "(foo[2][*])" so
509 * that further derefs will look like "(foo[2][*])[ssa_6]"
510 */
511 name = ralloc_asprintf(mem_ctx, "(%s)", name);
512
513 nir_variable_mode mode = var_info->base_var->data.mode;
514 if (mode == nir_var_function_temp) {
515 split->var = nir_local_variable_create(impl,
516 var_info->split_var_type, name);
517 } else {
518 split->var = nir_variable_create(shader, mode,
519 var_info->split_var_type, name);
520 }
521 } else {
522 assert(var_info->levels[level].split);
523 split->num_splits = var_info->levels[level].array_len;
524 split->splits = rzalloc_array(mem_ctx, struct array_split,
525 split->num_splits);
526 for (unsigned i = 0; i < split->num_splits; i++) {
527 create_split_array_vars(var_info, level + 1, &split->splits[i],
528 ralloc_asprintf(mem_ctx, "%s[%d]", name, i),
529 shader, impl, mem_ctx);
530 }
531 }
532 }
533
534 static bool
535 split_var_list_arrays(nir_shader *shader,
536 nir_function_impl *impl,
537 struct exec_list *vars,
538 struct hash_table *var_info_map,
539 void *mem_ctx)
540 {
541 struct exec_list split_vars;
542 exec_list_make_empty(&split_vars);
543
544 nir_foreach_variable_safe(var, vars) {
545 struct array_var_info *info = get_array_var_info(var, var_info_map);
546 if (!info)
547 continue;
548
549 bool has_split = false;
550 const struct glsl_type *split_type =
551 glsl_without_array_or_matrix(var->type);
552 for (int i = info->num_levels - 1; i >= 0; i--) {
553 if (info->levels[i].split) {
554 has_split = true;
555 continue;
556 }
557
558 /* If the original type was a matrix type, we'd like to keep that so
559 * we don't convert matrices into arrays.
560 */
561 if (i == info->num_levels - 1 &&
562 glsl_type_is_matrix(glsl_without_array(var->type))) {
563 split_type = glsl_matrix_type(glsl_get_base_type(split_type),
564 glsl_get_components(split_type),
565 info->levels[i].array_len);
566 } else {
567 split_type = glsl_array_type(split_type, info->levels[i].array_len, 0);
568 }
569 }
570
571 if (has_split) {
572 info->split_var_type = split_type;
573 /* To avoid list confusion (we'll be adding things as we split
574 * variables), pull all of the variables we plan to split off of the
575 * main variable list.
576 */
577 exec_node_remove(&var->node);
578 exec_list_push_tail(&split_vars, &var->node);
579 } else {
580 assert(split_type == glsl_get_bare_type(var->type));
581 /* If we're not modifying this variable, delete the info so we skip
582 * it faster in later passes.
583 */
584 _mesa_hash_table_remove_key(var_info_map, var);
585 }
586 }
587
588 nir_foreach_variable(var, &split_vars) {
589 struct array_var_info *info = get_array_var_info(var, var_info_map);
590 create_split_array_vars(info, 0, &info->root_split, var->name,
591 shader, impl, mem_ctx);
592 }
593
594 return !exec_list_is_empty(&split_vars);
595 }
596
597 static bool
598 deref_has_split_wildcard(nir_deref_path *path,
599 struct array_var_info *info)
600 {
601 if (info == NULL)
602 return false;
603
604 assert(path->path[0]->var == info->base_var);
605 for (unsigned i = 0; i < info->num_levels; i++) {
606 if (path->path[i + 1]->deref_type == nir_deref_type_array_wildcard &&
607 info->levels[i].split)
608 return true;
609 }
610
611 return false;
612 }
613
614 static bool
615 array_path_is_out_of_bounds(nir_deref_path *path,
616 struct array_var_info *info)
617 {
618 if (info == NULL)
619 return false;
620
621 assert(path->path[0]->var == info->base_var);
622 for (unsigned i = 0; i < info->num_levels; i++) {
623 nir_deref_instr *p = path->path[i + 1];
624 if (p->deref_type == nir_deref_type_array_wildcard)
625 continue;
626
627 if (nir_src_is_const(p->arr.index) &&
628 nir_src_as_uint(p->arr.index) >= info->levels[i].array_len)
629 return true;
630 }
631
632 return false;
633 }
634
635 static void
636 emit_split_copies(nir_builder *b,
637 struct array_var_info *dst_info, nir_deref_path *dst_path,
638 unsigned dst_level, nir_deref_instr *dst,
639 struct array_var_info *src_info, nir_deref_path *src_path,
640 unsigned src_level, nir_deref_instr *src)
641 {
642 nir_deref_instr *dst_p, *src_p;
643
644 while ((dst_p = dst_path->path[dst_level + 1])) {
645 if (dst_p->deref_type == nir_deref_type_array_wildcard)
646 break;
647
648 dst = nir_build_deref_follower(b, dst, dst_p);
649 dst_level++;
650 }
651
652 while ((src_p = src_path->path[src_level + 1])) {
653 if (src_p->deref_type == nir_deref_type_array_wildcard)
654 break;
655
656 src = nir_build_deref_follower(b, src, src_p);
657 src_level++;
658 }
659
660 if (src_p == NULL || dst_p == NULL) {
661 assert(src_p == NULL && dst_p == NULL);
662 nir_copy_deref(b, dst, src);
663 } else {
664 assert(dst_p->deref_type == nir_deref_type_array_wildcard &&
665 src_p->deref_type == nir_deref_type_array_wildcard);
666
667 if ((dst_info && dst_info->levels[dst_level].split) ||
668 (src_info && src_info->levels[src_level].split)) {
669 /* There are no indirects at this level on one of the source or the
670 * destination so we are lowering it.
671 */
672 assert(glsl_get_length(dst_path->path[dst_level]->type) ==
673 glsl_get_length(src_path->path[src_level]->type));
674 unsigned len = glsl_get_length(dst_path->path[dst_level]->type);
675 for (unsigned i = 0; i < len; i++) {
676 emit_split_copies(b, dst_info, dst_path, dst_level + 1,
677 nir_build_deref_array_imm(b, dst, i),
678 src_info, src_path, src_level + 1,
679 nir_build_deref_array_imm(b, src, i));
680 }
681 } else {
682 /* Neither side is being split so we just keep going */
683 emit_split_copies(b, dst_info, dst_path, dst_level + 1,
684 nir_build_deref_array_wildcard(b, dst),
685 src_info, src_path, src_level + 1,
686 nir_build_deref_array_wildcard(b, src));
687 }
688 }
689 }
690
691 static void
692 split_array_copies_impl(nir_function_impl *impl,
693 struct hash_table *var_info_map,
694 nir_variable_mode modes,
695 void *mem_ctx)
696 {
697 nir_builder b;
698 nir_builder_init(&b, impl);
699
700 nir_foreach_block(block, impl) {
701 nir_foreach_instr_safe(instr, block) {
702 if (instr->type != nir_instr_type_intrinsic)
703 continue;
704
705 nir_intrinsic_instr *copy = nir_instr_as_intrinsic(instr);
706 if (copy->intrinsic != nir_intrinsic_copy_deref)
707 continue;
708
709 nir_deref_instr *dst_deref = nir_src_as_deref(copy->src[0]);
710 nir_deref_instr *src_deref = nir_src_as_deref(copy->src[1]);
711
712 struct array_var_info *dst_info =
713 get_array_deref_info(dst_deref, var_info_map, modes);
714 struct array_var_info *src_info =
715 get_array_deref_info(src_deref, var_info_map, modes);
716
717 if (!src_info && !dst_info)
718 continue;
719
720 nir_deref_path dst_path, src_path;
721 nir_deref_path_init(&dst_path, dst_deref, mem_ctx);
722 nir_deref_path_init(&src_path, src_deref, mem_ctx);
723
724 if (!deref_has_split_wildcard(&dst_path, dst_info) &&
725 !deref_has_split_wildcard(&src_path, src_info))
726 continue;
727
728 b.cursor = nir_instr_remove(&copy->instr);
729
730 emit_split_copies(&b, dst_info, &dst_path, 0, dst_path.path[0],
731 src_info, &src_path, 0, src_path.path[0]);
732 }
733 }
734 }
735
736 static void
737 split_array_access_impl(nir_function_impl *impl,
738 struct hash_table *var_info_map,
739 nir_variable_mode modes,
740 void *mem_ctx)
741 {
742 nir_builder b;
743 nir_builder_init(&b, impl);
744
745 nir_foreach_block(block, impl) {
746 nir_foreach_instr_safe(instr, block) {
747 if (instr->type == nir_instr_type_deref) {
748 /* Clean up any dead derefs we find lying around. They may refer
749 * to variables we're planning to split.
750 */
751 nir_deref_instr *deref = nir_instr_as_deref(instr);
752 if (deref->mode & modes)
753 nir_deref_instr_remove_if_unused(deref);
754 continue;
755 }
756
757 if (instr->type != nir_instr_type_intrinsic)
758 continue;
759
760 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
761 if (intrin->intrinsic != nir_intrinsic_load_deref &&
762 intrin->intrinsic != nir_intrinsic_store_deref &&
763 intrin->intrinsic != nir_intrinsic_copy_deref)
764 continue;
765
766 const unsigned num_derefs =
767 intrin->intrinsic == nir_intrinsic_copy_deref ? 2 : 1;
768
769 for (unsigned d = 0; d < num_derefs; d++) {
770 nir_deref_instr *deref = nir_src_as_deref(intrin->src[d]);
771
772 struct array_var_info *info =
773 get_array_deref_info(deref, var_info_map, modes);
774 if (!info)
775 continue;
776
777 nir_deref_path path;
778 nir_deref_path_init(&path, deref, mem_ctx);
779
780 b.cursor = nir_before_instr(&intrin->instr);
781
782 if (array_path_is_out_of_bounds(&path, info)) {
783 /* If one of the derefs is out-of-bounds, we just delete the
784 * instruction. If a destination is out of bounds, then it may
785 * have been in-bounds prior to shrinking so we don't want to
786 * accidentally stomp something. However, we've already proven
787 * that it will never be read so it's safe to delete. If a
788 * source is out of bounds then it is loading random garbage.
789 * For loads, we replace their uses with an undef instruction
790 * and for copies we just delete the copy since it was writing
791 * undefined garbage anyway and we may as well leave the random
792 * garbage in the destination alone.
793 */
794 if (intrin->intrinsic == nir_intrinsic_load_deref) {
795 nir_ssa_def *u =
796 nir_ssa_undef(&b, intrin->dest.ssa.num_components,
797 intrin->dest.ssa.bit_size);
798 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
799 nir_src_for_ssa(u));
800 }
801 nir_instr_remove(&intrin->instr);
802 for (unsigned i = 0; i < num_derefs; i++)
803 nir_deref_instr_remove_if_unused(nir_src_as_deref(intrin->src[i]));
804 break;
805 }
806
807 struct array_split *split = &info->root_split;
808 for (unsigned i = 0; i < info->num_levels; i++) {
809 if (info->levels[i].split) {
810 nir_deref_instr *p = path.path[i + 1];
811 unsigned index = nir_src_as_uint(p->arr.index);
812 assert(index < info->levels[i].array_len);
813 split = &split->splits[index];
814 }
815 }
816 assert(!split->splits && split->var);
817
818 nir_deref_instr *new_deref = nir_build_deref_var(&b, split->var);
819 for (unsigned i = 0; i < info->num_levels; i++) {
820 if (!info->levels[i].split) {
821 new_deref = nir_build_deref_follower(&b, new_deref,
822 path.path[i + 1]);
823 }
824 }
825 assert(new_deref->type == deref->type);
826
827 /* Rewrite the deref source to point to the split one */
828 nir_instr_rewrite_src(&intrin->instr, &intrin->src[d],
829 nir_src_for_ssa(&new_deref->dest.ssa));
830 nir_deref_instr_remove_if_unused(deref);
831 }
832 }
833 }
834 }
835
836 /** A pass for splitting arrays of vectors into multiple variables
837 *
838 * This pass looks at arrays (possibly multiple levels) of vectors (not
839 * structures or other types) and tries to split them into piles of variables,
840 * one for each array element. The heuristic used is simple: If a given array
841 * level is never used with an indirect, that array level will get split.
842 *
843 * This pass probably could handles structures easily enough but making a pass
844 * that could see through an array of structures of arrays would be difficult
845 * so it's best to just run nir_split_struct_vars first.
846 */
847 bool
848 nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
849 {
850 void *mem_ctx = ralloc_context(NULL);
851 struct hash_table *var_info_map = _mesa_pointer_hash_table_create(mem_ctx);
852 struct set *complex_vars = NULL;
853
854 assert((modes & (nir_var_shader_temp | nir_var_function_temp)) == modes);
855
856 bool has_global_array = false;
857 if (modes & nir_var_shader_temp) {
858 has_global_array = init_var_list_array_infos(shader,
859 &shader->globals,
860 var_info_map,
861 &complex_vars,
862 mem_ctx);
863 }
864
865 bool has_any_array = false;
866 nir_foreach_function(function, shader) {
867 if (!function->impl)
868 continue;
869
870 bool has_local_array = false;
871 if (modes & nir_var_function_temp) {
872 has_local_array = init_var_list_array_infos(shader,
873 &function->impl->locals,
874 var_info_map,
875 &complex_vars,
876 mem_ctx);
877 }
878
879 if (has_global_array || has_local_array) {
880 has_any_array = true;
881 mark_array_usage_impl(function->impl, var_info_map, modes, mem_ctx);
882 }
883 }
884
885 /* If we failed to find any arrays of arrays, bail early. */
886 if (!has_any_array) {
887 ralloc_free(mem_ctx);
888 return false;
889 }
890
891 bool has_global_splits = false;
892 if (modes & nir_var_shader_temp) {
893 has_global_splits = split_var_list_arrays(shader, NULL,
894 &shader->globals,
895 var_info_map, mem_ctx);
896 }
897
898 bool progress = false;
899 nir_foreach_function(function, shader) {
900 if (!function->impl)
901 continue;
902
903 bool has_local_splits = false;
904 if (modes & nir_var_function_temp) {
905 has_local_splits = split_var_list_arrays(shader, function->impl,
906 &function->impl->locals,
907 var_info_map, mem_ctx);
908 }
909
910 if (has_global_splits || has_local_splits) {
911 split_array_copies_impl(function->impl, var_info_map, modes, mem_ctx);
912 split_array_access_impl(function->impl, var_info_map, modes, mem_ctx);
913
914 nir_metadata_preserve(function->impl, nir_metadata_block_index |
915 nir_metadata_dominance);
916 progress = true;
917 }
918 }
919
920 ralloc_free(mem_ctx);
921
922 return progress;
923 }
924
925 struct array_level_usage {
926 unsigned array_len;
927
928 /* The value UINT_MAX will be used to indicate an indirect */
929 unsigned max_read;
930 unsigned max_written;
931
932 /* True if there is a copy that isn't to/from a shrinkable array */
933 bool has_external_copy;
934 struct set *levels_copied;
935 };
936
937 struct vec_var_usage {
938 /* Convenience set of all components this variable has */
939 nir_component_mask_t all_comps;
940
941 nir_component_mask_t comps_read;
942 nir_component_mask_t comps_written;
943
944 nir_component_mask_t comps_kept;
945
946 /* True if there is a copy that isn't to/from a shrinkable vector */
947 bool has_external_copy;
948 bool has_complex_use;
949 struct set *vars_copied;
950
951 unsigned num_levels;
952 struct array_level_usage levels[0];
953 };
954
955 static struct vec_var_usage *
956 get_vec_var_usage(nir_variable *var,
957 struct hash_table *var_usage_map,
958 bool add_usage_entry, void *mem_ctx)
959 {
960 struct hash_entry *entry = _mesa_hash_table_search(var_usage_map, var);
961 if (entry)
962 return entry->data;
963
964 if (!add_usage_entry)
965 return NULL;
966
967 /* Check to make sure that we are working with an array of vectors. We
968 * don't bother to shrink single vectors because we figure that we can
969 * clean it up better with SSA than by inserting piles of vecN instructions
970 * to compact results.
971 */
972 int num_levels = num_array_levels_in_array_of_vector_type(var->type);
973 if (num_levels < 1)
974 return NULL; /* Not an array of vectors */
975
976 struct vec_var_usage *usage =
977 rzalloc_size(mem_ctx, sizeof(*usage) +
978 num_levels * sizeof(usage->levels[0]));
979
980 usage->num_levels = num_levels;
981 const struct glsl_type *type = var->type;
982 for (unsigned i = 0; i < num_levels; i++) {
983 usage->levels[i].array_len = glsl_get_length(type);
984 type = glsl_get_array_element(type);
985 }
986 assert(glsl_type_is_vector_or_scalar(type));
987
988 usage->all_comps = (1 << glsl_get_components(type)) - 1;
989
990 _mesa_hash_table_insert(var_usage_map, var, usage);
991
992 return usage;
993 }
994
995 static struct vec_var_usage *
996 get_vec_deref_usage(nir_deref_instr *deref,
997 struct hash_table *var_usage_map,
998 nir_variable_mode modes,
999 bool add_usage_entry, void *mem_ctx)
1000 {
1001 if (!(deref->mode & modes))
1002 return NULL;
1003
1004 return get_vec_var_usage(nir_deref_instr_get_variable(deref),
1005 var_usage_map, add_usage_entry, mem_ctx);
1006 }
1007
1008 static void
1009 mark_deref_if_complex(nir_deref_instr *deref,
1010 struct hash_table *var_usage_map,
1011 nir_variable_mode modes,
1012 void *mem_ctx)
1013 {
1014 if (!(deref->mode & modes))
1015 return;
1016
1017 /* Only bother with var derefs because nir_deref_instr_has_complex_use is
1018 * recursive.
1019 */
1020 if (deref->deref_type != nir_deref_type_var)
1021 return;
1022
1023 if (!nir_deref_instr_has_complex_use(deref))
1024 return;
1025
1026 struct vec_var_usage *usage =
1027 get_vec_var_usage(deref->var, var_usage_map, true, mem_ctx);
1028 if (!usage)
1029 return;
1030
1031 usage->has_complex_use = true;
1032 }
1033
1034 static void
1035 mark_deref_used(nir_deref_instr *deref,
1036 nir_component_mask_t comps_read,
1037 nir_component_mask_t comps_written,
1038 nir_deref_instr *copy_deref,
1039 struct hash_table *var_usage_map,
1040 nir_variable_mode modes,
1041 void *mem_ctx)
1042 {
1043 if (!(deref->mode & modes))
1044 return;
1045
1046 nir_variable *var = nir_deref_instr_get_variable(deref);
1047 if (var == NULL)
1048 return;
1049
1050 struct vec_var_usage *usage =
1051 get_vec_var_usage(var, var_usage_map, true, mem_ctx);
1052 if (!usage)
1053 return;
1054
1055 usage->comps_read |= comps_read & usage->all_comps;
1056 usage->comps_written |= comps_written & usage->all_comps;
1057
1058 struct vec_var_usage *copy_usage = NULL;
1059 if (copy_deref) {
1060 copy_usage = get_vec_deref_usage(copy_deref, var_usage_map, modes,
1061 true, mem_ctx);
1062 if (copy_usage) {
1063 if (usage->vars_copied == NULL) {
1064 usage->vars_copied = _mesa_pointer_set_create(mem_ctx);
1065 }
1066 _mesa_set_add(usage->vars_copied, copy_usage);
1067 } else {
1068 usage->has_external_copy = true;
1069 }
1070 }
1071
1072 nir_deref_path path;
1073 nir_deref_path_init(&path, deref, mem_ctx);
1074
1075 nir_deref_path copy_path;
1076 if (copy_usage)
1077 nir_deref_path_init(&copy_path, copy_deref, mem_ctx);
1078
1079 unsigned copy_i = 0;
1080 for (unsigned i = 0; i < usage->num_levels; i++) {
1081 struct array_level_usage *level = &usage->levels[i];
1082 nir_deref_instr *deref = path.path[i + 1];
1083 assert(deref->deref_type == nir_deref_type_array ||
1084 deref->deref_type == nir_deref_type_array_wildcard);
1085
1086 unsigned max_used;
1087 if (deref->deref_type == nir_deref_type_array) {
1088 max_used = nir_src_is_const(deref->arr.index) ?
1089 nir_src_as_uint(deref->arr.index) : UINT_MAX;
1090 } else {
1091 /* For wildcards, we read or wrote the whole thing. */
1092 assert(deref->deref_type == nir_deref_type_array_wildcard);
1093 max_used = level->array_len - 1;
1094
1095 if (copy_usage) {
1096 /* Match each wildcard level with the level on copy_usage */
1097 for (; copy_path.path[copy_i + 1]; copy_i++) {
1098 if (copy_path.path[copy_i + 1]->deref_type ==
1099 nir_deref_type_array_wildcard)
1100 break;
1101 }
1102 struct array_level_usage *copy_level =
1103 &copy_usage->levels[copy_i++];
1104
1105 if (level->levels_copied == NULL) {
1106 level->levels_copied = _mesa_pointer_set_create(mem_ctx);
1107 }
1108 _mesa_set_add(level->levels_copied, copy_level);
1109 } else {
1110 /* We have a wildcard and it comes from a variable we aren't
1111 * tracking; flag it and we'll know to not shorten this array.
1112 */
1113 level->has_external_copy = true;
1114 }
1115 }
1116
1117 if (comps_written)
1118 level->max_written = MAX2(level->max_written, max_used);
1119 if (comps_read)
1120 level->max_read = MAX2(level->max_read, max_used);
1121 }
1122 }
1123
1124 static bool
1125 src_is_load_deref(nir_src src, nir_src deref_src)
1126 {
1127 nir_intrinsic_instr *load = nir_src_as_intrinsic(src);
1128 if (load == NULL || load->intrinsic != nir_intrinsic_load_deref)
1129 return false;
1130
1131 assert(load->src[0].is_ssa);
1132
1133 return load->src[0].ssa == deref_src.ssa;
1134 }
1135
1136 /* Returns all non-self-referential components of a store instruction. A
1137 * component is self-referential if it comes from the same component of a load
1138 * instruction on the same deref. If the only data in a particular component
1139 * of a variable came directly from that component then it's undefined. The
1140 * only way to get defined data into a component of a variable is for it to
1141 * get written there by something outside or from a different component.
1142 *
1143 * This is a fairly common pattern in shaders that come from either GLSL IR or
1144 * GLSLang because both glsl_to_nir and GLSLang implement write-masking with
1145 * load-vec-store.
1146 */
1147 static nir_component_mask_t
1148 get_non_self_referential_store_comps(nir_intrinsic_instr *store)
1149 {
1150 nir_component_mask_t comps = nir_intrinsic_write_mask(store);
1151
1152 assert(store->src[1].is_ssa);
1153 nir_instr *src_instr = store->src[1].ssa->parent_instr;
1154 if (src_instr->type != nir_instr_type_alu)
1155 return comps;
1156
1157 nir_alu_instr *src_alu = nir_instr_as_alu(src_instr);
1158
1159 if (src_alu->op == nir_op_mov) {
1160 /* If it's just a swizzle of a load from the same deref, discount any
1161 * channels that don't move in the swizzle.
1162 */
1163 if (src_is_load_deref(src_alu->src[0].src, store->src[0])) {
1164 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
1165 if (src_alu->src[0].swizzle[i] == i)
1166 comps &= ~(1u << i);
1167 }
1168 }
1169 } else if (src_alu->op == nir_op_vec2 ||
1170 src_alu->op == nir_op_vec3 ||
1171 src_alu->op == nir_op_vec4) {
1172 /* If it's a vec, discount any channels that are just loads from the
1173 * same deref put in the same spot.
1174 */
1175 for (unsigned i = 0; i < nir_op_infos[src_alu->op].num_inputs; i++) {
1176 if (src_is_load_deref(src_alu->src[i].src, store->src[0]) &&
1177 src_alu->src[i].swizzle[0] == i)
1178 comps &= ~(1u << i);
1179 }
1180 }
1181
1182 return comps;
1183 }
1184
1185 static void
1186 find_used_components_impl(nir_function_impl *impl,
1187 struct hash_table *var_usage_map,
1188 nir_variable_mode modes,
1189 void *mem_ctx)
1190 {
1191 nir_foreach_block(block, impl) {
1192 nir_foreach_instr(instr, block) {
1193 if (instr->type == nir_instr_type_deref) {
1194 mark_deref_if_complex(nir_instr_as_deref(instr),
1195 var_usage_map, modes, mem_ctx);
1196 }
1197
1198 if (instr->type != nir_instr_type_intrinsic)
1199 continue;
1200
1201 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1202 switch (intrin->intrinsic) {
1203 case nir_intrinsic_load_deref:
1204 mark_deref_used(nir_src_as_deref(intrin->src[0]),
1205 nir_ssa_def_components_read(&intrin->dest.ssa), 0,
1206 NULL, var_usage_map, modes, mem_ctx);
1207 break;
1208
1209 case nir_intrinsic_store_deref:
1210 mark_deref_used(nir_src_as_deref(intrin->src[0]),
1211 0, get_non_self_referential_store_comps(intrin),
1212 NULL, var_usage_map, modes, mem_ctx);
1213 break;
1214
1215 case nir_intrinsic_copy_deref: {
1216 /* Just mark everything used for copies. */
1217 nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
1218 nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
1219 mark_deref_used(dst, 0, ~0, src, var_usage_map, modes, mem_ctx);
1220 mark_deref_used(src, ~0, 0, dst, var_usage_map, modes, mem_ctx);
1221 break;
1222 }
1223
1224 default:
1225 break;
1226 }
1227 }
1228 }
1229 }
1230
1231 static bool
1232 shrink_vec_var_list(struct exec_list *vars,
1233 struct hash_table *var_usage_map)
1234 {
1235 /* Initialize the components kept field of each variable. This is the
1236 * AND of the components written and components read. If a component is
1237 * written but never read, it's dead. If it is read but never written,
1238 * then all values read are undefined garbage and we may as well not read
1239 * them.
1240 *
1241 * The same logic applies to the array length. We make the array length
1242 * the minimum needed required length between read and write and plan to
1243 * discard any OOB access. The one exception here is indirect writes
1244 * because we don't know where they will land and we can't shrink an array
1245 * with indirect writes because previously in-bounds writes may become
1246 * out-of-bounds and have undefined behavior.
1247 *
1248 * Also, if we have a copy that to/from something we can't shrink, we need
1249 * to leave components and array_len of any wildcards alone.
1250 */
1251 nir_foreach_variable(var, vars) {
1252 struct vec_var_usage *usage =
1253 get_vec_var_usage(var, var_usage_map, false, NULL);
1254 if (!usage)
1255 continue;
1256
1257 assert(usage->comps_kept == 0);
1258 if (usage->has_external_copy || usage->has_complex_use)
1259 usage->comps_kept = usage->all_comps;
1260 else
1261 usage->comps_kept = usage->comps_read & usage->comps_written;
1262
1263 for (unsigned i = 0; i < usage->num_levels; i++) {
1264 struct array_level_usage *level = &usage->levels[i];
1265 assert(level->array_len > 0);
1266
1267 if (level->max_written == UINT_MAX || level->has_external_copy ||
1268 usage->has_complex_use)
1269 continue; /* Can't shrink */
1270
1271 unsigned max_used = MIN2(level->max_read, level->max_written);
1272 level->array_len = MIN2(max_used, level->array_len - 1) + 1;
1273 }
1274 }
1275
1276 /* In order for variable copies to work, we have to have the same data type
1277 * on the source and the destination. In order to satisfy this, we run a
1278 * little fixed-point algorithm to transitively ensure that we get enough
1279 * components and array elements for this to hold for all copies.
1280 */
1281 bool fp_progress;
1282 do {
1283 fp_progress = false;
1284 nir_foreach_variable(var, vars) {
1285 struct vec_var_usage *var_usage =
1286 get_vec_var_usage(var, var_usage_map, false, NULL);
1287 if (!var_usage || !var_usage->vars_copied)
1288 continue;
1289
1290 set_foreach(var_usage->vars_copied, copy_entry) {
1291 struct vec_var_usage *copy_usage = (void *)copy_entry->key;
1292 if (copy_usage->comps_kept != var_usage->comps_kept) {
1293 nir_component_mask_t comps_kept =
1294 (var_usage->comps_kept | copy_usage->comps_kept);
1295 var_usage->comps_kept = comps_kept;
1296 copy_usage->comps_kept = comps_kept;
1297 fp_progress = true;
1298 }
1299 }
1300
1301 for (unsigned i = 0; i < var_usage->num_levels; i++) {
1302 struct array_level_usage *var_level = &var_usage->levels[i];
1303 if (!var_level->levels_copied)
1304 continue;
1305
1306 set_foreach(var_level->levels_copied, copy_entry) {
1307 struct array_level_usage *copy_level = (void *)copy_entry->key;
1308 if (var_level->array_len != copy_level->array_len) {
1309 unsigned array_len =
1310 MAX2(var_level->array_len, copy_level->array_len);
1311 var_level->array_len = array_len;
1312 copy_level->array_len = array_len;
1313 fp_progress = true;
1314 }
1315 }
1316 }
1317 }
1318 } while (fp_progress);
1319
1320 bool vars_shrunk = false;
1321 nir_foreach_variable_safe(var, vars) {
1322 struct vec_var_usage *usage =
1323 get_vec_var_usage(var, var_usage_map, false, NULL);
1324 if (!usage)
1325 continue;
1326
1327 bool shrunk = false;
1328 const struct glsl_type *vec_type = var->type;
1329 for (unsigned i = 0; i < usage->num_levels; i++) {
1330 /* If we've reduced the array to zero elements at some level, just
1331 * set comps_kept to 0 and delete the variable.
1332 */
1333 if (usage->levels[i].array_len == 0) {
1334 usage->comps_kept = 0;
1335 break;
1336 }
1337
1338 assert(usage->levels[i].array_len <= glsl_get_length(vec_type));
1339 if (usage->levels[i].array_len < glsl_get_length(vec_type))
1340 shrunk = true;
1341 vec_type = glsl_get_array_element(vec_type);
1342 }
1343 assert(glsl_type_is_vector_or_scalar(vec_type));
1344
1345 assert(usage->comps_kept == (usage->comps_kept & usage->all_comps));
1346 if (usage->comps_kept != usage->all_comps)
1347 shrunk = true;
1348
1349 if (usage->comps_kept == 0) {
1350 /* This variable is dead, remove it */
1351 vars_shrunk = true;
1352 exec_node_remove(&var->node);
1353 continue;
1354 }
1355
1356 if (!shrunk) {
1357 /* This variable doesn't need to be shrunk. Remove it from the
1358 * hash table so later steps will ignore it.
1359 */
1360 _mesa_hash_table_remove_key(var_usage_map, var);
1361 continue;
1362 }
1363
1364 /* Build the new var type */
1365 unsigned new_num_comps = util_bitcount(usage->comps_kept);
1366 const struct glsl_type *new_type =
1367 glsl_vector_type(glsl_get_base_type(vec_type), new_num_comps);
1368 for (int i = usage->num_levels - 1; i >= 0; i--) {
1369 assert(usage->levels[i].array_len > 0);
1370 /* If the original type was a matrix type, we'd like to keep that so
1371 * we don't convert matrices into arrays.
1372 */
1373 if (i == usage->num_levels - 1 &&
1374 glsl_type_is_matrix(glsl_without_array(var->type)) &&
1375 new_num_comps > 1 && usage->levels[i].array_len > 1) {
1376 new_type = glsl_matrix_type(glsl_get_base_type(new_type),
1377 new_num_comps,
1378 usage->levels[i].array_len);
1379 } else {
1380 new_type = glsl_array_type(new_type, usage->levels[i].array_len, 0);
1381 }
1382 }
1383 var->type = new_type;
1384
1385 vars_shrunk = true;
1386 }
1387
1388 return vars_shrunk;
1389 }
1390
1391 static bool
1392 vec_deref_is_oob(nir_deref_instr *deref,
1393 struct vec_var_usage *usage)
1394 {
1395 nir_deref_path path;
1396 nir_deref_path_init(&path, deref, NULL);
1397
1398 bool oob = false;
1399 for (unsigned i = 0; i < usage->num_levels; i++) {
1400 nir_deref_instr *p = path.path[i + 1];
1401 if (p->deref_type == nir_deref_type_array_wildcard)
1402 continue;
1403
1404 if (nir_src_is_const(p->arr.index) &&
1405 nir_src_as_uint(p->arr.index) >= usage->levels[i].array_len) {
1406 oob = true;
1407 break;
1408 }
1409 }
1410
1411 nir_deref_path_finish(&path);
1412
1413 return oob;
1414 }
1415
1416 static bool
1417 vec_deref_is_dead_or_oob(nir_deref_instr *deref,
1418 struct hash_table *var_usage_map,
1419 nir_variable_mode modes)
1420 {
1421 struct vec_var_usage *usage =
1422 get_vec_deref_usage(deref, var_usage_map, modes, false, NULL);
1423 if (!usage)
1424 return false;
1425
1426 return usage->comps_kept == 0 || vec_deref_is_oob(deref, usage);
1427 }
1428
1429 static void
1430 shrink_vec_var_access_impl(nir_function_impl *impl,
1431 struct hash_table *var_usage_map,
1432 nir_variable_mode modes)
1433 {
1434 nir_builder b;
1435 nir_builder_init(&b, impl);
1436
1437 nir_foreach_block(block, impl) {
1438 nir_foreach_instr_safe(instr, block) {
1439 switch (instr->type) {
1440 case nir_instr_type_deref: {
1441 nir_deref_instr *deref = nir_instr_as_deref(instr);
1442 if (!(deref->mode & modes))
1443 break;
1444
1445 /* Clean up any dead derefs we find lying around. They may refer
1446 * to variables we've deleted.
1447 */
1448 if (nir_deref_instr_remove_if_unused(deref))
1449 break;
1450
1451 /* Update the type in the deref to keep the types consistent as
1452 * you walk down the chain. We don't need to check if this is one
1453 * of the derefs we're shrinking because this is a no-op if it
1454 * isn't. The worst that could happen is that we accidentally fix
1455 * an invalid deref.
1456 */
1457 if (deref->deref_type == nir_deref_type_var) {
1458 deref->type = deref->var->type;
1459 } else if (deref->deref_type == nir_deref_type_array ||
1460 deref->deref_type == nir_deref_type_array_wildcard) {
1461 nir_deref_instr *parent = nir_deref_instr_parent(deref);
1462 assert(glsl_type_is_array(parent->type) ||
1463 glsl_type_is_matrix(parent->type));
1464 deref->type = glsl_get_array_element(parent->type);
1465 }
1466 break;
1467 }
1468
1469 case nir_instr_type_intrinsic: {
1470 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1471
1472 /* If we have a copy whose source or destination has been deleted
1473 * because we determined the variable was dead, then we just
1474 * delete the copy instruction. If the source variable was dead
1475 * then it was writing undefined garbage anyway and if it's the
1476 * destination variable that's dead then the write isn't needed.
1477 */
1478 if (intrin->intrinsic == nir_intrinsic_copy_deref) {
1479 nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
1480 nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
1481 if (vec_deref_is_dead_or_oob(dst, var_usage_map, modes) ||
1482 vec_deref_is_dead_or_oob(src, var_usage_map, modes)) {
1483 nir_instr_remove(&intrin->instr);
1484 nir_deref_instr_remove_if_unused(dst);
1485 nir_deref_instr_remove_if_unused(src);
1486 }
1487 continue;
1488 }
1489
1490 if (intrin->intrinsic != nir_intrinsic_load_deref &&
1491 intrin->intrinsic != nir_intrinsic_store_deref)
1492 continue;
1493
1494 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1495 if (!(deref->mode & modes))
1496 continue;
1497
1498 struct vec_var_usage *usage =
1499 get_vec_deref_usage(deref, var_usage_map, modes, false, NULL);
1500 if (!usage)
1501 continue;
1502
1503 if (usage->comps_kept == 0 || vec_deref_is_oob(deref, usage)) {
1504 if (intrin->intrinsic == nir_intrinsic_load_deref) {
1505 nir_ssa_def *u =
1506 nir_ssa_undef(&b, intrin->dest.ssa.num_components,
1507 intrin->dest.ssa.bit_size);
1508 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
1509 nir_src_for_ssa(u));
1510 }
1511 nir_instr_remove(&intrin->instr);
1512 nir_deref_instr_remove_if_unused(deref);
1513 continue;
1514 }
1515
1516 /* If we're not dropping any components, there's no need to
1517 * compact vectors.
1518 */
1519 if (usage->comps_kept == usage->all_comps)
1520 continue;
1521
1522 if (intrin->intrinsic == nir_intrinsic_load_deref) {
1523 b.cursor = nir_after_instr(&intrin->instr);
1524
1525 nir_ssa_def *undef =
1526 nir_ssa_undef(&b, 1, intrin->dest.ssa.bit_size);
1527 nir_ssa_def *vec_srcs[NIR_MAX_VEC_COMPONENTS];
1528 unsigned c = 0;
1529 for (unsigned i = 0; i < intrin->num_components; i++) {
1530 if (usage->comps_kept & (1u << i))
1531 vec_srcs[i] = nir_channel(&b, &intrin->dest.ssa, c++);
1532 else
1533 vec_srcs[i] = undef;
1534 }
1535 nir_ssa_def *vec = nir_vec(&b, vec_srcs, intrin->num_components);
1536
1537 nir_ssa_def_rewrite_uses_after(&intrin->dest.ssa,
1538 nir_src_for_ssa(vec),
1539 vec->parent_instr);
1540
1541 /* The SSA def is now only used by the swizzle. It's safe to
1542 * shrink the number of components.
1543 */
1544 assert(list_length(&intrin->dest.ssa.uses) == c);
1545 intrin->num_components = c;
1546 intrin->dest.ssa.num_components = c;
1547 } else {
1548 nir_component_mask_t write_mask =
1549 nir_intrinsic_write_mask(intrin);
1550
1551 unsigned swizzle[NIR_MAX_VEC_COMPONENTS];
1552 nir_component_mask_t new_write_mask = 0;
1553 unsigned c = 0;
1554 for (unsigned i = 0; i < intrin->num_components; i++) {
1555 if (usage->comps_kept & (1u << i)) {
1556 swizzle[c] = i;
1557 if (write_mask & (1u << i))
1558 new_write_mask |= 1u << c;
1559 c++;
1560 }
1561 }
1562
1563 b.cursor = nir_before_instr(&intrin->instr);
1564
1565 nir_ssa_def *swizzled =
1566 nir_swizzle(&b, intrin->src[1].ssa, swizzle, c);
1567
1568 /* Rewrite to use the compacted source */
1569 nir_instr_rewrite_src(&intrin->instr, &intrin->src[1],
1570 nir_src_for_ssa(swizzled));
1571 nir_intrinsic_set_write_mask(intrin, new_write_mask);
1572 intrin->num_components = c;
1573 }
1574 break;
1575 }
1576
1577 default:
1578 break;
1579 }
1580 }
1581 }
1582 }
1583
1584 static bool
1585 function_impl_has_vars_with_modes(nir_function_impl *impl,
1586 nir_variable_mode modes)
1587 {
1588 nir_shader *shader = impl->function->shader;
1589
1590 if ((modes & nir_var_shader_temp) && !exec_list_is_empty(&shader->globals))
1591 return true;
1592
1593 if ((modes & nir_var_function_temp) && !exec_list_is_empty(&impl->locals))
1594 return true;
1595
1596 return false;
1597 }
1598
1599 /** Attempt to shrink arrays of vectors
1600 *
1601 * This pass looks at variables which contain a vector or an array (possibly
1602 * multiple dimensions) of vectors and attempts to lower to a smaller vector
1603 * or array. If the pass can prove that a component of a vector (or array of
1604 * vectors) is never really used, then that component will be removed.
1605 * Similarly, the pass attempts to shorten arrays based on what elements it
1606 * can prove are never read or never contain valid data.
1607 */
1608 bool
1609 nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes)
1610 {
1611 assert((modes & (nir_var_shader_temp | nir_var_function_temp)) == modes);
1612
1613 void *mem_ctx = ralloc_context(NULL);
1614
1615 struct hash_table *var_usage_map =
1616 _mesa_pointer_hash_table_create(mem_ctx);
1617
1618 bool has_vars_to_shrink = false;
1619 nir_foreach_function(function, shader) {
1620 if (!function->impl)
1621 continue;
1622
1623 /* Don't even bother crawling the IR if we don't have any variables.
1624 * Given that this pass deletes any unused variables, it's likely that
1625 * we will be in this scenario eventually.
1626 */
1627 if (function_impl_has_vars_with_modes(function->impl, modes)) {
1628 has_vars_to_shrink = true;
1629 find_used_components_impl(function->impl, var_usage_map,
1630 modes, mem_ctx);
1631 }
1632 }
1633 if (!has_vars_to_shrink) {
1634 ralloc_free(mem_ctx);
1635 return false;
1636 }
1637
1638 bool globals_shrunk = false;
1639 if (modes & nir_var_shader_temp)
1640 globals_shrunk = shrink_vec_var_list(&shader->globals, var_usage_map);
1641
1642 bool progress = false;
1643 nir_foreach_function(function, shader) {
1644 if (!function->impl)
1645 continue;
1646
1647 bool locals_shrunk = false;
1648 if (modes & nir_var_function_temp) {
1649 locals_shrunk = shrink_vec_var_list(&function->impl->locals,
1650 var_usage_map);
1651 }
1652
1653 if (globals_shrunk || locals_shrunk) {
1654 shrink_vec_var_access_impl(function->impl, var_usage_map, modes);
1655
1656 nir_metadata_preserve(function->impl, nir_metadata_block_index |
1657 nir_metadata_dominance);
1658 progress = true;
1659 }
1660 }
1661
1662 ralloc_free(mem_ctx);
1663
1664 return progress;
1665 }