nir/lower_io: Choose to set access based on intrinsic metadata
[mesa.git] / src / compiler / nir / nir_lower_io.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 * Jason Ekstrand (jason@jlekstrand.net)
26 *
27 */
28
29 /*
30 * This lowering pass converts references to input/output variables with
31 * loads/stores to actual input/output intrinsics.
32 */
33
34 #include "nir.h"
35 #include "nir_builder.h"
36 #include "nir_deref.h"
37
38 #include "util/u_math.h"
39
40 struct lower_io_state {
41 void *dead_ctx;
42 nir_builder builder;
43 int (*type_size)(const struct glsl_type *type, bool);
44 nir_variable_mode modes;
45 nir_lower_io_options options;
46 };
47
48 static nir_intrinsic_op
49 ssbo_atomic_for_deref(nir_intrinsic_op deref_op)
50 {
51 switch (deref_op) {
52 #define OP(O) case nir_intrinsic_deref_##O: return nir_intrinsic_ssbo_##O;
53 OP(atomic_exchange)
54 OP(atomic_comp_swap)
55 OP(atomic_add)
56 OP(atomic_imin)
57 OP(atomic_umin)
58 OP(atomic_imax)
59 OP(atomic_umax)
60 OP(atomic_and)
61 OP(atomic_or)
62 OP(atomic_xor)
63 OP(atomic_fadd)
64 OP(atomic_fmin)
65 OP(atomic_fmax)
66 OP(atomic_fcomp_swap)
67 #undef OP
68 default:
69 unreachable("Invalid SSBO atomic");
70 }
71 }
72
73 static nir_intrinsic_op
74 global_atomic_for_deref(nir_intrinsic_op deref_op)
75 {
76 switch (deref_op) {
77 #define OP(O) case nir_intrinsic_deref_##O: return nir_intrinsic_global_##O;
78 OP(atomic_exchange)
79 OP(atomic_comp_swap)
80 OP(atomic_add)
81 OP(atomic_imin)
82 OP(atomic_umin)
83 OP(atomic_imax)
84 OP(atomic_umax)
85 OP(atomic_and)
86 OP(atomic_or)
87 OP(atomic_xor)
88 OP(atomic_fadd)
89 OP(atomic_fmin)
90 OP(atomic_fmax)
91 OP(atomic_fcomp_swap)
92 #undef OP
93 default:
94 unreachable("Invalid SSBO atomic");
95 }
96 }
97
98 static nir_intrinsic_op
99 shared_atomic_for_deref(nir_intrinsic_op deref_op)
100 {
101 switch (deref_op) {
102 #define OP(O) case nir_intrinsic_deref_##O: return nir_intrinsic_shared_##O;
103 OP(atomic_exchange)
104 OP(atomic_comp_swap)
105 OP(atomic_add)
106 OP(atomic_imin)
107 OP(atomic_umin)
108 OP(atomic_imax)
109 OP(atomic_umax)
110 OP(atomic_and)
111 OP(atomic_or)
112 OP(atomic_xor)
113 OP(atomic_fadd)
114 OP(atomic_fmin)
115 OP(atomic_fmax)
116 OP(atomic_fcomp_swap)
117 #undef OP
118 default:
119 unreachable("Invalid shared atomic");
120 }
121 }
122
123 void
124 nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
125 int (*type_size)(const struct glsl_type *, bool))
126 {
127 unsigned location = 0;
128
129 nir_foreach_variable(var, var_list) {
130 /*
131 * UBOs have their own address spaces, so don't count them towards the
132 * number of global uniforms
133 */
134 if (var->data.mode == nir_var_mem_ubo || var->data.mode == nir_var_mem_ssbo)
135 continue;
136
137 var->data.driver_location = location;
138 bool bindless_type_size = var->data.mode == nir_var_shader_in ||
139 var->data.mode == nir_var_shader_out ||
140 var->data.bindless;
141 location += type_size(var->type, bindless_type_size);
142 }
143
144 *size = location;
145 }
146
147 /**
148 * Return true if the given variable is a per-vertex input/output array.
149 * (such as geometry shader inputs).
150 */
151 bool
152 nir_is_per_vertex_io(const nir_variable *var, gl_shader_stage stage)
153 {
154 if (var->data.patch || !glsl_type_is_array(var->type))
155 return false;
156
157 if (var->data.mode == nir_var_shader_in)
158 return stage == MESA_SHADER_GEOMETRY ||
159 stage == MESA_SHADER_TESS_CTRL ||
160 stage == MESA_SHADER_TESS_EVAL;
161
162 if (var->data.mode == nir_var_shader_out)
163 return stage == MESA_SHADER_TESS_CTRL;
164
165 return false;
166 }
167
168 static nir_ssa_def *
169 get_io_offset(nir_builder *b, nir_deref_instr *deref,
170 nir_ssa_def **vertex_index,
171 int (*type_size)(const struct glsl_type *, bool),
172 unsigned *component, bool bts)
173 {
174 nir_deref_path path;
175 nir_deref_path_init(&path, deref, NULL);
176
177 assert(path.path[0]->deref_type == nir_deref_type_var);
178 nir_deref_instr **p = &path.path[1];
179
180 /* For per-vertex input arrays (i.e. geometry shader inputs), keep the
181 * outermost array index separate. Process the rest normally.
182 */
183 if (vertex_index != NULL) {
184 assert((*p)->deref_type == nir_deref_type_array);
185 *vertex_index = nir_ssa_for_src(b, (*p)->arr.index, 1);
186 p++;
187 }
188
189 if (path.path[0]->var->data.compact) {
190 assert((*p)->deref_type == nir_deref_type_array);
191 assert(glsl_type_is_scalar((*p)->type));
192
193 /* We always lower indirect dereferences for "compact" array vars. */
194 const unsigned index = nir_src_as_uint((*p)->arr.index);
195 const unsigned total_offset = *component + index;
196 const unsigned slot_offset = total_offset / 4;
197 *component = total_offset % 4;
198 return nir_imm_int(b, type_size(glsl_vec4_type(), bts) * slot_offset);
199 }
200
201 /* Just emit code and let constant-folding go to town */
202 nir_ssa_def *offset = nir_imm_int(b, 0);
203
204 for (; *p; p++) {
205 if ((*p)->deref_type == nir_deref_type_array) {
206 unsigned size = type_size((*p)->type, bts);
207
208 nir_ssa_def *mul =
209 nir_amul_imm(b, nir_ssa_for_src(b, (*p)->arr.index, 1), size);
210
211 offset = nir_iadd(b, offset, mul);
212 } else if ((*p)->deref_type == nir_deref_type_struct) {
213 /* p starts at path[1], so this is safe */
214 nir_deref_instr *parent = *(p - 1);
215
216 unsigned field_offset = 0;
217 for (unsigned i = 0; i < (*p)->strct.index; i++) {
218 field_offset += type_size(glsl_get_struct_field(parent->type, i), bts);
219 }
220 offset = nir_iadd_imm(b, offset, field_offset);
221 } else {
222 unreachable("Unsupported deref type");
223 }
224 }
225
226 nir_deref_path_finish(&path);
227
228 return offset;
229 }
230
231 static nir_ssa_def *
232 emit_load(struct lower_io_state *state,
233 nir_ssa_def *vertex_index, nir_variable *var, nir_ssa_def *offset,
234 unsigned component, unsigned num_components, unsigned bit_size,
235 nir_alu_type type)
236 {
237 nir_builder *b = &state->builder;
238 const nir_shader *nir = b->shader;
239 nir_variable_mode mode = var->data.mode;
240 nir_ssa_def *barycentric = NULL;
241
242 nir_intrinsic_op op;
243 switch (mode) {
244 case nir_var_shader_in:
245 if (nir->info.stage == MESA_SHADER_FRAGMENT &&
246 nir->options->use_interpolated_input_intrinsics &&
247 var->data.interpolation != INTERP_MODE_FLAT) {
248 if (var->data.interpolation == INTERP_MODE_EXPLICIT) {
249 assert(vertex_index != NULL);
250 op = nir_intrinsic_load_input_vertex;
251 } else {
252 assert(vertex_index == NULL);
253
254 nir_intrinsic_op bary_op;
255 if (var->data.sample ||
256 (state->options & nir_lower_io_force_sample_interpolation))
257 bary_op = nir_intrinsic_load_barycentric_sample;
258 else if (var->data.centroid)
259 bary_op = nir_intrinsic_load_barycentric_centroid;
260 else
261 bary_op = nir_intrinsic_load_barycentric_pixel;
262
263 barycentric = nir_load_barycentric(&state->builder, bary_op,
264 var->data.interpolation);
265 op = nir_intrinsic_load_interpolated_input;
266 }
267 } else {
268 op = vertex_index ? nir_intrinsic_load_per_vertex_input :
269 nir_intrinsic_load_input;
270 }
271 break;
272 case nir_var_shader_out:
273 op = vertex_index ? nir_intrinsic_load_per_vertex_output :
274 nir_intrinsic_load_output;
275 break;
276 case nir_var_uniform:
277 op = nir_intrinsic_load_uniform;
278 break;
279 default:
280 unreachable("Unknown variable mode");
281 }
282
283 nir_intrinsic_instr *load =
284 nir_intrinsic_instr_create(state->builder.shader, op);
285 load->num_components = num_components;
286
287 nir_intrinsic_set_base(load, var->data.driver_location);
288 if (mode == nir_var_shader_in || mode == nir_var_shader_out)
289 nir_intrinsic_set_component(load, component);
290
291 if (load->intrinsic == nir_intrinsic_load_uniform)
292 nir_intrinsic_set_range(load,
293 state->type_size(var->type, var->data.bindless));
294
295 if (load->intrinsic == nir_intrinsic_load_input ||
296 load->intrinsic == nir_intrinsic_load_input_vertex ||
297 load->intrinsic == nir_intrinsic_load_uniform)
298 nir_intrinsic_set_type(load, type);
299
300 if (vertex_index) {
301 load->src[0] = nir_src_for_ssa(vertex_index);
302 load->src[1] = nir_src_for_ssa(offset);
303 } else if (barycentric) {
304 load->src[0] = nir_src_for_ssa(barycentric);
305 load->src[1] = nir_src_for_ssa(offset);
306 } else {
307 load->src[0] = nir_src_for_ssa(offset);
308 }
309
310 nir_ssa_dest_init(&load->instr, &load->dest,
311 num_components, bit_size, NULL);
312 nir_builder_instr_insert(b, &load->instr);
313
314 return &load->dest.ssa;
315 }
316
317 static nir_ssa_def *
318 lower_load(nir_intrinsic_instr *intrin, struct lower_io_state *state,
319 nir_ssa_def *vertex_index, nir_variable *var, nir_ssa_def *offset,
320 unsigned component, const struct glsl_type *type)
321 {
322 assert(intrin->dest.is_ssa);
323 if (intrin->dest.ssa.bit_size == 64 &&
324 (state->options & nir_lower_io_lower_64bit_to_32)) {
325 nir_builder *b = &state->builder;
326
327 const unsigned slot_size = state->type_size(glsl_dvec_type(2), false);
328
329 nir_ssa_def *comp64[4];
330 assert(component == 0 || component == 2);
331 unsigned dest_comp = 0;
332 while (dest_comp < intrin->dest.ssa.num_components) {
333 const unsigned num_comps =
334 MIN2(intrin->dest.ssa.num_components - dest_comp,
335 (4 - component) / 2);
336
337 nir_ssa_def *data32 =
338 emit_load(state, vertex_index, var, offset, component,
339 num_comps * 2, 32, nir_type_uint32);
340 for (unsigned i = 0; i < num_comps; i++) {
341 comp64[dest_comp + i] =
342 nir_pack_64_2x32(b, nir_channels(b, data32, 3 << (i * 2)));
343 }
344
345 /* Only the first store has a component offset */
346 component = 0;
347 dest_comp += num_comps;
348 offset = nir_iadd_imm(b, offset, slot_size);
349 }
350
351 return nir_vec(b, comp64, intrin->dest.ssa.num_components);
352 } else if (intrin->dest.ssa.bit_size == 1) {
353 /* Booleans are 32-bit */
354 assert(glsl_type_is_boolean(type));
355 return nir_b2b1(&state->builder,
356 emit_load(state, vertex_index, var, offset, component,
357 intrin->dest.ssa.num_components, 32,
358 nir_type_bool32));
359 } else {
360 return emit_load(state, vertex_index, var, offset, component,
361 intrin->dest.ssa.num_components,
362 intrin->dest.ssa.bit_size,
363 nir_get_nir_type_for_glsl_type(type));
364 }
365 }
366
367 static void
368 emit_store(struct lower_io_state *state, nir_ssa_def *data,
369 nir_ssa_def *vertex_index, nir_variable *var, nir_ssa_def *offset,
370 unsigned component, unsigned num_components,
371 nir_component_mask_t write_mask, nir_alu_type type)
372 {
373 nir_builder *b = &state->builder;
374 nir_variable_mode mode = var->data.mode;
375
376 assert(mode == nir_var_shader_out);
377 nir_intrinsic_op op;
378 op = vertex_index ? nir_intrinsic_store_per_vertex_output :
379 nir_intrinsic_store_output;
380
381 nir_intrinsic_instr *store =
382 nir_intrinsic_instr_create(state->builder.shader, op);
383 store->num_components = num_components;
384
385 store->src[0] = nir_src_for_ssa(data);
386
387 nir_intrinsic_set_base(store, var->data.driver_location);
388
389 if (mode == nir_var_shader_out)
390 nir_intrinsic_set_component(store, component);
391
392 if (store->intrinsic == nir_intrinsic_store_output)
393 nir_intrinsic_set_type(store, type);
394
395 nir_intrinsic_set_write_mask(store, write_mask);
396
397 if (vertex_index)
398 store->src[1] = nir_src_for_ssa(vertex_index);
399
400 store->src[vertex_index ? 2 : 1] = nir_src_for_ssa(offset);
401
402 nir_builder_instr_insert(b, &store->instr);
403 }
404
405 static void
406 lower_store(nir_intrinsic_instr *intrin, struct lower_io_state *state,
407 nir_ssa_def *vertex_index, nir_variable *var, nir_ssa_def *offset,
408 unsigned component, const struct glsl_type *type)
409 {
410 assert(intrin->src[1].is_ssa);
411 if (intrin->src[1].ssa->bit_size == 64 &&
412 (state->options & nir_lower_io_lower_64bit_to_32)) {
413 nir_builder *b = &state->builder;
414
415 const unsigned slot_size = state->type_size(glsl_dvec_type(2), false);
416
417 assert(component == 0 || component == 2);
418 unsigned src_comp = 0;
419 nir_component_mask_t write_mask = nir_intrinsic_write_mask(intrin);
420 while (src_comp < intrin->num_components) {
421 const unsigned num_comps =
422 MIN2(intrin->num_components - src_comp,
423 (4 - component) / 2);
424
425 if (write_mask & BITFIELD_MASK(num_comps)) {
426 nir_ssa_def *data =
427 nir_channels(b, intrin->src[1].ssa,
428 BITFIELD_RANGE(src_comp, num_comps));
429 nir_ssa_def *data32 = nir_bitcast_vector(b, data, 32);
430
431 nir_component_mask_t write_mask32 = 0;
432 for (unsigned i = 0; i < num_comps; i++) {
433 if (write_mask & BITFIELD_MASK(num_comps) & (1 << i))
434 write_mask32 |= 3 << (i * 2);
435 }
436
437 emit_store(state, data32, vertex_index, var, offset,
438 component, data32->num_components, write_mask32,
439 nir_type_uint32);
440 }
441
442 /* Only the first store has a component offset */
443 component = 0;
444 src_comp += num_comps;
445 write_mask >>= num_comps;
446 offset = nir_iadd_imm(b, offset, slot_size);
447 }
448 } else if (intrin->dest.ssa.bit_size == 1) {
449 /* Booleans are 32-bit */
450 assert(glsl_type_is_boolean(type));
451 nir_ssa_def *b32_val = nir_b2b32(&state->builder, intrin->src[1].ssa);
452 emit_store(state, b32_val, vertex_index, var, offset,
453 component, intrin->num_components,
454 nir_intrinsic_write_mask(intrin),
455 nir_type_bool32);
456 } else {
457 emit_store(state, intrin->src[1].ssa, vertex_index, var, offset,
458 component, intrin->num_components,
459 nir_intrinsic_write_mask(intrin),
460 nir_get_nir_type_for_glsl_type(type));
461 }
462 }
463
464 static nir_ssa_def *
465 lower_interpolate_at(nir_intrinsic_instr *intrin, struct lower_io_state *state,
466 nir_variable *var, nir_ssa_def *offset, unsigned component,
467 const struct glsl_type *type)
468 {
469 nir_builder *b = &state->builder;
470 assert(var->data.mode == nir_var_shader_in);
471
472 /* Ignore interpolateAt() for flat variables - flat is flat. Lower
473 * interpolateAtVertex() for explicit variables.
474 */
475 if (var->data.interpolation == INTERP_MODE_FLAT ||
476 var->data.interpolation == INTERP_MODE_EXPLICIT) {
477 nir_ssa_def *vertex_index = NULL;
478
479 if (var->data.interpolation == INTERP_MODE_EXPLICIT) {
480 assert(intrin->intrinsic == nir_intrinsic_interp_deref_at_vertex);
481 vertex_index = intrin->src[1].ssa;
482 }
483
484 return lower_load(intrin, state, vertex_index, var, offset, component, type);
485 }
486
487 /* None of the supported APIs allow interpolation on 64-bit things */
488 assert(intrin->dest.is_ssa && intrin->dest.ssa.bit_size <= 32);
489
490 nir_intrinsic_op bary_op;
491 switch (intrin->intrinsic) {
492 case nir_intrinsic_interp_deref_at_centroid:
493 bary_op = (state->options & nir_lower_io_force_sample_interpolation) ?
494 nir_intrinsic_load_barycentric_sample :
495 nir_intrinsic_load_barycentric_centroid;
496 break;
497 case nir_intrinsic_interp_deref_at_sample:
498 bary_op = nir_intrinsic_load_barycentric_at_sample;
499 break;
500 case nir_intrinsic_interp_deref_at_offset:
501 bary_op = nir_intrinsic_load_barycentric_at_offset;
502 break;
503 default:
504 unreachable("Bogus interpolateAt() intrinsic.");
505 }
506
507 nir_intrinsic_instr *bary_setup =
508 nir_intrinsic_instr_create(state->builder.shader, bary_op);
509
510 nir_ssa_dest_init(&bary_setup->instr, &bary_setup->dest, 2, 32, NULL);
511 nir_intrinsic_set_interp_mode(bary_setup, var->data.interpolation);
512
513 if (intrin->intrinsic == nir_intrinsic_interp_deref_at_sample ||
514 intrin->intrinsic == nir_intrinsic_interp_deref_at_offset ||
515 intrin->intrinsic == nir_intrinsic_interp_deref_at_vertex)
516 nir_src_copy(&bary_setup->src[0], &intrin->src[1], bary_setup);
517
518 nir_builder_instr_insert(b, &bary_setup->instr);
519
520 nir_intrinsic_instr *load =
521 nir_intrinsic_instr_create(state->builder.shader,
522 nir_intrinsic_load_interpolated_input);
523 load->num_components = intrin->num_components;
524
525 nir_intrinsic_set_base(load, var->data.driver_location);
526 nir_intrinsic_set_component(load, component);
527
528 load->src[0] = nir_src_for_ssa(&bary_setup->dest.ssa);
529 load->src[1] = nir_src_for_ssa(offset);
530
531 assert(intrin->dest.is_ssa);
532 nir_ssa_dest_init(&load->instr, &load->dest,
533 intrin->dest.ssa.num_components,
534 intrin->dest.ssa.bit_size, NULL);
535 nir_builder_instr_insert(b, &load->instr);
536
537 return &load->dest.ssa;
538 }
539
540 static bool
541 nir_lower_io_block(nir_block *block,
542 struct lower_io_state *state)
543 {
544 nir_builder *b = &state->builder;
545 const nir_shader_compiler_options *options = b->shader->options;
546 bool progress = false;
547
548 nir_foreach_instr_safe(instr, block) {
549 if (instr->type != nir_instr_type_intrinsic)
550 continue;
551
552 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
553
554 switch (intrin->intrinsic) {
555 case nir_intrinsic_load_deref:
556 case nir_intrinsic_store_deref:
557 /* We can lower the io for this nir instrinsic */
558 break;
559 case nir_intrinsic_interp_deref_at_centroid:
560 case nir_intrinsic_interp_deref_at_sample:
561 case nir_intrinsic_interp_deref_at_offset:
562 case nir_intrinsic_interp_deref_at_vertex:
563 /* We can optionally lower these to load_interpolated_input */
564 if (options->use_interpolated_input_intrinsics)
565 break;
566 default:
567 /* We can't lower the io for this nir instrinsic, so skip it */
568 continue;
569 }
570
571 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
572
573 nir_variable_mode mode = deref->mode;
574 assert(util_is_power_of_two_nonzero(mode));
575 if ((state->modes & mode) == 0)
576 continue;
577
578 nir_variable *var = nir_deref_instr_get_variable(deref);
579
580 b->cursor = nir_before_instr(instr);
581
582 const bool per_vertex = nir_is_per_vertex_io(var, b->shader->info.stage);
583
584 nir_ssa_def *offset;
585 nir_ssa_def *vertex_index = NULL;
586 unsigned component_offset = var->data.location_frac;
587 bool bindless_type_size = mode == nir_var_shader_in ||
588 mode == nir_var_shader_out ||
589 var->data.bindless;
590
591 offset = get_io_offset(b, deref, per_vertex ? &vertex_index : NULL,
592 state->type_size, &component_offset,
593 bindless_type_size);
594
595 nir_ssa_def *replacement = NULL;
596
597 switch (intrin->intrinsic) {
598 case nir_intrinsic_load_deref:
599 replacement = lower_load(intrin, state, vertex_index, var, offset,
600 component_offset, deref->type);
601 break;
602
603 case nir_intrinsic_store_deref:
604 lower_store(intrin, state, vertex_index, var, offset,
605 component_offset, deref->type);
606 break;
607
608 case nir_intrinsic_interp_deref_at_centroid:
609 case nir_intrinsic_interp_deref_at_sample:
610 case nir_intrinsic_interp_deref_at_offset:
611 case nir_intrinsic_interp_deref_at_vertex:
612 assert(vertex_index == NULL);
613 replacement = lower_interpolate_at(intrin, state, var, offset,
614 component_offset, deref->type);
615 break;
616
617 default:
618 continue;
619 }
620
621 if (replacement) {
622 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
623 nir_src_for_ssa(replacement));
624 }
625 nir_instr_remove(&intrin->instr);
626 progress = true;
627 }
628
629 return progress;
630 }
631
632 static bool
633 nir_lower_io_impl(nir_function_impl *impl,
634 nir_variable_mode modes,
635 int (*type_size)(const struct glsl_type *, bool),
636 nir_lower_io_options options)
637 {
638 struct lower_io_state state;
639 bool progress = false;
640
641 nir_builder_init(&state.builder, impl);
642 state.dead_ctx = ralloc_context(NULL);
643 state.modes = modes;
644 state.type_size = type_size;
645 state.options = options;
646
647 ASSERTED nir_variable_mode supported_modes =
648 nir_var_shader_in | nir_var_shader_out | nir_var_uniform;
649 assert(!(modes & ~supported_modes));
650
651 nir_foreach_block(block, impl) {
652 progress |= nir_lower_io_block(block, &state);
653 }
654
655 ralloc_free(state.dead_ctx);
656
657 nir_metadata_preserve(impl, nir_metadata_block_index |
658 nir_metadata_dominance);
659 return progress;
660 }
661
662 /** Lower load/store_deref intrinsics on I/O variables to offset-based intrinsics
663 *
664 * This pass is intended to be used for cross-stage shader I/O and driver-
665 * managed uniforms to turn deref-based access into a simpler model using
666 * locations or offsets. For fragment shader inputs, it can optionally turn
667 * load_deref into an explicit interpolation using barycentrics coming from
668 * one of the load_barycentric_* intrinsics. This pass requires that all
669 * deref chains are complete and contain no casts.
670 */
671 bool
672 nir_lower_io(nir_shader *shader, nir_variable_mode modes,
673 int (*type_size)(const struct glsl_type *, bool),
674 nir_lower_io_options options)
675 {
676 bool progress = false;
677
678 nir_foreach_function(function, shader) {
679 if (function->impl) {
680 progress |= nir_lower_io_impl(function->impl, modes,
681 type_size, options);
682 }
683 }
684
685 return progress;
686 }
687
688 static unsigned
689 type_scalar_size_bytes(const struct glsl_type *type)
690 {
691 assert(glsl_type_is_vector_or_scalar(type) ||
692 glsl_type_is_matrix(type));
693 return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
694 }
695
696 static nir_ssa_def *
697 build_addr_iadd(nir_builder *b, nir_ssa_def *addr,
698 nir_address_format addr_format, nir_ssa_def *offset)
699 {
700 assert(offset->num_components == 1);
701 assert(addr->bit_size == offset->bit_size);
702
703 switch (addr_format) {
704 case nir_address_format_32bit_global:
705 case nir_address_format_64bit_global:
706 case nir_address_format_32bit_offset:
707 assert(addr->num_components == 1);
708 return nir_iadd(b, addr, offset);
709
710 case nir_address_format_64bit_bounded_global:
711 assert(addr->num_components == 4);
712 return nir_vec4(b, nir_channel(b, addr, 0),
713 nir_channel(b, addr, 1),
714 nir_channel(b, addr, 2),
715 nir_iadd(b, nir_channel(b, addr, 3), offset));
716
717 case nir_address_format_32bit_index_offset:
718 assert(addr->num_components == 2);
719 return nir_vec2(b, nir_channel(b, addr, 0),
720 nir_iadd(b, nir_channel(b, addr, 1), offset));
721 case nir_address_format_vec2_index_32bit_offset:
722 assert(addr->num_components == 3);
723 return nir_vec3(b, nir_channel(b, addr, 0), nir_channel(b, addr, 1),
724 nir_iadd(b, nir_channel(b, addr, 2), offset));
725 case nir_address_format_logical:
726 unreachable("Unsupported address format");
727 }
728 unreachable("Invalid address format");
729 }
730
731 static nir_ssa_def *
732 build_addr_iadd_imm(nir_builder *b, nir_ssa_def *addr,
733 nir_address_format addr_format, int64_t offset)
734 {
735 return build_addr_iadd(b, addr, addr_format,
736 nir_imm_intN_t(b, offset, addr->bit_size));
737 }
738
739 static nir_ssa_def *
740 addr_to_index(nir_builder *b, nir_ssa_def *addr,
741 nir_address_format addr_format)
742 {
743 if (addr_format == nir_address_format_32bit_index_offset) {
744 assert(addr->num_components == 2);
745 return nir_channel(b, addr, 0);
746 } else if (addr_format == nir_address_format_vec2_index_32bit_offset) {
747 assert(addr->num_components == 3);
748 return nir_channels(b, addr, 0x3);
749 } else {
750 unreachable("bad address format for index");
751 }
752 }
753
754 static nir_ssa_def *
755 addr_to_offset(nir_builder *b, nir_ssa_def *addr,
756 nir_address_format addr_format)
757 {
758 if (addr_format == nir_address_format_32bit_index_offset) {
759 assert(addr->num_components == 2);
760 return nir_channel(b, addr, 1);
761 } else if (addr_format == nir_address_format_vec2_index_32bit_offset) {
762 assert(addr->num_components == 3);
763 return nir_channel(b, addr, 2);
764 } else {
765 unreachable("bad address format for offset");
766 }
767 }
768
769 /** Returns true if the given address format resolves to a global address */
770 static bool
771 addr_format_is_global(nir_address_format addr_format)
772 {
773 return addr_format == nir_address_format_32bit_global ||
774 addr_format == nir_address_format_64bit_global ||
775 addr_format == nir_address_format_64bit_bounded_global;
776 }
777
778 static bool
779 addr_format_is_offset(nir_address_format addr_format)
780 {
781 return addr_format == nir_address_format_32bit_offset;
782 }
783
784 static nir_ssa_def *
785 addr_to_global(nir_builder *b, nir_ssa_def *addr,
786 nir_address_format addr_format)
787 {
788 switch (addr_format) {
789 case nir_address_format_32bit_global:
790 case nir_address_format_64bit_global:
791 assert(addr->num_components == 1);
792 return addr;
793
794 case nir_address_format_64bit_bounded_global:
795 assert(addr->num_components == 4);
796 return nir_iadd(b, nir_pack_64_2x32(b, nir_channels(b, addr, 0x3)),
797 nir_u2u64(b, nir_channel(b, addr, 3)));
798
799 case nir_address_format_32bit_index_offset:
800 case nir_address_format_vec2_index_32bit_offset:
801 case nir_address_format_32bit_offset:
802 case nir_address_format_logical:
803 unreachable("Cannot get a 64-bit address with this address format");
804 }
805
806 unreachable("Invalid address format");
807 }
808
809 static bool
810 addr_format_needs_bounds_check(nir_address_format addr_format)
811 {
812 return addr_format == nir_address_format_64bit_bounded_global;
813 }
814
815 static nir_ssa_def *
816 addr_is_in_bounds(nir_builder *b, nir_ssa_def *addr,
817 nir_address_format addr_format, unsigned size)
818 {
819 assert(addr_format == nir_address_format_64bit_bounded_global);
820 assert(addr->num_components == 4);
821 return nir_ige(b, nir_channel(b, addr, 2),
822 nir_iadd_imm(b, nir_channel(b, addr, 3), size));
823 }
824
825 static nir_ssa_def *
826 build_explicit_io_load(nir_builder *b, nir_intrinsic_instr *intrin,
827 nir_ssa_def *addr, nir_address_format addr_format,
828 unsigned num_components)
829 {
830 nir_variable_mode mode = nir_src_as_deref(intrin->src[0])->mode;
831
832 nir_intrinsic_op op;
833 switch (mode) {
834 case nir_var_mem_ubo:
835 op = nir_intrinsic_load_ubo;
836 break;
837 case nir_var_mem_ssbo:
838 if (addr_format_is_global(addr_format))
839 op = nir_intrinsic_load_global;
840 else
841 op = nir_intrinsic_load_ssbo;
842 break;
843 case nir_var_mem_global:
844 assert(addr_format_is_global(addr_format));
845 op = nir_intrinsic_load_global;
846 break;
847 case nir_var_shader_in:
848 assert(addr_format_is_global(addr_format));
849 op = nir_intrinsic_load_kernel_input;
850 break;
851 case nir_var_mem_shared:
852 assert(addr_format_is_offset(addr_format));
853 op = nir_intrinsic_load_shared;
854 break;
855 case nir_var_shader_temp:
856 case nir_var_function_temp:
857 assert(addr_format_is_offset(addr_format));
858 op = nir_intrinsic_load_scratch;
859 break;
860 default:
861 unreachable("Unsupported explicit IO variable mode");
862 }
863
864 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
865
866 if (addr_format_is_global(addr_format)) {
867 load->src[0] = nir_src_for_ssa(addr_to_global(b, addr, addr_format));
868 } else if (addr_format == nir_address_format_32bit_offset) {
869 assert(addr->num_components == 1);
870 load->src[0] = nir_src_for_ssa(addr);
871 } else {
872 load->src[0] = nir_src_for_ssa(addr_to_index(b, addr, addr_format));
873 load->src[1] = nir_src_for_ssa(addr_to_offset(b, addr, addr_format));
874 }
875
876 if (nir_intrinsic_infos[op].index_map[NIR_INTRINSIC_ACCESS] > 0)
877 nir_intrinsic_set_access(load, nir_intrinsic_access(intrin));
878
879 unsigned bit_size = intrin->dest.ssa.bit_size;
880 if (bit_size == 1) {
881 /* TODO: Make the native bool bit_size an option. */
882 bit_size = 32;
883 }
884
885 /* TODO: We should try and provide a better alignment. For OpenCL, we need
886 * to plumb the alignment through from SPIR-V when we have one.
887 */
888 nir_intrinsic_set_align(load, bit_size / 8, 0);
889
890 assert(intrin->dest.is_ssa);
891 load->num_components = num_components;
892 nir_ssa_dest_init(&load->instr, &load->dest, num_components,
893 bit_size, intrin->dest.ssa.name);
894
895 assert(bit_size % 8 == 0);
896
897 nir_ssa_def *result;
898 if (addr_format_needs_bounds_check(addr_format)) {
899 /* The Vulkan spec for robustBufferAccess gives us quite a few options
900 * as to what we can do with an OOB read. Unfortunately, returning
901 * undefined values isn't one of them so we return an actual zero.
902 */
903 nir_ssa_def *zero = nir_imm_zero(b, load->num_components, bit_size);
904
905 const unsigned load_size = (bit_size / 8) * load->num_components;
906 nir_push_if(b, addr_is_in_bounds(b, addr, addr_format, load_size));
907
908 nir_builder_instr_insert(b, &load->instr);
909
910 nir_pop_if(b, NULL);
911
912 result = nir_if_phi(b, &load->dest.ssa, zero);
913 } else {
914 nir_builder_instr_insert(b, &load->instr);
915 result = &load->dest.ssa;
916 }
917
918 if (intrin->dest.ssa.bit_size == 1) {
919 /* For shared, we can go ahead and use NIR's and/or the back-end's
920 * standard encoding for booleans rather than forcing a 0/1 boolean.
921 * This should save an instruction or two.
922 */
923 if (mode == nir_var_mem_shared)
924 result = nir_b2b1(b, result);
925 else
926 result = nir_i2b(b, result);
927 }
928
929 return result;
930 }
931
932 static void
933 build_explicit_io_store(nir_builder *b, nir_intrinsic_instr *intrin,
934 nir_ssa_def *addr, nir_address_format addr_format,
935 nir_ssa_def *value, nir_component_mask_t write_mask)
936 {
937 nir_variable_mode mode = nir_src_as_deref(intrin->src[0])->mode;
938
939 nir_intrinsic_op op;
940 switch (mode) {
941 case nir_var_mem_ssbo:
942 if (addr_format_is_global(addr_format))
943 op = nir_intrinsic_store_global;
944 else
945 op = nir_intrinsic_store_ssbo;
946 break;
947 case nir_var_mem_global:
948 assert(addr_format_is_global(addr_format));
949 op = nir_intrinsic_store_global;
950 break;
951 case nir_var_mem_shared:
952 assert(addr_format_is_offset(addr_format));
953 op = nir_intrinsic_store_shared;
954 break;
955 case nir_var_shader_temp:
956 case nir_var_function_temp:
957 assert(addr_format_is_offset(addr_format));
958 op = nir_intrinsic_store_scratch;
959 break;
960 default:
961 unreachable("Unsupported explicit IO variable mode");
962 }
963
964 nir_intrinsic_instr *store = nir_intrinsic_instr_create(b->shader, op);
965
966 if (value->bit_size == 1) {
967 /* For shared, we can go ahead and use NIR's and/or the back-end's
968 * standard encoding for booleans rather than forcing a 0/1 boolean.
969 * This should save an instruction or two.
970 *
971 * TODO: Make the native bool bit_size an option.
972 */
973 if (mode == nir_var_mem_shared)
974 value = nir_b2b32(b, value);
975 else
976 value = nir_b2i(b, value, 32);
977 }
978
979 store->src[0] = nir_src_for_ssa(value);
980 if (addr_format_is_global(addr_format)) {
981 store->src[1] = nir_src_for_ssa(addr_to_global(b, addr, addr_format));
982 } else if (addr_format == nir_address_format_32bit_offset) {
983 assert(addr->num_components == 1);
984 store->src[1] = nir_src_for_ssa(addr);
985 } else {
986 store->src[1] = nir_src_for_ssa(addr_to_index(b, addr, addr_format));
987 store->src[2] = nir_src_for_ssa(addr_to_offset(b, addr, addr_format));
988 }
989
990 nir_intrinsic_set_write_mask(store, write_mask);
991
992 if (nir_intrinsic_infos[op].index_map[NIR_INTRINSIC_ACCESS] > 0)
993 nir_intrinsic_set_access(store, nir_intrinsic_access(intrin));
994
995 /* TODO: We should try and provide a better alignment. For OpenCL, we need
996 * to plumb the alignment through from SPIR-V when we have one.
997 */
998 nir_intrinsic_set_align(store, value->bit_size / 8, 0);
999
1000 assert(value->num_components == 1 ||
1001 value->num_components == intrin->num_components);
1002 store->num_components = value->num_components;
1003
1004 assert(value->bit_size % 8 == 0);
1005
1006 if (addr_format_needs_bounds_check(addr_format)) {
1007 const unsigned store_size = (value->bit_size / 8) * store->num_components;
1008 nir_push_if(b, addr_is_in_bounds(b, addr, addr_format, store_size));
1009
1010 nir_builder_instr_insert(b, &store->instr);
1011
1012 nir_pop_if(b, NULL);
1013 } else {
1014 nir_builder_instr_insert(b, &store->instr);
1015 }
1016 }
1017
1018 static nir_ssa_def *
1019 build_explicit_io_atomic(nir_builder *b, nir_intrinsic_instr *intrin,
1020 nir_ssa_def *addr, nir_address_format addr_format)
1021 {
1022 nir_variable_mode mode = nir_src_as_deref(intrin->src[0])->mode;
1023 const unsigned num_data_srcs =
1024 nir_intrinsic_infos[intrin->intrinsic].num_srcs - 1;
1025
1026 nir_intrinsic_op op;
1027 switch (mode) {
1028 case nir_var_mem_ssbo:
1029 if (addr_format_is_global(addr_format))
1030 op = global_atomic_for_deref(intrin->intrinsic);
1031 else
1032 op = ssbo_atomic_for_deref(intrin->intrinsic);
1033 break;
1034 case nir_var_mem_global:
1035 assert(addr_format_is_global(addr_format));
1036 op = global_atomic_for_deref(intrin->intrinsic);
1037 break;
1038 case nir_var_mem_shared:
1039 assert(addr_format == nir_address_format_32bit_offset);
1040 op = shared_atomic_for_deref(intrin->intrinsic);
1041 break;
1042 default:
1043 unreachable("Unsupported explicit IO variable mode");
1044 }
1045
1046 nir_intrinsic_instr *atomic = nir_intrinsic_instr_create(b->shader, op);
1047
1048 unsigned src = 0;
1049 if (addr_format_is_global(addr_format)) {
1050 atomic->src[src++] = nir_src_for_ssa(addr_to_global(b, addr, addr_format));
1051 } else if (addr_format == nir_address_format_32bit_offset) {
1052 assert(addr->num_components == 1);
1053 atomic->src[src++] = nir_src_for_ssa(addr);
1054 } else {
1055 atomic->src[src++] = nir_src_for_ssa(addr_to_index(b, addr, addr_format));
1056 atomic->src[src++] = nir_src_for_ssa(addr_to_offset(b, addr, addr_format));
1057 }
1058 for (unsigned i = 0; i < num_data_srcs; i++) {
1059 atomic->src[src++] = nir_src_for_ssa(intrin->src[1 + i].ssa);
1060 }
1061
1062 /* Global atomics don't have access flags because they assume that the
1063 * address may be non-uniform.
1064 */
1065 if (nir_intrinsic_infos[op].index_map[NIR_INTRINSIC_ACCESS] > 0)
1066 nir_intrinsic_set_access(atomic, nir_intrinsic_access(intrin));
1067
1068 assert(intrin->dest.ssa.num_components == 1);
1069 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
1070 1, intrin->dest.ssa.bit_size, intrin->dest.ssa.name);
1071
1072 assert(atomic->dest.ssa.bit_size % 8 == 0);
1073
1074 if (addr_format_needs_bounds_check(addr_format)) {
1075 const unsigned atomic_size = atomic->dest.ssa.bit_size / 8;
1076 nir_push_if(b, addr_is_in_bounds(b, addr, addr_format, atomic_size));
1077
1078 nir_builder_instr_insert(b, &atomic->instr);
1079
1080 nir_pop_if(b, NULL);
1081 return nir_if_phi(b, &atomic->dest.ssa,
1082 nir_ssa_undef(b, 1, atomic->dest.ssa.bit_size));
1083 } else {
1084 nir_builder_instr_insert(b, &atomic->instr);
1085 return &atomic->dest.ssa;
1086 }
1087 }
1088
1089 nir_ssa_def *
1090 nir_explicit_io_address_from_deref(nir_builder *b, nir_deref_instr *deref,
1091 nir_ssa_def *base_addr,
1092 nir_address_format addr_format)
1093 {
1094 assert(deref->dest.is_ssa);
1095 switch (deref->deref_type) {
1096 case nir_deref_type_var:
1097 assert(deref->mode & (nir_var_shader_in | nir_var_mem_shared |
1098 nir_var_shader_temp | nir_var_function_temp));
1099 return nir_imm_intN_t(b, deref->var->data.driver_location,
1100 deref->dest.ssa.bit_size);
1101
1102 case nir_deref_type_array: {
1103 nir_deref_instr *parent = nir_deref_instr_parent(deref);
1104
1105 unsigned stride = glsl_get_explicit_stride(parent->type);
1106 if ((glsl_type_is_matrix(parent->type) &&
1107 glsl_matrix_type_is_row_major(parent->type)) ||
1108 (glsl_type_is_vector(parent->type) && stride == 0))
1109 stride = type_scalar_size_bytes(parent->type);
1110
1111 assert(stride > 0);
1112
1113 nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
1114 index = nir_i2i(b, index, base_addr->bit_size);
1115 return build_addr_iadd(b, base_addr, addr_format,
1116 nir_amul_imm(b, index, stride));
1117 }
1118
1119 case nir_deref_type_ptr_as_array: {
1120 nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
1121 index = nir_i2i(b, index, base_addr->bit_size);
1122 unsigned stride = nir_deref_instr_ptr_as_array_stride(deref);
1123 return build_addr_iadd(b, base_addr, addr_format,
1124 nir_amul_imm(b, index, stride));
1125 }
1126
1127 case nir_deref_type_array_wildcard:
1128 unreachable("Wildcards should be lowered by now");
1129 break;
1130
1131 case nir_deref_type_struct: {
1132 nir_deref_instr *parent = nir_deref_instr_parent(deref);
1133 int offset = glsl_get_struct_field_offset(parent->type,
1134 deref->strct.index);
1135 assert(offset >= 0);
1136 return build_addr_iadd_imm(b, base_addr, addr_format, offset);
1137 }
1138
1139 case nir_deref_type_cast:
1140 /* Nothing to do here */
1141 return base_addr;
1142 }
1143
1144 unreachable("Invalid NIR deref type");
1145 }
1146
1147 void
1148 nir_lower_explicit_io_instr(nir_builder *b,
1149 nir_intrinsic_instr *intrin,
1150 nir_ssa_def *addr,
1151 nir_address_format addr_format)
1152 {
1153 b->cursor = nir_after_instr(&intrin->instr);
1154
1155 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1156 unsigned vec_stride = glsl_get_explicit_stride(deref->type);
1157 unsigned scalar_size = type_scalar_size_bytes(deref->type);
1158 assert(vec_stride == 0 || glsl_type_is_vector(deref->type));
1159 assert(vec_stride == 0 || vec_stride >= scalar_size);
1160
1161 if (intrin->intrinsic == nir_intrinsic_load_deref) {
1162 nir_ssa_def *value;
1163 if (vec_stride > scalar_size) {
1164 nir_ssa_def *comps[4] = { NULL, };
1165 for (unsigned i = 0; i < intrin->num_components; i++) {
1166 nir_ssa_def *comp_addr = build_addr_iadd_imm(b, addr, addr_format,
1167 vec_stride * i);
1168 comps[i] = build_explicit_io_load(b, intrin, comp_addr,
1169 addr_format, 1);
1170 }
1171 value = nir_vec(b, comps, intrin->num_components);
1172 } else {
1173 value = build_explicit_io_load(b, intrin, addr, addr_format,
1174 intrin->num_components);
1175 }
1176 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(value));
1177 } else if (intrin->intrinsic == nir_intrinsic_store_deref) {
1178 assert(intrin->src[1].is_ssa);
1179 nir_ssa_def *value = intrin->src[1].ssa;
1180 nir_component_mask_t write_mask = nir_intrinsic_write_mask(intrin);
1181 if (vec_stride > scalar_size) {
1182 for (unsigned i = 0; i < intrin->num_components; i++) {
1183 if (!(write_mask & (1 << i)))
1184 continue;
1185
1186 nir_ssa_def *comp_addr = build_addr_iadd_imm(b, addr, addr_format,
1187 vec_stride * i);
1188 build_explicit_io_store(b, intrin, comp_addr, addr_format,
1189 nir_channel(b, value, i), 1);
1190 }
1191 } else {
1192 build_explicit_io_store(b, intrin, addr, addr_format,
1193 value, write_mask);
1194 }
1195 } else {
1196 nir_ssa_def *value =
1197 build_explicit_io_atomic(b, intrin, addr, addr_format);
1198 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(value));
1199 }
1200
1201 nir_instr_remove(&intrin->instr);
1202 }
1203
1204 static void
1205 lower_explicit_io_deref(nir_builder *b, nir_deref_instr *deref,
1206 nir_address_format addr_format)
1207 {
1208 /* Just delete the deref if it's not used. We can't use
1209 * nir_deref_instr_remove_if_unused here because it may remove more than
1210 * one deref which could break our list walking since we walk the list
1211 * backwards.
1212 */
1213 assert(list_is_empty(&deref->dest.ssa.if_uses));
1214 if (list_is_empty(&deref->dest.ssa.uses)) {
1215 nir_instr_remove(&deref->instr);
1216 return;
1217 }
1218
1219 b->cursor = nir_after_instr(&deref->instr);
1220
1221 nir_ssa_def *base_addr = NULL;
1222 if (deref->deref_type != nir_deref_type_var) {
1223 assert(deref->parent.is_ssa);
1224 base_addr = deref->parent.ssa;
1225 }
1226
1227 nir_ssa_def *addr = nir_explicit_io_address_from_deref(b, deref, base_addr,
1228 addr_format);
1229
1230 nir_instr_remove(&deref->instr);
1231 nir_ssa_def_rewrite_uses(&deref->dest.ssa, nir_src_for_ssa(addr));
1232 }
1233
1234 static void
1235 lower_explicit_io_access(nir_builder *b, nir_intrinsic_instr *intrin,
1236 nir_address_format addr_format)
1237 {
1238 assert(intrin->src[0].is_ssa);
1239 nir_lower_explicit_io_instr(b, intrin, intrin->src[0].ssa, addr_format);
1240 }
1241
1242 static void
1243 lower_explicit_io_array_length(nir_builder *b, nir_intrinsic_instr *intrin,
1244 nir_address_format addr_format)
1245 {
1246 b->cursor = nir_after_instr(&intrin->instr);
1247
1248 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1249
1250 assert(glsl_type_is_array(deref->type));
1251 assert(glsl_get_length(deref->type) == 0);
1252 unsigned stride = glsl_get_explicit_stride(deref->type);
1253 assert(stride > 0);
1254
1255 assert(addr_format == nir_address_format_32bit_index_offset ||
1256 addr_format == nir_address_format_vec2_index_32bit_offset);
1257 nir_ssa_def *addr = &deref->dest.ssa;
1258 nir_ssa_def *index = addr_to_index(b, addr, addr_format);
1259 nir_ssa_def *offset = addr_to_offset(b, addr, addr_format);
1260
1261 nir_intrinsic_instr *bsize =
1262 nir_intrinsic_instr_create(b->shader, nir_intrinsic_get_buffer_size);
1263 bsize->src[0] = nir_src_for_ssa(index);
1264 nir_ssa_dest_init(&bsize->instr, &bsize->dest, 1, 32, NULL);
1265 nir_builder_instr_insert(b, &bsize->instr);
1266
1267 nir_ssa_def *arr_size =
1268 nir_idiv(b, nir_isub(b, &bsize->dest.ssa, offset),
1269 nir_imm_int(b, stride));
1270
1271 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(arr_size));
1272 nir_instr_remove(&intrin->instr);
1273 }
1274
1275 static bool
1276 nir_lower_explicit_io_impl(nir_function_impl *impl, nir_variable_mode modes,
1277 nir_address_format addr_format)
1278 {
1279 bool progress = false;
1280
1281 nir_builder b;
1282 nir_builder_init(&b, impl);
1283
1284 /* Walk in reverse order so that we can see the full deref chain when we
1285 * lower the access operations. We lower them assuming that the derefs
1286 * will be turned into address calculations later.
1287 */
1288 nir_foreach_block_reverse(block, impl) {
1289 nir_foreach_instr_reverse_safe(instr, block) {
1290 switch (instr->type) {
1291 case nir_instr_type_deref: {
1292 nir_deref_instr *deref = nir_instr_as_deref(instr);
1293 if (deref->mode & modes) {
1294 lower_explicit_io_deref(&b, deref, addr_format);
1295 progress = true;
1296 }
1297 break;
1298 }
1299
1300 case nir_instr_type_intrinsic: {
1301 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1302 switch (intrin->intrinsic) {
1303 case nir_intrinsic_load_deref:
1304 case nir_intrinsic_store_deref:
1305 case nir_intrinsic_deref_atomic_add:
1306 case nir_intrinsic_deref_atomic_imin:
1307 case nir_intrinsic_deref_atomic_umin:
1308 case nir_intrinsic_deref_atomic_imax:
1309 case nir_intrinsic_deref_atomic_umax:
1310 case nir_intrinsic_deref_atomic_and:
1311 case nir_intrinsic_deref_atomic_or:
1312 case nir_intrinsic_deref_atomic_xor:
1313 case nir_intrinsic_deref_atomic_exchange:
1314 case nir_intrinsic_deref_atomic_comp_swap:
1315 case nir_intrinsic_deref_atomic_fadd:
1316 case nir_intrinsic_deref_atomic_fmin:
1317 case nir_intrinsic_deref_atomic_fmax:
1318 case nir_intrinsic_deref_atomic_fcomp_swap: {
1319 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1320 if (deref->mode & modes) {
1321 lower_explicit_io_access(&b, intrin, addr_format);
1322 progress = true;
1323 }
1324 break;
1325 }
1326
1327 case nir_intrinsic_deref_buffer_array_length: {
1328 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1329 if (deref->mode & modes) {
1330 lower_explicit_io_array_length(&b, intrin, addr_format);
1331 progress = true;
1332 }
1333 break;
1334 }
1335
1336 default:
1337 break;
1338 }
1339 break;
1340 }
1341
1342 default:
1343 /* Nothing to do */
1344 break;
1345 }
1346 }
1347 }
1348
1349 if (progress) {
1350 nir_metadata_preserve(impl, nir_metadata_block_index |
1351 nir_metadata_dominance);
1352 }
1353
1354 return progress;
1355 }
1356
1357 /** Lower explicitly laid out I/O access to byte offset/address intrinsics
1358 *
1359 * This pass is intended to be used for any I/O which touches memory external
1360 * to the shader or which is directly visible to the client. It requires that
1361 * all data types in the given modes have a explicit stride/offset decorations
1362 * to tell it exactly how to calculate the offset/address for the given load,
1363 * store, or atomic operation. If the offset/stride information does not come
1364 * from the client explicitly (as with shared variables in GL or Vulkan),
1365 * nir_lower_vars_to_explicit_types() can be used to add them.
1366 *
1367 * Unlike nir_lower_io, this pass is fully capable of handling incomplete
1368 * pointer chains which may contain cast derefs. It does so by walking the
1369 * deref chain backwards and simply replacing each deref, one at a time, with
1370 * the appropriate address calculation. The pass takes a nir_address_format
1371 * parameter which describes how the offset or address is to be represented
1372 * during calculations. By ensuring that the address is always in a
1373 * consistent format, pointers can safely be conjured from thin air by the
1374 * driver, stored to variables, passed through phis, etc.
1375 *
1376 * The one exception to the simple algorithm described above is for handling
1377 * row-major matrices in which case we may look down one additional level of
1378 * the deref chain.
1379 */
1380 bool
1381 nir_lower_explicit_io(nir_shader *shader, nir_variable_mode modes,
1382 nir_address_format addr_format)
1383 {
1384 bool progress = false;
1385
1386 nir_foreach_function(function, shader) {
1387 if (function->impl &&
1388 nir_lower_explicit_io_impl(function->impl, modes, addr_format))
1389 progress = true;
1390 }
1391
1392 return progress;
1393 }
1394
1395 static bool
1396 nir_lower_vars_to_explicit_types_impl(nir_function_impl *impl,
1397 nir_variable_mode modes,
1398 glsl_type_size_align_func type_info)
1399 {
1400 bool progress = false;
1401
1402 nir_foreach_block(block, impl) {
1403 nir_foreach_instr(instr, block) {
1404 if (instr->type != nir_instr_type_deref)
1405 continue;
1406
1407 nir_deref_instr *deref = nir_instr_as_deref(instr);
1408 if (!(deref->mode & modes))
1409 continue;
1410
1411 unsigned size, alignment;
1412 const struct glsl_type *new_type =
1413 glsl_get_explicit_type_for_size_align(deref->type, type_info, &size, &alignment);
1414 if (new_type != deref->type) {
1415 progress = true;
1416 deref->type = new_type;
1417 }
1418 if (deref->deref_type == nir_deref_type_cast) {
1419 /* See also glsl_type::get_explicit_type_for_size_align() */
1420 unsigned new_stride = align(size, alignment);
1421 if (new_stride != deref->cast.ptr_stride) {
1422 deref->cast.ptr_stride = new_stride;
1423 progress = true;
1424 }
1425 }
1426 }
1427 }
1428
1429 if (progress) {
1430 nir_metadata_preserve(impl, nir_metadata_block_index |
1431 nir_metadata_dominance |
1432 nir_metadata_live_ssa_defs |
1433 nir_metadata_loop_analysis);
1434 }
1435
1436 return progress;
1437 }
1438
1439 static bool
1440 lower_vars_to_explicit(nir_shader *shader,
1441 struct exec_list *vars, nir_variable_mode mode,
1442 glsl_type_size_align_func type_info)
1443 {
1444 bool progress = false;
1445 unsigned offset;
1446 switch (mode) {
1447 case nir_var_function_temp:
1448 case nir_var_shader_temp:
1449 offset = shader->scratch_size;
1450 break;
1451 case nir_var_mem_shared:
1452 offset = 0;
1453 break;
1454 default:
1455 unreachable("Unsupported mode");
1456 }
1457 nir_foreach_variable(var, vars) {
1458 unsigned size, align;
1459 const struct glsl_type *explicit_type =
1460 glsl_get_explicit_type_for_size_align(var->type, type_info, &size, &align);
1461
1462 if (explicit_type != var->type) {
1463 progress = true;
1464 var->type = explicit_type;
1465 }
1466
1467 var->data.driver_location = ALIGN_POT(offset, align);
1468 offset = var->data.driver_location + size;
1469 }
1470
1471 switch (mode) {
1472 case nir_var_shader_temp:
1473 case nir_var_function_temp:
1474 shader->scratch_size = offset;
1475 break;
1476 case nir_var_mem_shared:
1477 shader->info.cs.shared_size = offset;
1478 shader->num_shared = offset;
1479 break;
1480 default:
1481 unreachable("Unsupported mode");
1482 }
1483
1484 return progress;
1485 }
1486
1487 bool
1488 nir_lower_vars_to_explicit_types(nir_shader *shader,
1489 nir_variable_mode modes,
1490 glsl_type_size_align_func type_info)
1491 {
1492 /* TODO: Situations which need to be handled to support more modes:
1493 * - row-major matrices
1494 * - compact shader inputs/outputs
1495 * - interface types
1496 */
1497 ASSERTED nir_variable_mode supported = nir_var_mem_shared |
1498 nir_var_shader_temp | nir_var_function_temp;
1499 assert(!(modes & ~supported) && "unsupported");
1500
1501 bool progress = false;
1502
1503 if (modes & nir_var_mem_shared)
1504 progress |= lower_vars_to_explicit(shader, &shader->shared, nir_var_mem_shared, type_info);
1505 if (modes & nir_var_shader_temp)
1506 progress |= lower_vars_to_explicit(shader, &shader->globals, nir_var_shader_temp, type_info);
1507
1508 nir_foreach_function(function, shader) {
1509 if (function->impl) {
1510 if (modes & nir_var_function_temp)
1511 progress |= lower_vars_to_explicit(shader, &function->impl->locals, nir_var_function_temp, type_info);
1512
1513 progress |= nir_lower_vars_to_explicit_types_impl(function->impl, modes, type_info);
1514 }
1515 }
1516
1517 return progress;
1518 }
1519
1520 /**
1521 * Return the offset source for a load/store intrinsic.
1522 */
1523 nir_src *
1524 nir_get_io_offset_src(nir_intrinsic_instr *instr)
1525 {
1526 switch (instr->intrinsic) {
1527 case nir_intrinsic_load_input:
1528 case nir_intrinsic_load_output:
1529 case nir_intrinsic_load_shared:
1530 case nir_intrinsic_load_uniform:
1531 case nir_intrinsic_load_global:
1532 case nir_intrinsic_load_scratch:
1533 case nir_intrinsic_load_fs_input_interp_deltas:
1534 return &instr->src[0];
1535 case nir_intrinsic_load_ubo:
1536 case nir_intrinsic_load_ssbo:
1537 case nir_intrinsic_load_per_vertex_input:
1538 case nir_intrinsic_load_per_vertex_output:
1539 case nir_intrinsic_load_interpolated_input:
1540 case nir_intrinsic_store_output:
1541 case nir_intrinsic_store_shared:
1542 case nir_intrinsic_store_global:
1543 case nir_intrinsic_store_scratch:
1544 case nir_intrinsic_ssbo_atomic_add:
1545 case nir_intrinsic_ssbo_atomic_imin:
1546 case nir_intrinsic_ssbo_atomic_umin:
1547 case nir_intrinsic_ssbo_atomic_imax:
1548 case nir_intrinsic_ssbo_atomic_umax:
1549 case nir_intrinsic_ssbo_atomic_and:
1550 case nir_intrinsic_ssbo_atomic_or:
1551 case nir_intrinsic_ssbo_atomic_xor:
1552 case nir_intrinsic_ssbo_atomic_exchange:
1553 case nir_intrinsic_ssbo_atomic_comp_swap:
1554 case nir_intrinsic_ssbo_atomic_fadd:
1555 case nir_intrinsic_ssbo_atomic_fmin:
1556 case nir_intrinsic_ssbo_atomic_fmax:
1557 case nir_intrinsic_ssbo_atomic_fcomp_swap:
1558 return &instr->src[1];
1559 case nir_intrinsic_store_ssbo:
1560 case nir_intrinsic_store_per_vertex_output:
1561 return &instr->src[2];
1562 default:
1563 return NULL;
1564 }
1565 }
1566
1567 /**
1568 * Return the vertex index source for a load/store per_vertex intrinsic.
1569 */
1570 nir_src *
1571 nir_get_io_vertex_index_src(nir_intrinsic_instr *instr)
1572 {
1573 switch (instr->intrinsic) {
1574 case nir_intrinsic_load_per_vertex_input:
1575 case nir_intrinsic_load_per_vertex_output:
1576 return &instr->src[0];
1577 case nir_intrinsic_store_per_vertex_output:
1578 return &instr->src[1];
1579 default:
1580 return NULL;
1581 }
1582 }
1583
1584 /**
1585 * Return the numeric constant that identify a NULL pointer for each address
1586 * format.
1587 */
1588 const nir_const_value *
1589 nir_address_format_null_value(nir_address_format addr_format)
1590 {
1591 const static nir_const_value null_values[][NIR_MAX_VEC_COMPONENTS] = {
1592 [nir_address_format_32bit_global] = {{0}},
1593 [nir_address_format_64bit_global] = {{0}},
1594 [nir_address_format_64bit_bounded_global] = {{0}},
1595 [nir_address_format_32bit_index_offset] = {{.u32 = ~0}, {.u32 = ~0}},
1596 [nir_address_format_vec2_index_32bit_offset] = {{.u32 = ~0}, {.u32 = ~0}, {.u32 = ~0}},
1597 [nir_address_format_32bit_offset] = {{.u32 = ~0}},
1598 [nir_address_format_logical] = {{.u32 = ~0}},
1599 };
1600
1601 assert(addr_format < ARRAY_SIZE(null_values));
1602 return null_values[addr_format];
1603 }
1604
1605 nir_ssa_def *
1606 nir_build_addr_ieq(nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
1607 nir_address_format addr_format)
1608 {
1609 switch (addr_format) {
1610 case nir_address_format_32bit_global:
1611 case nir_address_format_64bit_global:
1612 case nir_address_format_64bit_bounded_global:
1613 case nir_address_format_32bit_index_offset:
1614 case nir_address_format_vec2_index_32bit_offset:
1615 case nir_address_format_32bit_offset:
1616 return nir_ball_iequal(b, addr0, addr1);
1617
1618 case nir_address_format_logical:
1619 unreachable("Unsupported address format");
1620 }
1621
1622 unreachable("Invalid address format");
1623 }
1624
1625 nir_ssa_def *
1626 nir_build_addr_isub(nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
1627 nir_address_format addr_format)
1628 {
1629 switch (addr_format) {
1630 case nir_address_format_32bit_global:
1631 case nir_address_format_64bit_global:
1632 case nir_address_format_32bit_offset:
1633 assert(addr0->num_components == 1);
1634 assert(addr1->num_components == 1);
1635 return nir_isub(b, addr0, addr1);
1636
1637 case nir_address_format_64bit_bounded_global:
1638 return nir_isub(b, addr_to_global(b, addr0, addr_format),
1639 addr_to_global(b, addr1, addr_format));
1640
1641 case nir_address_format_32bit_index_offset:
1642 assert(addr0->num_components == 2);
1643 assert(addr1->num_components == 2);
1644 /* Assume the same buffer index. */
1645 return nir_isub(b, nir_channel(b, addr0, 1), nir_channel(b, addr1, 1));
1646
1647 case nir_address_format_vec2_index_32bit_offset:
1648 assert(addr0->num_components == 3);
1649 assert(addr1->num_components == 3);
1650 /* Assume the same buffer index. */
1651 return nir_isub(b, nir_channel(b, addr0, 2), nir_channel(b, addr1, 2));
1652
1653 case nir_address_format_logical:
1654 unreachable("Unsupported address format");
1655 }
1656
1657 unreachable("Invalid address format");
1658 }
1659
1660 static bool
1661 is_input(nir_intrinsic_instr *intrin)
1662 {
1663 return intrin->intrinsic == nir_intrinsic_load_input ||
1664 intrin->intrinsic == nir_intrinsic_load_per_vertex_input ||
1665 intrin->intrinsic == nir_intrinsic_load_interpolated_input ||
1666 intrin->intrinsic == nir_intrinsic_load_fs_input_interp_deltas;
1667 }
1668
1669 static bool
1670 is_output(nir_intrinsic_instr *intrin)
1671 {
1672 return intrin->intrinsic == nir_intrinsic_load_output ||
1673 intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
1674 intrin->intrinsic == nir_intrinsic_store_output ||
1675 intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
1676 }
1677
1678
1679 /**
1680 * This pass adds constant offsets to instr->const_index[0] for input/output
1681 * intrinsics, and resets the offset source to 0. Non-constant offsets remain
1682 * unchanged - since we don't know what part of a compound variable is
1683 * accessed, we allocate storage for the entire thing. For drivers that use
1684 * nir_lower_io_to_temporaries() before nir_lower_io(), this guarantees that
1685 * the offset source will be 0, so that they don't have to add it in manually.
1686 */
1687
1688 static bool
1689 add_const_offset_to_base_block(nir_block *block, nir_builder *b,
1690 nir_variable_mode mode)
1691 {
1692 bool progress = false;
1693 nir_foreach_instr_safe(instr, block) {
1694 if (instr->type != nir_instr_type_intrinsic)
1695 continue;
1696
1697 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1698
1699 if ((mode == nir_var_shader_in && is_input(intrin)) ||
1700 (mode == nir_var_shader_out && is_output(intrin))) {
1701 nir_src *offset = nir_get_io_offset_src(intrin);
1702
1703 if (nir_src_is_const(*offset)) {
1704 intrin->const_index[0] += nir_src_as_uint(*offset);
1705 b->cursor = nir_before_instr(&intrin->instr);
1706 nir_instr_rewrite_src(&intrin->instr, offset,
1707 nir_src_for_ssa(nir_imm_int(b, 0)));
1708 progress = true;
1709 }
1710 }
1711 }
1712
1713 return progress;
1714 }
1715
1716 bool
1717 nir_io_add_const_offset_to_base(nir_shader *nir, nir_variable_mode mode)
1718 {
1719 bool progress = false;
1720
1721 nir_foreach_function(f, nir) {
1722 if (f->impl) {
1723 nir_builder b;
1724 nir_builder_init(&b, f->impl);
1725 nir_foreach_block(block, f->impl) {
1726 progress |= add_const_offset_to_base_block(block, &b, mode);
1727 }
1728 }
1729 }
1730
1731 return progress;
1732 }
1733