nir: Vectorize intrinsics
[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);
144 load_const->num_components = 1;
145 load_const->value.u[0] = size;
146 load_const->dest.is_ssa = true;
147 nir_ssa_def_init(&load_const->instr, &load_const->dest.ssa,
148 1, NULL);
149 nir_instr_insert_before(instr, &load_const->instr);
150
151 nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx,
152 nir_op_imul);
153 mul->src[0].src.is_ssa = true;
154 mul->src[0].src.ssa = &load_const->dest.ssa;
155 mul->src[1].src = nir_src_copy(deref_array->indirect,
156 state->mem_ctx);
157 mul->dest.write_mask = 1;
158 mul->dest.dest.is_ssa = true;
159 nir_ssa_def_init(&mul->instr, &mul->dest.dest.ssa, 1, NULL);
160 nir_instr_insert_before(instr, &mul->instr);
161
162 if (found_indirect) {
163 nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
164 nir_op_iadd);
165 add->src[0].src = *indirect;
166 add->src[1].src.is_ssa = true;
167 add->src[1].src.ssa = &mul->dest.dest.ssa;
168 add->dest.write_mask = 1;
169 add->dest.dest.is_ssa = true;
170 nir_ssa_def_init(&add->instr, &add->dest.dest.ssa, 1, NULL);
171 nir_instr_insert_before(instr, &add->instr);
172
173 indirect->is_ssa = true;
174 indirect->ssa = &add->dest.dest.ssa;
175 } else {
176 indirect->is_ssa = true;
177 indirect->ssa = &mul->dest.dest.ssa;
178 found_indirect = true;
179 }
180 }
181 } else if (tail->deref_type == nir_deref_type_struct) {
182 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
183
184 for (unsigned i = 0; i < deref_struct->index; i++)
185 base_offset += type_size(glsl_get_struct_field(parent_type, i));
186 }
187 }
188
189 return base_offset;
190 }
191
192 static bool
193 nir_lower_io_block(nir_block *block, void *void_state)
194 {
195 struct lower_io_state *state = void_state;
196
197 nir_foreach_instr_safe(block, instr) {
198 if (instr->type != nir_instr_type_intrinsic)
199 continue;
200
201 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
202
203 switch (intrin->intrinsic) {
204 case nir_intrinsic_load_var: {
205 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
206 if (mode != nir_var_shader_in && mode != nir_var_uniform)
207 continue;
208
209 bool has_indirect = deref_has_indirect(intrin->variables[0]);
210
211 nir_intrinsic_op load_op;
212 switch (mode) {
213 case nir_var_shader_in:
214 if (has_indirect) {
215 load_op = nir_intrinsic_load_input_indirect;
216 } else {
217 load_op = nir_intrinsic_load_input;
218 }
219 break;
220 case nir_var_uniform:
221 if (has_indirect) {
222 load_op = nir_intrinsic_load_uniform_indirect;
223 } else {
224 load_op = nir_intrinsic_load_uniform;
225 }
226 break;
227 default:
228 unreachable("Unknown variable mode");
229 }
230 nir_intrinsic_instr *load = nir_intrinsic_instr_create(state->mem_ctx,
231 load_op);
232 load->num_components = intrin->num_components;
233
234 nir_src indirect;
235 unsigned offset = get_io_offset(intrin->variables[0],
236 &intrin->instr, &indirect, state);
237 offset += intrin->variables[0]->var->data.driver_location;
238
239 load->const_index[0] = offset;
240 load->const_index[1] = 1;
241
242 if (has_indirect)
243 load->src[0] = indirect;
244
245 if (intrin->dest.is_ssa) {
246 load->dest.is_ssa = true;
247 nir_ssa_def_init(&load->instr, &load->dest.ssa,
248 intrin->num_components, NULL);
249
250 nir_src new_src = {
251 .is_ssa = true,
252 .ssa = &load->dest.ssa,
253 };
254
255 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, new_src,
256 state->mem_ctx);
257 } else {
258 load->dest = nir_dest_copy(intrin->dest, state->mem_ctx);
259 }
260
261 nir_instr_insert_before(&intrin->instr, &load->instr);
262 nir_instr_remove(&intrin->instr);
263 break;
264 }
265
266 case nir_intrinsic_store_var: {
267 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
268 continue;
269
270 bool has_indirect = deref_has_indirect(intrin->variables[0]);
271
272 nir_intrinsic_op store_op;
273 if (has_indirect) {
274 store_op = nir_intrinsic_store_output_indirect;
275 } else {
276 store_op = nir_intrinsic_store_output;
277 }
278
279 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
280 store_op);
281 store->num_components = intrin->num_components;
282
283 nir_src indirect;
284 unsigned offset = get_io_offset(intrin->variables[0],
285 &intrin->instr, &indirect, state);
286 offset += intrin->variables[0]->var->data.driver_location;
287
288 store->const_index[0] = offset;
289 store->const_index[1] = 1;
290
291 store->src[0] = nir_src_copy(intrin->src[0], state->mem_ctx);
292
293 if (has_indirect)
294 store->src[1] = indirect;
295
296 nir_instr_insert_before(&intrin->instr, &store->instr);
297 nir_instr_remove(&intrin->instr);
298 break;
299 }
300
301 default:
302 break;
303 }
304 }
305
306 return true;
307 }
308
309 static void
310 nir_lower_io_impl(nir_function_impl *impl)
311 {
312 struct lower_io_state state;
313
314 state.mem_ctx = ralloc_parent(impl);
315
316 nir_foreach_block(impl, nir_lower_io_block, &state);
317 }
318
319 void
320 nir_lower_io(nir_shader *shader)
321 {
322 assign_var_locations_shader(shader);
323
324 nir_foreach_overload(shader, overload) {
325 if (overload->impl)
326 nir_lower_io_impl(overload->impl);
327 }
328 }