nir: add deref lowering sanity checking
[mesa.git] / src / compiler / nir / nir_linking_helpers.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 "util/set.h"
26 #include "util/hash_table.h"
27
28 /* This file contains various little helpers for doing simple linking in
29 * NIR. Eventually, we'll probably want a full-blown varying packing
30 * implementation in here. Right now, it just deletes unused things.
31 */
32
33 /**
34 * Returns the bits in the inputs_read, outputs_written, or
35 * system_values_read bitfield corresponding to this variable.
36 */
37 static uint64_t
38 get_variable_io_mask(nir_variable *var, gl_shader_stage stage)
39 {
40 if (var->data.location < 0)
41 return 0;
42
43 unsigned location = var->data.patch ?
44 var->data.location - VARYING_SLOT_PATCH0 : var->data.location;
45
46 assert(var->data.mode == nir_var_shader_in ||
47 var->data.mode == nir_var_shader_out ||
48 var->data.mode == nir_var_system_value);
49 assert(var->data.location >= 0);
50
51 const struct glsl_type *type = var->type;
52 if (nir_is_per_vertex_io(var, stage)) {
53 assert(glsl_type_is_array(type));
54 type = glsl_get_array_element(type);
55 }
56
57 unsigned slots = glsl_count_attribute_slots(type, false);
58 return ((1ull << slots) - 1) << location;
59 }
60
61 static void
62 tcs_add_output_reads(nir_shader *shader, uint64_t *read, uint64_t *patches_read)
63 {
64 nir_foreach_function(function, shader) {
65 if (function->impl) {
66 nir_foreach_block(block, function->impl) {
67 nir_foreach_instr(instr, block) {
68 if (instr->type != nir_instr_type_intrinsic)
69 continue;
70
71 nir_intrinsic_instr *intrin_instr =
72 nir_instr_as_intrinsic(instr);
73 if (intrin_instr->intrinsic == nir_intrinsic_load_var &&
74 intrin_instr->variables[0]->var->data.mode ==
75 nir_var_shader_out) {
76
77 nir_variable *var = intrin_instr->variables[0]->var;
78 if (var->data.patch) {
79 patches_read[var->data.location_frac] |=
80 get_variable_io_mask(intrin_instr->variables[0]->var,
81 shader->info.stage);
82 } else {
83 read[var->data.location_frac] |=
84 get_variable_io_mask(intrin_instr->variables[0]->var,
85 shader->info.stage);
86 }
87 }
88 }
89 }
90 }
91 }
92 }
93
94 static bool
95 remove_unused_io_vars(nir_shader *shader, struct exec_list *var_list,
96 uint64_t *used_by_other_stage,
97 uint64_t *used_by_other_stage_patches)
98 {
99 bool progress = false;
100 uint64_t *used;
101
102 nir_foreach_variable_safe(var, var_list) {
103 if (var->data.patch)
104 used = used_by_other_stage_patches;
105 else
106 used = used_by_other_stage;
107
108 if (var->data.location < VARYING_SLOT_VAR0 && var->data.location >= 0)
109 continue;
110
111 if (var->data.always_active_io)
112 continue;
113
114 uint64_t other_stage = used[var->data.location_frac];
115
116 if (!(other_stage & get_variable_io_mask(var, shader->info.stage))) {
117 /* This one is invalid, make it a global variable instead */
118 var->data.location = 0;
119 var->data.mode = nir_var_global;
120
121 exec_node_remove(&var->node);
122 exec_list_push_tail(&shader->globals, &var->node);
123
124 progress = true;
125 }
126 }
127
128 return progress;
129 }
130
131 bool
132 nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer)
133 {
134 assert(producer->info.stage != MESA_SHADER_FRAGMENT);
135 assert(consumer->info.stage != MESA_SHADER_VERTEX);
136 nir_assert_lowered_derefs(producer, nir_lower_load_store_derefs);
137 nir_assert_lowered_derefs(consumer, nir_lower_load_store_derefs);
138
139 uint64_t read[4] = { 0 }, written[4] = { 0 };
140 uint64_t patches_read[4] = { 0 }, patches_written[4] = { 0 };
141
142 nir_foreach_variable(var, &producer->outputs) {
143 if (var->data.patch) {
144 patches_written[var->data.location_frac] |=
145 get_variable_io_mask(var, producer->info.stage);
146 } else {
147 written[var->data.location_frac] |=
148 get_variable_io_mask(var, producer->info.stage);
149 }
150 }
151
152 nir_foreach_variable(var, &consumer->inputs) {
153 if (var->data.patch) {
154 patches_read[var->data.location_frac] |=
155 get_variable_io_mask(var, consumer->info.stage);
156 } else {
157 read[var->data.location_frac] |=
158 get_variable_io_mask(var, consumer->info.stage);
159 }
160 }
161
162 /* Each TCS invocation can read data written by other TCS invocations,
163 * so even if the outputs are not used by the TES we must also make
164 * sure they are not read by the TCS before demoting them to globals.
165 */
166 if (producer->info.stage == MESA_SHADER_TESS_CTRL)
167 tcs_add_output_reads(producer, read, patches_read);
168
169 bool progress = false;
170 progress = remove_unused_io_vars(producer, &producer->outputs, read,
171 patches_read);
172
173 progress = remove_unused_io_vars(consumer, &consumer->inputs, written,
174 patches_written) || progress;
175
176 return progress;
177 }
178
179 static uint8_t
180 get_interp_type(nir_variable *var, bool default_to_smooth_interp)
181 {
182 if (var->data.interpolation != INTERP_MODE_NONE)
183 return var->data.interpolation;
184 else if (default_to_smooth_interp)
185 return INTERP_MODE_SMOOTH;
186 else
187 return INTERP_MODE_NONE;
188 }
189
190 #define INTERPOLATE_LOC_SAMPLE 0
191 #define INTERPOLATE_LOC_CENTROID 1
192 #define INTERPOLATE_LOC_CENTER 2
193
194 static uint8_t
195 get_interp_loc(nir_variable *var)
196 {
197 if (var->data.sample)
198 return INTERPOLATE_LOC_SAMPLE;
199 else if (var->data.centroid)
200 return INTERPOLATE_LOC_CENTROID;
201 else
202 return INTERPOLATE_LOC_CENTER;
203 }
204
205 static void
206 get_slot_component_masks_and_interp_types(struct exec_list *var_list,
207 uint8_t *comps,
208 uint8_t *interp_type,
209 uint8_t *interp_loc,
210 gl_shader_stage stage,
211 bool default_to_smooth_interp)
212 {
213 nir_foreach_variable_safe(var, var_list) {
214 assert(var->data.location >= 0);
215
216 /* Only remap things that aren't built-ins.
217 * TODO: add TES patch support.
218 */
219 if (var->data.location >= VARYING_SLOT_VAR0 &&
220 var->data.location - VARYING_SLOT_VAR0 < 32) {
221
222 const struct glsl_type *type = var->type;
223 if (nir_is_per_vertex_io(var, stage)) {
224 assert(glsl_type_is_array(type));
225 type = glsl_get_array_element(type);
226 }
227
228 unsigned location = var->data.location - VARYING_SLOT_VAR0;
229 unsigned elements =
230 glsl_get_vector_elements(glsl_without_array(type));
231
232 bool dual_slot = glsl_type_is_dual_slot(glsl_without_array(type));
233 unsigned slots = glsl_count_attribute_slots(type, false);
234 unsigned comps_slot2 = 0;
235 for (unsigned i = 0; i < slots; i++) {
236 interp_type[location + i] =
237 get_interp_type(var, default_to_smooth_interp);
238 interp_loc[location + i] = get_interp_loc(var);
239
240 if (dual_slot) {
241 if (i & 1) {
242 comps[location + i] |= ((1 << comps_slot2) - 1);
243 } else {
244 unsigned num_comps = 4 - var->data.location_frac;
245 comps_slot2 = (elements * 2) - num_comps;
246
247 /* Assume ARB_enhanced_layouts packing rules for doubles */
248 assert(var->data.location_frac == 0 ||
249 var->data.location_frac == 2);
250 assert(comps_slot2 <= 4);
251
252 comps[location + i] |=
253 ((1 << num_comps) - 1) << var->data.location_frac;
254 }
255 } else {
256 comps[location + i] |=
257 ((1 << elements) - 1) << var->data.location_frac;
258 }
259 }
260 }
261 }
262 }
263
264 struct varying_loc
265 {
266 uint8_t component;
267 uint32_t location;
268 };
269
270 static void
271 remap_slots_and_components(struct exec_list *var_list, gl_shader_stage stage,
272 struct varying_loc (*remap)[4],
273 uint64_t *slots_used, uint64_t *out_slots_read)
274 {
275 uint64_t out_slots_read_tmp = 0;
276
277 /* We don't touch builtins so just copy the bitmask */
278 uint64_t slots_used_tmp =
279 *slots_used & (((uint64_t)1 << (VARYING_SLOT_VAR0 - 1)) - 1);
280
281 nir_foreach_variable(var, var_list) {
282 assert(var->data.location >= 0);
283
284 /* Only remap things that aren't built-ins */
285 if (var->data.location >= VARYING_SLOT_VAR0 &&
286 var->data.location - VARYING_SLOT_VAR0 < 32) {
287 assert(var->data.location - VARYING_SLOT_VAR0 < 32);
288
289 const struct glsl_type *type = var->type;
290 if (nir_is_per_vertex_io(var, stage)) {
291 assert(glsl_type_is_array(type));
292 type = glsl_get_array_element(type);
293 }
294
295 unsigned num_slots = glsl_count_attribute_slots(type, false);
296 bool used_across_stages = false;
297 bool outputs_read = false;
298
299 unsigned location = var->data.location - VARYING_SLOT_VAR0;
300 struct varying_loc *new_loc = &remap[location][var->data.location_frac];
301
302 uint64_t slots = (((uint64_t)1 << num_slots) - 1) << var->data.location;
303 if (slots & *slots_used)
304 used_across_stages = true;
305
306 if (slots & *out_slots_read)
307 outputs_read = true;
308
309 if (new_loc->location) {
310 var->data.location = new_loc->location;
311 var->data.location_frac = new_loc->component;
312 }
313
314 if (var->data.always_active_io) {
315 /* We can't apply link time optimisations (specifically array
316 * splitting) to these so we need to copy the existing mask
317 * otherwise we will mess up the mask for things like partially
318 * marked arrays.
319 */
320 if (used_across_stages) {
321 slots_used_tmp |=
322 *slots_used & (((uint64_t)1 << num_slots) - 1) << var->data.location;
323 }
324
325 if (outputs_read) {
326 out_slots_read_tmp |=
327 *out_slots_read & (((uint64_t)1 << num_slots) - 1) << var->data.location;
328 }
329
330 } else {
331 for (unsigned i = 0; i < num_slots; i++) {
332 if (used_across_stages)
333 slots_used_tmp |= (uint64_t)1 << (var->data.location + i);
334
335 if (outputs_read)
336 out_slots_read_tmp |= (uint64_t)1 << (var->data.location + i);
337 }
338 }
339 }
340 }
341
342 *slots_used = slots_used_tmp;
343 *out_slots_read = out_slots_read_tmp;
344 }
345
346 /* If there are empty components in the slot compact the remaining components
347 * as close to component 0 as possible. This will make it easier to fill the
348 * empty components with components from a different slot in a following pass.
349 */
350 static void
351 compact_components(nir_shader *producer, nir_shader *consumer, uint8_t *comps,
352 uint8_t *interp_type, uint8_t *interp_loc,
353 bool default_to_smooth_interp)
354 {
355 struct exec_list *input_list = &consumer->inputs;
356 struct exec_list *output_list = &producer->outputs;
357 struct varying_loc remap[32][4] = {{{0}, {0}}};
358
359 /* Create a cursor for each interpolation type */
360 unsigned cursor[4] = {0};
361
362 /* We only need to pass over one stage and we choose the consumer as it seems
363 * to cause a larger reduction in instruction counts (tested on i965).
364 */
365 nir_foreach_variable(var, input_list) {
366
367 /* Only remap things that aren't builtins.
368 * TODO: add TES patch support.
369 */
370 if (var->data.location >= VARYING_SLOT_VAR0 &&
371 var->data.location - VARYING_SLOT_VAR0 < 32) {
372
373 /* We can't repack xfb varyings. */
374 if (var->data.always_active_io)
375 continue;
376
377 const struct glsl_type *type = var->type;
378 if (nir_is_per_vertex_io(var, consumer->info.stage)) {
379 assert(glsl_type_is_array(type));
380 type = glsl_get_array_element(type);
381 }
382
383 /* Skip types that require more complex packing handling.
384 * TODO: add support for these types.
385 */
386 if (glsl_type_is_array(type) ||
387 glsl_type_is_dual_slot(type) ||
388 glsl_type_is_matrix(type) ||
389 glsl_type_is_struct(type) ||
390 glsl_type_is_64bit(type))
391 continue;
392
393 /* We ignore complex types above and all other vector types should
394 * have been split into scalar variables by the lower_io_to_scalar
395 * pass. The only exeption should by OpenGL xfb varyings.
396 */
397 if (glsl_get_vector_elements(type) != 1)
398 continue;
399
400 unsigned location = var->data.location - VARYING_SLOT_VAR0;
401 uint8_t used_comps = comps[location];
402
403 /* If there are no empty components there is nothing more for us to do.
404 */
405 if (used_comps == 0xf)
406 continue;
407
408 bool found_new_offset = false;
409 uint8_t interp = get_interp_type(var, default_to_smooth_interp);
410 for (; cursor[interp] < 32; cursor[interp]++) {
411 uint8_t cursor_used_comps = comps[cursor[interp]];
412
413 /* We couldn't find anywhere to pack the varying continue on. */
414 if (cursor[interp] == location &&
415 (var->data.location_frac == 0 ||
416 cursor_used_comps & ((1 << (var->data.location_frac)) - 1)))
417 break;
418
419 /* We can only pack varyings with matching interpolation types */
420 if (interp_type[cursor[interp]] != interp)
421 continue;
422
423 /* Interpolation loc must match also.
424 * TODO: i965 can handle these if they don't match, but the
425 * radeonsi nir backend handles everything as vec4s and so expects
426 * this to be the same for all components. We could make this
427 * check driver specfific or drop it if NIR ever become the only
428 * radeonsi backend.
429 */
430 if (interp_loc[cursor[interp]] != get_interp_loc(var))
431 continue;
432
433 /* If the slot is empty just skip it for now, compact_var_list()
434 * can be called after this function to remove empty slots for us.
435 * TODO: finish implementing compact_var_list() requires array and
436 * matrix splitting.
437 */
438 if (!cursor_used_comps)
439 continue;
440
441 uint8_t unused_comps = ~cursor_used_comps;
442
443 for (unsigned i = 0; i < 4; i++) {
444 uint8_t new_var_comps = 1 << i;
445 if (unused_comps & new_var_comps) {
446 remap[location][var->data.location_frac].component = i;
447 remap[location][var->data.location_frac].location =
448 cursor[interp] + VARYING_SLOT_VAR0;
449
450 found_new_offset = true;
451
452 /* Turn off the mask for the component we are remapping */
453 if (comps[location] & 1 << var->data.location_frac) {
454 comps[location] ^= 1 << var->data.location_frac;
455 comps[cursor[interp]] |= new_var_comps;
456 }
457 break;
458 }
459 }
460
461 if (found_new_offset)
462 break;
463 }
464 }
465 }
466
467 uint64_t zero = 0;
468 remap_slots_and_components(input_list, consumer->info.stage, remap,
469 &consumer->info.inputs_read, &zero);
470 remap_slots_and_components(output_list, producer->info.stage, remap,
471 &producer->info.outputs_written,
472 &producer->info.outputs_read);
473 }
474
475 /* We assume that this has been called more-or-less directly after
476 * remove_unused_varyings. At this point, all of the varyings that we
477 * aren't going to be using have been completely removed and the
478 * inputs_read and outputs_written fields in nir_shader_info reflect
479 * this. Therefore, the total set of valid slots is the OR of the two
480 * sets of varyings; this accounts for varyings which one side may need
481 * to read/write even if the other doesn't. This can happen if, for
482 * instance, an array is used indirectly from one side causing it to be
483 * unsplittable but directly from the other.
484 */
485 void
486 nir_compact_varyings(nir_shader *producer, nir_shader *consumer,
487 bool default_to_smooth_interp)
488 {
489 assert(producer->info.stage != MESA_SHADER_FRAGMENT);
490 assert(consumer->info.stage != MESA_SHADER_VERTEX);
491
492 uint8_t comps[32] = {0};
493 uint8_t interp_type[32] = {0};
494 uint8_t interp_loc[32] = {0};
495
496 get_slot_component_masks_and_interp_types(&producer->outputs, comps,
497 interp_type, interp_loc,
498 producer->info.stage,
499 default_to_smooth_interp);
500 get_slot_component_masks_and_interp_types(&consumer->inputs, comps,
501 interp_type, interp_loc,
502 consumer->info.stage,
503 default_to_smooth_interp);
504
505 compact_components(producer, consumer, comps, interp_type, interp_loc,
506 default_to_smooth_interp);
507 }