• 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 ** vertex_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 **vertex_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 per-vertex input arrays (i.e. geometry shader inputs), skip the
47     * outermost array index.  Process the rest normally.
48     */
49    if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
50       *vertex_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_per_vertex_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                                   nir_src_for_ssa(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 *vertex_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                                       &vertex_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_per_vertex_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_per_vertex_io(var, b->shader->info.stage)) {
168       assert(vertex_index);
169       element_deref = nir_build_deref_array(b, element_deref, vertex_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                       &element_intr->instr);
186       }
187 
188       nir_ssa_def_rewrite_uses(&intr->dest.ssa,
189                                nir_src_for_ssa(&element_intr->dest.ssa));
190    } else {
191       nir_intrinsic_set_write_mask(element_intr,
192                                    nir_intrinsic_write_mask(intr));
193       nir_src_copy(&element_intr->src[1], &intr->src[1],
194                    &element_intr->instr);
195    }
196 
197    nir_builder_instr_insert(b, &element_intr->instr);
198 
199    /* Remove the old load intrinsic */
200    nir_instr_remove(&intr->instr);
201 }
202 
203 static bool
deref_has_indirect(nir_builder * b,nir_variable * var,nir_deref_path * path)204 deref_has_indirect(nir_builder *b, nir_variable *var, nir_deref_path *path)
205 {
206    assert(path->path[0]->deref_type == nir_deref_type_var);
207    nir_deref_instr **p = &path->path[1];
208 
209    if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
210       p++;
211    }
212 
213    for (; *p; p++) {
214       if ((*p)->deref_type != nir_deref_type_array)
215          continue;
216 
217       if (!nir_src_is_const((*p)->arr.index))
218          return true;
219    }
220 
221    return false;
222 }
223 
224 /* Creates a mask of locations that contains arrays that are indexed via
225  * indirect indexing.
226  */
227 static void
create_indirects_mask(nir_shader * shader,BITSET_WORD * indirects,nir_variable_mode mode)228 create_indirects_mask(nir_shader *shader,
229                       BITSET_WORD *indirects, nir_variable_mode mode)
230 {
231    nir_foreach_function(function, shader) {
232       if (function->impl) {
233          nir_builder b;
234          nir_builder_init(&b, function->impl);
235 
236          nir_foreach_block(block, function->impl) {
237             nir_foreach_instr_safe(instr, block) {
238 
239                if (instr->type != nir_instr_type_intrinsic)
240                   continue;
241 
242                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
243 
244                if (intr->intrinsic != nir_intrinsic_load_deref &&
245                    intr->intrinsic != nir_intrinsic_store_deref &&
246                    intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
247                    intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
248                    intr->intrinsic != nir_intrinsic_interp_deref_at_offset &&
249                    intr->intrinsic != nir_intrinsic_interp_deref_at_vertex)
250                   continue;
251 
252                nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
253                if (!nir_deref_mode_is(deref, mode))
254                   continue;
255 
256                nir_variable *var = nir_deref_instr_get_variable(deref);
257 
258                nir_deref_path path;
259                nir_deref_path_init(&path, deref, NULL);
260 
261                int loc = var->data.location * 4 + var->data.location_frac;
262                if (deref_has_indirect(&b, var, &path))
263                   BITSET_SET(indirects, loc);
264 
265                nir_deref_path_finish(&path);
266             }
267          }
268       }
269    }
270 }
271 
272 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)273 lower_io_arrays_to_elements(nir_shader *shader, nir_variable_mode mask,
274                             BITSET_WORD *indirects,
275                             struct hash_table *varyings,
276                             bool after_cross_stage_opts)
277 {
278    nir_foreach_function(function, shader) {
279       if (function->impl) {
280          nir_builder b;
281          nir_builder_init(&b, function->impl);
282 
283          nir_foreach_block(block, function->impl) {
284             nir_foreach_instr_safe(instr, block) {
285                if (instr->type != nir_instr_type_intrinsic)
286                   continue;
287 
288                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
289 
290                if (intr->intrinsic != nir_intrinsic_load_deref &&
291                    intr->intrinsic != nir_intrinsic_store_deref &&
292                    intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
293                    intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
294                    intr->intrinsic != nir_intrinsic_interp_deref_at_offset &&
295                    intr->intrinsic != nir_intrinsic_interp_deref_at_vertex)
296                   continue;
297 
298                nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
299                if (!nir_deref_mode_is_one_of(deref, mask))
300                   continue;
301 
302                nir_variable *var = nir_deref_instr_get_variable(deref);
303 
304                /* Drivers assume compact arrays are, in fact, arrays. */
305                if (var->data.compact)
306                   continue;
307 
308                /* Per-view variables are expected to remain arrays. */
309                if (var->data.per_view)
310                   continue;
311 
312                /* Skip indirects */
313                int loc = var->data.location * 4 + var->data.location_frac;
314                if (BITSET_TEST(indirects, loc))
315                   continue;
316 
317                nir_variable_mode mode = var->data.mode;
318 
319                const struct glsl_type *type = var->type;
320                if (nir_is_per_vertex_io(var, b.shader->info.stage)) {
321                   assert(glsl_type_is_array(type));
322                   type = glsl_get_array_element(type);
323                }
324 
325                /* Skip types we cannot split.
326                 *
327                 * TODO: Add support for struct splitting.
328                 */
329                if ((!glsl_type_is_array(type) && !glsl_type_is_matrix(type))||
330                    glsl_type_is_struct_or_ifc(glsl_without_array(type)))
331                   continue;
332 
333                /* Skip builtins */
334                if (!after_cross_stage_opts &&
335                    var->data.location < VARYING_SLOT_VAR0 &&
336                    var->data.location >= 0)
337                   continue;
338 
339                /* Don't bother splitting if we can't opt away any unused
340                 * elements.
341                 */
342                if (!after_cross_stage_opts && var->data.always_active_io)
343                   continue;
344 
345                switch (intr->intrinsic) {
346                case nir_intrinsic_interp_deref_at_centroid:
347                case nir_intrinsic_interp_deref_at_sample:
348                case nir_intrinsic_interp_deref_at_offset:
349                case nir_intrinsic_interp_deref_at_vertex:
350                case nir_intrinsic_load_deref:
351                case nir_intrinsic_store_deref:
352                   if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
353                       (mask & nir_var_shader_out && mode == nir_var_shader_out))
354                      lower_array(&b, intr, var, varyings);
355                   break;
356                default:
357                   break;
358                }
359             }
360          }
361       }
362    }
363 }
364 
365 void
nir_lower_io_arrays_to_elements_no_indirects(nir_shader * shader,bool outputs_only)366 nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
367                                              bool outputs_only)
368 {
369    struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
370    struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
371 
372    BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = {0};
373 
374    lower_io_arrays_to_elements(shader, nir_var_shader_out,
375                                indirects, split_outputs, true);
376 
377    if (!outputs_only) {
378       lower_io_arrays_to_elements(shader, nir_var_shader_in,
379                                   indirects, split_inputs, true);
380 
381       /* Remove old input from the shaders inputs list */
382       hash_table_foreach(split_inputs, entry) {
383          nir_variable *var = (nir_variable *) entry->key;
384          exec_node_remove(&var->node);
385 
386          free(entry->data);
387       }
388    }
389 
390    /* Remove old output from the shaders outputs list */
391    hash_table_foreach(split_outputs, entry) {
392       nir_variable *var = (nir_variable *) entry->key;
393       exec_node_remove(&var->node);
394 
395       free(entry->data);
396    }
397 
398    _mesa_hash_table_destroy(split_inputs, NULL);
399    _mesa_hash_table_destroy(split_outputs, NULL);
400 
401    nir_remove_dead_derefs(shader);
402 }
403 
404 void
nir_lower_io_arrays_to_elements(nir_shader * producer,nir_shader * consumer)405 nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer)
406 {
407    struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
408    struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
409 
410    BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = {0};
411 
412    create_indirects_mask(producer, indirects, nir_var_shader_out);
413    create_indirects_mask(consumer, indirects, nir_var_shader_in);
414 
415    lower_io_arrays_to_elements(producer, nir_var_shader_out,
416                                indirects, split_outputs, false);
417 
418    lower_io_arrays_to_elements(consumer, nir_var_shader_in,
419                                indirects, split_inputs, false);
420 
421    /* Remove old input from the shaders inputs list */
422    hash_table_foreach(split_inputs, entry) {
423       nir_variable *var = (nir_variable *) entry->key;
424       exec_node_remove(&var->node);
425 
426       free(entry->data);
427    }
428 
429    /* Remove old output from the shaders outputs list */
430    hash_table_foreach(split_outputs, entry) {
431       nir_variable *var = (nir_variable *) entry->key;
432       exec_node_remove(&var->node);
433 
434       free(entry->data);
435    }
436 
437    _mesa_hash_table_destroy(split_inputs, NULL);
438    _mesa_hash_table_destroy(split_outputs, NULL);
439 
440    nir_remove_dead_derefs(producer);
441    nir_remove_dead_derefs(consumer);
442 }
443