2 * Copyright © 2018 Intel Corporation
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"
28 * Recursively removes unused deref instructions
31 nir_deref_instr_remove_if_unused(nir_deref_instr
*instr
)
33 bool progress
= false;
35 for (nir_deref_instr
*d
= instr
; d
; d
= nir_deref_instr_parent(d
)) {
36 /* If anyone is using this deref, leave it alone */
37 assert(d
->dest
.is_ssa
);
38 if (!list_empty(&d
->dest
.ssa
.uses
))
41 nir_instr_remove(&d
->instr
);
49 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
51 bool progress
= false;
53 nir_foreach_block(block
, impl
) {
54 nir_foreach_instr_safe(instr
, block
) {
55 if (instr
->type
== nir_instr_type_deref
&&
56 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
62 nir_metadata_preserve(impl
, nir_metadata_block_index
|
63 nir_metadata_dominance
);
69 nir_remove_dead_derefs(nir_shader
*shader
)
71 bool progress
= false;
72 nir_foreach_function(function
, shader
) {
73 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
81 nir_deref_instr_to_deref(nir_deref_instr
*instr
, void *mem_ctx
)
83 nir_deref
*deref
= NULL
;
85 while (instr
->deref_type
!= nir_deref_type_var
) {
87 switch (instr
->deref_type
) {
88 case nir_deref_type_array
:
89 case nir_deref_type_array_wildcard
: {
90 nir_deref_array
*deref_arr
= nir_deref_array_create(mem_ctx
);
91 if (instr
->deref_type
== nir_deref_type_array
) {
92 nir_const_value
*const_index
=
93 nir_src_as_const_value(instr
->arr
.index
);
95 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
96 deref_arr
->base_offset
= const_index
->u32
[0];
98 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
99 deref_arr
->base_offset
= 0;
100 nir_src_copy(&deref_arr
->indirect
, &instr
->arr
.index
, mem_ctx
);
103 deref_arr
->deref_array_type
= nir_deref_array_type_wildcard
;
105 nderef
= &deref_arr
->deref
;
109 case nir_deref_type_struct
:
110 nderef
= &nir_deref_struct_create(mem_ctx
, instr
->strct
.index
)->deref
;
114 unreachable("Invalid deref instruction type");
117 nderef
->child
= deref
;
118 ralloc_steal(nderef
, deref
);
119 nderef
->type
= instr
->type
;
122 assert(instr
->parent
.is_ssa
);
123 instr
= nir_src_as_deref(instr
->parent
);
126 assert(instr
->deref_type
== nir_deref_type_var
);
127 nir_deref_var
*deref_var
= nir_deref_var_create(mem_ctx
, instr
->var
);
128 deref_var
->deref
.child
= deref
;
129 ralloc_steal(deref_var
, deref
);
134 static nir_deref_var
*
135 nir_deref_src_to_deref(nir_src src
, void *mem_ctx
)
137 return nir_deref_instr_to_deref(nir_src_as_deref(src
), mem_ctx
);
141 nir_lower_deref_instrs_tex(nir_tex_instr
*tex
)
143 bool progress
= false;
145 /* Remove the instruction before we modify it. This way we won't mess up
146 * use-def chains when we move sources around.
148 nir_cursor cursor
= nir_instr_remove(&tex
->instr
);
150 unsigned new_num_srcs
= 0;
151 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
152 if (tex
->src
[i
].src_type
== nir_tex_src_texture_deref
) {
153 tex
->texture
= nir_deref_src_to_deref(tex
->src
[i
].src
, tex
);
156 } else if (tex
->src
[i
].src_type
== nir_tex_src_sampler_deref
) {
157 tex
->sampler
= nir_deref_src_to_deref(tex
->src
[i
].src
, tex
);
162 /* Compact the sources down to remove the deref sources */
163 assert(new_num_srcs
<= i
);
164 tex
->src
[new_num_srcs
++] = tex
->src
[i
];
166 tex
->num_srcs
= new_num_srcs
;
168 nir_instr_insert(cursor
, &tex
->instr
);
174 nir_lower_deref_instrs_intrin(nir_intrinsic_instr
*intrin
,
175 enum nir_lower_deref_flags flags
)
177 nir_intrinsic_op deref_op
= intrin
->intrinsic
;
178 nir_intrinsic_op var_op
;
182 case nir_intrinsic_##a##_deref: \
183 if (!(flags & nir_lower_load_store_derefs)) \
185 var_op = nir_intrinsic_##a##_var; \
193 case nir_intrinsic_interp_deref_##a: \
194 if (!(flags & nir_lower_interp_derefs)) \
196 var_op = nir_intrinsic_interp_var_##a; \
204 case nir_intrinsic_atomic_counter_##a##_deref: \
205 if (!(flags & nir_lower_atomic_counter_derefs)) \
207 var_op = nir_intrinsic_atomic_counter_##a##_var; \
223 case nir_intrinsic_deref_atomic_##a: \
224 if (!(flags & nir_lower_atomic_derefs)) \
226 var_op = nir_intrinsic_var_atomic_##a; \
241 case nir_intrinsic_image_deref_##a: \
242 if (!(flags & nir_lower_image_derefs)) \
244 var_op = nir_intrinsic_image_var_##a; \
254 CASE(atomic_exchange
)
255 CASE(atomic_comp_swap
)
264 /* Remove the instruction before we modify it. This way we won't mess up
265 * use-def chains when we move sources around.
267 nir_cursor cursor
= nir_instr_remove(&intrin
->instr
);
269 unsigned num_derefs
= nir_intrinsic_infos
[var_op
].num_variables
;
270 assert(nir_intrinsic_infos
[var_op
].num_srcs
+ num_derefs
==
271 nir_intrinsic_infos
[deref_op
].num_srcs
);
273 /* Move deref sources to variables */
274 for (unsigned i
= 0; i
< num_derefs
; i
++)
275 intrin
->variables
[i
] = nir_deref_src_to_deref(intrin
->src
[i
], intrin
);
277 /* Shift all the other sources down */
278 for (unsigned i
= 0; i
< nir_intrinsic_infos
[var_op
].num_srcs
; i
++)
279 nir_src_copy(&intrin
->src
[i
], &intrin
->src
[i
+ num_derefs
], intrin
);
281 /* Rewrite the extra sources to NIR_SRC_INIT just in case */
282 for (unsigned i
= 0; i
< num_derefs
; i
++)
283 intrin
->src
[nir_intrinsic_infos
[var_op
].num_srcs
+ i
] = NIR_SRC_INIT
;
285 /* It's safe to just stomp the intrinsic to var intrinsic since every
286 * intrinsic has room for some variables and the number of sources only
289 intrin
->intrinsic
= var_op
;
291 nir_instr_insert(cursor
, &intrin
->instr
);
297 nir_lower_deref_instrs_impl(nir_function_impl
*impl
,
298 enum nir_lower_deref_flags flags
)
300 bool progress
= false;
302 /* Walk the instructions in reverse order so that we can safely clean up
303 * the deref instructions after we clean up their uses.
305 nir_foreach_block_reverse(block
, impl
) {
306 nir_foreach_instr_reverse_safe(instr
, block
) {
307 switch (instr
->type
) {
308 case nir_instr_type_deref
:
309 if (list_empty(&nir_instr_as_deref(instr
)->dest
.ssa
.uses
)) {
310 nir_instr_remove(instr
);
315 case nir_instr_type_tex
:
316 if (flags
& nir_lower_texture_derefs
)
317 progress
|= nir_lower_deref_instrs_tex(nir_instr_as_tex(instr
));
320 case nir_instr_type_intrinsic
:
322 nir_lower_deref_instrs_intrin(nir_instr_as_intrinsic(instr
),
327 break; /* Nothing to do */
333 nir_metadata_preserve(impl
, nir_metadata_block_index
|
334 nir_metadata_dominance
);
341 nir_lower_deref_instrs(nir_shader
*shader
,
342 enum nir_lower_deref_flags flags
)
344 bool progress
= false;
346 nir_foreach_function(function
, shader
) {
350 progress
|= nir_lower_deref_instrs_impl(function
->impl
, flags
);
353 shader
->lowered_derefs
|= flags
;
359 nir_fixup_deref_modes(nir_shader
*shader
)
361 nir_foreach_function(function
, shader
) {
365 nir_foreach_block(block
, function
->impl
) {
366 nir_foreach_instr(instr
, block
) {
367 if (instr
->type
!= nir_instr_type_deref
)
370 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
372 nir_variable_mode parent_mode
;
373 if (deref
->deref_type
== nir_deref_type_var
) {
374 parent_mode
= deref
->var
->data
.mode
;
376 assert(deref
->parent
.is_ssa
);
377 nir_deref_instr
*parent
=
378 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
379 parent_mode
= parent
->mode
;
382 deref
->mode
= parent_mode
;