<div dir="ltr"><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>><br>Tested-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>><br><br></div>And here's the shader-db stats for the commit message:<br><br>total NIR instructions in shared programs: 2045293 -> 2041209 (-0.20%)<br>NIR instructions in affected programs:     126564 -> 122480 (-3.23%)<br>helped:                                615<br>HURT:                                  0<br><br>total FS instructions in shared programs: 4321840 -> 4320392 (-0.03%)<br>FS instructions in affected programs:     24622 -> 23174 (-5.88%)<br>helped:                                138<br>HURT:                                  0<br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 2, 2015 at 10:59 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 Tue, Feb 3, 2015 at 1:54 AM, Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
> This removes phi nodes whose sources all point to the same thing.<br>
><br>
> Only compile tested.<br>
><br>
> Signed-off-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
> ---<br>
>  src/glsl/Makefile.sources          |   1 +<br>
>  src/glsl/nir/nir.h                 |   2 +<br>
>  src/glsl/nir/nir_opt_remove_phis.c | 111 +++++++++++++++++++++++++++++++++++++<br>
>  3 files changed, 114 insertions(+)<br>
>  create mode 100644 src/glsl/nir/nir_opt_remove_phis.c<br>
><br>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> index face22e..6d51c71 100644<br>
> --- a/src/glsl/Makefile.sources<br>
> +++ b/src/glsl/Makefile.sources<br>
> @@ -44,6 +44,7 @@ NIR_FILES = \<br>
>         nir/nir_opt_dce.c \<br>
>         nir/nir_opt_global_to_local.c \<br>
>         nir/nir_opt_peephole_select.c \<br>
> +       nir/nir_opt_remove_phis.c \<br>
>         nir/nir_print.c \<br>
>         nir/nir_remove_dead_variables.c \<br>
>         nir/nir_search.c \<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index 98d2689..d1860d4 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1559,6 +1559,8 @@ bool nir_opt_dce(nir_shader *shader);<br>
>  bool nir_opt_peephole_select(nir_shader *shader);<br>
>  bool nir_opt_peephole_ffma(nir_shader *shader);<br>
><br>
> +bool nir_opt_remove_phis(nir_shader *shader);<br>
> +<br>
>  #ifdef __cplusplus<br>
>  } /* extern "C" */<br>
>  #endif<br>
> diff --git a/src/glsl/nir/nir_opt_remove_phis.c b/src/glsl/nir/nir_opt_remove_phis.c<br>
> new file mode 100644<br>
> index 0000000..aec5dc01<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_opt_remove_phis.c<br>
> @@ -0,0 +1,111 @@<br>
> +/*<br>
> + * Copyright © 2015 Connor Abbott<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>
> + * Authors:<br>
> + *    Connor Abbott (<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>)<br>
> + *<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +<br>
> +/*<br>
> + * This is a pass for removing phi nodes that look like:<br>
> + * a = phi(b, b, b, ...)<br>
> + *<br>
> + * Note that we can't ignore undef sources here, or else we may create a<br>
> + * situation where the definition of b isn't dominated by its uses. We're<br>
> + * allowed to do this since the definition of b must dominate all of the<br>
> + * phi node's predecessors, which means it must dominate the phi node as well<br>
> + * as all of the phi node's uses. In essence, the phi node acts as a copy<br>
> + * instruction. b can't be another phi node in the same block, since the only<br>
> + * time when phi nodes can source other phi nodes defined in the same block is<br>
> + * at the loop header, and in that case one of the sources of the phi has to<br>
> + * be from before the loop and that source can't be b.<br>
> + */<br>
> +<br>
> +/* returns true if any progress was made */<br>
<br>
</div></div>Gah, this shouldn't be there. Fixed locally.<br>
<div class="HOEnZb"><div class="h5"><br>
> +<br>
> +static bool<br>
> +remove_phis_block(nir_block *block, void *state)<br>
> +{<br>
> +   bool *progress = state;<br>
> +<br>
> +   void *mem_ctx = ralloc_parent(block);<br>
> +<br>
> +   nir_foreach_instr_safe(block, instr) {<br>
> +      if (instr->type != nir_instr_type_phi)<br>
> +         break;<br>
> +<br>
> +      nir_phi_instr *phi = nir_instr_as_phi(instr);<br>
> +<br>
> +      nir_ssa_def *def = NULL;<br>
> +      bool srcs_same = true;<br>
> +<br>
> +      nir_foreach_phi_src(phi, src) {<br>
> +         assert(src->src.is_ssa);<br>
> +<br>
> +         if (def == NULL) {<br>
> +            def  = src->src.ssa;<br>
> +         } else {<br>
> +            if (src->src.ssa != def) {<br>
> +               srcs_same = false;<br>
> +               break;<br>
> +            }<br>
> +         }<br>
> +      }<br>
> +<br>
> +      if (!srcs_same)<br>
> +         continue;<br>
> +<br>
> +      assert(phi->dest.is_ssa);<br>
> +      nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def),<br>
> +                               mem_ctx);<br>
> +      nir_instr_remove(instr);<br>
> +<br>
> +      *progress = true;<br>
> +   }<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +remove_phis_impl(nir_function_impl *impl)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_foreach_block(impl, remove_phis_block, &progress);<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_opt_remove_phis(nir_shader *shader)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_foreach_overload(shader, overload)<br>
> +      if (overload->impl)<br>
> +         progress = remove_phis_impl(overload->impl) || progress;<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> --<br>
> 2.1.0<br>
><br>
_______________________________________________<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" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</div></div></blockquote></div><br></div>