nir: Consider deref instructions in lower_phis_to_scalar
[mesa.git] / src / compiler / nir / nir_lower_phis_to_scalar.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29
30 /*
31 * Implements a pass that lowers vector phi nodes to scalar phi nodes when
32 * we don't think it will hurt anything.
33 */
34
35 struct lower_phis_to_scalar_state {
36 void *mem_ctx;
37 void *dead_ctx;
38
39 /* Hash table marking which phi nodes are scalarizable. The key is
40 * pointers to phi instructions and the entry is either NULL for not
41 * scalarizable or non-null for scalarizable.
42 */
43 struct hash_table *phi_table;
44 };
45
46 static bool
47 should_lower_phi(nir_phi_instr *phi, struct lower_phis_to_scalar_state *state);
48
49 static bool
50 is_phi_src_scalarizable(nir_phi_src *src,
51 struct lower_phis_to_scalar_state *state)
52 {
53 /* Don't know what to do with non-ssa sources */
54 if (!src->src.is_ssa)
55 return false;
56
57 nir_instr *src_instr = src->src.ssa->parent_instr;
58 switch (src_instr->type) {
59 case nir_instr_type_alu: {
60 nir_alu_instr *src_alu = nir_instr_as_alu(src_instr);
61
62 /* ALU operations with output_size == 0 should be scalarized. We
63 * will also see a bunch of vecN operations from scalarizing ALU
64 * operations and, since they can easily be copy-propagated, they
65 * are ok too.
66 */
67 return nir_op_infos[src_alu->op].output_size == 0 ||
68 src_alu->op == nir_op_vec2 ||
69 src_alu->op == nir_op_vec3 ||
70 src_alu->op == nir_op_vec4;
71 }
72
73 case nir_instr_type_phi:
74 /* A phi is scalarizable if we're going to lower it */
75 return should_lower_phi(nir_instr_as_phi(src_instr), state);
76
77 case nir_instr_type_load_const:
78 case nir_instr_type_ssa_undef:
79 /* These are trivially scalarizable */
80 return true;
81
82 case nir_instr_type_intrinsic: {
83 nir_intrinsic_instr *src_intrin = nir_instr_as_intrinsic(src_instr);
84
85 switch (src_intrin->intrinsic) {
86 case nir_intrinsic_load_deref: {
87 nir_deref_instr *deref = nir_src_as_deref(src_intrin->src[0]);
88 return deref->mode == nir_var_shader_in ||
89 deref->mode == nir_var_uniform;
90 }
91
92 case nir_intrinsic_load_var:
93 return src_intrin->variables[0]->var->data.mode == nir_var_shader_in ||
94 src_intrin->variables[0]->var->data.mode == nir_var_uniform;
95
96 case nir_intrinsic_interp_deref_at_centroid:
97 case nir_intrinsic_interp_deref_at_sample:
98 case nir_intrinsic_interp_deref_at_offset:
99 case nir_intrinsic_interp_var_at_centroid:
100 case nir_intrinsic_interp_var_at_sample:
101 case nir_intrinsic_interp_var_at_offset:
102 case nir_intrinsic_load_uniform:
103 case nir_intrinsic_load_ubo:
104 case nir_intrinsic_load_ssbo:
105 case nir_intrinsic_load_input:
106 return true;
107 default:
108 break;
109 }
110 }
111
112 default:
113 /* We can't scalarize this type of instruction */
114 return false;
115 }
116 }
117
118 /**
119 * Determines if the given phi node should be lowered. The only phi nodes
120 * we will scalarize at the moment are those where all of the sources are
121 * scalarizable.
122 *
123 * The reason for this comes down to coalescing. Since phi sources can't
124 * swizzle, swizzles on phis have to be resolved by inserting a mov right
125 * before the phi. The choice then becomes between movs to pick off
126 * components for a scalar phi or potentially movs to recombine components
127 * for a vector phi. The problem is that the movs generated to pick off
128 * the components are almost uncoalescable. We can't coalesce them in NIR
129 * because we need them to pick off components and we can't coalesce them
130 * in the backend because the source register is a vector and the
131 * destination is a scalar that may be used at other places in the program.
132 * On the other hand, if we have a bunch of scalars going into a vector
133 * phi, the situation is much better. In this case, if the SSA def is
134 * generated in the predecessor block to the corresponding phi source, the
135 * backend code will be an ALU op into a temporary and then a mov into the
136 * given vector component; this move can almost certainly be coalesced
137 * away.
138 */
139 static bool
140 should_lower_phi(nir_phi_instr *phi, struct lower_phis_to_scalar_state *state)
141 {
142 /* Already scalar */
143 if (phi->dest.ssa.num_components == 1)
144 return false;
145
146 struct hash_entry *entry = _mesa_hash_table_search(state->phi_table, phi);
147 if (entry)
148 return entry->data != NULL;
149
150 /* Insert an entry and mark it as scalarizable for now. That way
151 * we don't recurse forever and a cycle in the dependence graph
152 * won't automatically make us fail to scalarize.
153 */
154 entry = _mesa_hash_table_insert(state->phi_table, phi, (void *)(intptr_t)1);
155
156 bool scalarizable = true;
157
158 nir_foreach_phi_src(src, phi) {
159 scalarizable = is_phi_src_scalarizable(src, state);
160 if (!scalarizable)
161 break;
162 }
163
164 /* The hash table entry for 'phi' may have changed while recursing the
165 * dependence graph, so we need to reset it */
166 entry = _mesa_hash_table_search(state->phi_table, phi);
167 assert(entry);
168
169 entry->data = (void *)(intptr_t)scalarizable;
170
171 return scalarizable;
172 }
173
174 static bool
175 lower_phis_to_scalar_block(nir_block *block,
176 struct lower_phis_to_scalar_state *state)
177 {
178 bool progress = false;
179
180 /* Find the last phi node in the block */
181 nir_phi_instr *last_phi = NULL;
182 nir_foreach_instr(instr, block) {
183 if (instr->type != nir_instr_type_phi)
184 break;
185
186 last_phi = nir_instr_as_phi(instr);
187 }
188
189 /* We have to handle the phi nodes in their own pass due to the way
190 * we're modifying the linked list of instructions.
191 */
192 nir_foreach_instr_safe(instr, block) {
193 if (instr->type != nir_instr_type_phi)
194 break;
195
196 nir_phi_instr *phi = nir_instr_as_phi(instr);
197
198 if (!should_lower_phi(phi, state))
199 continue;
200
201 unsigned bit_size = phi->dest.ssa.bit_size;
202
203 /* Create a vecN operation to combine the results. Most of these
204 * will be redundant, but copy propagation should clean them up for
205 * us. No need to add the complexity here.
206 */
207 nir_op vec_op;
208 switch (phi->dest.ssa.num_components) {
209 case 2: vec_op = nir_op_vec2; break;
210 case 3: vec_op = nir_op_vec3; break;
211 case 4: vec_op = nir_op_vec4; break;
212 default: unreachable("Invalid number of components");
213 }
214
215 nir_alu_instr *vec = nir_alu_instr_create(state->mem_ctx, vec_op);
216 nir_ssa_dest_init(&vec->instr, &vec->dest.dest,
217 phi->dest.ssa.num_components,
218 bit_size, NULL);
219 vec->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
220
221 for (unsigned i = 0; i < phi->dest.ssa.num_components; i++) {
222 nir_phi_instr *new_phi = nir_phi_instr_create(state->mem_ctx);
223 nir_ssa_dest_init(&new_phi->instr, &new_phi->dest, 1,
224 phi->dest.ssa.bit_size, NULL);
225
226 vec->src[i].src = nir_src_for_ssa(&new_phi->dest.ssa);
227
228 nir_foreach_phi_src(src, phi) {
229 /* We need to insert a mov to grab the i'th component of src */
230 nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,
231 nir_op_imov);
232 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, 1, bit_size, NULL);
233 mov->dest.write_mask = 1;
234 nir_src_copy(&mov->src[0].src, &src->src, state->mem_ctx);
235 mov->src[0].swizzle[0] = i;
236
237 /* Insert at the end of the predecessor but before the jump */
238 nir_instr *pred_last_instr = nir_block_last_instr(src->pred);
239 if (pred_last_instr && pred_last_instr->type == nir_instr_type_jump)
240 nir_instr_insert_before(pred_last_instr, &mov->instr);
241 else
242 nir_instr_insert_after_block(src->pred, &mov->instr);
243
244 nir_phi_src *new_src = ralloc(new_phi, nir_phi_src);
245 new_src->pred = src->pred;
246 new_src->src = nir_src_for_ssa(&mov->dest.dest.ssa);
247
248 exec_list_push_tail(&new_phi->srcs, &new_src->node);
249 }
250
251 nir_instr_insert_before(&phi->instr, &new_phi->instr);
252 }
253
254 nir_instr_insert_after(&last_phi->instr, &vec->instr);
255
256 nir_ssa_def_rewrite_uses(&phi->dest.ssa,
257 nir_src_for_ssa(&vec->dest.dest.ssa));
258
259 ralloc_steal(state->dead_ctx, phi);
260 nir_instr_remove(&phi->instr);
261
262 progress = true;
263
264 /* We're using the safe iterator and inserting all the newly
265 * scalarized phi nodes before their non-scalarized version so that's
266 * ok. However, we are also inserting vec operations after all of
267 * the last phi node so once we get here, we can't trust even the
268 * safe iterator to stop properly. We have to break manually.
269 */
270 if (instr == &last_phi->instr)
271 break;
272 }
273
274 return progress;
275 }
276
277 static bool
278 lower_phis_to_scalar_impl(nir_function_impl *impl)
279 {
280 struct lower_phis_to_scalar_state state;
281 bool progress = false;
282
283 state.mem_ctx = ralloc_parent(impl);
284 state.dead_ctx = ralloc_context(NULL);
285 state.phi_table = _mesa_hash_table_create(state.dead_ctx, _mesa_hash_pointer,
286 _mesa_key_pointer_equal);
287
288 nir_foreach_block(block, impl) {
289 progress = lower_phis_to_scalar_block(block, &state) || progress;
290 }
291
292 nir_metadata_preserve(impl, nir_metadata_block_index |
293 nir_metadata_dominance);
294
295 ralloc_free(state.dead_ctx);
296 return progress;
297 }
298
299 /** A pass that lowers vector phi nodes to scalar
300 *
301 * This pass loops through the blocks and lowers looks for vector phi nodes
302 * it can lower to scalar phi nodes. Not all phi nodes are lowered. For
303 * instance, if one of the sources is a non-scalarizable vector, then we
304 * don't bother lowering because that would generate hard-to-coalesce movs.
305 */
306 bool
307 nir_lower_phis_to_scalar(nir_shader *shader)
308 {
309 bool progress = false;
310
311 nir_foreach_function(function, shader) {
312 if (function->impl)
313 progress = lower_phis_to_scalar_impl(function->impl) || progress;
314 }
315
316 return progress;
317 }