<div dir="auto"><div><div class="gmail_extra"><div class="gmail_quote">On Dec 25, 2016 2:15 PM, "Mark Janes" <<a href="mailto:mark.a.janes@intel.com">mark.a.janes@intel.com</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="elided-text">Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> writes:<br>
<br>
> When shaders come in from SPIR-V, we handle continue blocks by placing<br>
> the contents of the continue inside of a "if (!first_iteration)".  We do<br>
> this so that we can properly handle the fact that continues in SPIR-V<br>
> jump to the continue block at the end of the loop rather than jumping<br>
> directly to the top of the loop like they do in NIR.  In particular, the<br>
> increment step of a simple for loop ends up in the continue block.  This<br>
> pass looks for this case in loops that don't actually have any continues<br>
> and moves the continue contents to the end of the loop instead.  We need<br>
> this because loop unrolling doesn't work if the increment is inside of a<br>
> condition.<br>
> ---<br>
>  src/compiler/Makefile.sources |   1 +<br>
>  src/compiler/nir/nir.h        |   2 +<br>
>  src/compiler/nir/nir_opt_if.c | 253 ++++++++++++++++++++++++++++++<wbr>++++++++++++<br>
>  3 files changed, 256 insertions(+)<br>
>  create mode 100644 src/compiler/nir/nir_opt_if.c<br>
><br>
> diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
> index a0abede..6f701af 100644<br>
> --- a/src/compiler/Makefile.<wbr>sources<br>
> +++ b/src/compiler/Makefile.<wbr>sources<br>
> @@ -239,6 +239,7 @@ NIR_FILES = \<br>
>       nir/nir_opt_dead_cf.c \<br>
>       nir/nir_opt_gcm.c \<br>
>       nir/nir_opt_global_to_local.c \<br>
> +     nir/nir_opt_if.c \<br>
>       nir/nir_opt_loop_unroll.c \<br>
>       nir/nir_opt_peephole_select.c \<br>
>       nir/nir_opt_remove_phis.c \<br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index a6c8956..7b582a6 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2557,6 +2557,8 @@ bool nir_opt_dead_cf(nir_shader *shader);<br>
><br>
>  bool nir_opt_gcm(nir_shader *shader, bool value_number);<br>
><br>
> +bool nir_opt_if(nir_shader *shader);<br>
> +<br>
>  bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask);<br>
><br>
>  bool nir_opt_peephole_select(nir_<wbr>shader *shader, unsigned limit);<br>
> diff --git a/src/compiler/nir/nir_opt_if.<wbr>c b/src/compiler/nir/nir_opt_if.<wbr>c<br>
> new file mode 100644<br>
> index 0000000..5590684<br>
> --- /dev/null<br>
> +++ b/src/compiler/nir/nir_opt_if.<wbr>c<br>
> @@ -0,0 +1,253 @@<br>
> +/*<br>
> + * Copyright © 2016 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_control_flow.h"<br>
> +<br>
> +/**<br>
> + * This optimization detects if statements at the tops of loops where the<br>
> + * condition is a phi node of two constants and moves half of the if to above<br>
> + * the loop and the other half of the if to the end of the loop.  A simple for<br>
> + * loop "for (int i = 0; i < 4; i++)", when run through the SPIR-V front-end,<br>
> + * ends up looking something like this:<br>
> + *<br>
> + * vec1 32 ssa_0 = load_const (0x00000000)<br>
> + * vec1 32 ssa_1 = load_const (0xffffffff)<br>
> + * loop {<br>
> + *    block block_1:<br>
> + *    vec1 32 ssa_2 = phi block_0: ssa_0, block_7: ssa_5<br>
> + *    vec1 32 ssa_3 = phi block_0: ssa_0, block_7: ssa_1<br>
> + *    if ssa_2 {<br>
> + *       block block_2:<br>
> + *       vec1 32 ssa_4 = load_const (0x00000001)<br>
> + *       vec1 32 ssa_5 = iadd ssa_2, ssa_4<br>
> + *    } else {<br>
> + *       block block_3:<br>
> + *    }<br>
> + *    block block_4:<br>
> + *    vec1 32 ssa_6 = load_const (0x00000004)<br>
> + *    vec1 32 ssa_7 = ilt ssa_5, ssa_6<br>
> + *    if ssa_7 {<br>
> + *       block block_5:<br>
> + *    } else {<br>
> + *       block block_6:<br>
> + *       break<br>
> + *    }<br>
> + *    block block_7:<br>
> + * }<br>
> + *<br>
> + * This turns it into something like this:<br>
> + *<br>
> + * // Stuff from block 1<br>
> + * // Stuff from block 3<br>
> + * loop {<br>
> + *    block block_1:<br>
> + *    vec1 32 ssa_3 = phi block_0: ssa_0, block_7: ssa_1<br>
> + *    vec1 32 ssa_6 = load_const (0x00000004)<br>
> + *    vec1 32 ssa_7 = ilt ssa_5, ssa_6<br>
> + *    if ssa_7 {<br>
> + *       block block_5:<br>
> + *    } else {<br>
> + *       block block_6:<br>
> + *       break<br>
> + *    }<br>
> + *    block block_7:<br>
> + *    // Stuff from block 1<br>
> + *    // Stuff from block 2<br>
> + *    vec1 32 ssa_4 = load_const (0x00000001)<br>
> + *    vec1 32 ssa_5 = iadd ssa_2, ssa_4<br>
> + * }<br>
> + */<br>
> +static bool<br>
> +opt_peel_loop_initial_if(nir_<wbr>loop *loop)<br>
> +{<br>
> +   nir_block *header_block = nir_loop_first_block(loop);<br>
> +   nir_block *prev_block =<br>
> +      nir_cf_node_as_block(nir_cf_<wbr>node_prev(&loop->cf_node));<br>
> +<br>
> +   /* It would be insane if this were not true */<br>
> +   assert(_mesa_set_search(<wbr>header_block->predecessors, prev_block));<br>
> +   /* It must have exactly one "continue" */<br>
> +   if (header_block->predecessors-><wbr>entries != 2)<br>
> +      return false;<br>
> +<br>
> +   nir_block *cont_block = NULL;<br>
> +   struct set_entry *pred_entry;<br>
> +   set_foreach(header_block-><wbr>predecessors, pred_entry) {<br>
> +      if (pred_entry->key != prev_block)<br>
> +         cont_block = (void *)pred_entry->key;<br>
> +   }<br>
> +<br>
> +   nir_cf_node *if_node = nir_cf_node_next(&header_<wbr>block->cf_node);<br>
> +   if (!if_node || if_node->type != nir_cf_node_if)<br>
> +      return false;<br>
> +<br>
> +   nir_if *nif = nir_cf_node_as_if(if_node);<br>
> +   assert(nif->condition.is_ssa);<br>
> +<br>
> +   nir_ssa_def *cond = nif->condition.ssa;<br>
> +   if (cond->parent_instr->type != nir_instr_type_phi)<br>
> +      return false;<br>
> +<br>
> +   nir_phi_instr *cond_phi = nir_instr_as_phi(cond->parent_<wbr>instr);<br>
> +   if (cond->parent_instr->block != header_block)<br>
> +      return false;<br>
> +<br>
> +   /* We already know we have exactly one continue */<br>
> +   uint32_t entry_val, cont_val;<br>
<br>
</div>Coverity complains in CID 1397794 that entry_val and cont_val need to be<br>
initialized.</blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">Initializing them both to zero probably wouldn't hurt but... If they don't both get initialized, something has gone terribly wrong.  I don't think this one is worth wiring about.</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="quoted-text">
> +   assert(exec_list_length(&cond_<wbr>phi->srcs) == 2);<br>
> +   nir_foreach_phi_src(src, cond_phi) {<br>
> +      assert(src->src.is_ssa);<br>
> +      nir_const_value *const_src = nir_src_as_const_value(src-><wbr>src);<br>
> +      if (!const_src)<br>
> +         return false;<br>
> +<br>
> +      if (src->pred == cont_block) {<br>
> +         cont_val = const_src->u32[0];<br>
> +      } else {<br>
> +         assert(src->pred == prev_block);<br>
> +         entry_val = const_src->u32[0];<br>
> +      }<br>
> +   }<br>
> +<br>
> +   /* If they both execute or both don't execute, this is a job for<br>
> +    * nir_dead_cf, not this pass.<br>
> +    */<br>
> +   if ((entry_val && cont_val) || (!entry_val && !cont_val))<br>
<br>
</div>Might the variables still be undefined at this point?<br>
<div class="elided-text"><br>
> +      return false;<br>
> +<br>
> +   struct exec_list *cont_list, *entry_list;<br>
> +   if (cont_val) {<br>
> +      cont_list = &nif->then_list;<br>
> +      entry_list = &nif->else_list;<br>
> +   } else {<br>
> +      cont_list = &nif->else_list;<br>
> +      entry_list = &nif->then_list;<br>
> +   }<br>
> +<br>
> +   /* We want to be moving the contents of entry_list to above the loop so it<br>
> +    * can't contain any break or continue instructions.<br>
> +    */<br>
> +   foreach_list_typed(nir_cf_<wbr>node, cf_node, node, entry_list) {<br>
> +      nir_foreach_block_in_cf_node(<wbr>block, cf_node) {<br>
> +         nir_instr *last_instr = nir_block_last_instr(block);<br>
> +         if (last_instr && last_instr->type == nir_instr_type_jump)<br>
> +            return false;<br>
> +      }<br>
> +   }<br>
> +<br>
> +   /* Before we do anything, convert the loop to LCSSA.  We're about to<br>
> +    * replace a bunch of SSA defs with registers and this will prevent any of<br>
> +    * it from leaking outside the loop.<br>
> +    */<br>
> +   nir_convert_loop_to_lcssa(<wbr>loop);<br>
> +<br>
> +   nir_block *after_if_block =<br>
> +      nir_cf_node_as_block(nir_cf_<wbr>node_next(&nif->cf_node));<br>
> +<br>
> +   /* Get rid of phis in the header block since we will be duplicating it */<br>
> +   nir_lower_phis_to_regs_block(<wbr>header_block);<br>
> +   /* Get rid of phis after the if since dominance will change */<br>
> +   nir_lower_phis_to_regs_block(<wbr>after_if_block);<br>
> +<br>
> +   /* Get rid of SSA defs in the pieces we're about to move around */<br>
> +   nir_lower_ssa_defs_to_regs_<wbr>block(header_block);<br>
> +   nir_foreach_block_in_cf_node(<wbr>block, &nif->cf_node)<br>
> +      nir_lower_ssa_defs_to_regs_<wbr>block(block);<br>
> +<br>
> +   nir_cf_list header, tmp;<br>
> +   nir_cf_extract(&header, nir_before_block(header_block)<wbr>,<br>
> +                           nir_after_block(header_block))<wbr>;<br>
> +<br>
> +   nir_cf_list_clone(&tmp, &header, &loop->cf_node, NULL);<br>
> +   nir_cf_reinsert(&tmp, nir_before_cf_node(&loop->cf_<wbr>node));<br>
> +   nir_cf_extract(&tmp, nir_before_cf_list(entry_list)<wbr>,<br>
> +                        nir_after_cf_list(entry_list))<wbr>;<br>
> +   nir_cf_reinsert(&tmp, nir_before_cf_node(&loop->cf_<wbr>node));<br>
> +<br>
> +   nir_cf_list_clone(&tmp, &header, &loop->cf_node, NULL);<br>
> +   nir_cf_reinsert(&tmp, nir_after_block_before_jump(<wbr>cont_block));<br>
> +   nir_cf_extract(&tmp, nir_before_cf_list(cont_list),<br>
> +                        nir_after_cf_list(cont_list));<br>
> +   nir_cf_reinsert(&tmp, nir_after_block_before_jump(<wbr>cont_block));<br>
> +<br>
> +   nir_cf_delete(&header);<br>
> +   nir_cf_node_remove(&nif->cf_<wbr>node);<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +opt_if_cf_list(struct exec_list *cf_list)<br>
> +{<br>
> +   bool progress = false;<br>
> +   foreach_list_typed(nir_cf_<wbr>node, cf_node, node, cf_list) {<br>
> +      switch (cf_node->type) {<br>
> +      case nir_cf_node_block:<br>
> +         break;<br>
> +<br>
> +      case nir_cf_node_if: {<br>
> +         nir_if *nif = nir_cf_node_as_if(cf_node);<br>
> +         progress |= opt_if_cf_list(&nif->then_<wbr>list);<br>
> +         progress |= opt_if_cf_list(&nif->else_<wbr>list);<br>
> +         break;<br>
> +      }<br>
> +<br>
> +      case nir_cf_node_loop: {<br>
> +         nir_loop *loop = nir_cf_node_as_loop(cf_node);<br>
> +         progress |= opt_if_cf_list(&loop->body);<br>
> +         progress |= opt_peel_loop_initial_if(loop)<wbr>;<br>
> +         break;<br>
> +      }<br>
> +<br>
> +      case nir_cf_node_function:<br>
> +         unreachable("Invalid cf type");<br>
> +      }<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_opt_if(nir_shader *shader)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_foreach_function(function, shader) {<br>
> +      if (function->impl == NULL)<br>
> +         continue;<br>
> +<br>
> +      if (opt_if_cf_list(&function-><wbr>impl->body)) {<br>
> +         nir_metadata_preserve(<wbr>function->impl, nir_metadata_none);<br>
> +<br>
> +         /* If that made progress, we're no longer really in SSA form.  We<br>
> +          * need to convert registers back into SSA defs and clean up SSA defs<br>
> +          * that don't dominate their uses.<br>
> +          */<br>
> +         nir_convert_to_ssa_impl(<wbr>function->impl);<br>
> +         progress = true;<br>
> +      }<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> --<br>
> 2.5.0.400.gff86faf<br>
><br>
</div>> ______________________________<wbr>_________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div></div>