nir_lower_io_arrays_to_elements.c 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright © 2017 Timothy Arceri
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "nir.h"
#include "nir_builder.h"
26
#include "nir_deref.h"
27
28
29
30
31
32
33
34
35

/** @file nir_lower_io_arrays_to_elements.c
 *
 * Split arrays/matrices with direct indexing into individual elements. This
 * will allow optimisation passes to better clean up unused elements.
 *
 */

static unsigned
36
get_io_offset(nir_builder *b, nir_deref_instr *deref, nir_variable *var,
37
38
              unsigned *element_index, unsigned *xfb_offset,
              nir_ssa_def **vertex_index)
39
{
40
41
42
43
44
   nir_deref_path path;
   nir_deref_path_init(&path, deref, NULL);

   assert(path.path[0]->deref_type == nir_deref_type_var);
   nir_deref_instr **p = &path.path[1];
45
46
47
48
49

   /* For per-vertex input arrays (i.e. geometry shader inputs), skip the
    * outermost array index.  Process the rest normally.
    */
   if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
50
51
      *vertex_index = nir_ssa_for_src(b, (*p)->arr.index, 1);
      p++;
52
53
54
   }

   unsigned offset = 0;
55
   *xfb_offset = 0;
56
57
   for (; *p; p++) {
      if ((*p)->deref_type == nir_deref_type_array) {
58
59
         /* must not be indirect dereference */
         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
65
         xfb_offset += index * glsl_get_component_slots((*p)->type) * 4;

66
67
         unsigned num_elements = glsl_type_is_array((*p)->type) ?
            glsl_get_aoa_size((*p)->type) : 1;
68

69
70
         num_elements *= glsl_type_is_matrix(glsl_without_array((*p)->type)) ?
            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
75
76
77
78
         /* TODO: we could also add struct splitting support to this pass */
         break;
      }
   }

79
80
   nir_deref_path_finish(&path);

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
   return offset;
}

static nir_variable **
get_array_elements(struct hash_table *ht, nir_variable *var,
                   gl_shader_stage stage)
{
   nir_variable **elements;
   struct hash_entry *entry = _mesa_hash_table_search(ht, var);
   if (!entry) {
      const struct glsl_type *type = var->type;
      if (nir_is_per_vertex_io(var, stage)) {
         assert(glsl_type_is_array(type));
         type = glsl_get_array_element(type);
      }

      unsigned num_elements = glsl_type_is_array(type) ?
         glsl_get_aoa_size(type) : 1;

      num_elements *= glsl_type_is_matrix(glsl_without_array(type)) ?
         glsl_get_matrix_columns(glsl_without_array(type)) : 1;

      elements = (nir_variable **) calloc(num_elements, sizeof(nir_variable *));
      _mesa_hash_table_insert(ht, var, elements);
   } else {
      elements = (nir_variable **) entry->data;
   }

   return elements;
}

static void
lower_array(nir_builder *b, nir_intrinsic_instr *intr, nir_variable *var,
            struct hash_table *varyings)
{
   b->cursor = nir_before_instr(&intr->instr);

   nir_variable **elements =
      get_array_elements(varyings, var, b->shader->info.stage);

121
   nir_ssa_def *vertex_index = NULL;
122
   unsigned elements_index = 0;
123
   unsigned xfb_offset = 0;
124
   unsigned io_offset = get_io_offset(b, nir_src_as_deref(intr->src[0]),
125
126
                                      var, &elements_index, &xfb_offset,
                                      &vertex_index);
127
128
129
130
131
132

   nir_variable *element = elements[elements_index];
   if (!element) {
         element = nir_variable_clone(var, b->shader);
         element->data.location =  var->data.location + io_offset;

133
134
135
         if (var->data.explicit_offset)
            element->data.offset = var->data.offset + xfb_offset;

136
137
138
         const struct glsl_type *type = glsl_without_array(element->type);

         /* This pass also splits matrices so we need give them a new type. */
139
140
         if (glsl_type_is_matrix(type))
            type = glsl_get_column_type(type);
141
142

         if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
143
144
            type = glsl_array_type(type, glsl_get_length(element->type),
                                   glsl_get_explicit_stride(element->type));
145
146
147
148
149
150
151
152
         }

         element->type = type;
         elements[elements_index] = element;

         nir_shader_add_variable(b->shader, element);
   }

153
154
155
156
157
158
159
   nir_deref_instr *element_deref = nir_build_deref_var(b, element);

   if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
      assert(vertex_index);
      element_deref = nir_build_deref_array(b, element_deref, vertex_index);
   }

