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