<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 28, 2015 at 12:40 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Mon, Dec 28, 2015 at 2:29 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> This commit adds a NIR pass for lowering away returns in functions. If the<br>
> return is in a loop, it is lowered to a break. If it is not in a loop,<br>
> it's lowered away by moving/deleting code as needed.<br>
> ---<br>
> src/glsl/Makefile.sources | 1 +<br>
> src/glsl/nir/nir.h | 3 +<br>
> src/glsl/nir/nir_lower_returns.c | 245 +++++++++++++++++++++++++++++++++++++++<br>
> 3 files changed, 249 insertions(+)<br>
> create mode 100644 src/glsl/nir/nir_lower_returns.c<br>
><br>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> index fc10f14..fa3868c 100644<br>
> --- a/src/glsl/Makefile.sources<br>
> +++ b/src/glsl/Makefile.sources<br>
> @@ -43,6 +43,7 @@ NIR_FILES = \<br>
> nir/nir_lower_alu_to_scalar.c \<br>
> nir/nir_lower_atomics.c \<br>
> nir/nir_lower_clip.c \<br>
> + nir/nir_lower_returns.c \<br>
> nir/nir_lower_global_vars_to_local.c \<br>
> nir/nir_lower_gs_intrinsics.c \<br>
> nir/nir_lower_load_const_to_scalar.c \<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index af95cc4..fa99d47 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1951,6 +1951,9 @@ int nir_gs_count_vertices(const nir_shader *shader);<br>
><br>
> bool nir_split_var_copies(nir_shader *shader);<br>
><br>
> +bool nir_lower_returns_impl(nir_function_impl *impl);<br>
> +bool nir_lower_returns(nir_shader *shader);<br>
> +<br>
> void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);<br>
> void nir_lower_var_copies(nir_shader *shader);<br>
><br>
> diff --git a/src/glsl/nir/nir_lower_returns.c b/src/glsl/nir/nir_lower_returns.c<br>
> new file mode 100644<br>
> index 0000000..178e454<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_lower_returns.c<br>
> @@ -0,0 +1,245 @@<br>
> +/*<br>
> + * Copyright © 2015 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +#include "nir_builder.h"<br>
> +#include "nir_control_flow.h"<br>
> +<br>
> +struct lower_returns_state {<br>
> + nir_builder builder;<br>
> + struct exec_list *cf_list;<br>
> + nir_loop *loop;<br>
> + nir_variable *return_flag;<br>
> +};<br>
> +<br>
> +static bool lower_returns_in_cf_list(struct exec_list *cf_list,<br>
> + struct lower_returns_state *state);<br>
> +<br>
> +static void<br>
> +predicate_following(nir_cf_node *node, struct lower_returns_state *state)<br>
> +{<br>
> + nir_builder *b = &state->builder;<br>
> + b->cursor = nir_after_cf_node_and_phis(node);<br>
<br>
</div></div>BTW, the control flow modification code already interprets "before a<br>
block" to mean "after any phi nodes," since trying to split a block<br>
before phi nodes is invalid. The comparison below wouldn't work<br>
correctly if there were any phi nodes and if we used<br>
nir_after_cf_node() though, so I guess it's necessary anyways.<br>
<div><div class="h5"><br>
> +<br>
> + if (nir_cursors_equal(b->cursor, nir_after_cf_list(state->cf_list)))<br>
> + return; /* Nothing to predicate */<br>
> +<br>
> + assert(state->return_flag);<br>
> +<br>
> + nir_if *if_stmt = nir_if_create(b->shader);<br>
> + if_stmt->condition = nir_src_for_ssa(nir_load_var(b, state->return_flag));<br>
> + nir_cf_node_insert(b->cursor, &if_stmt->cf_node);<br>
> +<br>
> + if (state->loop) {<br>
> + /* If we're inside of a loop, then all we need to do is insert a<br>
> + * conditional break.<br>
> + */<br>
> + nir_jump_instr *brk =<br>
> + nir_jump_instr_create(state->builder.shader, nir_jump_break);<br>
> + nir_instr_insert(nir_before_cf_list(&if_stmt->then_list), &brk->instr);<br>
> + } else {<br>
> + /* Otherwise, we need to actually move everything into the else case<br>
> + * of the if statement.<br>
> + */<br>
> + nir_cf_list list;<br>
> + nir_cf_extract(&list, nir_after_cf_node(&if_stmt->cf_node),<br>
> + nir_after_cf_list(state->cf_list));<br>
> + assert(!exec_list_is_empty(&list.list));<br>
> + nir_cf_reinsert(&list, nir_before_cf_list(&if_stmt->else_list));<br>
> + }<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_returns_in_loop(nir_loop *loop, struct lower_returns_state *state)<br>
> +{<br>
> + nir_loop *parent = state->loop;<br>
> + state->loop = loop;<br>
> + bool progress = lower_returns_in_cf_list(&loop->body, state);<br>
> + state->loop = parent;<br>
> +<br>
> + /* If the recursive call made progress, then there were returns inside<br>
> + * of the loop. These would have been lowered to breaks with the return<br>
> + * flag set to true. We need to predicate everything following the loop<br>
> + * on the return flag.<br>
> + */<br>
> + if (progress)<br>
> + predicate_following(&loop->cf_node, state);<br>
> +<br>
> + return progress;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_returns_in_if(nir_if *if_stmt, struct lower_returns_state *state)<br>
> +{<br>
> + bool progress;<br>
> +<br>
> + progress = lower_returns_in_cf_list(&if_stmt->then_list, state);<br>
> + progress = lower_returns_in_cf_list(&if_stmt->else_list, state) || progress;<br>
> +<br>
> + /* If either of the recursive calls made progress, then there were<br>
> + * returns inside of the body of the if. If we're in a loop, then these<br>
> + * were lowered to breaks which automatically skip to the end of the<br>
> + * loop so we don't have to do anything. If we're not in a loop, then<br>
> + * all we know is that the return flag is set appropreately and that the<br>
> + * recursive calls ensured that nothing gets executed *inside* the if<br>
> + * after a return. In order to ensure nothing outside gets executed<br>
> + * after a return, we need to predicate everything following on the<br>
> + * return flag.<br>
> + */<br>
> + if (progress && !state->loop)<br>
> + predicate_following(&if_stmt->cf_node, state);<br>
> +<br>
> + return progress;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_returns_in_block(nir_block *block, struct lower_returns_state *state)<br>
> +{<br>
> + if (block->predecessors->entries == 0 &&<br>
> + block != nir_start_block(state->builder.impl)) {<br>
> + /* This block is unreachable. Delete it and everything after it. */<br>
> + nir_cf_list list;<br>
> + nir_cf_extract(&list, nir_before_cf_node(&block->cf_node),<br>
> + nir_after_cf_list(state->cf_list));<br>
> +<br>
> + if (exec_list_is_empty(&list.list)) {<br>
> + /* There's nothing here, which also means there's nothing in this<br>
> + * block so we have nothing to do.<br>
> + */<br>
> + return false;<br>
> + } else {<br>
> + nir_cf_delete(&list);<br>
> + return true;<br>
> + }<br>
> + }<br>
> +<br>
> + nir_instr *last_instr = nir_block_last_instr(block);<br>
> + if (last_instr == NULL)<br>
> + return false;<br>
> +<br>
> + if (last_instr->type != nir_instr_type_jump)<br>
> + return false;<br>
> +<br>
> + nir_jump_instr *jump = nir_instr_as_jump(last_instr);<br>
> + if (jump->type != nir_jump_return)<br>
> + return false;<br>
> +<br>
> + nir_builder *b = &state->builder;<br>
> + b->cursor = nir_before_instr(&jump->instr);<br>
> +<br>
> + /* Set the return flag */<br>
> + if (state->return_flag == NULL) {<br>
> + state->return_flag =<br>
> + nir_local_variable_create(b->impl, glsl_bool_type(), "return");<br>
> +<br>
> + /* Set a default value of false */<br>
> + state->return_flag->constant_initializer =<br>
> + rzalloc(state->return_flag, nir_constant);<br>
> + }<br>
> + nir_store_var(b, state->return_flag, nir_imm_int(b, NIR_TRUE), 1);<br>
> +<br>
> + if (state->loop) {<br>
> + /* We're in a loop. Make the return a break. */<br>
> + jump->type = nir_jump_return;<br>
<br>
</div></div>nir_jump_break?<br></blockquote><div><br></div><div>Yup, thanks!<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Other than this, I couldn't find anything wrong, so it's<br>
<br>
Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br></blockquote><div><br></div><div>Thanks!<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
but I hope you can get loops working soon so you can test this (and<br>
hey, loops are useful for other things too :P).<br></blockquote><div><br></div><div>Yeah, I'm starting on adding a real CFG to spirv_to_nir today. It's the task that I've had on my todo list for a long time that I've been avoiding and hoping would go away. It didn't go away.<br></div><div>--Jason<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
> + } else {<br>
> + /* Not in a loop. Just delete the return; we'll deal with<br>
> + * predicating later.<br>
> + */<br>
> + assert(nir_cf_node_next(&block->cf_node) == NULL);<br>
> + nir_instr_remove(&jump->instr);<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_returns_in_cf_list(struct exec_list *cf_list,<br>
> + struct lower_returns_state *state)<br>
> +{<br>
> + bool progress = false;<br>
> +<br>
> + struct exec_list *parent_list = state->cf_list;<br>
> + state->cf_list = cf_list;<br>
> +<br>
> + /* We iterate over the list backwards because any given lower call may<br>
> + * take everything following the given CF node and predicate it. In<br>
> + * order to avoid recursion/iteration problems, we want everything after<br>
> + * a given node to already be lowered before this happens.<br>
> + */<br>
> + foreach_list_typed_reverse_safe(nir_cf_node, node, node, cf_list) {<br>
> + switch (node->type) {<br>
> + case nir_cf_node_block:<br>
> + if (lower_returns_in_block(nir_cf_node_as_block(node), state))<br>
> + progress = true;<br>
> + break;<br>
> +<br>
> + case nir_cf_node_if:<br>
> + if (lower_returns_in_if(nir_cf_node_as_if(node), state))<br>
> + progress = true;<br>
> + break;<br>
> +<br>
> + case nir_cf_node_loop:<br>
> + if (lower_returns_in_loop(nir_cf_node_as_loop(node), state))<br>
> + progress = true;<br>
> + break;<br>
> +<br>
> + default:<br>
> + unreachable("Invalid inner CF node type");<br>
> + }<br>
> + }<br>
> +<br>
> + state->cf_list = parent_list;<br>
> +<br>
> + return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_lower_returns_impl(nir_function_impl *impl)<br>
> +{<br>
> + struct lower_returns_state state;<br>
> +<br>
> + state.cf_list = &impl->body;<br>
> + state.loop = NULL;<br>
> + state.return_flag = NULL;<br>
> + nir_builder_init(&state.builder, impl);<br>
> +<br>
> + bool progress = lower_returns_in_cf_list(&impl->body, &state);<br>
> +<br>
> + if (progress)<br>
> + nir_metadata_preserve(impl, nir_metadata_none);<br>
> +<br>
> + return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_lower_returns(nir_shader *shader)<br>
> +{<br>
> + bool progress = false;<br>
> +<br>
> + nir_foreach_function(shader, function) {<br>
> + if (function->impl)<br>
> + progress = nir_lower_returns_impl(function->impl) || progress;<br>
> + }<br>
> +<br>
> + return progress;<br>
> +}<br>
> --<br>
> 2.5.0.400.gff86faf<br>
><br>
</div></div>> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>