160
161
162
   nir_intrinsic_instr *element_intr =
      nir_intrinsic_instr_create(b->shader, intr->intrinsic);
   element_intr->num_components = intr->num_components;
163
   element_intr->src[0] = nir_src_for_ssa(&element_deref->dest.ssa);
164

165
   if (intr->intrinsic != nir_intrinsic_store_deref) {
166
167
168
      nir_ssa_dest_init(&element_intr->instr, &element_intr->dest,
                        intr->num_components, intr->dest.ssa.bit_size, NULL);

169
170
171
      if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset ||
          intr->intrinsic == nir_intrinsic_interp_deref_at_sample) {
         nir_src_copy(&element_intr->src[1], &intr->src[1],
172
173
174
175
176
177
178
179
                      &element_intr->instr);
      }

      nir_ssa_def_rewrite_uses(&intr->dest.ssa,
                               nir_src_for_ssa(&element_intr->dest.ssa));
   } else {
      nir_intrinsic_set_write_mask(element_intr,
                                   nir_intrinsic_write_mask(intr));
180
      nir_src_copy(&element_intr->src[1], &intr->src[1],
181
182
183
184
185
186
187
188
189
190
                   &element_intr->instr);
   }

   nir_builder_instr_insert(b, &element_intr->instr);

   /* Remove the old load intrinsic */
   nir_instr_remove(&intr->instr);
}

static bool
191
deref_has_indirect(nir_builder *b, nir_variable *var, nir_deref_path *path)
192
{
193
194
   assert(path->path[0]->deref_type == nir_deref_type_var);
   nir_deref_instr **p = &path->path[1];
195
196

   if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
197
      p++;
198
199
   }

200
201
   for (; *p; p++) {
      if ((*p)->deref_type != nir_deref_type_array)
202
203
         continue;

204
      if (!nir_src_is_const((*p)->arr.index))
205
206
207
208
209
210
211
212
213
214
         return true;
   }

   return false;
}

/* Creates a mask of locations that contains arrays that are indexed via
 * indirect indexing.
 */
static void
215
216
create_indirects_mask(nir_shader *shader,
                      BITSET_WORD *indirects, nir_variable_mode mode)
217
218
219
220
221
222
223
224
225
226
227
228
229
230
{
   nir_foreach_function(function, shader) {
      if (function->impl) {
         nir_builder b;
         nir_builder_init(&b, function->impl);

         nir_foreach_block(block, function->impl) {
            nir_foreach_instr_safe(instr, block) {

               if (instr->type != nir_instr_type_intrinsic)
                  continue;

               nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);

231
232
233
234
235
               if (intr->intrinsic != nir_intrinsic_load_deref &&
                   intr->intrinsic != nir_intrinsic_store_deref &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
236
237
                  continue;

238
               nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
239
               if (deref->mode != mode)
240
241
                  continue;

242
243
               nir_variable *var = nir_deref_instr_get_variable(deref);

244
245
246
               nir_deref_path path;
               nir_deref_path_init(&path, deref, NULL);

247
248
249
               int loc = var->data.location * 4 + var->data.location_frac;
               if (deref_has_indirect(&b, var, &path))
                  BITSET_SET(indirects, loc);
250
251

               nir_deref_path_finish(&path);
252
253
254
255
256
257
258
259
            }
         }
      }
   }
}

static void
lower_io_arrays_to_elements(nir_shader *shader, nir_variable_mode mask,
260
                            BITSET_WORD *indirects,
261
262
                            struct hash_table *varyings,
                            bool after_cross_stage_opts)
