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"
26 #include "nir_deref.h"
29 nir_deref_path_init(nir_deref_path
*path
,
30 nir_deref_instr
*deref
, void *mem_ctx
)
32 assert(deref
!= NULL
);
34 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
35 * room for the NULL terminator.
37 static const int max_short_path_len
= ARRAY_SIZE(path
->_short_path
) - 1;
41 nir_deref_instr
**tail
= &path
->_short_path
[max_short_path_len
];
42 nir_deref_instr
**head
= tail
;
45 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
47 if (count
<= max_short_path_len
)
51 if (count
<= max_short_path_len
) {
52 /* If we're under max_short_path_len, just use the short path. */
58 /* Just in case someone uses short_path by accident */
59 for (unsigned i
= 0; i
< ARRAY_SIZE(path
->_short_path
); i
++)
60 path
->_short_path
[i
] = (void *)0xdeadbeef;
63 path
->path
= ralloc_array(mem_ctx
, nir_deref_instr
*, count
+ 1);
64 head
= tail
= path
->path
+ count
;
66 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
))
70 assert(head
== path
->path
);
71 assert(tail
== head
+ count
);
72 assert((*head
)->deref_type
== nir_deref_type_var
);
73 assert(*tail
== NULL
);
77 nir_deref_path_finish(nir_deref_path
*path
)
79 if (path
->path
< &path
->_short_path
[0] ||
80 path
->path
> &path
->_short_path
[ARRAY_SIZE(path
->_short_path
) - 1])
81 ralloc_free(path
->path
);
85 * Recursively removes unused deref instructions
88 nir_deref_instr_remove_if_unused(nir_deref_instr
*instr
)
90 bool progress
= false;
92 for (nir_deref_instr
*d
= instr
; d
; d
= nir_deref_instr_parent(d
)) {
93 /* If anyone is using this deref, leave it alone */
94 assert(d
->dest
.is_ssa
);
95 if (!list_empty(&d
->dest
.ssa
.uses
))
98 nir_instr_remove(&d
->instr
);
106 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
108 bool progress
= false;
110 nir_foreach_block(block
, impl
) {
111 nir_foreach_instr_safe(instr
, block
) {
112 if (instr
->type
== nir_instr_type_deref
&&
113 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
119 nir_metadata_preserve(impl
, nir_metadata_block_index
|
120 nir_metadata_dominance
);
126 nir_remove_dead_derefs(nir_shader
*shader
)
128 bool progress
= false;
129 nir_foreach_function(function
, shader
) {
130 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
138 nir_deref_instr_to_deref(nir_deref_instr
*instr
, void *mem_ctx
)
140 nir_deref
*deref
= NULL
;
142 while (instr
->deref_type
!= nir_deref_type_var
) {
144 switch (instr
->deref_type
) {
145 case nir_deref_type_array
:
146 case nir_deref_type_array_wildcard
: {
147 nir_deref_array
*deref_arr
= nir_deref_array_create(mem_ctx
);
148 if (instr
->deref_type
== nir_deref_type_array
) {
149 nir_const_value
*const_index
=
150 nir_src_as_const_value(instr
->arr
.index
);
152 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
153 deref_arr
->base_offset
= const_index
->u32
[0];
155 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
156 deref_arr
->base_offset
= 0;
157 nir_src_copy(&deref_arr
->indirect
, &instr
->arr
.index
, mem_ctx
);
160 deref_arr
->deref_array_type
= nir_deref_array_type_wildcard
;
162 nderef
= &deref_arr
->deref
;
166 case nir_deref_type_struct
:
167 nderef
= &nir_deref_struct_create(mem_ctx
, instr
->strct
.index
)->deref
;
171 unreachable("Invalid deref instruction type");
174 nderef
->child
= deref
;
175 ralloc_steal(nderef
, deref
);
176 nderef
->type
= instr
->type
;
179 assert(instr
->parent
.is_ssa
);
180 instr
= nir_src_as_deref(instr
->parent
);
183 assert(instr
->deref_type
== nir_deref_type_var
);
184 nir_deref_var
*deref_var
= nir_deref_var_create(mem_ctx
, instr
->var
);
185 deref_var
->deref
.child
= deref
;
186 ralloc_steal(deref_var
, deref
);
191 static nir_deref_var
*
192 nir_deref_src_to_deref(nir_src src
, void *mem_ctx
)
194 return nir_deref_instr_to_deref(nir_src_as_deref(src
), mem_ctx
);
198 nir_lower_deref_instrs_tex(nir_tex_instr
*tex
)
200 bool progress
= false;
202 /* Remove the instruction before we modify it. This way we won't mess up
203 * use-def chains when we move sources around.
205 nir_cursor cursor
= nir_instr_remove(&tex
->instr
);
207 unsigned new_num_srcs
= 0;
208 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
209 if (tex
->src
[i
].src_type
== nir_tex_src_texture_deref
) {
210 tex
->texture
= nir_deref_src_to_deref(tex
->src
[i
].src
, tex
);
213 } else if (tex
->src
[i
].src_type
== nir_tex_src_sampler_deref
) {
214 tex
->sampler
= nir_deref_src_to_deref(tex
->src
[i
].src
, tex
);
219 /* Compact the sources down to remove the deref sources */
220 assert(new_num_srcs
<= i
);
221 tex
->src
[new_num_srcs
++] = tex
->src
[i
];
223 tex
->num_srcs
= new_num_srcs
;
225 nir_instr_insert(cursor
, &tex
->instr
);
231 nir_lower_deref_instrs_intrin(nir_intrinsic_instr
*intrin
,
232 enum nir_lower_deref_flags flags
)
234 nir_intrinsic_op deref_op
= intrin
->intrinsic
;
235 nir_intrinsic_op var_op
;
239 case nir_intrinsic_##a##_deref: \
240 if (!(flags & nir_lower_load_store_derefs)) \
242 var_op = nir_intrinsic_##a##_var; \
250 case nir_intrinsic_interp_deref_##a: \
251 if (!(flags & nir_lower_interp_derefs)) \
253 var_op = nir_intrinsic_interp_var_##a; \
261 case nir_intrinsic_atomic_counter_##a##_deref: \
262 if (!(flags & nir_lower_atomic_counter_derefs)) \
264 var_op = nir_intrinsic_atomic_counter_##a##_var; \
280 case nir_intrinsic_deref_atomic_##a: \
281 if (!(flags & nir_lower_atomic_derefs)) \
283 var_op = nir_intrinsic_var_atomic_##a; \
298 case nir_intrinsic_image_deref_##a: \
299 if (!(flags & nir_lower_image_derefs)) \
301 var_op = nir_intrinsic_image_var_##a; \
311 CASE(atomic_exchange
)
312 CASE(atomic_comp_swap
)
321 /* Remove the instruction before we modify it. This way we won't mess up
322 * use-def chains when we move sources around.
324 nir_cursor cursor
= nir_instr_remove(&intrin
->instr
);
326 unsigned num_derefs
= nir_intrinsic_infos
[var_op
].num_variables
;
327 assert(nir_intrinsic_infos
[var_op
].num_srcs
+ num_derefs
==
328 nir_intrinsic_infos
[deref_op
].num_srcs
);
330 /* Move deref sources to variables */
331 for (unsigned i
= 0; i
< num_derefs
; i
++)
332 intrin
->variables
[i
] = nir_deref_src_to_deref(intrin
->src
[i
], intrin
);
334 /* Shift all the other sources down */
335 for (unsigned i
= 0; i
< nir_intrinsic_infos
[var_op
].num_srcs
; i
++)
336 nir_src_copy(&intrin
->src
[i
], &intrin
->src
[i
+ num_derefs
], intrin
);
338 /* Rewrite the extra sources to NIR_SRC_INIT just in case */
339 for (unsigned i
= 0; i
< num_derefs
; i
++)
340 intrin
->src
[nir_intrinsic_infos
[var_op
].num_srcs
+ i
] = NIR_SRC_INIT
;
342 /* It's safe to just stomp the intrinsic to var intrinsic since every
343 * intrinsic has room for some variables and the number of sources only
346 intrin
->intrinsic
= var_op
;
348 nir_instr_insert(cursor
, &intrin
->instr
);
354 nir_lower_deref_instrs_impl(nir_function_impl
*impl
,
355 enum nir_lower_deref_flags flags
)
357 bool progress
= false;
359 /* Walk the instructions in reverse order so that we can safely clean up
360 * the deref instructions after we clean up their uses.
362 nir_foreach_block_reverse(block
, impl
) {
363 nir_foreach_instr_reverse_safe(instr
, block
) {
364 switch (instr
->type
) {
365 case nir_instr_type_deref
:
366 if (list_empty(&nir_instr_as_deref(instr
)->dest
.ssa
.uses
)) {
367 nir_instr_remove(instr
);
372 case nir_instr_type_tex
:
373 if (flags
& nir_lower_texture_derefs
)
374 progress
|= nir_lower_deref_instrs_tex(nir_instr_as_tex(instr
));
377 case nir_instr_type_intrinsic
:
379 nir_lower_deref_instrs_intrin(nir_instr_as_intrinsic(instr
),
384 break; /* Nothing to do */
390 nir_metadata_preserve(impl
, nir_metadata_block_index
|
391 nir_metadata_dominance
);
398 nir_lower_deref_instrs(nir_shader
*shader
,
399 enum nir_lower_deref_flags flags
)
401 bool progress
= false;
403 nir_foreach_function(function
, shader
) {
407 progress
|= nir_lower_deref_instrs_impl(function
->impl
, flags
);
410 shader
->lowered_derefs
|= flags
;
416 nir_fixup_deref_modes(nir_shader
*shader
)
418 nir_foreach_function(function
, shader
) {
422 nir_foreach_block(block
, function
->impl
) {
423 nir_foreach_instr(instr
, block
) {
424 if (instr
->type
!= nir_instr_type_deref
)
427 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
429 nir_variable_mode parent_mode
;
430 if (deref
->deref_type
== nir_deref_type_var
) {
431 parent_mode
= deref
->var
->data
.mode
;
433 assert(deref
->parent
.is_ssa
);
434 nir_deref_instr
*parent
=
435 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
436 parent_mode
= parent
->mode
;
439 deref
->mode
= parent_mode
;