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