nir/algebraic: Simplify fsat of fsign
[mesa.git] / src / compiler / nir / nir_lower_io_arrays_to_elements.c
1 /*
2 * Copyright © 2017 Timothy Arceri
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_arrays_to_elements.c
29 *
30 * Split arrays/matrices with direct indexing into individual elements. This
31 * will allow optimisation passes to better clean up unused elements.
32 *
33 */
34
35 static unsigned
36 get_io_offset(nir_builder *b, nir_deref_instr *deref, nir_variable *var,
37 unsigned *element_index, nir_ssa_def **vertex_index)
38 {
39 nir_deref_path path;
40 nir_deref_path_init(&path, deref, NULL);
41
42 assert(path.path[0]->deref_type == nir_deref_type_var);
43 nir_deref_instr **p = &path.path[1];
44
45 /* For per-vertex input arrays (i.e. geometry shader inputs), skip the
46 * outermost array index. Process the rest normally.
47 */
48 if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
49 *vertex_index = nir_ssa_for_src(b, (*p)->arr.index, 1);
50 p++;
51 }
52
53 unsigned offset = 0;
54 for (; *p; p++) {
55 if ((*p)->deref_type == nir_deref_type_array) {
56 nir_const_value *c = nir_src_as_const_value((*p)->arr.index);
57
58 assert(c); /* must not be indirect dereference */
59
60 unsigned size = glsl_count_attribute_slots((*p)->type, false);
61 offset += size * c->u32[0];
62
63 unsigned num_elements = glsl_type_is_array((*p)->type) ?
64 glsl_get_aoa_size((*p)->type) : 1;
65
66 num_elements *= glsl_type_is_matrix(glsl_without_array((*p)->type)) ?
67 glsl_get_matrix_columns(glsl_without_array((*p)->type)) : 1;
68
69 *element_index += num_elements * c->u32[0];
70 } else if ((*p)->deref_type == nir_deref_type_struct) {
71 /* TODO: we could also add struct splitting support to this pass */
72 break;
73 }
74 }
75
76 nir_deref_path_finish(&path);
77
78 return offset;
79 }
80
81 static nir_variable **
82 get_array_elements(struct hash_table *ht, nir_variable *var,
83 gl_shader_stage stage)
84 {
85 nir_variable **elements;
86 struct hash_entry *entry = _mesa_hash_table_search(ht, var);
87 if (!entry) {
88 const struct glsl_type *type = var->type;
89 if (nir_is_per_vertex_io(var, stage)) {
90 assert(glsl_type_is_array(type));
91 type = glsl_get_array_element(type);
92 }
93
94 unsigned num_elements = glsl_type_is_array(type) ?
95 glsl_get_aoa_size(type) : 1;
96
97 num_elements *= glsl_type_is_matrix(glsl_without_array(type)) ?
98 glsl_get_matrix_columns(glsl_without_array(type)) : 1;
99
100 elements = (nir_variable **) calloc(num_elements, sizeof(nir_variable *));
101 _mesa_hash_table_insert(ht, var, elements);
102 } else {
103 elements = (nir_variable **) entry->data;
104 }
105
106 return elements;
107 }
108
109 static void
110 lower_array(nir_builder *b, nir_intrinsic_instr *intr, nir_variable *var,
111 struct hash_table *varyings)
112 {
113 b->cursor = nir_before_instr(&intr->instr);
114
115 nir_variable **elements =
116 get_array_elements(varyings, var, b->shader->info.stage);
117
118 nir_ssa_def *vertex_index = NULL;
119 unsigned elements_index = 0;
120 unsigned io_offset = get_io_offset(b, nir_src_as_deref(intr->src[0]),
121 var, &elements_index, &vertex_index);
122
123 nir_variable *element = elements[elements_index];
124 if (!element) {
125 element = nir_variable_clone(var, b->shader);
126 element->data.location = var->data.location + io_offset;
127
128 const struct glsl_type *type = glsl_without_array(element->type);
129
130 /* This pass also splits matrices so we need give them a new type. */
131 if (glsl_type_is_matrix(type)) {
132 type = glsl_vector_type(glsl_get_base_type(type),
133 glsl_get_vector_elements(type));
134 }
135
136 if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
137 type = glsl_get_array_instance(type,
138 glsl_get_length(element->type));
139 }
140
141 element->type = type;
142 elements[elements_index] = element;
143
144 nir_shader_add_variable(b->shader, element);
145 }
146
147 nir_deref_instr *element_deref = nir_build_deref_var(b, element);
148
149 if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
150 assert(vertex_index);
151 element_deref = nir_build_deref_array(b, element_deref, vertex_index);
152 }
153
154 nir_intrinsic_instr *element_intr =
155 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
156 element_intr->num_components = intr->num_components;
157 element_intr->src[0] = nir_src_for_ssa(&element_deref->dest.ssa);
158
159 if (intr->intrinsic != nir_intrinsic_store_deref) {
160 nir_ssa_dest_init(&element_intr->instr, &element_intr->dest,
161 intr->num_components, intr->dest.ssa.bit_size, NULL);
162
163 if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset ||
164 intr->intrinsic == nir_intrinsic_interp_deref_at_sample) {
165 nir_src_copy(&element_intr->src[1], &intr->src[1],
166 &element_intr->instr);
167 }
168
169 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
170 nir_src_for_ssa(&element_intr->dest.ssa));
171 } else {
172 nir_intrinsic_set_write_mask(element_intr,
173 nir_intrinsic_write_mask(intr));
174 nir_src_copy(&element_intr->src[1], &intr->src[1],
175 &element_intr->instr);
176 }
177
178 nir_builder_instr_insert(b, &element_intr->instr);
179
180 /* Remove the old load intrinsic */
181 nir_instr_remove(&intr->instr);
182 }
183
184 static bool
185 deref_has_indirect(nir_builder *b, nir_variable *var, nir_deref_path *path)
186 {
187 assert(path->path[0]->deref_type == nir_deref_type_var);
188 nir_deref_instr **p = &path->path[1];
189
190 if (nir_is_per_vertex_io(var, b->shader->info.stage)) {
191 p++;
192 }
193
194 for (; *p; p++) {
195 if ((*p)->deref_type != nir_deref_type_array)
196 continue;
197
198 if (!nir_src_as_const_value((*p)->arr.index))
199 return true;
200 }
201
202 return false;
203 }
204
205 /* Creates a mask of locations that contains arrays that are indexed via
206 * indirect indexing.
207 */
208 static void
209 create_indirects_mask(nir_shader *shader, uint64_t *indirects,
210 uint64_t *patch_indirects, nir_variable_mode mode)
211 {
212 nir_foreach_function(function, shader) {
213 if (function->impl) {
214 nir_builder b;
215 nir_builder_init(&b, function->impl);
216
217 nir_foreach_block(block, function->impl) {
218 nir_foreach_instr_safe(instr, block) {
219
220 if (instr->type != nir_instr_type_intrinsic)
221 continue;
222
223 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
224
225 if (intr->intrinsic != nir_intrinsic_load_deref &&
226 intr->intrinsic != nir_intrinsic_store_deref &&
227 intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
228 intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
229 intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
230 continue;
231
232 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
233 nir_variable *var = nir_deref_instr_get_variable(deref);
234
235 if (var->data.mode != mode)
236 continue;
237
238 nir_deref_path path;
239 nir_deref_path_init(&path, deref, NULL);
240
241 uint64_t loc_mask = ((uint64_t)1) << var->data.location;
242 if (var->data.patch) {
243 if (deref_has_indirect(&b, var, &path))
244 patch_indirects[var->data.location_frac] |= loc_mask;
245 } else {
246 if (deref_has_indirect(&b, var, &path))
247 indirects[var->data.location_frac] |= loc_mask;
248 }
249
250 nir_deref_path_finish(&path);
251 }
252 }
253 }
254 }
255 }
256
257 static void
258 lower_io_arrays_to_elements(nir_shader *shader, nir_variable_mode mask,
259 uint64_t *indirects, uint64_t *patch_indirects,
260 struct hash_table *varyings,
261 bool after_cross_stage_opts)
262 {
263 nir_foreach_function(function, shader) {
264 if (function->impl) {
265 nir_builder b;
266 nir_builder_init(&b, function->impl);
267
268 nir_foreach_block(block, function->impl) {
269 nir_foreach_instr_safe(instr, block) {
270 if (instr->type != nir_instr_type_intrinsic)
271 continue;
272
273 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
274
275 if (intr->intrinsic != nir_intrinsic_load_deref &&
276 intr->intrinsic != nir_intrinsic_store_deref &&
277 intr->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
278 intr->intrinsic != nir_intrinsic_interp_deref_at_sample &&
279 intr->intrinsic != nir_intrinsic_interp_deref_at_offset)
280 continue;
281
282 nir_variable *var =
283 nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
284
285 /* Skip indirects */
286 uint64_t loc_mask = ((uint64_t)1) << var->data.location;
287 if (var->data.patch) {
288 if (patch_indirects[var->data.location_frac] & loc_mask)
289 continue;
290 } else {
291 if (indirects[var->data.location_frac] & loc_mask)
292 continue;
293 }
294
295 nir_variable_mode mode = var->data.mode;
296
297 const struct glsl_type *type = var->type;
298 if (nir_is_per_vertex_io(var, b.shader->info.stage)) {
299 assert(glsl_type_is_array(type));
300 type = glsl_get_array_element(type);
301 }
302
303 /* Skip types we cannot split.
304 *
305 * TODO: Add support for struct splitting.
306 */
307 if ((!glsl_type_is_array(type) && !glsl_type_is_matrix(type))||
308 glsl_type_is_struct(glsl_without_array(type)))
309 continue;
310
311 /* Skip builtins */
312 if (!after_cross_stage_opts &&
313 var->data.location < VARYING_SLOT_VAR0 &&
314 var->data.location >= 0)
315 continue;
316
317 /* Don't bother splitting if we can't opt away any unused
318 * elements.
319 */
320 if (!after_cross_stage_opts && var->data.always_active_io)
321 continue;
322
323 switch (intr->intrinsic) {
324 case nir_intrinsic_interp_deref_at_centroid:
325 case nir_intrinsic_interp_deref_at_sample:
326 case nir_intrinsic_interp_deref_at_offset:
327 case nir_intrinsic_load_deref:
328 case nir_intrinsic_store_deref:
329 if ((mask & nir_var_shader_in && mode == nir_var_shader_in) ||
330 (mask & nir_var_shader_out && mode == nir_var_shader_out))
331 lower_array(&b, intr, var, varyings);
332 break;
333 default:
334 break;
335 }
336 }
337 }
338 }
339 }
340 }
341
342 void
343 nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
344 bool outputs_only)
345 {
346 struct hash_table *split_inputs =
347 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
348 _mesa_key_pointer_equal);
349 struct hash_table *split_outputs =
350 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
351 _mesa_key_pointer_equal);
352
353 uint64_t indirects[4] = {0}, patch_indirects[4] = {0};
354
355 lower_io_arrays_to_elements(shader, nir_var_shader_out, indirects,
356 patch_indirects, split_outputs, true);
357
358 if (!outputs_only) {
359 lower_io_arrays_to_elements(shader, nir_var_shader_in, indirects,
360 patch_indirects, split_inputs, true);
361
362 /* Remove old input from the shaders inputs list */
363 struct hash_entry *entry;
364 hash_table_foreach(split_inputs, entry) {
365 nir_variable *var = (nir_variable *) entry->key;
366 exec_node_remove(&var->node);
367
368 free(entry->data);
369 }
370 }
371
372 /* Remove old output from the shaders outputs list */
373 struct hash_entry *entry;
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 }
386
387 void
388 nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer)
389 {
390 struct hash_table *split_inputs =
391 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
392 _mesa_key_pointer_equal);
393 struct hash_table *split_outputs =
394 _mesa_hash_table_create(NULL, _mesa_hash_pointer,
395 _mesa_key_pointer_equal);
396
397 uint64_t indirects[4] = {0}, patch_indirects[4] = {0};
398 create_indirects_mask(producer, indirects, patch_indirects,
399 nir_var_shader_out);
400 create_indirects_mask(consumer, indirects, patch_indirects,
401 nir_var_shader_in);
402
403 lower_io_arrays_to_elements(producer, nir_var_shader_out, indirects,
404 patch_indirects, split_outputs, false);
405
406 lower_io_arrays_to_elements(consumer, nir_var_shader_in, indirects,
407 patch_indirects, split_inputs, false);
408
409 /* Remove old input from the shaders inputs list */
410 struct hash_entry *entry;
411 hash_table_foreach(split_inputs, entry) {
412 nir_variable *var = (nir_variable *) entry->key;
413 exec_node_remove(&var->node);
414
415 free(entry->data);
416 }
417
418 /* Remove old output from the shaders outputs list */
419 hash_table_foreach(split_outputs, entry) {
420 nir_variable *var = (nir_variable *) entry->key;
421 exec_node_remove(&var->node);
422
423 free(entry->data);
424 }
425
426 _mesa_hash_table_destroy(split_inputs, NULL);
427 _mesa_hash_table_destroy(split_outputs, NULL);
428
429 nir_remove_dead_derefs(producer);
430 nir_remove_dead_derefs(consumer);
431 }