2 * Copyright © 2016 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 * This lowering pass converts references to variables with loads/stores to
30 * scratch space based on a few configurable parameters.
34 #include "nir_builder.h"
35 #include "nir_deref.h"
38 deref_has_indirect(nir_deref_instr
*deref
)
40 while (deref
->deref_type
!= nir_deref_type_var
) {
41 if (deref
->deref_type
== nir_deref_type_array
&&
42 nir_src_as_const_value(deref
->arr
.index
) == NULL
)
45 deref
= nir_deref_instr_parent(deref
);
52 lower_load_store(nir_builder
*b
,
53 nir_intrinsic_instr
*intrin
,
54 glsl_type_size_align_func size_align
)
56 b
->cursor
= nir_before_instr(&intrin
->instr
);
58 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
59 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
62 nir_iadd_imm(b
, nir_build_deref_offset(b
, deref
, size_align
),
65 unsigned align
, UNUSED size
;
66 size_align(deref
->type
, &size
, &align
);
68 if (intrin
->intrinsic
== nir_intrinsic_load_deref
) {
69 nir_intrinsic_instr
*load
=
70 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_scratch
);
71 load
->num_components
= intrin
->num_components
;
72 load
->src
[0] = nir_src_for_ssa(offset
);
73 nir_intrinsic_set_align(load
, align
, 0);
74 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
75 intrin
->dest
.ssa
.num_components
,
76 intrin
->dest
.ssa
.bit_size
, NULL
);
77 nir_builder_instr_insert(b
, &load
->instr
);
79 nir_ssa_def
*value
= &load
->dest
.ssa
;
80 if (glsl_type_is_boolean(deref
->type
))
81 value
= nir_b2i32(b
, value
);
83 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
84 nir_src_for_ssa(&load
->dest
.ssa
));
86 assert(intrin
->intrinsic
== nir_intrinsic_store_deref
);
88 assert(intrin
->src
[1].is_ssa
);
89 nir_ssa_def
*value
= intrin
->src
[1].ssa
;
90 if (glsl_type_is_boolean(deref
->type
))
91 value
= nir_i2b(b
, value
);
93 nir_intrinsic_instr
*store
=
94 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_store_scratch
);
95 store
->num_components
= intrin
->num_components
;
96 store
->src
[0] = nir_src_for_ssa(value
);
97 store
->src
[1] = nir_src_for_ssa(offset
);
98 nir_intrinsic_set_write_mask(store
, nir_intrinsic_write_mask(intrin
));
99 nir_intrinsic_set_align(store
, align
, 0);
100 nir_builder_instr_insert(b
, &store
->instr
);
103 nir_instr_remove(&intrin
->instr
);
104 nir_deref_instr_remove_if_unused(deref
);
108 nir_lower_vars_to_scratch(nir_shader
*shader
,
109 nir_variable_mode modes
,
111 glsl_type_size_align_func size_align
)
113 /* First, we walk the instructions and flag any variables we want to lower
114 * by removing them from their respective list and setting the mode to 0.
116 nir_foreach_function(function
, shader
) {
117 nir_foreach_block(block
, function
->impl
) {
118 nir_foreach_instr(instr
, block
) {
119 if (instr
->type
!= nir_instr_type_intrinsic
)
122 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
123 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
&&
124 intrin
->intrinsic
!= nir_intrinsic_store_deref
)
127 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
128 if (!(deref
->mode
& modes
))
131 if (!deref_has_indirect(nir_src_as_deref(intrin
->src
[0])))
134 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
136 /* We set var->mode to 0 to indicate that a variable will be moved
137 * to scratch. Don't assign a scratch location twice.
139 if (var
->data
.mode
== 0)
142 unsigned var_size
, var_align
;
143 size_align(var
->type
, &var_size
, &var_align
);
144 if (var_size
<= size_threshold
)
147 /* Remove it from its list */
148 exec_node_remove(&var
->node
);
149 /* Invalid mode used to flag "moving to scratch" */
152 var
->data
.location
= ALIGN_POT(shader
->scratch_size
, var_align
);
153 shader
->scratch_size
= var
->data
.location
+ var_size
;
158 bool progress
= false;
159 nir_foreach_function(function
, shader
) {
164 nir_builder_init(&build
, function
->impl
);
166 bool impl_progress
= false;
167 nir_foreach_block(block
, function
->impl
) {
168 nir_foreach_instr_safe(instr
, block
) {
169 if (instr
->type
!= nir_instr_type_intrinsic
)
172 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
173 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
&&
174 intrin
->intrinsic
!= nir_intrinsic_store_deref
)
177 nir_variable
*var
= nir_intrinsic_get_var(intrin
, 0);
178 /* Variables flagged for lowering above have mode == 0 */
179 if (!var
|| var
->data
.mode
)
182 lower_load_store(&build
, intrin
, size_align
);
183 impl_progress
= true;
189 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
190 nir_metadata_dominance
);