<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jul 21, 2016 at 2:52 AM, Iago Toral <span dir="ltr"><<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.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 Wed, 2016-07-20 at 15:28 -0700, Jason Ekstrand wrote:<br>
> Signed-off-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br>
> ---<br>
>  src/compiler/Makefile.<wbr>sources                      |<wbr>   1 +<br>
>  src/compiler/nir/nir.h       <wbr>                      |   2 +<br>
>  src/compiler/nir/nir_lower_<wbr>constant_initializers.c | 102<br>
> +++++++++++++++++++++<br>
>  3 files changed, 105 insertions(+)<br>
>  create mode 100644<br>
> src/compiler/nir/nir_lower_<wbr>constant_initializers.c<br>
><br>
> diff --git a/src/compiler/Makefile.<wbr>sources<br>
> b/src/compiler/Makefile.<wbr>sources<br>
> index 0ff9b23..127b62e 100644<br>
> --- a/src/compiler/Makefile.<wbr>sources<br>
> +++ b/src/compiler/Makefile.<wbr>sources<br>
> @@ -190,6 +190,7 @@ NIR_FILES = \<br>
>       nir/nir_lower_bitmap.c \<br>
>       nir/nir_lower_clamp_color_<wbr>outputs.c \<br>
>       nir/nir_lower_clip.c \<br>
> +     nir/nir_lower_constant_<wbr>initializers.c \<br>
>       nir/nir_lower_double_ops.c \<br>
>       nir/nir_lower_double_packing.c \<br>
>       nir/nir_lower_drawpixels.c \<br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index 54598a2..99c2fc0 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2330,6 +2330,8 @@ void nir_lower_io_types(nir_shader *shader);<br>
>  void nir_lower_vars_to_ssa(nir_<wbr>shader *shader);<br>
>  <br>
>  bool nir_remove_dead_variables(nir_<wbr>shader *shader, nir_variable_mode<br>
> modes);<br>
> +bool nir_lower_constant_<wbr>initializers(nir_shader *shader,<br>
> +                             <wbr>        nir_variable_mode modes);<br>
>  <br>
>  void nir_move_vec_src_uses_to_dest(<wbr>nir_shader *shader);<br>
>  bool nir_lower_vec_to_movs(nir_<wbr>shader *shader);<br>
> diff --git a/src/compiler/nir/nir_lower_<wbr>constant_initializers.c<br>
> b/src/compiler/nir/nir_lower_<wbr>constant_initializers.c<br>
> new file mode 100644<br>
> index 0000000..d0935e9<br>
> --- /dev/null<br>
> +++ b/src/compiler/nir/nir_lower_<wbr>constant_initializers.c<br>
> @@ -0,0 +1,102 @@<br>
> +/*<br>
> + * Copyright © 2016 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person<br>
> obtaining a<br>
> + * copy of this software and associated documentation files (the<br>
> "Software"),<br>
> + * to deal in the Software without restriction, including without<br>
> limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute,<br>
> sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom<br>
> the<br>
> + * Software is furnished to do so, subject to the following<br>
> conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including<br>
> the next<br>
> + * paragraph) shall be included in all copies or substantial<br>
> portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,<br>
> EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF<br>
> MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO<br>
> EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES<br>
> OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
> ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR<br>
> OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +#include "nir_builder.h"<br>
> +<br>
> +static bool<br>
> +deref_apply_constant_<wbr>initializer(nir_deref_var *deref, void *state)<br>
> +{<br>
> +   struct nir_builder *b = state;<br>
> +<br>
> +   nir_load_const_instr *initializer =<br>
> +      nir_deref_get_const_<wbr>initializer_load(b->shader, deref);<br>
> +   nir_builder_instr_insert(<wbr>b, &initializer->instr);<br>
> +<br>
> +   nir_store_deref_var(b, deref, &initializer->def, 0xf);<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_const_initializer(<wbr>struct nir_builder *b, struct exec_list<br>
> *var_list)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   b->cursor = nir_before_cf_list(&b->impl-><wbr>body);<br>
> +<br>
> +   nir_foreach_variable(var, var_list) {<br>
> +      if (!var->constant_initializer)<br>
> +         continue;<br>
> +<br>
> +      progress = true;<br>
> +<br>
> +      nir_deref_var deref;<br>
> +      deref.deref.deref_type = nir_deref_type_var,<br>
> +      deref.deref.child = NULL;<br>
> +      deref.deref.type = var->type,<br>
> +      deref.var = var;<br>
> +<br>
> +      nir_deref_foreach_leaf(<wbr>&deref,<br>
> deref_apply_constant_<wbr>initializer, b);<br>
> +<br>
> +      var->constant_<wbr>initializer = NULL;<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_lower_constant_<wbr>initializers(nir_shader *shader,<br>
> nir_variable_mode modes)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_builder builder;<br>
> +   if (modes & ~nir_var_local)<br>
> +      nir_builder_init(&<wbr>builder, nir_shader_get_entrypoint(<wbr>shader)-<br>
> >impl);<br>
> +<br>
> +   if (modes & nir_var_shader_out)<br>
> +      progress |= lower_const_initializer(&<wbr>builder, &shader-<br>
> >outputs);<br>
> +<br>
> +   if (modes & nir_var_global)<br>
> +      progress |= lower_const_initializer(&<wbr>builder, &shader-<br>
> >globals);<br>
> +<br>
> +   if (modes & nir_var_system_value)<br>
> +      progress |= lower_const_initializer(&<wbr>builder, &shader-<br>
> >system_values);<br>
> +<br>
> +   if (modes & nir_var_local) {<br>
> +      nir_foreach_function(<wbr>function, shader) {<br>
> +         if (!function->impl)<br>
> +            continue;<br>
> +<br>
> +         nir_builder_init(&<wbr>builder, function->impl);<br>
> +         if (lower_const_initializer(&<wbr>builder, &function->impl-<br>
> >locals)) {<br>
> +            nir_metadata_<wbr>preserve(function->impl,<br>
> nir_metadata_block_index |<br>
> +                             <wbr>                     nir_<wbr>metadata_domin<br>
> ance |<br>
> +                             <wbr>                     nir_<wbr>metadata_live_<br>
> ssa_defs);<br>
<br>
</div></div>why don't we need to call nir_metadata_preserve if we make progress<br>
with non-local variables? Assuming there is a reason for that this<br>
looks good to me.<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div>No, we should call it if we have any progress at all.  I've added a "if (progress) loop over the functions"<br><br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="HOEnZb"><font color="#888888">
Iago<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
> +            progress = true;<br>
> +         }<br>
> +      }<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
</div></div></blockquote></div><br></div></div>