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