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