xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_returns.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
27 
28 struct lower_returns_state {
29    nir_builder builder;
30    struct exec_list *cf_list;
31    nir_loop *loop;
32    nir_variable *return_flag;
33 
34    /* This indicates that we have a return which is predicated on some form of
35     * control-flow.  Since whether or not the return happens can only be
36     * determined dynamically at run-time, everything that occurs afterwards
37     * needs to be predicated on the return flag variable.
38     */
39    bool has_predicated_return;
40 
41    bool removed_unreachable_code;
42 };
43 
44 static bool lower_returns_in_cf_list(struct exec_list *cf_list,
45                                      struct lower_returns_state *state);
46 
47 static void
predicate_following(nir_cf_node * node,struct lower_returns_state * state)48 predicate_following(nir_cf_node *node, struct lower_returns_state *state)
49 {
50    nir_builder *b = &state->builder;
51    b->cursor = nir_after_cf_node_and_phis(node);
52 
53    if (!state->loop && nir_cursors_equal(b->cursor, nir_after_cf_list(state->cf_list)))
54       return; /* Nothing to predicate */
55 
56    assert(state->return_flag);
57 
58    nir_if *if_stmt = nir_push_if(b, nir_load_var(b, state->return_flag));
59 
60    if (state->loop) {
61       /* If we're inside of a loop, then all we need to do is insert a
62        * conditional break.
63        */
64       nir_jump(b, nir_jump_break);
65 
66       nir_block *block = nir_cursor_current_block(b->cursor);
67       nir_insert_phi_undef(block->successors[0], block);
68    } else {
69       /* Otherwise, we need to actually move everything into the else case
70        * of the if statement.
71        */
72       nir_cf_list list;
73       nir_cf_extract(&list, nir_after_cf_node(&if_stmt->cf_node),
74                      nir_after_cf_list(state->cf_list));
75       assert(!exec_list_is_empty(&list.list));
76       nir_cf_reinsert(&list, nir_before_cf_list(&if_stmt->else_list));
77    }
78 
79    nir_pop_if(b, NULL);
80 }
81 
82 static bool
lower_returns_in_loop(nir_loop * loop,struct lower_returns_state * state)83 lower_returns_in_loop(nir_loop *loop, struct lower_returns_state *state)
84 {
85    assert(!nir_loop_has_continue_construct(loop));
86    nir_loop *parent = state->loop;
87    state->loop = loop;
88    bool progress = lower_returns_in_cf_list(&loop->body, state);
89    state->loop = parent;
90 
91    /* If the recursive call made progress, then there were returns inside
92     * of the loop.  These would have been lowered to breaks with the return
93     * flag set to true.  We need to predicate everything following the loop
94     * on the return flag.
95     */
96    if (progress) {
97       predicate_following(&loop->cf_node, state);
98       state->has_predicated_return = true;
99    }
100 
101    return progress;
102 }
103 
104 static bool
lower_returns_in_if(nir_if * if_stmt,struct lower_returns_state * state)105 lower_returns_in_if(nir_if *if_stmt, struct lower_returns_state *state)
106 {
107    bool progress, then_progress, else_progress;
108 
109    bool has_predicated_return = state->has_predicated_return;
110    state->has_predicated_return = false;
111 
112    then_progress = lower_returns_in_cf_list(&if_stmt->then_list, state);
113    else_progress = lower_returns_in_cf_list(&if_stmt->else_list, state);
114    progress = then_progress || else_progress;
115 
116    /* If either of the recursive calls made progress, then there were
117     * returns inside of the body of the if.  If we're in a loop, then these
118     * were lowered to breaks which automatically skip to the end of the
119     * loop so we don't have to do anything.  If we're not in a loop, then
120     * all we know is that the return flag is set appropriately and that the
121     * recursive calls ensured that nothing gets executed *inside* the if
122     * after a return.  In order to ensure nothing outside gets executed
123     * after a return, we need to predicate everything following on the
124     * return flag.
125     */
126    if (progress && !state->loop) {
127       if (state->has_predicated_return) {
128          predicate_following(&if_stmt->cf_node, state);
129       } else {
130          /* If there are no nested returns we can just add the instructions to
131           * the end of the branch that doesn't have the return.
132           */
133 
134          /* nir_cf_extract will not extract phis at the start of the block. In
135           * this case we know that any phis will have to have had a single
136           * predecessor, and should've been removed by the opt_remove_phis before
137           * beginning this pass.
138           */
139          ASSERTED nir_block *succ_block = nir_after_cf_node(&if_stmt->cf_node).block;
140          assert(nir_block_first_instr(succ_block) == NULL ||
141                 nir_block_first_instr(succ_block)->type != nir_instr_type_phi);
142 
143          nir_cf_list list;
144          nir_cf_extract(&list, nir_after_cf_node(&if_stmt->cf_node),
145                         nir_after_cf_list(state->cf_list));
146 
147          if (then_progress && else_progress) {
148             /* Both branches return so delete instructions following the if */
149             nir_cf_delete(&list);
150          } else if (then_progress) {
151             nir_cf_reinsert(&list, nir_after_cf_list(&if_stmt->else_list));
152          } else {
153             nir_cf_reinsert(&list, nir_after_cf_list(&if_stmt->then_list));
154          }
155       }
156    }
157 
158    state->has_predicated_return = progress || has_predicated_return;
159 
160    return progress;
161 }
162 
163 static bool
lower_returns_in_block(nir_block * block,struct lower_returns_state * state)164 lower_returns_in_block(nir_block *block, struct lower_returns_state *state)
165 {
166    if (block->predecessors->entries == 0 &&
167        block != nir_start_block(state->builder.impl)) {
168       /* This block is unreachable.  Delete it and everything after it. */
169       nir_cf_list list;
170       nir_cf_extract(&list, nir_before_cf_node(&block->cf_node),
171                      nir_after_cf_list(state->cf_list));
172 
173       if (exec_list_is_empty(&list.list)) {
174          /* There's nothing here, which also means there's nothing in this
175           * block so we have nothing to do.
176           */
177          return false;
178       } else {
179          state->removed_unreachable_code = true;
180          nir_cf_delete(&list);
181          return false;
182       }
183    }
184 
185    nir_instr *last_instr = nir_block_last_instr(block);
186    if (last_instr == NULL)
187       return false;
188 
189    if (last_instr->type != nir_instr_type_jump)
190       return false;
191 
192    nir_jump_instr *jump = nir_instr_as_jump(last_instr);
193    if (jump->type != nir_jump_return)
194       return false;
195 
196    nir_instr_remove(&jump->instr);
197 
198    /* If this is a return in the last block of the function there is nothing
199     * more to do once its removed.
200     */
201    if (block == nir_impl_last_block(state->builder.impl))
202       return true;
203 
204    nir_builder *b = &state->builder;
205 
206    /* Set the return flag */
207    if (state->return_flag == NULL) {
208       state->return_flag =
209          nir_local_variable_create(b->impl, glsl_bool_type(), "return");
210 
211       /* Initialize the variable to 0 */
212       b->cursor = nir_before_impl(b->impl);
213       nir_store_var(b, state->return_flag, nir_imm_false(b), 1);
214    }
215 
216    b->cursor = nir_after_block(block);
217    nir_store_var(b, state->return_flag, nir_imm_true(b), 1);
218 
219    if (state->loop) {
220       /* We're in a loop;  we need to break out of it. */
221       nir_jump(b, nir_jump_break);
222 
223       nir_insert_phi_undef(block->successors[0], block);
224    } else {
225       /* Not in a loop;  we'll deal with predicating later*/
226       assert(nir_cf_node_next(&block->cf_node) == NULL);
227    }
228 
229    return true;
230 }
231 
232 static bool
lower_returns_in_cf_list(struct exec_list * cf_list,struct lower_returns_state * state)233 lower_returns_in_cf_list(struct exec_list *cf_list,
234                          struct lower_returns_state *state)
235 {
236    bool progress = false;
237 
238    struct exec_list *parent_list = state->cf_list;
239    state->cf_list = cf_list;
240 
241    /* We iterate over the list backwards because any given lower call may
242     * take everything following the given CF node and predicate it.  In
243     * order to avoid recursion/iteration problems, we want everything after
244     * a given node to already be lowered before this happens.
245     */
246    foreach_list_typed_reverse_safe(nir_cf_node, node, node, cf_list) {
247       switch (node->type) {
248       case nir_cf_node_block:
249          if (lower_returns_in_block(nir_cf_node_as_block(node), state))
250             progress = true;
251          break;
252 
253       case nir_cf_node_if:
254          if (lower_returns_in_if(nir_cf_node_as_if(node), state))
255             progress = true;
256          break;
257 
258       case nir_cf_node_loop:
259          if (lower_returns_in_loop(nir_cf_node_as_loop(node), state))
260             progress = true;
261          break;
262 
263       default:
264          unreachable("Invalid inner CF node type");
265       }
266    }
267 
268    state->cf_list = parent_list;
269 
270    return progress;
271 }
272 
273 bool
nir_lower_returns_impl(nir_function_impl * impl)274 nir_lower_returns_impl(nir_function_impl *impl)
275 {
276    struct lower_returns_state state;
277 
278    state.cf_list = &impl->body;
279    state.loop = NULL;
280    state.return_flag = NULL;
281    state.has_predicated_return = false;
282    state.removed_unreachable_code = false;
283    state.builder = nir_builder_create(impl);
284 
285    bool progress = lower_returns_in_cf_list(&impl->body, &state);
286    progress = progress || state.removed_unreachable_code;
287 
288    if (progress) {
289       nir_metadata_preserve(impl, nir_metadata_none);
290       nir_rematerialize_derefs_in_use_blocks_impl(impl);
291       nir_repair_ssa_impl(impl);
292    } else {
293       nir_metadata_preserve(impl, nir_metadata_all);
294    }
295 
296    return progress;
297 }
298 
299 bool
nir_lower_returns(nir_shader * shader)300 nir_lower_returns(nir_shader *shader)
301 {
302    /* Before removing jumps and adding undef sources to otherwise single-source phis,
303     * go ahead and simplify those single-source phis.
304     */
305    bool progress = nir_opt_remove_phis(shader);
306 
307    nir_foreach_function_impl(impl, shader) {
308       progress |= nir_lower_returns_impl(impl) || progress;
309    }
310 
311    return progress;
312 }
313