nir: Silence unused parameter warnings
[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
37 struct lower_io_state {
38 nir_builder builder;
39 void *mem_ctx;
40 int (*type_size)(const struct glsl_type *type);
41 nir_variable_mode modes;
42 };
43
44 void
45 nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
46 int (*type_size)(const struct glsl_type *))
47 {
48 unsigned location = 0;
49
50 nir_foreach_variable(var, var_list) {
51 /*
52 * UBO's have their own address spaces, so don't count them towards the
53 * number of global uniforms
54 */
55 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
56 var->interface_type != NULL)
57 continue;
58
59 var->data.driver_location = location;
60 location += type_size(var->type);
61 }
62
63 *size = location;
64 }
65
66 /**
67 * Returns true if we're processing a stage whose inputs are arrays indexed
68 * by a vertex number (such as geometry shader inputs).
69 */
70 static bool
71 is_per_vertex_input(struct lower_io_state *state, nir_variable *var)
72 {
73 gl_shader_stage stage = state->builder.shader->stage;
74
75 return var->data.mode == nir_var_shader_in && !var->data.patch &&
76 (stage == MESA_SHADER_TESS_CTRL ||
77 stage == MESA_SHADER_TESS_EVAL ||
78 stage == MESA_SHADER_GEOMETRY);
79 }
80
81 static bool
82 is_per_vertex_output(struct lower_io_state *state, nir_variable *var)
83 {
84 gl_shader_stage stage = state->builder.shader->stage;
85 return var->data.mode == nir_var_shader_out && !var->data.patch &&
86 stage == MESA_SHADER_TESS_CTRL;
87 }
88
89 static nir_ssa_def *
90 get_io_offset(nir_builder *b, nir_deref_var *deref,
91 nir_ssa_def **vertex_index,
92 int (*type_size)(const struct glsl_type *))
93 {
94 nir_deref *tail = &deref->deref;
95
96 /* For per-vertex input arrays (i.e. geometry shader inputs), keep the
97 * outermost array index separate. Process the rest normally.
98 */
99 if (vertex_index != NULL) {
100 tail = tail->child;
101 assert(tail->deref_type == nir_deref_type_array);
102 nir_deref_array *deref_array = nir_deref_as_array(tail);
103
104 nir_ssa_def *vtx = nir_imm_int(b, deref_array->base_offset);
105 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
106 vtx = nir_iadd(b, vtx, nir_ssa_for_src(b, deref_array->indirect, 1));
107 }
108 *vertex_index = vtx;
109 }
110
111 /* Just emit code and let constant-folding go to town */
112 nir_ssa_def *offset = nir_imm_int(b, 0);
113
114 while (tail->child != NULL) {
115 const struct glsl_type *parent_type = tail->type;
116 tail = tail->child;
117
118 if (tail->deref_type == nir_deref_type_array) {
119 nir_deref_array *deref_array = nir_deref_as_array(tail);
120 unsigned size = type_size(tail->type);
121
122 offset = nir_iadd(b, offset,
123 nir_imm_int(b, size * deref_array->base_offset));
124
125 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
126 nir_ssa_def *mul =
127 nir_imul(b, nir_imm_int(b, size),
128 nir_ssa_for_src(b, deref_array->indirect, 1));
129
130 offset = nir_iadd(b, offset, mul);
131 }
132 } else if (tail->deref_type == nir_deref_type_struct) {
133 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
134
135 unsigned field_offset = 0;
136 for (unsigned i = 0; i < deref_struct->index; i++) {
137 field_offset += type_size(glsl_get_struct_field(parent_type, i));
138 }
139 offset = nir_iadd(b, offset, nir_imm_int(b, field_offset));
140 }
141 }
142
143 return offset;
144 }
145
146 static nir_intrinsic_op
147 load_op(nir_variable_mode mode, bool per_vertex)
148 {
149 nir_intrinsic_op op;
150 switch (mode) {
151 case nir_var_shader_in:
152 op = per_vertex ? nir_intrinsic_load_per_vertex_input :
153 nir_intrinsic_load_input;
154 break;
155 case nir_var_shader_out:
156 op = per_vertex ? nir_intrinsic_load_per_vertex_output :
157 nir_intrinsic_load_output;
158 break;
159 case nir_var_uniform:
160 op = nir_intrinsic_load_uniform;
161 break;
162 case nir_var_shared:
163 op = nir_intrinsic_load_shared;
164 break;
165 default:
166 unreachable("Unknown variable mode");
167 }
168 return op;
169 }
170
171 static nir_intrinsic_op
172 store_op(struct lower_io_state *state,
173 nir_variable_mode mode, bool per_vertex)
174 {
175 nir_intrinsic_op op;
176 switch (mode) {
177 case nir_var_shader_in:
178 case nir_var_shader_out:
179 op = per_vertex ? nir_intrinsic_store_per_vertex_output :
180 nir_intrinsic_store_output;
181 break;
182 case nir_var_shared:
183 op = nir_intrinsic_store_shared;
184 break;
185 default:
186 unreachable("Unknown variable mode");
187 }
188 return op;
189 }
190
191 static nir_intrinsic_op
192 atomic_op(nir_intrinsic_op opcode)
193 {
194 switch (opcode) {
195 #define OP(O) case nir_intrinsic_var_##O: return nir_intrinsic_shared_##O;
196 OP(atomic_exchange)
197 OP(atomic_comp_swap)
198 OP(atomic_add)
199 OP(atomic_imin)
200 OP(atomic_umin)
201 OP(atomic_imax)
202 OP(atomic_umax)
203 OP(atomic_and)
204 OP(atomic_or)
205 OP(atomic_xor)
206 #undef OP
207 default:
208 unreachable("Invalid atomic");
209 }
210 }
211
212 static bool
213 nir_lower_io_block(nir_block *block,
214 struct lower_io_state *state)
215 {
216 nir_builder *b = &state->builder;
217
218 nir_foreach_instr_safe(instr, block) {
219 if (instr->type != nir_instr_type_intrinsic)
220 continue;
221
222 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
223
224 switch (intrin->intrinsic) {
225 case nir_intrinsic_load_var:
226 case nir_intrinsic_store_var:
227 case nir_intrinsic_var_atomic_add:
228 case nir_intrinsic_var_atomic_imin:
229 case nir_intrinsic_var_atomic_umin:
230 case nir_intrinsic_var_atomic_imax:
231 case nir_intrinsic_var_atomic_umax:
232 case nir_intrinsic_var_atomic_and:
233 case nir_intrinsic_var_atomic_or:
234 case nir_intrinsic_var_atomic_xor:
235 case nir_intrinsic_var_atomic_exchange:
236 case nir_intrinsic_var_atomic_comp_swap:
237 /* We can lower the io for this nir instrinsic */
238 break;
239 default:
240 /* We can't lower the io for this nir instrinsic, so skip it */
241 continue;
242 }
243
244 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
245
246 if ((state->modes & mode) == 0)
247 continue;
248
249 if (mode != nir_var_shader_in &&
250 mode != nir_var_shader_out &&
251 mode != nir_var_shared &&
252 mode != nir_var_uniform)
253 continue;
254
255 b->cursor = nir_before_instr(instr);
256
257 switch (intrin->intrinsic) {
258 case nir_intrinsic_load_var: {
259 bool per_vertex =
260 is_per_vertex_input(state, intrin->variables[0]->var) ||
261 is_per_vertex_output(state, intrin->variables[0]->var);
262
263 nir_ssa_def *offset;
264 nir_ssa_def *vertex_index;
265
266 offset = get_io_offset(b, intrin->variables[0],
267 per_vertex ? &vertex_index : NULL,
268 state->type_size);
269
270 nir_intrinsic_instr *load =
271 nir_intrinsic_instr_create(state->mem_ctx,
272 load_op(mode, per_vertex));
273 load->num_components = intrin->num_components;
274
275 nir_intrinsic_set_base(load,
276 intrin->variables[0]->var->data.driver_location);
277
278 if (load->intrinsic == nir_intrinsic_load_uniform) {
279 nir_intrinsic_set_range(load,
280 state->type_size(intrin->variables[0]->var->type));
281 }
282
283 if (per_vertex)
284 load->src[0] = nir_src_for_ssa(vertex_index);
285
286 load->src[per_vertex ? 1 : 0] = nir_src_for_ssa(offset);
287
288 if (intrin->dest.is_ssa) {
289 nir_ssa_dest_init(&load->instr, &load->dest,
290 intrin->num_components,
291 intrin->dest.ssa.bit_size, NULL);
292 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
293 nir_src_for_ssa(&load->dest.ssa));
294 } else {
295 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
296 }
297
298 nir_instr_insert_before(&intrin->instr, &load->instr);
299 nir_instr_remove(&intrin->instr);
300 break;
301 }
302
303 case nir_intrinsic_store_var: {
304 assert(mode == nir_var_shader_out || mode == nir_var_shared);
305
306 nir_ssa_def *offset;
307 nir_ssa_def *vertex_index;
308
309 bool per_vertex =
310 is_per_vertex_output(state, intrin->variables[0]->var);
311
312 offset = get_io_offset(b, intrin->variables[0],
313 per_vertex ? &vertex_index : NULL,
314 state->type_size);
315
316 nir_intrinsic_instr *store =
317 nir_intrinsic_instr_create(state->mem_ctx,
318 store_op(state, mode, per_vertex));
319 store->num_components = intrin->num_components;
320
321 nir_src_copy(&store->src[0], &intrin->src[0], store);
322
323 nir_intrinsic_set_base(store,
324 intrin->variables[0]->var->data.driver_location);
325 nir_intrinsic_set_write_mask(store, nir_intrinsic_write_mask(intrin));
326
327 if (per_vertex)
328 store->src[1] = nir_src_for_ssa(vertex_index);
329
330 store->src[per_vertex ? 2 : 1] = nir_src_for_ssa(offset);
331
332 nir_instr_insert_before(&intrin->instr, &store->instr);
333 nir_instr_remove(&intrin->instr);
334 break;
335 }
336
337 case nir_intrinsic_var_atomic_add:
338 case nir_intrinsic_var_atomic_imin:
339 case nir_intrinsic_var_atomic_umin:
340 case nir_intrinsic_var_atomic_imax:
341 case nir_intrinsic_var_atomic_umax:
342 case nir_intrinsic_var_atomic_and:
343 case nir_intrinsic_var_atomic_or:
344 case nir_intrinsic_var_atomic_xor:
345 case nir_intrinsic_var_atomic_exchange:
346 case nir_intrinsic_var_atomic_comp_swap: {
347 assert(mode == nir_var_shared);
348
349 nir_ssa_def *offset;
350
351 offset = get_io_offset(b, intrin->variables[0],
352 NULL, state->type_size);
353
354 nir_intrinsic_instr *atomic =
355 nir_intrinsic_instr_create(state->mem_ctx,
356 atomic_op(intrin->intrinsic));
357
358 atomic->src[0] = nir_src_for_ssa(offset);
359
360 atomic->const_index[0] =
361 intrin->variables[0]->var->data.driver_location;
362
363 for (unsigned i = 0;
364 i < nir_op_infos[intrin->intrinsic].num_inputs;
365 i++) {
366 nir_src_copy(&atomic->src[i+1], &intrin->src[i], atomic);
367 }
368
369 if (intrin->dest.is_ssa) {
370 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
371 intrin->dest.ssa.num_components,
372 intrin->dest.ssa.bit_size, NULL);
373 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
374 nir_src_for_ssa(&atomic->dest.ssa));
375 } else {
376 nir_dest_copy(&atomic->dest, &intrin->dest, state->mem_ctx);
377 }
378
379 nir_instr_insert_before(&intrin->instr, &atomic->instr);
380 nir_instr_remove(&intrin->instr);
381 break;
382 }
383
384 default:
385 break;
386 }
387 }
388
389 return true;
390 }
391
392 static void
393 nir_lower_io_impl(nir_function_impl *impl,
394 nir_variable_mode modes,
395 int (*type_size)(const struct glsl_type *))
396 {
397 struct lower_io_state state;
398
399 nir_builder_init(&state.builder, impl);
400 state.mem_ctx = ralloc_parent(impl);
401 state.modes = modes;
402 state.type_size = type_size;
403
404 nir_foreach_block(block, impl) {
405 nir_lower_io_block(block, &state);
406 }
407
408 nir_metadata_preserve(impl, nir_metadata_block_index |
409 nir_metadata_dominance);
410 }
411
412 void
413 nir_lower_io(nir_shader *shader, nir_variable_mode modes,
414 int (*type_size)(const struct glsl_type *))
415 {
416 nir_foreach_function(function, shader) {
417 if (function->impl)
418 nir_lower_io_impl(function->impl, modes, type_size);
419 }
420 }
421
422 /**
423 * Return the offset soruce for a load/store intrinsic.
424 */
425 nir_src *
426 nir_get_io_offset_src(nir_intrinsic_instr *instr)
427 {
428 switch (instr->intrinsic) {
429 case nir_intrinsic_load_input:
430 case nir_intrinsic_load_output:
431 case nir_intrinsic_load_uniform:
432 return &instr->src[0];
433 case nir_intrinsic_load_ubo:
434 case nir_intrinsic_load_ssbo:
435 case nir_intrinsic_load_per_vertex_input:
436 case nir_intrinsic_load_per_vertex_output:
437 case nir_intrinsic_store_output:
438 return &instr->src[1];
439 case nir_intrinsic_store_ssbo:
440 case nir_intrinsic_store_per_vertex_output:
441 return &instr->src[2];
442 default:
443 return NULL;
444 }
445 }
446
447 /**
448 * Return the vertex index source for a load/store per_vertex intrinsic.
449 */
450 nir_src *
451 nir_get_io_vertex_index_src(nir_intrinsic_instr *instr)
452 {
453 switch (instr->intrinsic) {
454 case nir_intrinsic_load_per_vertex_input:
455 case nir_intrinsic_load_per_vertex_output:
456 return &instr->src[0];
457 case nir_intrinsic_store_per_vertex_output:
458 return &instr->src[1];
459 default:
460 return NULL;
461 }
462 }