nir: Make an easier helper for setting up SSA defs.
[mesa.git] / src / glsl / 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 * NOTE: This pass really only works for scalar backends at the moment due
34 * to the way it packes the input/output data.
35 */
36
37 #include "nir.h"
38
39 struct lower_io_state {
40 void *mem_ctx;
41 };
42
43 static unsigned
44 type_size(const struct glsl_type *type)
45 {
46 unsigned int size, i;
47
48 switch (glsl_get_base_type(type)) {
49 case GLSL_TYPE_UINT:
50 case GLSL_TYPE_INT:
51 case GLSL_TYPE_FLOAT:
52 case GLSL_TYPE_BOOL:
53 return glsl_get_components(type);
54 case GLSL_TYPE_ARRAY:
55 return type_size(glsl_get_array_element(type)) * glsl_get_length(type);
56 case GLSL_TYPE_STRUCT:
57 size = 0;
58 for (i = 0; i < glsl_get_length(type); i++) {
59 size += type_size(glsl_get_struct_field(type, i));
60 }
61 return size;
62 case GLSL_TYPE_SAMPLER:
63 return 0;
64 case GLSL_TYPE_ATOMIC_UINT:
65 return 0;
66 case GLSL_TYPE_INTERFACE:
67 return 0;
68 case GLSL_TYPE_IMAGE:
69 return 0;
70 case GLSL_TYPE_VOID:
71 case GLSL_TYPE_ERROR:
72 unreachable("not reached");
73 }
74
75 return 0;
76 }
77
78 static void
79 assign_var_locations(struct hash_table *ht, unsigned *size)
80 {
81 unsigned location = 0;
82
83 struct hash_entry *entry;
84 hash_table_foreach(ht, entry) {
85 nir_variable *var = (nir_variable *) entry->data;
86
87 /*
88 * UBO's have their own address spaces, so don't count them towards the
89 * number of global uniforms
90 */
91 if (var->data.mode == nir_var_uniform && var->interface_type != NULL)
92 continue;
93
94 var->data.driver_location = location;
95 location += type_size(var->type);
96 }
97
98 *size = location;
99 }
100
101 static void
102 assign_var_locations_shader(nir_shader *shader)
103 {
104 assign_var_locations(shader->inputs, &shader->num_inputs);
105 assign_var_locations(shader->outputs, &shader->num_outputs);
106 assign_var_locations(shader->uniforms, &shader->num_uniforms);
107 }
108
109 static bool
110 deref_has_indirect(nir_deref_var *deref)
111 {
112 for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
113 if (tail->deref_type == nir_deref_type_array) {
114 nir_deref_array *arr = nir_deref_as_array(tail);
115 if (arr->deref_array_type == nir_deref_array_type_indirect)
116 return true;
117 }
118 }
119
120 return false;
121 }
122
123 static unsigned
124 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
125 struct lower_io_state *state)
126 {
127 bool found_indirect = false;
128 unsigned base_offset = 0;
129
130 nir_deref *tail = &deref->deref;
131 while (tail->child != NULL) {
132 const struct glsl_type *parent_type = tail->type;
133 tail = tail->child;
134
135 if (tail->deref_type == nir_deref_type_array) {
136 nir_deref_array *deref_array = nir_deref_as_array(tail);
137 unsigned size = type_size(tail->type);
138
139 base_offset += size * deref_array->base_offset;
140
141 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
142 nir_load_const_instr *load_const =
143 nir_load_const_instr_create(state->mem_ctx, 1);
144 load_const->value.u[0] = size;
145 nir_instr_insert_before(instr, &load_const->instr);
146
147 nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx,
148 nir_op_imul);
149 mul->src[0].src.is_ssa = true;
150 mul->src[0].src.ssa = &load_const->def;
151 mul->src[1].src = nir_src_copy(deref_array->indirect,
152 state->mem_ctx);
153 mul->dest.write_mask = 1;
154 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
155 nir_instr_insert_before(instr, &mul->instr);
156
157 if (found_indirect) {
158 nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
159 nir_op_iadd);
160 add->src[0].src = *indirect;
161 add->src[1].src.is_ssa = true;
162 add->src[1].src.ssa = &mul->dest.dest.ssa;
163 add->dest.write_mask = 1;
164 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
165 nir_instr_insert_before(instr, &add->instr);
166
167 indirect->is_ssa = true;
168 indirect->ssa = &add->dest.dest.ssa;
169 } else {
170 indirect->is_ssa = true;
171 indirect->ssa = &mul->dest.dest.ssa;
172 found_indirect = true;
173 }
174 }
175 } else if (tail->deref_type == nir_deref_type_struct) {
176 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
177
178 for (unsigned i = 0; i < deref_struct->index; i++)
179 base_offset += type_size(glsl_get_struct_field(parent_type, i));
180 }
181 }
182
183 return base_offset;
184 }
185
186 static bool
187 nir_lower_io_block(nir_block *block, void *void_state)
188 {
189 struct lower_io_state *state = void_state;
190
191 nir_foreach_instr_safe(block, instr) {
192 if (instr->type != nir_instr_type_intrinsic)
193 continue;
194
195 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
196
197 switch (intrin->intrinsic) {
198 case nir_intrinsic_load_var: {
199 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
200 if (mode != nir_var_shader_in && mode != nir_var_uniform)
201 continue;
202
203 bool has_indirect = deref_has_indirect(intrin->variables[0]);
204
205 /* Figure out the opcode */
206 nir_intrinsic_op load_op;
207 switch (mode) {
208 case nir_var_shader_in:
209 load_op = has_indirect ? nir_intrinsic_load_input_indirect :
210 nir_intrinsic_load_input;
211 break;
212 case nir_var_uniform:
213 load_op = has_indirect ? nir_intrinsic_load_uniform_indirect :
214 nir_intrinsic_load_uniform;
215 break;
216 default:
217 unreachable("Unknown variable mode");
218 }
219
220 nir_intrinsic_instr *load = nir_intrinsic_instr_create(state->mem_ctx,
221 load_op);
222 load->num_components = intrin->num_components;
223
224 nir_src indirect;
225 unsigned offset = get_io_offset(intrin->variables[0],
226 &intrin->instr, &indirect, state);
227 offset += intrin->variables[0]->var->data.driver_location;
228
229 load->const_index[0] = offset;
230 load->const_index[1] = 1;
231
232 if (has_indirect)
233 load->src[0] = indirect;
234
235 if (intrin->dest.is_ssa) {
236 nir_ssa_dest_init(&load->instr, &load->dest,
237 intrin->num_components, NULL);
238 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
239 nir_src_for_ssa(&load->dest.ssa),
240 state->mem_ctx);
241 } else {
242 load->dest = nir_dest_copy(intrin->dest, state->mem_ctx);
243 }
244
245 nir_instr_insert_before(&intrin->instr, &load->instr);
246 nir_instr_remove(&intrin->instr);
247 break;
248 }
249
250 case nir_intrinsic_store_var: {
251 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
252 continue;
253
254 bool has_indirect = deref_has_indirect(intrin->variables[0]);
255
256 nir_intrinsic_op store_op;
257 if (has_indirect) {
258 store_op = nir_intrinsic_store_output_indirect;
259 } else {
260 store_op = nir_intrinsic_store_output;
261 }
262
263 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
264 store_op);
265 store->num_components = intrin->num_components;
266
267 nir_src indirect;
268 unsigned offset = get_io_offset(intrin->variables[0],
269 &intrin->instr, &indirect, state);
270 offset += intrin->variables[0]->var->data.driver_location;
271
272 store->const_index[0] = offset;
273 store->const_index[1] = 1;
274
275 store->src[0] = nir_src_copy(intrin->src[0], state->mem_ctx);
276
277 if (has_indirect)
278 store->src[1] = indirect;
279
280 nir_instr_insert_before(&intrin->instr, &store->instr);
281 nir_instr_remove(&intrin->instr);
282 break;
283 }
284
285 default:
286 break;
287 }
288 }
289
290 return true;
291 }
292
293 static void
294 nir_lower_io_impl(nir_function_impl *impl)
295 {
296 struct lower_io_state state;
297
298 state.mem_ctx = ralloc_parent(impl);
299
300 nir_foreach_block(impl, nir_lower_io_block, &state);
301
302 nir_metadata_preserve(impl, nir_metadata_block_index |
303 nir_metadata_dominance);
304 }
305
306 void
307 nir_lower_io(nir_shader *shader)
308 {
309 assign_var_locations_shader(shader);
310
311 nir_foreach_overload(shader, overload) {
312 if (overload->impl)
313 nir_lower_io_impl(overload->impl);
314 }
315 }