nir: convert lower_io_to_scalar to deref instructions
[mesa.git] / src / compiler / nir / nir_lower_io_to_scalar.c
1 /*
2 * Copyright © 2016 Broadcom
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
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27
28 /** @file nir_lower_io_to_scalar.c
29 *
30 * Replaces nir_load_input/nir_store_output operations with num_components !=
31 * 1 with individual per-channel operations.
32 */
33
34 static void
35 lower_load_input_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
36 {
37 b->cursor = nir_before_instr(&intr->instr);
38
39 assert(intr->dest.is_ssa);
40
41 nir_ssa_def *loads[4];
42
43 for (unsigned i = 0; i < intr->num_components; i++) {
44 nir_intrinsic_instr *chan_intr =
45 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
46 nir_ssa_dest_init(&chan_intr->instr, &chan_intr->dest,
47 1, intr->dest.ssa.bit_size, NULL);
48 chan_intr->num_components = 1;
49
50 nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
51 nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
52 /* offset */
53 nir_src_copy(&chan_intr->src[0], &intr->src[0], chan_intr);
54
55 nir_builder_instr_insert(b, &chan_intr->instr);
56
57 loads[i] = &chan_intr->dest.ssa;
58 }
59
60 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
61 nir_src_for_ssa(nir_vec(b, loads,
62 intr->num_components)));
63 nir_instr_remove(&intr->instr);
64 }
65
66 static void
67 lower_store_output_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
68 {
69 b->cursor = nir_before_instr(&intr->instr);
70
71 nir_ssa_def *value = nir_ssa_for_src(b, intr->src[0], intr->num_components);
72
73 for (unsigned i = 0; i < intr->num_components; i++) {
74 if (!(nir_intrinsic_write_mask(intr) & (1 << i)))
75 continue;
76
77 nir_intrinsic_instr *chan_intr =
78 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
79 chan_intr->num_components = 1;
80
81 nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
82 nir_intrinsic_set_write_mask(chan_intr, 0x1);
83 nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
84
85 /* value */
86 chan_intr->src[0] = nir_src_for_ssa(nir_channel(b, value, i));
87 /* offset */
88 nir_src_copy(&chan_intr->src[1], &intr->src[1], chan_intr);
89
90 nir_builder_instr_insert(b, &chan_intr->instr);
91 }
92
93 nir_instr_remove(&intr->instr);
94 }
95
96 void
97 nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask)
98 {
99 nir_foreach_function(function, shader) {
100 if (function->impl) {
101 nir_builder b;
102 nir_builder_init(&b, function->impl);
103
104 nir_foreach_block(block, function->impl) {
105 nir_foreach_instr_safe(instr, block) {
106 if (instr->type != nir_instr_type_intrinsic)
107 continue;
108
109 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
110
111 if (intr->num_components == 1)
112 continue;
113
114 switch (intr->intrinsic) {
115 case nir_intrinsic_load_input:
116 if (mask & nir_var_shader_in)
117 lower_load_input_to_scalar(&b, intr);
118 break;
119 case nir_intrinsic_store_output:
120 if (mask & nir_var_shader_out)
121 lower_store_output_to_scalar(&b, intr);
122 break;
123 default:
124 break;
125 }
126 }
127 }
128 }
129 }
130 }
131
132 static nir_variable **
133 get_channel_variables(struct hash_table *ht, nir_variable *var)
134 {
135 nir_variable **chan_vars;
136 struct hash_entry *entry = _mesa_hash_table_search(ht, var);
137 if (!entry) {
138 chan_vars = (nir_variable **) calloc(4, sizeof(nir_variable *));
139 _mesa_hash_table_insert(ht, var, chan_vars);
140 } else {
141 chan_vars = (nir_variable **) entry->data;
142 }
143
144 return chan_vars;
145 }
146
147 /*
148 * Note that the src deref that we are cloning is the head of the
149 * chain of deref instructions from the original intrinsic, but
150 * the dst we are cloning to is the tail (because chains of deref
151 * instructions are created back to front)
152 */
153
154 static nir_deref_instr *
155 clone_deref_array(nir_builder *b, nir_deref_instr *dst_tail,
156 const nir_deref_instr *src_head)
157 {
158 const nir_deref_instr *parent = nir_deref_instr_parent(src_head);
159
160 if (!parent)
161 return dst_tail;
162
163 assert(src_head->deref_type == nir_deref_type_array);
164
165 dst_tail = clone_deref_array(b, dst_tail, parent);
166
167 return nir_build_deref_array(b, dst_tail,
168 nir_ssa_for_src(b, src_head->arr.index, 1));
169 }
170
171 static void
172 lower_load_to_scalar_early(nir_builder *b, nir_intrinsic_instr *intr,
173 nir_variable *var, struct hash_table *split_inputs,
174 struct hash_table *split_outputs)
175 {
176 b->cursor = nir_before_instr(&intr->instr);
177
178 assert(intr->dest.is_ssa);
179
180 nir_ssa_def *loads[4];
181
182 nir_variable **chan_vars;
183 if (var->data.mode == nir_var_shader_in) {
184 chan_vars = get_channel_variables(split_inputs, var);
185 } else {
186 chan_vars = get_channel_variables(split_outputs, var);
187 }
188
189 for (unsigned i = 0; i < intr->num_components; i++) {
190 nir_variable *chan_var = chan_vars[var->data.location_frac + i];
191 if (!chan_vars[var->data.location_frac + i]) {
192 chan_var = nir_variable_clone(var, b->shader);
193 chan_var->data.location_frac = var->data.location_frac + i;
194 chan_var->type = glsl_channel_type(chan_var->type);
195
196 chan_vars[var->data.location_frac + i] = chan_var;
197
198 nir_shader_add_variable(b->shader, chan_var);
199 }
200
201 nir_intrinsic_instr *chan_intr =
202 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
203 nir_ssa_dest_init(&chan_intr->instr, &chan_intr->dest,
204 1, intr->dest.ssa.bit_size, NULL);
205 chan_intr->num_components = 1;
206
207 nir_deref_instr *deref = nir_build_deref_var(b, chan_var);
208
209 deref = clone_deref_array(b, deref, nir_src_as_deref(intr->src[0]));
210
211 chan_intr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
212
213 if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset ||
214 intr->intrinsic == nir_intrinsic_interp_deref_at_sample)
215 nir_src_copy(&chan_intr->src[1], &intr->src[1], &chan_intr->instr);
216
217 nir_builder_instr_insert(b, &chan_intr->instr);
218
219 loads[i] = &chan_intr->dest.ssa;
220 }
221
222 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
223 nir_src_for_ssa(nir_vec(b, loads,
224 intr->num_components)));
225
226 /* Remove the old load intrinsic */
227 nir_instr_remove(&intr->instr);
228 }
229
230 static void
231 lower_store_output_to_scalar_early(nir_builder *b, nir_intrinsic_instr *intr,
232 nir_variable *var,
233 struct hash_table *split_outputs)
234 {
235 b->cursor = nir_before_instr(&intr->instr);
236
237 nir_ssa_def *value = nir_ssa_for_src(b, intr->src[1], intr->num_components);
238
239 nir_variable **chan_vars = get_channel_variables(split_outputs, var);
240 for (unsigned i = 0; i < intr->num_components; i++) {
241 if (!(nir_intrinsic_write_mask(intr) & (1 << i)))
242 continue;
243
244 nir_variable *chan_var = chan_vars[var->data.location_frac + i];
245 if (!chan_vars[var->data.location_frac + i]) {
246 chan_var = nir_variable_clone(var, b->shader);
247 chan_var->data.location_frac = var->data.location_frac + i;
248 chan_var->type = glsl_channel_type(chan_var->type);
249
250 chan_vars[var->data.location_frac + i] = chan_var;
251
252 nir_shader_add_variable(b->shader, chan_var);
253 }
254
255 nir_intrinsic_instr *chan_intr =
256 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
257 chan_intr->num_components = 1;
258
259 nir_intrinsic_set_write_mask(chan_intr, 0x1);
260
261 nir_deref_instr *deref = nir_build_deref_var(b, chan_var);
262
263 deref = clone_deref_array(b, deref, nir_src_as_deref(intr->src[0]));
264
265 chan_intr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
266 chan_intr->src[1] = nir_src_for_ssa(nir_channel(b, value, i));
267
268 nir_builder_instr_insert(b, &chan_intr->instr);
269 }
270
271 /* Remove the old store intrinsic */
272 nir_instr_remove(&intr->instr);
273 }
274
275 /*
276 * This function is intended to be called earlier than nir_lower_io_to_scalar()
277 * i.e. before nir_lower_io() is called.
278 */
279 void
280 nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask)
281 {
282 struct hash_table *split_inputs =
283 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
284 _mesa_key_pointer_equal);
285 struct hash_table *split_outputs =
286 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
287 _mesa_key_pointer_equal);
288
289 nir_assert_unlowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs);
290
291 nir_foreach_function(function, shader) {
292 if (function->impl) {
293 nir_builder b;
294 nir_builder_init(&b, function->impl);
295
296 nir_foreach_block(block, function->impl) {
297 nir_foreach_instr_safe(instr, block) {
298 if (instr->type != nir_instr_type_intrinsic)
299 continue;
300
301 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
302
303 if (intr->num_components == 1)
304 continue;
305
306 if (intr->intrinsic != nir_intrinsic_load_deref &&
307 intr->intrinsic != nir_intrinsic_store_deref &&
308 intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
309 intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
310 intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
311 continue;
312
313 nir_variable *var =
314 nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
315 nir_variable_mode mode = var->data.mode;
316
317 /* TODO: add patch support */
318 if (var->data.patch)
319 continue;
320
321 /* TODO: add doubles support */
322 if (glsl_type_is_64bit(glsl_without_array(var->type)))
323 continue;
324
325 if (var->data.location < VARYING_SLOT_VAR0 &&
326 var->data.location >= 0)
327 continue;
328
329 /* Don't bother splitting if we can't opt away any unused
330 * components.
331 */
332 if (var->data.always_active_io)
333 continue;
334
335 /* Skip types we cannot split */
336 if (glsl_type_is_matrix(glsl_without_array(var->type)) ||
337 glsl_type_is_struct(glsl_without_array(var->type)))
338 continue;
339
340 switch (intr->intrinsic) {
341 case nir_intrinsic_interp_deref_at_centroid:
342 case nir_intrinsic_interp_deref_at_sample:
343 case nir_intrinsic_interp_deref_at_offset:
344 case nir_intrinsic_load_deref:
345 if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
346 (mask & nir_var_shader_out && mode == nir_var_shader_out))
347 lower_load_to_scalar_early(&b, intr, var, split_inputs,
348 split_outputs);
349 break;
350 case nir_intrinsic_store_deref:
351 if (mask & nir_var_shader_out &&
352 mode == nir_var_shader_out)
353 lower_store_output_to_scalar_early(&b, intr, var,
354 split_outputs);
355 break;
356 default:
357 break;
358 }
359 }
360 }
361 }
362 }
363
364 /* Remove old input from the shaders inputs list */
365 struct hash_entry *entry;
366 hash_table_foreach(split_inputs, entry) {
367 nir_variable *var = (nir_variable *) entry->key;
368 exec_node_remove(&var->node);
369
370 free(entry->data);
371 }
372
373 /* Remove old output from the shaders outputs list */
374 hash_table_foreach(split_outputs, entry) {
375 nir_variable *var = (nir_variable *) entry->key;
376 exec_node_remove(&var->node);
377
378 free(entry->data);
379 }
380
381 _mesa_hash_table_destroy(split_inputs, NULL);
382 _mesa_hash_table_destroy(split_outputs, NULL);
383
384 nir_remove_dead_derefs(shader);
385 }