<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 2, 2015 at 9:08 PM, Kenneth Graunke <span dir="ltr"><<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thursday, January 29, 2015 01:40:18 PM Jason Ekstrand wrote:<br>
[snip]<br>
<div><div class="h5">> diff --git a/src/glsl/nir/nir_lower_phis_to_scalar.c b/src/glsl/nir/nir_lower_phis_to_scalar.c<br>
> new file mode 100644<br>
> index 0000000..bf65f5a<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_lower_phis_to_scalar.c<br>
> @@ -0,0 +1,278 @@<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>
> + * Authors:<br>
> + *    Jason Ekstrand (<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>)<br>
> + *<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +<br>
> +/*<br>
<br>
</div></div>I believe proper doxygen would be:<br>
<br>
/**<br>
 * \file nir_lower_phis_to_scalar.c<br>
 *<br>
 * Implements a pass that...<br>
 */<br>
<br>
(double star opening, \file line)<br>
<span class=""><br>
> + * Implements a pass that lowers vector phi nodes to scalar phi nodes when<br>
> + * we don't think it will hurt anything.<br>
> + */<br>
> +<br>
> +struct lower_phis_to_scalar_state {<br>
> +   void *mem_ctx;<br>
> +   void *dead_ctx;<br>
> +<br>
> +   /* Hash table marking which phi nodes are scalarizable.  The key is<br>
> +    * pointers to phi instructions and the entry is either NULL for not<br>
> +    * scalarizable or non-null for scalarizable.<br>
> +    */<br>
> +   struct hash_table *phi_table;<br>
> +};<br>
> +<br>
> +static bool<br>
> +should_lower_phi(nir_phi_instr *phi, struct lower_phis_to_scalar_state *state);<br>
> +<br>
> +static bool<br>
> +is_phi_src_scalarizable(nir_phi_src *src,<br>
> +                        struct lower_phis_to_scalar_state *state)<br>
> +{<br>
> +   /* Don't know what to do with non-ssa sources */<br>
> +   if (!src->src.is_ssa)<br>
<br>
</span>Can phi nodes with non-SSA sources even exist?  That sounds crazy.<br>
Perhaps just assert(src->src.is_ssa)?<br><div><div class="h5"></div></div></blockquote><div><br></div><div>It's possible.  We should probably disallow it as we can't coalesce them anyway, but it can happen at the moment.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
> +      return false;<br>
> +<br>
> +   nir_instr *src_instr = src->src.ssa->parent_instr;<br>
> +   switch (src_instr->type) {<br>
> +   case nir_instr_type_alu: {<br>
> +      nir_alu_instr *src_alu = nir_instr_as_alu(src_instr);<br>
> +<br>
> +      /* ALU operations with output_size == 0 should be scalarized.  We<br>
> +       * will also see a bunch of vecN operations from scalarizing ALU<br>
> +       * operations and, since they can easily be copy-propagated, they<br>
> +       * are ok too.<br>
> +       */<br>
> +      return nir_op_infos[src_alu->op].output_size == 0 ||<br>
> +             src_alu->op != nir_op_vec2 ||<br>
> +             src_alu->op != nir_op_vec3 ||<br>
> +             src_alu->op != nir_op_vec4;<br>
> +   }<br>
> +<br>
> +   case nir_instr_type_phi:<br>
> +      /* A phi is scalarizable if we're going to lower it */<br>
> +      return should_lower_phi(nir_instr_as_phi(src_instr), state);<br>
> +<br>
> +   case nir_instr_type_load_const:<br>
> +      /* These are trivially scalarizable */<br>
> +      return true;<br>
> +<br>
> +   case nir_instr_type_intrinsic: {<br>
> +      nir_intrinsic_instr *src_intrin = nir_instr_as_intrinsic(src_instr);<br>
> +<br>
> +      switch (src_intrin->intrinsic) {<br>
> +      case nir_intrinsic_load_var:<br>
> +         return src_intrin->variables[0]->var->data.mode == nir_var_shader_in ||<br>
> +                src_intrin->variables[0]->var->data.mode == nir_var_uniform;<br>
<br>
</div></div>Perhaps add:<br>
<br>
      case nir_intrinsic_load_uniform:<br>
      case nir_intrinsic_load_const:<br>
         return true;<br>
<br>
so that the pass could handle this same concept after nir_lower_io()?<br></blockquote><div><br></div><div>I have a patch floating arround that adds load_const and it helps by a few instructions.  We should probably do the same for inputs and uniforms but I thought I had some reason not to.  Can't remember what it is now.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
It looks like patch 2 adds this to brw_fs_nir.cpp's nir_optimize() loop, which<br>
is run after nir_lower_io().  It's possible that there will be no additional<br>
work to do then, so it might be useless, but...might be worth adding anyway...<span class=""><br></span></blockquote><div><br></div><div>Yeah, it probably does nothing there.  But *someone* decided to have a common optimization loop to use both places.  I think his name started with a K...<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> +      default:<br>
> +         break;<br>
> +      }<br>
> +   }<br>
> +<br>
> +   default:<br>
> +      /* We can't scalarize this type of instruction */<br>
> +      return false;<br>
> +   }<br>
> +}<br>
> +<br>
<br>
</span>/**<br>
 * Determines ...<br><div><div class="h5"></div></div></blockquote><div><br></div><div>yup<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
> +/* Determines if the given phi node should be lowered.  The only phi nodes<br>
> + * we will scalarize at the moment are those where all of the sources are<br>
> + * scalarizable.<br>
> + *<br>
> + * The reason for this comes down to coalescing.  Since phi sources can't<br>
> + * swizzle, swizzles on phis have to be resolved by inserting a mov right<br>
> + * before the phi.  The choice then becomes between movs to pick off<br>
> + * components for a scalar phi or potentially movs to recombine components<br>
> + * for a vector phi.  The problem is that the movs generated to pick off<br>
> + * the components are almost uncoalescable.  We can't coalesce them in NIR<br>
> + * because we need them to pick off components and we can't coalesce them<br>
> + * in the backend because the source register is a vector and the<br>
> + * destination is a scalar that may be used at other places in the program.<br>
> + * On the other hand, if we have a bunch of scalars going into a vector<br>
> + * phi, the situation is much better.  In this case, if the SSA def is<br>
> + * generated in the predecessor block to the corresponding phi source, the<br>
> + * backend code will be an ALU op into a temporary and then a mov into the<br>
> + * given vector component;  this move can almost certainly be coalesced<br>
> + * away.<br>
> + */<br>
> +static bool<br>
> +should_lower_phi(nir_phi_instr *phi, struct lower_phis_to_scalar_state *state)<br>
> +{<br>
> +   /* Already scalar */<br>
> +   if (phi->dest.ssa.num_components == 1)<br>
> +      return false;<br>
> +<br>
> +   struct hash_entry *entry = _mesa_hash_table_search(state->phi_table, phi);<br>
> +   if (entry)<br>
> +      return entry->data != NULL;<br>
> +<br>
> +   /* Insert an entry and mark it as scalarizable for now. That way<br>
> +    * we don't recurse forever and a cycle in the depencence graph<br>
<br>
</div></div>dependence graph<br></blockquote><div><br></div><div>thanks<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> +    * won't automatically make us fail to scalarize.<br>
> +    */<br>
> +   entry = _mesa_hash_table_insert(state->phi_table, phi, (void *)(intptr_t)1);<br>
<br>
</span>This is weird.  Why are you using a hash table and not a set?  It sounds<br>
like you're trying to store a tri-state: unknown (no entry in hash<br>
table), known-not-scalarizable (entry in hash table, value of 0), and<br>
known-scalarizable (entry in hash table, value of 1).<br>
<br>
However, I don't see any code that actually inserts or updates a value<br>
to 0 - are you missing some code?<br>
<br>
Otherwise, this looks good to me...<br>
<div class="HOEnZb"><div class="h5"><br>
> +<br>
> +   bool scalarizable = true;<br>
> +<br>
> +   nir_foreach_phi_src(phi, src) {<br>
> +      scalarizable = is_phi_src_scalarizable(src, state);<br>
> +      if (!scalarizable)<br>
> +         break;<br>
> +   }<br>
> +<br>
> +   entry->data = (void *)(intptr_t)scalarizable;<br></div></div></blockquote><div><br></div><div>It's updated right here ^^<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">
> +<br>
> +   return scalarizable;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_phis_to_scalar_block(nir_block *block, void *void_state)<br>
> +{<br>
> +   struct lower_phis_to_scalar_state *state = void_state;<br>
> +<br>
> +   /* Find the last phi node in the block */<br>
> +   nir_phi_instr *last_phi = NULL;<br>
> +   nir_foreach_instr(block, instr) {<br>
> +      if (instr->type != nir_instr_type_phi)<br>
> +         break;<br>
> +<br>
> +      last_phi = nir_instr_as_phi(instr);<br>
> +   }<br>
> +<br>
> +   /* We have to handle the phi nodes in their own pass due to the way<br>
> +    * we're modifying the linked list of instructions.<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>
> +      if (!should_lower_phi(phi, state))<br>
> +         continue;<br>
> +<br>
> +      /* Create a vecN operation to combine the results.  Most of these<br>
> +       * will be redundant, but copy propagation should clean them up for<br>
> +       * us.  No need to add the complexity here.<br>
> +       */<br>
> +      nir_op vec_op;<br>
> +      switch (phi->dest.ssa.num_components) {<br>
> +      case 2: vec_op = nir_op_vec2; break;<br>
> +      case 3: vec_op = nir_op_vec3; break;<br>
> +      case 4: vec_op = nir_op_vec4; break;<br>
> +      default: unreachable("Invalid number of components");<br>
> +      }<br>
> +<br>
> +      nir_alu_instr *vec = nir_alu_instr_create(state->mem_ctx, vec_op);<br>
> +      nir_ssa_dest_init(&vec->instr, &vec->dest.dest,<br>
> +                        phi->dest.ssa.num_components, NULL);<br>
> +      vec->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;<br>
> +<br>
> +      for (unsigned i = 0; i < phi->dest.ssa.num_components; i++) {<br>
> +         nir_phi_instr *new_phi = nir_phi_instr_create(state->mem_ctx);<br>
> +         nir_ssa_dest_init(&new_phi->instr, &new_phi->dest, 1, NULL);<br>
> +<br>
> +         vec->src[i].src = nir_src_for_ssa(&new_phi->dest.ssa);<br>
> +<br>
> +         nir_foreach_phi_src(phi, src) {<br>
> +            /* We need to insert a mov to grab the i'th component of src */<br>
> +            nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,<br>
> +                                                      nir_op_imov);<br>
> +            nir_ssa_dest_init(&mov->instr, &mov->dest.dest, 1, NULL);<br>
> +            mov->dest.write_mask = 1;<br>
> +            nir_src_copy(&mov->src[0].src, &src->src, state->mem_ctx);<br>
> +            mov->src[0].swizzle[0] = i;<br>
> +<br>
> +            /* Insert at the end of the predecessor but before the jump */<br>
> +            nir_instr *pred_last_instr = nir_block_last_instr(src->pred);<br>
> +            if (pred_last_instr && pred_last_instr->type == nir_instr_type_jump)<br>
> +               nir_instr_insert_before(pred_last_instr, &mov->instr);<br>
> +            else<br>
> +               nir_instr_insert_after_block(src->pred, &mov->instr);<br>
> +<br>
> +            nir_phi_src *new_src = ralloc(state->mem_ctx, nir_phi_src);<br>
> +            new_src->pred = src->pred;<br>
> +            new_src->src = nir_src_for_ssa(&mov->dest.dest.ssa);<br>
> +<br>
> +            exec_list_push_tail(&new_phi->srcs, &new_src->node);<br>
> +         }<br>
> +<br>
> +         nir_instr_insert_before(&phi->instr, &new_phi->instr);<br>
> +      }<br>
> +<br>
> +      nir_instr_insert_after(&last_phi->instr, &vec->instr);<br>
> +<br>
> +      nir_ssa_def_rewrite_uses(&phi->dest.ssa,<br>
> +                               nir_src_for_ssa(&vec->dest.dest.ssa),<br>
> +                               state->mem_ctx);<br>
> +<br>
> +      ralloc_steal(state->dead_ctx, phi);<br>
> +      nir_instr_remove(&phi->instr);<br>
> +<br>
> +      /* We're using the safe iterator and inserting all the newly<br>
> +       * scalarized phi nodes before their non-scalarized version so that's<br>
> +       * ok.  However, we are also inserting vec operations after all of<br>
> +       * the last phi node so once we get here, we can't trust even the<br>
> +       * safe iterator to stop properly.  We have to break manually.<br>
> +       */<br>
> +      if (instr == &last_phi->instr)<br>
> +         break;<br>
> +   }<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static void<br>
> +lower_phis_to_scalar_impl(nir_function_impl *impl)<br>
> +{<br>
> +   struct lower_phis_to_scalar_state state;<br>
> +<br>
> +   state.mem_ctx = ralloc_parent(impl);<br>
> +   state.dead_ctx = ralloc_context(NULL);<br>
> +   state.phi_table = _mesa_hash_table_create(state.dead_ctx, _mesa_hash_pointer,<br>
> +                                             _mesa_key_pointer_equal);<br>
> +<br>
> +   nir_foreach_block(impl, lower_phis_to_scalar_block, &state);<br>
> +<br>
> +   nir_metadata_preserve(impl, nir_metadata_block_index |<br>
> +                               nir_metadata_dominance);<br>
> +<br>
> +   ralloc_free(state.dead_ctx);<br>
> +}<br>
> +<br>
> +/** A pass that lowers vector phi nodes to scalar<br>
> + *<br>
> + * This pass loops through the blocks and lowers looks for vector phi nodes<br>
> + * it can lower to scalar phi nodes.  Not all phi nodes are lowered.  For<br>
> + * instance, if one of the sources is a non-scalarizable vector, then we<br>
> + * don't bother lowering because that would generate hard-to-coalesce movs.<br>
> + */<br>
> +void<br>
> +nir_lower_phis_to_scalar(nir_shader *shader)<br>
> +{<br>
> +   nir_foreach_overload(shader, overload) {<br>
> +      if (overload->impl)<br>
> +         lower_phis_to_scalar_impl(overload->impl);<br>
> +   }<br>
> +}<br>
><br>
</div></div></blockquote></div><br></div></div>