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_builder.h"
31 struct locals_to_regs_state
{
34 /* A hash table mapping derefs to registers */
35 struct hash_table
*regs_table
;
40 /* The following two functions implement a hash and equality check for
41 * variable dreferences. When the hash or equality function encounters an
42 * array, it ignores the offset and whether it is direct or indirect
46 hash_deref(const void *void_deref
)
48 uint32_t hash
= _mesa_fnv32_1a_offset_bias
;
50 for (const nir_deref_instr
*deref
= void_deref
; deref
;
51 deref
= nir_deref_instr_parent(deref
)) {
52 switch (deref
->deref_type
) {
53 case nir_deref_type_var
:
54 return _mesa_fnv32_1a_accumulate(hash
, deref
->var
);
56 case nir_deref_type_array
:
57 continue; /* Do nothing */
59 case nir_deref_type_struct
:
60 hash
= _mesa_fnv32_1a_accumulate(hash
, deref
->strct
.index
);
64 unreachable("Invalid deref type");
68 unreachable("We should have hit a variable dereference");
72 derefs_equal(const void *void_a
, const void *void_b
)
74 for (const nir_deref_instr
*a
= void_a
, *b
= void_b
; a
|| b
;
75 a
= nir_deref_instr_parent(a
), b
= nir_deref_instr_parent(b
)) {
76 if (a
->deref_type
!= b
->deref_type
)
79 switch (a
->deref_type
) {
80 case nir_deref_type_var
:
81 return a
->var
== b
->var
;
83 case nir_deref_type_array
:
84 continue; /* Do nothing */
86 case nir_deref_type_struct
:
87 if (a
->strct
.index
!= b
->strct
.index
)
92 unreachable("Invalid deref type");
96 unreachable("We should have hit a variable dereference");
100 get_reg_for_deref(nir_deref_instr
*deref
, struct locals_to_regs_state
*state
)
102 uint32_t hash
= hash_deref(deref
);
104 assert(nir_deref_instr_get_variable(deref
)->constant_initializer
== NULL
);
106 struct hash_entry
*entry
=
107 _mesa_hash_table_search_pre_hashed(state
->regs_table
, hash
, deref
);
111 unsigned array_size
= 1;
112 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
113 if (d
->deref_type
== nir_deref_type_array
)
114 array_size
*= glsl_get_length(nir_deref_instr_parent(d
)->type
);
117 assert(glsl_type_is_vector_or_scalar(deref
->type
));
119 nir_register
*reg
= nir_local_reg_create(state
->builder
.impl
);
120 reg
->num_components
= glsl_get_vector_elements(deref
->type
);
121 reg
->num_array_elems
= array_size
> 1 ? array_size
: 0;
122 reg
->bit_size
= glsl_get_bit_size(deref
->type
);
124 _mesa_hash_table_insert_pre_hashed(state
->regs_table
, hash
, deref
, reg
);
130 get_deref_reg_src(nir_deref_instr
*deref
, struct locals_to_regs_state
*state
)
132 nir_builder
*b
= &state
->builder
;
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 unsigned inner_array_size
= 1;
150 for (const nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
151 if (d
->deref_type
!= nir_deref_type_array
)
154 nir_const_value
*const_index
= nir_src_as_const_value(d
->arr
.index
);
155 if (const_index
&& !src
.reg
.indirect
) {
156 src
.reg
.base_offset
+= const_index
->u32
[0] * inner_array_size
;
158 if (src
.reg
.indirect
) {
159 assert(src
.reg
.base_offset
== 0);
161 src
.reg
.indirect
= ralloc(b
->shader
, nir_src
);
163 nir_src_for_ssa(nir_imm_int(b
, src
.reg
.base_offset
));
164 src
.reg
.base_offset
= 0;
167 assert(src
.reg
.indirect
->is_ssa
);
168 src
.reg
.indirect
->ssa
=
169 nir_iadd(b
, src
.reg
.indirect
->ssa
,
170 nir_imul(b
, nir_ssa_for_src(b
, d
->arr
.index
, 1),
171 nir_imm_int(b
, inner_array_size
)));
174 inner_array_size
*= glsl_get_length(nir_deref_instr_parent(d
)->type
);
181 lower_locals_to_regs_block(nir_block
*block
,
182 struct locals_to_regs_state
*state
)
184 nir_builder
*b
= &state
->builder
;
186 nir_foreach_instr_safe(instr
, block
) {
187 if (instr
->type
!= nir_instr_type_intrinsic
)
190 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
192 switch (intrin
->intrinsic
) {
193 case nir_intrinsic_load_deref
: {
194 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
195 if (deref
->mode
!= nir_var_local
)
198 b
->cursor
= nir_before_instr(&intrin
->instr
);
200 nir_alu_instr
*mov
= nir_alu_instr_create(b
->shader
, nir_op_imov
);
201 mov
->src
[0].src
= get_deref_reg_src(deref
, state
);
202 mov
->dest
.write_mask
= (1 << intrin
->num_components
) - 1;
203 if (intrin
->dest
.is_ssa
) {
204 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
205 intrin
->num_components
,
206 intrin
->dest
.ssa
.bit_size
, NULL
);
207 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
208 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
210 nir_dest_copy(&mov
->dest
.dest
, &intrin
->dest
, &mov
->instr
);
212 nir_builder_instr_insert(b
, &mov
->instr
);
214 nir_instr_remove(&intrin
->instr
);
215 state
->progress
= true;
219 case nir_intrinsic_store_deref
: {
220 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
221 if (deref
->mode
!= nir_var_local
)
224 b
->cursor
= nir_before_instr(&intrin
->instr
);
226 nir_src reg_src
= get_deref_reg_src(deref
, state
);
228 nir_alu_instr
*mov
= nir_alu_instr_create(b
->shader
, nir_op_imov
);
229 nir_src_copy(&mov
->src
[0].src
, &intrin
->src
[1], mov
);
230 mov
->dest
.write_mask
= nir_intrinsic_write_mask(intrin
);
231 mov
->dest
.dest
.is_ssa
= false;
232 mov
->dest
.dest
.reg
.reg
= reg_src
.reg
.reg
;
233 mov
->dest
.dest
.reg
.base_offset
= reg_src
.reg
.base_offset
;
234 mov
->dest
.dest
.reg
.indirect
= reg_src
.reg
.indirect
;
236 nir_builder_instr_insert(b
, &mov
->instr
);
238 nir_instr_remove(&intrin
->instr
);
239 state
->progress
= true;
243 case nir_intrinsic_copy_deref
:
244 unreachable("There should be no copies whatsoever at this point");
256 nir_lower_locals_to_regs_impl(nir_function_impl
*impl
)
258 struct locals_to_regs_state state
;
260 nir_builder_init(&state
.builder
, impl
);
261 state
.progress
= false;
262 state
.regs_table
= _mesa_hash_table_create(NULL
, hash_deref
, derefs_equal
);
264 nir_metadata_require(impl
, nir_metadata_dominance
);
266 nir_foreach_block(block
, impl
) {
267 lower_locals_to_regs_block(block
, &state
);
270 nir_metadata_preserve(impl
, nir_metadata_block_index
|
271 nir_metadata_dominance
);
273 _mesa_hash_table_destroy(state
.regs_table
, NULL
);
275 return state
.progress
;
279 nir_lower_locals_to_regs(nir_shader
*shader
)
281 bool progress
= false;
283 nir_foreach_function(function
, shader
) {
285 progress
= nir_lower_locals_to_regs_impl(function
->impl
) || progress
;