263
264
265
266
267
268
269
270
271
272
273
274
275
{
   nir_foreach_function(function, shader) {
      if (function->impl) {
         nir_builder b;
         nir_builder_init(&b, function->impl);

         nir_foreach_block(block, function->impl) {
            nir_foreach_instr_safe(instr, block) {
               if (instr->type != nir_instr_type_intrinsic)
                  continue;

               nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);

276
277
278
279
280
               if (intr->intrinsic != nir_intrinsic_load_deref &&
                   intr->intrinsic != nir_intrinsic_store_deref &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
                   intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
281
282
                  continue;

283
284
285
286
287
               nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
               if (!(deref->mode & mask))
                  continue;

               nir_variable *var = nir_deref_instr_get_variable(deref);
288

289
290
291
292
               /* Drivers assume compact arrays are, in fact, arrays. */
               if (var->data.compact)
                  continue;

293
               /* Skip indirects */
294
295
296
               int loc = var->data.location * 4 + var->data.location_frac;
               if (BITSET_TEST(indirects, loc))
                  continue;
297
298
299
300
301
302
303
304
305
306
307
308
309
310

               nir_variable_mode mode = var->data.mode;

               const struct glsl_type *type = var->type;
               if (nir_is_per_vertex_io(var, b.shader->info.stage)) {
                  assert(glsl_type_is_array(type));
                  type = glsl_get_array_element(type);
               }

               /* Skip types we cannot split.
                *
                * TODO: Add support for struct splitting.
                */
               if ((!glsl_type_is_array(type) && !glsl_type_is_matrix(type))||
311
                   glsl_type_is_struct_or_ifc(glsl_without_array(type)))
312
313
                  continue;

314
315
316
               /* Skip builtins */
               if (!after_cross_stage_opts &&
                   var->data.location < VARYING_SLOT_VAR0 &&
317
318
319
320
321
322
                   var->data.location >= 0)
                  continue;

               /* Don't bother splitting if we can't opt away any unused
                * elements.
                */
323
               if (!after_cross_stage_opts && var->data.always_active_io)
324
325
326
                  continue;

               switch (intr->intrinsic) {
327
328
329
330
331
               case nir_intrinsic_interp_deref_at_centroid:
               case nir_intrinsic_interp_deref_at_sample:
               case nir_intrinsic_interp_deref_at_offset:
               case nir_intrinsic_load_deref:
               case nir_intrinsic_store_deref:
332
333
334
335
336
337
338
339
340
341
342
343
344
                  if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
                      (mask & nir_var_shader_out && mode == nir_var_shader_out))
                     lower_array(&b, intr, var, varyings);
                  break;
               default:
                  break;
               }
            }
         }
      }
   }
}

345
void
346
347
nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
                                             bool outputs_only)
348
{
349
350
   struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
   struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
351

352
   BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = {0};
353

354
355
   lower_io_arrays_to_elements(shader, nir_var_shader_out,
                               indirects, split_outputs, true);
356

357
   if (!outputs_only) {
358
359
      lower_io_arrays_to_elements(shader, nir_var_shader_in,
                                  indirects, split_inputs, true);
360

361
362
363
364
      /* Remove old input from the shaders inputs list */
      hash_table_foreach(split_inputs, entry) {
         nir_variable *var = (nir_variable *) entry->key;
         exec_node_remove(&var->node);
365

366
367
         free(entry->data);
      }
368
369
370
371
372
373
374
375
376
377
378
379
   }

   /* Remove old output from the shaders outputs list */
   hash_table_foreach(split_outputs, entry) {
      nir_variable *var = (nir_variable *) entry->key;
      exec_node_remove(&var->node);

      free(entry->data);
   }

   _mesa_hash_table_destroy(split_inputs, NULL);
   _mesa_hash_table_destroy(split_outputs, NULL);
380
381

   nir_remove_dead_derefs(shader);
382
383
}

384
385
386
void
nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer)
{
387
388
   struct hash_table *split_inputs = _mesa_pointer_hash_table_create(NULL);
   struct hash_table *split_outputs = _mesa_pointer_hash_table_create(NULL);
389

390
   BITSET_DECLARE(indirects, 4 * VARYING_SLOT_TESS_MAX) = {0};
391
392
393

   create_indirects_mask(producer, indirects, nir_var_shader_out);
   create_indirects_mask(consumer, indirects, nir_var_shader_in);
394

395
396
   lower_io_arrays_to_elements(producer, nir_var_shader_out,
                               indirects, split_outputs, false);
397

398
399
   lower_io_arrays_to_elements(consumer, nir_var_shader_in,
                               indirects, split_inputs, false);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

   /* Remove old input from the shaders inputs list */
   hash_table_foreach(split_inputs, entry) {
      nir_variable *var = (nir_variable *) entry->key;
      exec_node_remove(&var->node);

      free(entry->data);
   }

   /* Remove old output from the shaders outputs list */
   hash_table_foreach(split_outputs, entry) {
      nir_variable *var = (nir_variable *) entry->key;
      exec_node_remove(&var->node);

      free(entry->data);
   }

   _mesa_hash_table_destroy(split_inputs, NULL);
   _mesa_hash_table_destroy(split_outputs, NULL);
419
420
421

   nir_remove_dead_derefs(producer);
   nir_remove_dead_derefs(consumer);
422
}