2 * Copyright © 2017 Timothy Arceri
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:
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
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
25 #include "nir_builder.h"
26 #include "nir_deref.h"
28 /** @file nir_lower_io_arrays_to_elements.c
30 * Split arrays/matrices with direct indexing into individual elements. This
31 * will allow optimisation passes to better clean up unused elements.
36 get_io_offset(nir_builder
*b
, nir_deref_instr
*deref
, nir_variable
*var
,
37 unsigned *element_index
, nir_ssa_def
**vertex_index
)
40 nir_deref_path_init(&path
, deref
, NULL
);
42 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
43 nir_deref_instr
**p
= &path
.path
[1];
45 /* For per-vertex input arrays (i.e. geometry shader inputs), skip the
46 * outermost array index. Process the rest normally.
48 if (nir_is_per_vertex_io(var
, b
->shader
->info
.stage
)) {
49 *vertex_index
= nir_ssa_for_src(b
, (*p
)->arr
.index
, 1);
55 if ((*p
)->deref_type
== nir_deref_type_array
) {
56 /* must not be indirect dereference */
57 unsigned index
= nir_src_as_uint((*p
)->arr
.index
);
59 unsigned size
= glsl_count_attribute_slots((*p
)->type
, false);
60 offset
+= size
* index
;
62 unsigned num_elements
= glsl_type_is_array((*p
)->type
) ?
63 glsl_get_aoa_size((*p
)->type
) : 1;
65 num_elements
*= glsl_type_is_matrix(glsl_without_array((*p
)->type
)) ?
66 glsl_get_matrix_columns(glsl_without_array((*p
)->type
)) : 1;
68 *element_index
+= num_elements
* index
;
69 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
70 /* TODO: we could also add struct splitting support to this pass */
75 nir_deref_path_finish(&path
);
80 static nir_variable
**
81 get_array_elements(struct hash_table
*ht
, nir_variable
*var
,
82 gl_shader_stage stage
)
84 nir_variable
**elements
;
85 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, var
);
87 const struct glsl_type
*type
= var
->type
;
88 if (nir_is_per_vertex_io(var
, stage
)) {
89 assert(glsl_type_is_array(type
));
90 type
= glsl_get_array_element(type
);
93 unsigned num_elements
= glsl_type_is_array(type
) ?
94 glsl_get_aoa_size(type
) : 1;
96 num_elements
*= glsl_type_is_matrix(glsl_without_array(type
)) ?
97 glsl_get_matrix_columns(glsl_without_array(type
)) : 1;
99 elements
= (nir_variable
**) calloc(num_elements
, sizeof(nir_variable
*));
100 _mesa_hash_table_insert(ht
, var
, elements
);
102 elements
= (nir_variable
**) entry
->data
;
109 lower_array(nir_builder
*b
, nir_intrinsic_instr
*intr
, nir_variable
*var
,
110 struct hash_table
*varyings
)
112 b
->cursor
= nir_before_instr(&intr
->instr
);
114 nir_variable
**elements
=
115 get_array_elements(varyings
, var
, b
->shader
->info
.stage
);
117 nir_ssa_def
*vertex_index
= NULL
;
118 unsigned elements_index
= 0;
119 unsigned io_offset
= get_io_offset(b
, nir_src_as_deref(intr
->src
[0]),
120 var
, &elements_index
, &vertex_index
);
122 nir_variable
*element
= elements
[elements_index
];
124 element
= nir_variable_clone(var
, b
->shader
);
125 element
->data
.location
= var
->data
.location
+ io_offset
;
127 const struct glsl_type
*type
= glsl_without_array(element
->type
);
129 /* This pass also splits matrices so we need give them a new type. */
130 if (glsl_type_is_matrix(type
)) {
131 type
= glsl_vector_type(glsl_get_base_type(type
),
132 glsl_get_vector_elements(type
));
135 if (nir_is_per_vertex_io(var
, b
->shader
->info
.stage
)) {
136 type
= glsl_array_type(type
, glsl_get_length(element
->type
));
139 element
->type
= type
;
140 elements
[elements_index
] = element
;
142 nir_shader_add_variable(b
->shader
, element
);
145 nir_deref_instr
*element_deref
= nir_build_deref_var(b
, element
);
147 if (nir_is_per_vertex_io(var
, b
->shader
->info
.stage
)) {
148 assert(vertex_index
);
149 element_deref
= nir_build_deref_array(b
, element_deref
, vertex_index
);
152 nir_intrinsic_instr
*element_intr
=
153 nir_intrinsic_instr_create(b
->shader
, intr
->intrinsic
);
154 element_intr
->num_components
= intr
->num_components
;
155 element_intr
->src
[0] = nir_src_for_ssa(&element_deref
->dest
.ssa
);
157 if (intr
->intrinsic
!= nir_intrinsic_store_deref
) {
158 nir_ssa_dest_init(&element_intr
->instr
, &element_intr
->dest
,
159 intr
->num_components
, intr
->dest
.ssa
.bit_size
, NULL
);
161 if (intr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
||
162 intr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
163 nir_src_copy(&element_intr
->src
[1], &intr
->src
[1],
164 &element_intr
->instr
);
167 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
,
168 nir_src_for_ssa(&element_intr
->dest
.ssa
));
170 nir_intrinsic_set_write_mask(element_intr
,
171 nir_intrinsic_write_mask(intr
));
172 nir_src_copy(&element_intr
->src
[1], &intr
->src
[1],
173 &element_intr
->instr
);
176 nir_builder_instr_insert(b
, &element_intr
->instr
);
178 /* Remove the old load intrinsic */
179 nir_instr_remove(&intr
->instr
);
183 deref_has_indirect(nir_builder
*b
, nir_variable
*var
, nir_deref_path
*path
)
185 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
186 nir_deref_instr
**p
= &path
->path
[1];
188 if (nir_is_per_vertex_io(var
, b
->shader
->info
.stage
)) {
193 if ((*p
)->deref_type
!= nir_deref_type_array
)
196 if (!nir_src_is_const((*p
)->arr
.index
))
203 /* Creates a mask of locations that contains arrays that are indexed via
207 create_indirects_mask(nir_shader
*shader
, uint64_t *indirects
,
208 uint64_t *patch_indirects
, nir_variable_mode mode
)
210 nir_foreach_function(function
, shader
) {
211 if (function
->impl
) {
213 nir_builder_init(&b
, function
->impl
);
215 nir_foreach_block(block
, function
->impl
) {
216 nir_foreach_instr_safe(instr
, block
) {
218 if (instr
->type
!= nir_instr_type_intrinsic
)
221 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
223 if (intr
->intrinsic
!= nir_intrinsic_load_deref
&&
224 intr
->intrinsic
!= nir_intrinsic_store_deref
&&
225 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_centroid
&&
226 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_sample
&&
227 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_offset
)
230 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
231 if (deref
->mode
!= mode
)
234 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
237 nir_deref_path_init(&path
, deref
, NULL
);
239 uint64_t loc_mask
= ((uint64_t)1) << var
->data
.location
;
240 if (var
->data
.patch
) {
241 if (deref_has_indirect(&b
, var
, &path
))
242 patch_indirects
[var
->data
.location_frac
] |= loc_mask
;
244 if (deref_has_indirect(&b
, var
, &path
))
245 indirects
[var
->data
.location_frac
] |= loc_mask
;
248 nir_deref_path_finish(&path
);
256 lower_io_arrays_to_elements(nir_shader
*shader
, nir_variable_mode mask
,
257 uint64_t *indirects
, uint64_t *patch_indirects
,
258 struct hash_table
*varyings
,
259 bool after_cross_stage_opts
)
261 nir_foreach_function(function
, shader
) {
262 if (function
->impl
) {
264 nir_builder_init(&b
, function
->impl
);
266 nir_foreach_block(block
, function
->impl
) {
267 nir_foreach_instr_safe(instr
, block
) {
268 if (instr
->type
!= nir_instr_type_intrinsic
)
271 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
273 if (intr
->intrinsic
!= nir_intrinsic_load_deref
&&
274 intr
->intrinsic
!= nir_intrinsic_store_deref
&&
275 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_centroid
&&
276 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_sample
&&
277 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_offset
)
280 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
281 if (!(deref
->mode
& mask
))
284 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
287 uint64_t loc_mask
= ((uint64_t)1) << var
->data
.location
;
288 if (var
->data
.patch
) {
289 if (patch_indirects
[var
->data
.location_frac
] & loc_mask
)
292 if (indirects
[var
->data
.location_frac
] & loc_mask
)
296 nir_variable_mode mode
= var
->data
.mode
;
298 const struct glsl_type
*type
= var
->type
;
299 if (nir_is_per_vertex_io(var
, b
.shader
->info
.stage
)) {
300 assert(glsl_type_is_array(type
));
301 type
= glsl_get_array_element(type
);
304 /* Skip types we cannot split.
306 * TODO: Add support for struct splitting.
308 if ((!glsl_type_is_array(type
) && !glsl_type_is_matrix(type
))||
309 glsl_type_is_struct(glsl_without_array(type
)))
313 if (!after_cross_stage_opts
&&
314 var
->data
.location
< VARYING_SLOT_VAR0
&&
315 var
->data
.location
>= 0)
318 /* Don't bother splitting if we can't opt away any unused
321 if (!after_cross_stage_opts
&& var
->data
.always_active_io
)
324 switch (intr
->intrinsic
) {
325 case nir_intrinsic_interp_deref_at_centroid
:
326 case nir_intrinsic_interp_deref_at_sample
:
327 case nir_intrinsic_interp_deref_at_offset
:
328 case nir_intrinsic_load_deref
:
329 case nir_intrinsic_store_deref
:
330 if ((mask
& nir_var_shader_in
&& mode
== nir_var_shader_in
) ||
331 (mask
& nir_var_shader_out
&& mode
== nir_var_shader_out
))
332 lower_array(&b
, intr
, var
, varyings
);
344 nir_lower_io_arrays_to_elements_no_indirects(nir_shader
*shader
,
347 struct hash_table
*split_inputs
=
348 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
349 _mesa_key_pointer_equal
);
350 struct hash_table
*split_outputs
=
351 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
352 _mesa_key_pointer_equal
);
354 uint64_t indirects
[4] = {0}, patch_indirects
[4] = {0};
356 lower_io_arrays_to_elements(shader
, nir_var_shader_out
, indirects
,
357 patch_indirects
, split_outputs
, true);
360 lower_io_arrays_to_elements(shader
, nir_var_shader_in
, indirects
,
361 patch_indirects
, split_inputs
, true);
363 /* Remove old input from the shaders inputs list */
364 hash_table_foreach(split_inputs
, entry
) {
365 nir_variable
*var
= (nir_variable
*) entry
->key
;
366 exec_node_remove(&var
->node
);
372 /* Remove old output from the shaders outputs list */
373 hash_table_foreach(split_outputs
, entry
) {
374 nir_variable
*var
= (nir_variable
*) entry
->key
;
375 exec_node_remove(&var
->node
);
380 _mesa_hash_table_destroy(split_inputs
, NULL
);
381 _mesa_hash_table_destroy(split_outputs
, NULL
);
383 nir_remove_dead_derefs(shader
);
387 nir_lower_io_arrays_to_elements(nir_shader
*producer
, nir_shader
*consumer
)
389 struct hash_table
*split_inputs
=
390 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
391 _mesa_key_pointer_equal
);
392 struct hash_table
*split_outputs
=
393 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
394 _mesa_key_pointer_equal
);
396 uint64_t indirects
[4] = {0}, patch_indirects
[4] = {0};
397 create_indirects_mask(producer
, indirects
, patch_indirects
,
399 create_indirects_mask(consumer
, indirects
, patch_indirects
,
402 lower_io_arrays_to_elements(producer
, nir_var_shader_out
, indirects
,
403 patch_indirects
, split_outputs
, false);
405 lower_io_arrays_to_elements(consumer
, nir_var_shader_in
, indirects
,
406 patch_indirects
, split_inputs
, false);
408 /* Remove old input from the shaders inputs list */
409 hash_table_foreach(split_inputs
, entry
) {
410 nir_variable
*var
= (nir_variable
*) entry
->key
;
411 exec_node_remove(&var
->node
);
416 /* Remove old output from the shaders outputs list */
417 hash_table_foreach(split_outputs
, entry
) {
418 nir_variable
*var
= (nir_variable
*) entry
->key
;
419 exec_node_remove(&var
->node
);
424 _mesa_hash_table_destroy(split_inputs
, NULL
);
425 _mesa_hash_table_destroy(split_outputs
, NULL
);
427 nir_remove_dead_derefs(producer
);
428 nir_remove_dead_derefs(consumer
);