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