xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_io_arrays_to_elements.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2017 Timothy Arceri
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 
28 /** @file nir_lower_io_arrays_to_elements.c
29  *
30  * Split arrays/matrices with direct indexing into individual elements. This
31  * will allow optimisation passes to better clean up unused elements.
32  *
33  */
34 
35 static unsigned
get_io_offset(nir_builder * b,nir_deref_instr * deref,nir_variable * var,unsigned * element_index,unsigned * xfb_offset,nir_def ** array_index)36 get_io_offset(nir_builder *b, nir_deref_instr *deref, nir_variable *var,
37               unsigned *element_index, unsigned *xfb_offset,
38               nir_def **array_index)
39 {
40    nir_deref_path path;
41    nir_deref_path_init(&path, deref, NULL);
42 
43    assert(path.path[0]->deref_type == nir_deref_type_var);
44    nir_deref_instr **p = &path.path[1];
45 
46    /* For arrayed I/O (e.g., per-vertex input arrays in geometry shader
47     * inputs), skip the outermost array index.  Process the rest normally.
48     */
49    if (nir_is_arrayed_io(var, b->shader->info.stage)) {
50       *array_index = (*p)->arr.index.ssa;
51       p++;
52    }
53 
54    unsigned offset = 0;
55    *xfb_offset = 0;
56    for (; *p; p++) {
57       if ((*p)->deref_type == nir_deref_type_array) {
58          /* must not be indirect dereference */
59          unsigned index = nir_src_as_uint((*p)->arr.index);
60 
61          unsigned size = glsl_count_attribute_slots((*p)->type, false);
62          offset += size * index;
63 
64          *xfb_offset += index * glsl_get_component_slots((*p)->type) * 4;
65 
66          unsigned num_elements = glsl_type_is_array((*p)->type) ? glsl_get_aoa_size((*p)->type) : 1;
67 
68          num_elements *= glsl_type_is_matrix(glsl_without_array((*p)->type)) ? glsl_get_matrix_columns(glsl_without_array((*p)->type)) : 1;
69 
70          *element_index += num_elements * index;
71       } else if ((*p)->deref_type == nir_deref_type_struct) {
72          /* TODO: we could also add struct splitting support to this pass */
73          break;
74       }
75    }
76 
77    nir_deref_path_finish(&path);
78 
79    return offset;
80 }
81 
82 static nir_variable **
get_array_elements(struct hash_table * ht,nir_variable * var,gl_shader_stage stage)83 get_array_elements(struct hash_table *ht, nir_variable *var,
84                    gl_shader_stage stage)
85 {
86    nir_variable **elements;
87    struct hash_entry *entry = _mesa_hash_table_search(ht, var);
88    if (!entry) {
89       const struct glsl_type *type = var->type;
90       if (nir_is_arrayed_io(var, stage)) {
91          assert(glsl_type_is_array(type));
92          type = glsl_get_array_element(type);
93       }
94 
95       unsigned num_elements = glsl_type_is_array(type) ? glsl_get_aoa_size(type) : 1;
96 
97       num_elements *= glsl_type_is_matrix(glsl_without_array(type)) ? glsl_get_matrix_columns(glsl_without_array(type)) : 1;
98 
99       elements = (nir_variable **)calloc(num_elements, sizeof(nir_variable *));
100       _mesa_hash_table_insert(ht, var, elements);
101    } else {
102       elements = (nir_variable **)entry->data;
103    }
104 
105    return elements;
106 }
107 
108 static void
lower_array(nir_builder * b,nir_intrinsic_instr * intr,nir_variable * var,struct hash_table * varyings)109 lower_array(nir_builder *b, nir_intrinsic_instr *intr, nir_variable *var,
110             struct hash_table *varyings)
111 {
112    b->cursor = nir_before_instr(&intr->instr);
113 
114    if (nir_deref_instr_is_known_out_of_bounds(nir_src_as_deref(intr->src[0]))) {
115       /* See Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 */
116       if (intr->intrinsic != nir_intrinsic_store_deref) {
117          nir_def *zero = nir_imm_zero(b, intr->def.num_components,
118                                       intr->def.bit_size);
119          nir_def_rewrite_uses(&intr->def,
120                               zero);
121       }
122       nir_instr_remove(&intr->instr);
123       return;
124    }
125 
126    nir_variable **elements =
127       get_array_elements(varyings, var, b->shader->info.stage);
128 
129    nir_def *array_index = NULL;
130    unsigned elements_index = 0;
131    unsigned xfb_offset = 0;
132    unsigned io_offset = get_io_offset(b, nir_src_as_deref(intr->src[0]),
133                                       var, &elements_index, &xfb_offset,
134                                       &array_index);
135 
136    nir_variable *element = elements[elements_index];
137    if (!element) {
138       element = nir_variable_clone(var, b->shader);
139       element->data.location = var->data.location + io_offset;
140 
141       if (var->data.explicit_offset)
142          element->data.offset = var->data.offset + xfb_offset;
143 
144       const struct glsl_type *type = glsl_without_array(element->type);
145 
146       /* This pass also splits matrices so we need give them a new type. */
147       if (glsl_type_is_matrix(type))
148          type = glsl_get_column_type(type);
149 
150       if (nir_is_arrayed_io(var, b->shader->info.stage)) {
151          type = glsl_array_type(type, glsl_get_length(element->type),
152                                 glsl_get_explicit_stride(element->type));
153       }
154 
155       element->type = type;
156       elements[elements_index] = element;
157 
158       nir_shader_add_variable(b->shader, element);
159    }
160 
161    nir_deref_instr *element_deref = nir_build_deref_var(b, element);
162 
163    if (nir_is_arrayed_io(var, b->shader->info.stage)) {
164       assert(array_index);
165       element_deref = nir_build_deref_array(b, element_deref, array_index);
166    }
167 
168    nir_intrinsic_instr *element_intr =
169       nir_intrinsic_instr_create(b->shader, intr->intrinsic);
170    element_intr->num_components = intr->num_components;
171    element_intr->src[0] = nir_src_for_ssa(&element_deref->def);
172 
173    if (intr->intrinsic != nir_intrinsic_store_deref) {
174       nir_def_init(&element_intr->instr, &element_intr->def,
175                    intr->num_components, intr->def.bit_size);
176 
177       if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset ||
178           intr->intrinsic == nir_intrinsic_interp_deref_at_sample ||
179           intr->intrinsic == nir_intrinsic_interp_deref_at_vertex) {
180          element_intr->src[1] = nir_src_for_ssa(intr->src[1].ssa);
181       }
182 
183       nir_def_rewrite_uses(&intr->def,
184                            &element_intr->def);
185    } else {
186       nir_intrinsic_set_write_mask(element_intr,
187                                    nir_intrinsic_write_mask(intr));
188       element_intr->src[1] = nir_src_for_ssa(intr->src[1].ssa);
189    }
190 
191    nir_builder_instr_insert(b, &element_intr->instr);
192 
193    /* Remove the old load intrinsic */
194    nir_instr_remove(&intr->instr);
195 }
196 
197 static bool
deref_has_indirect(nir_builder * b,nir_variable * var,nir_deref_path * path)198 deref_has_indirect(nir_builder *b, nir_variable *var, nir_deref_path *path)
199 {
200    assert(path->path[0]->deref_type == nir_deref_type_var);
201    nir_deref_instr **p = &path->path[1];
202 
203    if (nir_is_arrayed_io(var, b->shader->info.stage)) {
204       p++;
205    }
206 
207    for (; *p; p++) {
208       if ((*p)->deref_type != nir_deref_type_array)
209          continue;
210 
211       if (!nir_src_is_const((*p)->arr.index))
212          return true;
213    }
214 
215    return false;
216 }
217 
218 /* Creates a mask of locations that contains arrays that are indexed via
219  * indirect indexing.
220  */
221 static void
create_indirects_mask(nir_shader * shader,BITSET_WORD * indirects,nir_variable_mode mode)222 create_indirects_mask(nir_shader *shader,
223                       BITSET_WORD *indirects, nir_variable_mode mode)
224 {
225    nir_foreach_function_impl(impl, shader) {
226       nir_builder b = nir_builder_create(impl);
227 
228       nir_foreach_block(block, impl) {
229          nir_foreach_instr_safe(instr, block) {
230 
231             if (instr->type != nir_instr_type_intrinsic)
232                continue;
233 
234             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
235 
236             if (intr->intrinsic != nir_intrinsic_load_deref &&
237                 intr->intrinsic != nir_intrinsic_store_deref &&
238                 intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
239                 intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
240                 intr->intrinsic != nir_intrinsic_interp_deref_at_offset &&
241                 intr->intrinsic != nir_intrinsic_interp_deref_at_vertex)
242                continue;
243 
244             nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
245             if (!nir_deref_mode_is(deref, mode))
246                continue;
247 
248             nir_variable *var = nir_deref_instr_get_variable(deref);
249 
250             nir_deref_path path;
251             nir_deref_path_init(&path, deref, NULL);
252 
253             int loc = var->data.location * 4 + var->data.location_frac;
254             if (deref_has_indirect(&b, var, &path))
255                BITSET_SET(indirects, loc);
256 
257             nir_deref_path_finish(&path);
258          }
259       }
260    }
261 }
262 
263 static void
lower_io_arrays_to_elements(nir_shader * shader,nir_variable_mode mask,BITSET_WORD * indirects,struct hash_table * varyings,bool after_cross_stage_opts)264 lower_io_arrays_to_elements(nir_shader *shader, nir_variable_mode mask,
265                             BITSET_WORD *indirects,
266                             struct hash_table *varyings,
267                             bool after_cross_stage_opts)
268 {
269    nir_foreach_function_impl(impl, shader) {
270       nir_builder b = nir_builder_create(impl);
271 
272       nir_foreach_block(block, impl) {
273          nir_foreach_instr_safe(instr, block) {
274             if (instr->type != nir_instr_type_intrinsic)
275                continue;
276 
277             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
278 
279             if (intr->intrinsic != nir_intrinsic_load_deref &&
280                 intr->intrinsic != nir_intrinsic_store_deref &&
281                 intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
282                 intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
283                 intr->intrinsic != nir_intrinsic_interp_deref_at_offset &&
284                 intr->intrinsic != nir_intrinsic_interp_deref_at_vertex)
285                continue;
286 
287             nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
288             if (!nir_deref_mode_is_one_of(deref, mask))
289                continue;
290 
291             nir_variable *var = nir_deref_instr_get_variable(deref);
292 
293             /* Drivers assume compact arrays are, in fact, arrays. */
294             if (var->data.compact)
295                continue;
296 
297             /* Per-view variables are expected to remain arrays. */
298             if (var->data.per_view)
299                continue;
300 
301             /* Skip indirects */
302             int loc = var->data.location * 4 + var->data.location_frac;
303             if (BITSET_TEST(indirects, loc))
304                continue;
305 
306             nir_variable_mode mode = var->data.mode;
307 
308             const struct glsl_type *type = var->type;
309             if (nir_is_arrayed_io(var, b.shader->info.stage)) {
310                assert(glsl_type_is_array(type));
311                type = glsl_get_array_element(type);
312             }
313 
314             /* Skip types we cannot split.
315              *
316              * TODO: Add support for struct splitting.
317              */
318             if ((!glsl_type_is_array(type) && !glsl_type_is_matrix(type)) ||
319                 glsl_type_is_struct_or_ifc(glsl_without_array(type)))
320                continue;
321 
322             /* Skip builtins */
323             if (!after_cross_stage_opts &&
324                 var->data.location < VARYING_SLOT_VAR0 &&
325                 var->data.location >= 0)
326                continue;
327 
328             /* Don't bother splitting if we can't opt away any unused
329              * elements.
330              */
331             if (!after_cross_stage_opts && var->data.always_active_io)
332                continue;
333 
334             switch (intr->intrinsic) {
335             case nir_intrinsic_interp_deref_at_centroid:
336             case nir_intrinsic_interp_deref_at_sample:
337             case nir_intrinsic_interp_deref_at_offset:
338             case nir_intrinsic_interp_deref_at_vertex:
339             case nir_intrinsic_load_deref:
340             case nir_intrinsic_store_deref:
341                if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
342                    (mask & nir_var_shader_out && mode == nir_var_shader_out))
343                   lower_array(&b, intr, var, varyings);
344                break;
345             default:
346                break;
347             }
348          }
349       }
350    }
351 }
352 
353 bool
nir_lower_io_arrays_to_elements_no_indirects(nir_shader * shader,bool outputs_only)354 nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
355                                              bool outputs_only)
356 {
357    struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
358    struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
359 
360    BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = { 0 };
361 
362    lower_io_arrays_to_elements(shader, nir_var_shader_out,
363                                indirects, split_outputs, true);
364 
365    if (!outputs_only) {
366       lower_io_arrays_to_elements(shader, nir_var_shader_in,
367                                   indirects, split_inputs, true);
368 
369       /* Remove old input from the shaders inputs list */
370       hash_table_foreach(split_inputs, entry) {
371          nir_variable *var = (nir_variable *)entry->key;
372          exec_node_remove(&var->node);
373 
374          free(entry->data);
375       }
376    }
377 
378    /* Remove old output from the shaders outputs list */
379    hash_table_foreach(split_outputs, entry) {
380       nir_variable *var = (nir_variable *)entry->key;
381       exec_node_remove(&var->node);
382 
383       free(entry->data);
384    }
385 
386    _mesa_hash_table_destroy(split_inputs, NULL);
387    _mesa_hash_table_destroy(split_outputs, NULL);
388 
389    nir_remove_dead_derefs(shader);
390    return true;
391 }
392 
393 void
nir_lower_io_arrays_to_elements(nir_shader * producer,nir_shader * consumer)394 nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer)
395 {
396    struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
397    struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
398 
399    BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = { 0 };
400 
401    create_indirects_mask(producer, indirects, nir_var_shader_out);
402    create_indirects_mask(consumer, indirects, nir_var_shader_in);
403 
404    lower_io_arrays_to_elements(producer, nir_var_shader_out,
405                                indirects, split_outputs, false);
406 
407    lower_io_arrays_to_elements(consumer, nir_var_shader_in,
408                                indirects, split_inputs, false);
409 
410    /* Remove old input from the shaders inputs list */
411    hash_table_foreach(split_inputs, entry) {
412       nir_variable *var = (nir_variable *)entry->key;
413       exec_node_remove(&var->node);
414 
415       free(entry->data);
416    }
417 
418    /* Remove old output from the shaders outputs list */
419    hash_table_foreach(split_outputs, entry) {
420       nir_variable *var = (nir_variable *)entry->key;
421       exec_node_remove(&var->node);
422 
423       free(entry->data);
424    }
425 
426    _mesa_hash_table_destroy(split_inputs, NULL);
427    _mesa_hash_table_destroy(split_outputs, NULL);
428 
429    nir_remove_dead_derefs(producer);
430    nir_remove_dead_derefs(consumer);
431 }
432