2 * Copyright © 2014 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
24 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_array.h"
31 struct locals_to_regs_state
{
33 nir_function_impl
*impl
;
35 /* A hash table mapping derefs to registers */
36 struct hash_table
*regs_table
;
38 /* A growing array of derefs that we have encountered. There is exactly
39 * one element of this array per element in the hash table. This is
40 * used to make adding register initialization code deterministic.
42 nir_array derefs_array
;
47 /* The following two functions implement a hash and equality check for
48 * variable dreferences. When the hash or equality function encounters an
49 * array, it ignores the offset and whether it is direct or indirect
53 hash_deref(const void *void_deref
)
55 uint32_t hash
= _mesa_fnv32_1a_offset_bias
;
57 const nir_deref_var
*deref_var
= void_deref
;
58 hash
= _mesa_fnv32_1a_accumulate(hash
, deref_var
->var
);
60 for (const nir_deref
*deref
= deref_var
->deref
.child
;
61 deref
; deref
= deref
->child
) {
62 if (deref
->deref_type
== nir_deref_type_struct
) {
63 const nir_deref_struct
*deref_struct
= nir_deref_as_struct(deref
);
64 hash
= _mesa_fnv32_1a_accumulate(hash
, deref_struct
->index
);
72 derefs_equal(const void *void_a
, const void *void_b
)
74 const nir_deref_var
*a_var
= void_a
;
75 const nir_deref_var
*b_var
= void_b
;
77 if (a_var
->var
!= b_var
->var
)
80 for (const nir_deref
*a
= a_var
->deref
.child
, *b
= b_var
->deref
.child
;
81 a
!= NULL
; a
= a
->child
, b
= b
->child
) {
82 if (a
->deref_type
!= b
->deref_type
)
85 if (a
->deref_type
== nir_deref_type_struct
) {
86 if (nir_deref_as_struct(a
)->index
!= nir_deref_as_struct(b
)->index
)
89 /* Do nothing for arrays. They're all the same. */
91 assert((a
->child
== NULL
) == (b
->child
== NULL
));
92 if((a
->child
== NULL
) != (b
->child
== NULL
))
100 get_reg_for_deref(nir_deref_var
*deref
, struct locals_to_regs_state
*state
)
102 uint32_t hash
= hash_deref(deref
);
104 struct hash_entry
*entry
=
105 _mesa_hash_table_search_pre_hashed(state
->regs_table
, hash
, deref
);
109 unsigned array_size
= 1;
110 nir_deref
*tail
= &deref
->deref
;
111 while (tail
->child
) {
112 if (tail
->child
->deref_type
== nir_deref_type_array
)
113 array_size
*= glsl_get_length(tail
->type
);
117 assert(glsl_type_is_vector(tail
->type
) || glsl_type_is_scalar(tail
->type
));
119 nir_register
*reg
= nir_local_reg_create(state
->impl
);
120 reg
->num_components
= glsl_get_vector_elements(tail
->type
);
121 reg
->num_array_elems
= array_size
> 1 ? array_size
: 0;
122 reg
->bit_size
= glsl_get_bit_size(glsl_get_base_type(tail
->type
));
124 _mesa_hash_table_insert_pre_hashed(state
->regs_table
, hash
, deref
, reg
);
125 nir_array_add(&state
->derefs_array
, nir_deref_var
*, deref
);
131 get_deref_reg_src(nir_deref_var
*deref
, nir_instr
*instr
,
132 struct locals_to_regs_state
*state
)
137 src
.reg
.reg
= get_reg_for_deref(deref
, state
);
138 src
.reg
.base_offset
= 0;
139 src
.reg
.indirect
= NULL
;
141 /* It is possible for a user to create a shader that has an array with a
142 * single element and then proceed to access it indirectly. Indirectly
143 * accessing a non-array register is not allowed in NIR. In order to
144 * handle this case we just convert it to a direct reference.
146 if (src
.reg
.reg
->num_array_elems
== 0)
149 nir_deref
*tail
= &deref
->deref
;
150 while (tail
->child
!= NULL
) {
151 const struct glsl_type
*parent_type
= tail
->type
;
154 if (tail
->deref_type
!= nir_deref_type_array
)
157 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
159 src
.reg
.base_offset
*= glsl_get_length(parent_type
);
160 src
.reg
.base_offset
+= deref_array
->base_offset
;
162 if (src
.reg
.indirect
) {
163 nir_load_const_instr
*load_const
=
164 nir_load_const_instr_create(state
->shader
, 1, 32);
165 load_const
->value
.u32
[0] = glsl_get_length(parent_type
);
166 nir_instr_insert_before(instr
, &load_const
->instr
);
168 nir_alu_instr
*mul
= nir_alu_instr_create(state
->shader
, nir_op_imul
);
169 mul
->src
[0].src
= *src
.reg
.indirect
;
170 mul
->src
[1].src
.is_ssa
= true;
171 mul
->src
[1].src
.ssa
= &load_const
->def
;
172 mul
->dest
.write_mask
= 1;
173 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, 32, NULL
);
174 nir_instr_insert_before(instr
, &mul
->instr
);
176 src
.reg
.indirect
->is_ssa
= true;
177 src
.reg
.indirect
->ssa
= &mul
->dest
.dest
.ssa
;
180 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
181 if (src
.reg
.indirect
== NULL
) {
182 src
.reg
.indirect
= ralloc(state
->shader
, nir_src
);
183 nir_src_copy(src
.reg
.indirect
, &deref_array
->indirect
,
186 nir_alu_instr
*add
= nir_alu_instr_create(state
->shader
,
188 add
->src
[0].src
= *src
.reg
.indirect
;
189 nir_src_copy(&add
->src
[1].src
, &deref_array
->indirect
, add
);
190 add
->dest
.write_mask
= 1;
191 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, 32, NULL
);
192 nir_instr_insert_before(instr
, &add
->instr
);
194 src
.reg
.indirect
->is_ssa
= true;
195 src
.reg
.indirect
->ssa
= &add
->dest
.dest
.ssa
;
204 lower_locals_to_regs_block(nir_block
*block
, void *void_state
)
206 struct locals_to_regs_state
*state
= void_state
;
208 nir_foreach_instr_safe(block
, instr
) {
209 if (instr
->type
!= nir_instr_type_intrinsic
)
212 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
214 switch (intrin
->intrinsic
) {
215 case nir_intrinsic_load_var
: {
216 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_local
)
219 nir_alu_instr
*mov
= nir_alu_instr_create(state
->shader
, nir_op_imov
);
220 mov
->src
[0].src
= get_deref_reg_src(intrin
->variables
[0],
221 &intrin
->instr
, state
);
222 mov
->dest
.write_mask
= (1 << intrin
->num_components
) - 1;
223 if (intrin
->dest
.is_ssa
) {
224 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
225 intrin
->num_components
,
226 intrin
->dest
.ssa
.bit_size
, NULL
);
227 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
228 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
230 nir_dest_copy(&mov
->dest
.dest
, &intrin
->dest
, &mov
->instr
);
232 nir_instr_insert_before(&intrin
->instr
, &mov
->instr
);
234 nir_instr_remove(&intrin
->instr
);
235 state
->progress
= true;
239 case nir_intrinsic_store_var
: {
240 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_local
)
243 nir_src reg_src
= get_deref_reg_src(intrin
->variables
[0],
244 &intrin
->instr
, state
);
246 nir_alu_instr
*mov
= nir_alu_instr_create(state
->shader
, nir_op_imov
);
247 nir_src_copy(&mov
->src
[0].src
, &intrin
->src
[0], mov
);
248 mov
->dest
.write_mask
= nir_intrinsic_write_mask(intrin
);
249 mov
->dest
.dest
.is_ssa
= false;
250 mov
->dest
.dest
.reg
.reg
= reg_src
.reg
.reg
;
251 mov
->dest
.dest
.reg
.base_offset
= reg_src
.reg
.base_offset
;
252 mov
->dest
.dest
.reg
.indirect
= reg_src
.reg
.indirect
;
254 nir_instr_insert_before(&intrin
->instr
, &mov
->instr
);
256 nir_instr_remove(&intrin
->instr
);
257 state
->progress
= true;
261 case nir_intrinsic_copy_var
:
262 unreachable("There should be no copies whatsoever at this point");
274 compute_reg_usedef_lca(nir_register
*reg
)
276 nir_block
*lca
= NULL
;
278 list_for_each_entry(nir_dest
, def_dest
, ®
->defs
, reg
.def_link
)
279 lca
= nir_dominance_lca(lca
, def_dest
->reg
.parent_instr
->block
);
281 list_for_each_entry(nir_src
, use_src
, ®
->uses
, use_link
)
282 lca
= nir_dominance_lca(lca
, use_src
->parent_instr
->block
);
284 list_for_each_entry(nir_src
, use_src
, ®
->if_uses
, use_link
) {
285 nir_cf_node
*prev_node
= nir_cf_node_prev(&use_src
->parent_if
->cf_node
);
286 assert(prev_node
->type
== nir_cf_node_block
);
287 lca
= nir_dominance_lca(lca
, nir_cf_node_as_block(prev_node
));
294 insert_constant_initializer(nir_deref_var
*deref_head
, nir_deref
*deref_tail
,
296 struct locals_to_regs_state
*state
)
298 if (deref_tail
->child
) {
299 switch (deref_tail
->child
->deref_type
) {
300 case nir_deref_type_array
: {
301 unsigned array_elems
= glsl_get_length(deref_tail
->type
);
303 nir_deref_array arr_deref
;
304 arr_deref
.deref
= *deref_tail
->child
;
305 arr_deref
.deref_array_type
= nir_deref_array_type_direct
;
307 nir_deref
*old_child
= deref_tail
->child
;
308 deref_tail
->child
= &arr_deref
.deref
;
309 for (unsigned i
= 0; i
< array_elems
; i
++) {
310 arr_deref
.base_offset
= i
;
311 insert_constant_initializer(deref_head
, &arr_deref
.deref
,
314 deref_tail
->child
= old_child
;
318 case nir_deref_type_struct
:
319 insert_constant_initializer(deref_head
, deref_tail
->child
,
324 unreachable("Invalid deref child type");
328 assert(deref_tail
->child
== NULL
);
330 nir_load_const_instr
*load
=
331 nir_deref_get_const_initializer_load(state
->shader
, deref_head
);
332 nir_instr_insert_before_block(block
, &load
->instr
);
334 nir_src reg_src
= get_deref_reg_src(deref_head
, &load
->instr
, state
);
336 nir_alu_instr
*mov
= nir_alu_instr_create(state
->shader
, nir_op_imov
);
337 mov
->src
[0].src
= nir_src_for_ssa(&load
->def
);
338 mov
->dest
.write_mask
= (1 << load
->def
.num_components
) - 1;
339 mov
->dest
.dest
.is_ssa
= false;
340 mov
->dest
.dest
.reg
.reg
= reg_src
.reg
.reg
;
341 mov
->dest
.dest
.reg
.base_offset
= reg_src
.reg
.base_offset
;
342 mov
->dest
.dest
.reg
.indirect
= reg_src
.reg
.indirect
;
344 nir_instr_insert_after(&load
->instr
, &mov
->instr
);
345 state
->progress
= true;
349 nir_lower_locals_to_regs_impl(nir_function_impl
*impl
)
351 struct locals_to_regs_state state
;
353 state
.shader
= impl
->function
->shader
;
355 state
.progress
= false;
356 state
.regs_table
= _mesa_hash_table_create(NULL
, hash_deref
, derefs_equal
);
357 nir_array_init(&state
.derefs_array
, NULL
);
359 nir_metadata_require(impl
, nir_metadata_dominance
);
361 nir_foreach_block(impl
, lower_locals_to_regs_block
, &state
);
363 nir_array_foreach(&state
.derefs_array
, nir_deref_var
*, deref_ptr
) {
364 nir_deref_var
*deref
= *deref_ptr
;
365 struct hash_entry
*deref_entry
=
366 _mesa_hash_table_search(state
.regs_table
, deref
);
367 assert(deref_entry
&& deref_entry
->key
== deref
);
368 nir_register
*reg
= (nir_register
*)deref_entry
->data
;
370 if (deref
->var
->constant_initializer
== NULL
)
373 nir_block
*usedef_lca
= compute_reg_usedef_lca(reg
);
375 insert_constant_initializer(deref
, &deref
->deref
, usedef_lca
, &state
);
378 nir_metadata_preserve(impl
, nir_metadata_block_index
|
379 nir_metadata_dominance
);
381 nir_array_fini(&state
.derefs_array
);
382 _mesa_hash_table_destroy(state
.regs_table
, NULL
);
384 return state
.progress
;
388 nir_lower_locals_to_regs(nir_shader
*shader
)
390 bool progress
= false;
392 nir_foreach_function(shader
, function
) {
394 progress
= nir_lower_locals_to_regs_impl(function
->impl
) || progress
;