<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 9, 2015 at 2:31 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, Feb 9, 2015 at 5:04 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
><br>
><br>
> On Mon, Feb 9, 2015 at 3:01 PM, Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
>><br>
>> So last time you posted this, I had some suggestions but I realized<br>
>> that in light of the fact that we want to do value numbering, it<br>
>> didn't seem like my suggestions were any good. But now that I've<br>
>> thought about it a little bit, it seems to me like a better plan might<br>
>> be to pull instructions out of their basic blocks and into the global<br>
>> list of instructions earlier. That is, instead of marking instructions<br>
>> as pinned in gcm_pin_instructions_block(), put the ones that aren't<br>
>> pinned in state->instrs and then instead of walking over all the basic<br>
>> blocks and bailing on pinned instructions when scheduling early and<br>
>> late, just walk over state->instrs. This should be faster, and when we<br>
>> do value-numbering there's a natural place to do it, i.e. after<br>
>> pulling instructions but before scheduling -- this means we only<br>
>> value-number the non-pinned instructions, which is what we want since<br>
>> we're not coalescing the pinned instructions in this pass. This is<br>
>> also more similar to how it's presented in the paper (although our<br>
>> scheduling algorithm will still be different I guess).<br>
>><br>
>> On Mon, Feb 9, 2015 at 3:19 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br>
>> wrote:<br>
>> > v2 Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>>:<br>
>> >  - Use nir_dominance_lca for computing least common anscestors<br>
>> >  - Use the block index for comparing dominance tree depths<br>
>> >  - Pin things that do partial derivatives<br>
>> > ---<br>
>> >  src/glsl/Makefile.sources  |   1 +<br>
>> >  src/glsl/nir/nir.h         |   2 +<br>
>> >  src/glsl/nir/nir_opt_gcm.c | 501<br>
>> > +++++++++++++++++++++++++++++++++++++++++++++<br>
>> >  3 files changed, 504 insertions(+)<br>
>> >  create mode 100644 src/glsl/nir/nir_opt_gcm.c<br>
>> ><br>
>> > diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
>> > index a580b6e..69cb2e6 100644<br>
>> > --- a/src/glsl/Makefile.sources<br>
>> > +++ b/src/glsl/Makefile.sources<br>
>> > @@ -43,6 +43,7 @@ NIR_FILES = \<br>
>> >         nir/nir_opt_copy_propagate.c \<br>
>> >         nir/nir_opt_cse.c \<br>
>> >         nir/nir_opt_dce.c \<br>
>> > +       nir/nir_opt_gcm.c \<br>
>> >         nir/nir_opt_global_to_local.c \<br>
>> >         nir/nir_opt_peephole_select.c \<br>
>> >         nir/nir_opt_remove_phis.c \<br>
>> > diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
>> > index 90a7001..55fb43d 100644<br>
>> > --- a/src/glsl/nir/nir.h<br>
>> > +++ b/src/glsl/nir/nir.h<br>
>> > @@ -1589,6 +1589,8 @@ bool nir_opt_cse(nir_shader *shader);<br>
>> >  bool nir_opt_dce_impl(nir_function_impl *impl);<br>
>> >  bool nir_opt_dce(nir_shader *shader);<br>
>> ><br>
>> > +void nir_opt_gcm(nir_shader *shader);<br>
>> > +<br>
>> >  bool nir_opt_peephole_select(nir_shader *shader);<br>
>> >  bool nir_opt_peephole_ffma(nir_shader *shader);<br>
>> ><br>
>> > diff --git a/src/glsl/nir/nir_opt_gcm.c b/src/glsl/nir/nir_opt_gcm.c<br>
>> > new file mode 100644<br>
>> > index 0000000..d48518b<br>
>> > --- /dev/null<br>
>> > +++ b/src/glsl/nir/nir_opt_gcm.c<br>
>> > @@ -0,0 +1,501 @@<br>
>> > +/*<br>
>> > + * Copyright © 2014 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 conditions:<br>
>> > + *<br>
>> > + * The above copyright notice and this permission notice (including the<br>
>> > next<br>
>> > + * paragraph) shall be included in all copies or substantial portions<br>
>> > 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 EVENT<br>
>> > SHALL<br>
>> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
>> > 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 OTHER<br>
>> > 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>
>> > + * Implements Global Code Motion.  A description of GCM can be found in<br>
>> > + * "Global Code Motion; Global Value Numbering" by Cliff Click.<br>
>> > + * Unfortunately, the algorithm presented in the paper is broken in a<br>
>> > + * number of ways.  The algorithm used here differs substantially from<br>
>> > the<br>
>> > + * one in the paper but it is, in my opinion, much easier to read and<br>
>> > + * verify correcness.<br>
>> > + */<br>
>> > +<br>
>> > +struct gcm_block_info {<br>
>> > +   /* Number of loops this block is inside */<br>
>> > +   unsigned loop_depth;<br>
>> > +<br>
>> > +   /* The last instruction inserted into this block.  This is used as<br>
>> > we<br>
>> > +    * traverse the instructions and insert them back into the program<br>
>> > to<br>
>> > +    * put them in the right order.<br>
>> > +    */<br>
>> > +   nir_instr *last_instr;<br>
>> > +};<br>
>> > +<br>
>> > +struct gcm_state {<br>
>> > +   nir_function_impl *impl;<br>
>> > +   nir_instr *instr;<br>
>> > +<br>
>> > +   /* Marks all instructions that have been visited by the curren pass<br>
>> > */<br>
>> > +   BITSET_WORD *visited;<br>
>> > +<br>
>> > +   /* Marks instructions that are "pinned", i.e. cannot be moved from<br>
>> > their<br>
>> > +    * basic block by code motion */<br>
>> > +   BITSET_WORD *pinned;<br>
>><br>
>> Note that there's already a boolean field embedded in each instruction<br>
>> called "live" since it's used by DCE. Maybe we can replace it with a<br>
>> uint8_t or so with a more generic name like "data" or "bitfield", and<br>
>> then we can use it for this pass as well to replace both visited and<br>
>> pinned. We'd only need 4 bits (pinned, scheduled early, scheduled<br>
>> late, and placed). This would also mean we wouldn't need the<br>
>> per-instruction index any more.<br>
><br>
><br>
> I was going to say that I don't want to walk the instructions to set a<br>
> bitfield to zero but then I realized we have to walk the whole pile just to<br>
> set the index anyway.  And we could do it at instruction pinning time which<br>
> would be nice.  Sure, I'll rework it *again* with that.  You really don't<br>
> like those bitsets do you? :-P<br>
<br>
</div></div>Hehe :P Btw, you didn't respond to my other orthogonal reworking idea<br>
at the beginning -- you don't have to follow it, but I'd like to know<br>
why. Were you confused by what I said?<br>
<div><div class="h5"><br>
><br>
>><br>
>> > +<br>
>> > +   /* The list of non-pinned instructions.  As we do the late<br>
>> > scheduling,<br>
>> > +    * we pull non-pinned instructions out of their blocks and place<br>
>> > them in<br>
>> > +    * this list.  This saves us from having linked-list problems when<br>
>> > we go<br>
>> > +    * to put instructions back in their blocks.<br>
>> > +    */<br>
>> > +   struct exec_list instrs;<br>
>> > +<br>
>> > +   struct gcm_block_info *blocks;<br>
>> > +};<br>
>> > +<br>
>> > +/* Recursively walks the CFG and builds the block_info structure */<br>
>> > +static void<br>
>> > +gcm_build_block_info(struct exec_list *cf_list, struct gcm_state<br>
>> > *state,<br>
>> > +                     unsigned loop_depth)<br>
>> > +{<br>
>> > +   foreach_list_typed(nir_cf_node, node, node, cf_list) {<br>
>> > +      switch (node->type) {<br>
>> > +      case nir_cf_node_block: {<br>
>> > +         nir_block *block = nir_cf_node_as_block(node);<br>
>> > +         state->blocks[block->index].loop_depth = loop_depth;<br>
>> > +         break;<br>
>> > +      }<br>
>> > +      case nir_cf_node_if: {<br>
>> > +         nir_if *if_stmt = nir_cf_node_as_if(node);<br>
>> > +         gcm_build_block_info(&if_stmt->then_list, state, loop_depth);<br>
>> > +         gcm_build_block_info(&if_stmt->else_list, state, loop_depth);<br>
>> > +         break;<br>
>> > +      }<br>
>> > +      case nir_cf_node_loop: {<br>
>> > +         nir_loop *loop = nir_cf_node_as_loop(node);<br>
>> > +         gcm_build_block_info(&loop->body, state, loop_depth + 1);<br>
>> > +         break;<br>
>> > +      }<br>
>> > +      default:<br>
>> > +         unreachable("Invalid CF node type");<br>
>> > +      }<br>
>> > +   }<br>
>> > +}<br>
>> > +<br>
>> > +/* Walks the instruction list and marks immovable instructions as<br>
>> > pinned */<br>
>> > +static bool<br>
>> > +gcm_pin_instructions_block(nir_block *block, void *void_state)<br>
>> > +{<br>
>> > +   struct gcm_state *state = void_state;<br>
>> > +<br>
>> > +   nir_foreach_instr_safe(block, instr) {<br>
>> > +      bool pinned;<br>
>> > +      switch (instr->type) {<br>
>> > +      case nir_instr_type_alu:<br>
>> > +         switch (nir_instr_as_alu(instr)->op) {<br>
>> > +         case nir_op_fddx:<br>
>> > +         case nir_op_fddy:<br>
>> > +         case nir_op_fddx_fine:<br>
>> > +         case nir_op_fddy_fine:<br>
>> > +         case nir_op_fddx_coarse:<br>
>> > +         case nir_op_fddy_coarse:<br>
>> > +            /* These can only go in uniform control flow; pin them for<br>
>> > now */<br>
>> > +            pinned = true;<br>
>> > +<br>
>> > +         default:<br>
>> > +            pinned = false;<br>
>> > +         }<br>
>> > +         break;<br>
>> > +<br>
>> > +      case nir_instr_type_tex:<br>
>> > +         /* We need to pin texture ops that do partial derivatives */<br>
>> > +         pinned = nir_instr_as_tex(instr)->op == nir_texop_txd;<br>
>><br>
>> I don't think this is correct -- txd is when we supply the derivatives<br>
>> ourselves, so there are no implicit partial derivatives. I *think* the<br>
>> ones where the HW takes partial derivatives are tex, txb, lod, and tg4<br>
>> (gather). I'd double-check that with someone else, though, since it's<br>
>> been a while since I had to do texture stuff.<br>
><br>
><br>
> Yeah, I'll fix that.  According to Ken, we only need to pin tex and txb.<br>
> lod takes an explicit LOD and tg4 doesn't need it.<br>
> --Jason<br>
<br>
</div></div>Wait, doesn't nir_tex_lod (really ir_lod) return the LOD as a result?<br></blockquote><div><br></div><div>Right.  I've added that to the switch statement.  Thanks for catching.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
ir.h says it's "texture lod query" and the form given for it is "(lod<br>
<type> <sampler> <coordinate>)" which would make me think it's<br>
implicitly taking a derivative and returning the LOD that would've<br>
been used. The GLSL 4.4  spec does say on page 161 (167 of the PDF):<br>
<br>
"Some texture functions (non-"Lod" and non-"Grad" versions) may<br>
require implicit derivatives."<br>
<br>
I think they missed an exception for textureGather and texelFetch<br>
there, since the spec seems to say on page 171 (177 of the PDF) that<br>
the base level is always used for textureGather and texelFetch<br>
obviously doesn't take any derivatives. So maybe another way would be<br>
to pin it if there aren't any nir_tex_src_lod or<br>
nir_tex_src_ddx/nir_tex_src_ddy sources and it's not a nir_texop_tg4<br>
or nir_texop_txf, although I guess that would be equivalent to just<br>
checking the op. Textures are stupid.<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
>><br>
>> > +         break;<br>
>> > +<br>
>> > +      case nir_instr_type_load_const:<br>
>> > +         pinned = false;<br>
>> > +         break;<br>
>> > +<br>
>> > +      case nir_instr_type_intrinsic: {<br>
>> > +         const nir_intrinsic_info *info =<br>
>> > +<br>
>> > &nir_intrinsic_infos[nir_instr_as_intrinsic(instr)->intrinsic];<br>
>> > +         pinned = !(info->flags & NIR_INTRINSIC_CAN_ELIMINATE) ||<br>
>> > +                  !(info->flags & NIR_INTRINSIC_CAN_REORDER);<br>
>> > +         break;<br>
>> > +      }<br>
>> > +<br>
>> > +      case nir_instr_type_jump:<br>
>> > +      case nir_instr_type_ssa_undef:<br>
>> > +      case nir_instr_type_phi:<br>
>> > +         pinned = true;<br>
>> > +         break;<br>
>> > +<br>
>> > +      default:<br>
>> > +         unreachable("Invalid instruction type in GCM");<br>
>> > +      }<br>
>> > +<br>
>> > +      if (pinned)<br>
>> > +         BITSET_SET(state->pinned, instr->index);<br>
>> > +   }<br>
>> > +<br>
>> > +   return true;<br>
>> > +}<br>
>> > +<br>
>> > +static void<br>
>> > +gcm_schedule_early_instr(nir_instr *instr, struct gcm_state *state);<br>
>> > +<br>
>> > +/** Update an instructions schedule for the given source<br>
>> > + *<br>
>> > + * This function is called iteratively as we walk the sources of an<br>
>> > + * instruction.  It ensures that the given source instruction has been<br>
>> > + * scheduled and then update this instruction's block if the source<br>
>> > + * instruction is lower down the tree.<br>
>> > + */<br>
>> > +static bool<br>
>> > +gcm_schedule_early_src(nir_src *src, void *void_state)<br>
>> > +{<br>
>> > +   struct gcm_state *state = void_state;<br>
>> > +   nir_instr *instr = state->instr;<br>
>> > +<br>
>> > +   assert(src->is_ssa);<br>
>> > +<br>
>> > +   gcm_schedule_early_instr(src->ssa->parent_instr, void_state);<br>
>> > +<br>
>> > +   /* While the index isn't a proper dominance depth, it does have the<br>
>> > +    * property that if A dominates B then A->index <= B->index.  Since<br>
>> > we<br>
>> > +    * know that this instruction must have been dominated by all of its<br>
>> > +    * sources at some point (even if it's gone through<br>
>> > value-numbering),<br>
>> > +    * all of the sources must lie on the same branch of the dominance<br>
>> > tree.<br>
>> > +    * Therefore, we can just go ahead and just compare indices.<br>
>> > +    */<br>
>> > +   if (instr->block->index < src->ssa->parent_instr->block->index)<br>
>> > +      instr->block = src->ssa->parent_instr->block;<br>
>> > +<br>
>> > +   /* We need to restore the state instruction because it may have been<br>
>> > +    * changed through the gcm_schedule_early_instr call above.  Since<br>
>> > we<br>
>> > +    * may still be iterating through sources and future calls to<br>
>> > +    * gcm_schedule_early_src for the same instruction will still need<br>
>> > it.<br>
>> > +    */<br>
>> > +   state->instr = instr;<br>
>> > +<br>
>> > +   return true;<br>
>> > +}<br>
>> > +<br>
>> > +/** Schedules an instruction early<br>
>> > + *<br>
>> > + * This function performs a recursive depth-first search starting at<br>
>> > the<br>
>> > + * given instruction and proceeding through the sources to schedule<br>
>> > + * instructions as early as they can possibly go in the dominance tree.<br>
>> > + * The instructions are "scheduled" by updating their instr->block<br>
>> > field.<br>
>> > + */<br>
>> > +static void<br>
>> > +gcm_schedule_early_instr(nir_instr *instr, struct gcm_state *state)<br>
>> > +{<br>
>> > +   if (BITSET_TEST(state->visited, instr->index))<br>
>> > +      return;<br>
>> > +<br>
>> > +   BITSET_SET(state->visited, instr->index);<br>
>> > +<br>
>> > +   /* Pinned instructions are already scheduled so we don't need to do<br>
>> > +    * anything.  Also, bailing here keeps us from ever following the<br>
>> > +    * sources of phi nodes which can be back-edges.<br>
>> > +    */<br>
>> > +   if (BITSET_TEST(state->pinned, instr->index))<br>
>> > +      return;<br>
>> > +<br>
>> > +   /* Start with the instruction at the top.  As we iterate over the<br>
>> > +    * sources, it will get moved down as needed.<br>
>> > +    */<br>
>> > +   instr->block = state->impl->start_block;<br>
>> > +   state->instr = instr;<br>
>> > +<br>
>> > +   nir_foreach_src(instr, gcm_schedule_early_src, state);<br>
>> > +}<br>
>> > +<br>
>> > +static bool<br>
>> > +gcm_schedule_early_block(nir_block *block, void *state)<br>
>> > +{<br>
>> > +   nir_foreach_instr(block, instr)<br>
>> > +      gcm_schedule_early_instr(instr, state);<br>
>> > +<br>
>> > +   return true;<br>
>> > +}<br>
>> > +<br>
>> > +static void<br>
>> > +gcm_schedule_late_instr(nir_instr *instr, struct gcm_state *state);<br>
>> > +<br>
>> > +/** Schedules the instruction associated with the given SSA def late<br>
>> > + *<br>
>> > + * This function works by first walking all of the uses of the given<br>
>> > SSA<br>
>> > + * definition, ensuring that they are scheduled, and then computing the<br>
>> > LCA<br>
>> > + * (least common ancestor) of its uses.  It then schedules this<br>
>> > instruction<br>
>> > + * as close to the LCA as possible while trying to stay out of loops.<br>
>> > + */<br>
>> > +static bool<br>
>> > +gcm_schedule_late_def(nir_ssa_def *def, void *void_state)<br>
>> > +{<br>
>> > +   struct gcm_state *state = void_state;<br>
>> > +<br>
>> > +   nir_block *lca = NULL;<br>
>> > +<br>
>> > +   struct set_entry *entry;<br>
>> > +   set_foreach(def->uses, entry) {<br>
>> > +      nir_instr *use_instr = (nir_instr *)entry->key;<br>
>> > +<br>
>> > +      gcm_schedule_late_instr(use_instr, state);<br>
>> > +<br>
>> > +      /* Phi instructions are a bit special.  SSA definitions don't<br>
>> > have to<br>
>> > +       * dominate the sources of the phi nodes that use them; instead,<br>
>> > they<br>
>> > +       * have to dominate the predecessor block corresponding to the<br>
>> > phi<br>
>> > +       * source.  We handle this by looking through the sources,<br>
>> > finding<br>
>> > +       * any that are usingg this SSA def, and using those blocks<br>
>> > instead<br>
>> > +       * of the one the phi lives in.<br>
>> > +       */<br>
>> > +      if (use_instr->type == nir_instr_type_phi) {<br>
>> > +         nir_phi_instr *phi = nir_instr_as_phi(use_instr);<br>
>> > +<br>
>> > +         nir_foreach_phi_src(phi, phi_src) {<br>
>> > +            if (phi_src->src.ssa == def)<br>
>> > +               lca = nir_dominance_lca(lca, phi_src->pred);<br>
>> > +         }<br>
>> > +      } else {<br>
>> > +         lca = nir_dominance_lca(lca, use_instr->block);<br>
>> > +      }<br>
>> > +   }<br>
>> > +<br>
>> > +   set_foreach(def->if_uses, entry) {<br>
>> > +      nir_if *if_stmt = (nir_if *)entry->key;<br>
>> > +<br>
>> > +      /* For if statements, we consider the block to be the one<br>
>> > immediately<br>
>> > +       * preceding the if CF node.<br>
>> > +       */<br>
>> > +      nir_block *pred_block =<br>
>> > +         nir_cf_node_as_block(nir_cf_node_prev(&if_stmt->cf_node));<br>
>> > +<br>
>> > +      lca = nir_dominance_lca(lca, pred_block);<br>
>> > +   }<br>
>> > +<br>
>> > +   /* Some instructions may never be used.  We'll just leave them<br>
>> > scheduled<br>
>> > +    * early and let dead code clean them up.<br>
>> > +    */<br>
>> > +   if (lca == NULL)<br>
>> > +      return true;<br>
>> > +<br>
>> > +   /* We know have the LCA of all of the uses.  If our invariants hold,<br>
>> > +    * this is dominated by the block that we chose when scheduling<br>
>> > early.<br>
>> > +    * We now walk up the dominance tree and pick the lowest block that<br>
>> > is<br>
>> > +    * as far outside loops as we can get.<br>
>> > +    */<br>
>> > +   nir_block *best = lca;<br>
>> > +   while (lca != def->parent_instr->block) {<br>
>> > +      assert(lca);<br>
>> > +      if (state->blocks[lca->index].loop_depth <<br>
>> > +          state->blocks[best->index].loop_depth)<br>
>> > +         best = lca;<br>
>> > +      lca = lca->imm_dom;<br>
>> > +   }<br>
>> > +   def->parent_instr->block = best;<br>
>><br>
>> I think this loop will exclude def->parent_instr->block from<br>
>> consideration unless lca == def->parent_instr->block. On the last<br>
>> iteration, we'll check lca to see if it's the best, then move lca up<br>
>> the tree, then discover that lca is equal to def->parent_instr->block,<br>
>> then bail out without checking it. I think you can fix this by<br>
>> swapping the if-statement and the last statement in the loop, which<br>
>> will also have the benefit of avoiding the unnecessary test on the<br>
>> first iteration of the loop. Of course, you'd want to move the assert<br>
>> as well, so it would look like:<br>
>><br>
>>    nir_block *best = lca;<br>
>>    while (lca != def->parent_instr->block) {<br>
>>       lca = lca->imm_dom;<br>
>>       assert(lca);<br>
>>       if (state->blocks[lca->index].loop_depth <<br>
>>           state->blocks[best->index].loop_depth)<br>
>>          best = lca;<br>
>>    }<br>
><br>
><br>
> Right.  Good catch!  I'll get that fixed up.<br>
><br>
>> > +<br>
>> > +   return true;<br>
>> > +}<br>
>> > +<br>
>> > +/** Schedules an instruction late<br>
>> > + *<br>
>> > + * This function performs a depth-first search starting at the given<br>
>> > + * instruction and proceeding through its uses to schedule instructions<br>
>> > as<br>
>> > + * late as they can reasonably go in the dominance tree.  The<br>
>> > instructions<br>
>> > + * are "scheduled" by updating their instr->block field.<br>
>> > + *<br>
>> > + * The name of this function is actually a bit of a misnomer as it<br>
>> > doesn't<br>
>> > + * schedule them "as late as possible" as the paper implies.  Instead,<br>
>> > it<br>
>> > + * first finds the lates possible place it can schedule the instruction<br>
>> > and<br>
>> > + * then possibly schedules it earlier than that.  The actual location<br>
>> > is as<br>
>> > + * far down the tree as we can go while trying to stay out of loops.<br>
>> > + */<br>
>> > +static void<br>
>> > +gcm_schedule_late_instr(nir_instr *instr, struct gcm_state *state)<br>
>> > +{<br>
>> > +   if (BITSET_TEST(state->visited, instr->index))<br>
>> > +      return;<br>
>> > +<br>
>> > +   BITSET_SET(state->visited, instr->index);<br>
>> > +<br>
>> > +   /* Pinned instructions are already scheduled so we don't need to do<br>
>> > +    * anything.  Also, bailing here keeps us from ever following phi<br>
>> > nodes<br>
>> > +    * which can be back-edges.<br>
>> > +    */<br>
>> > +   if (BITSET_TEST(state->pinned, instr->index))<br>
>> > +      return;<br>
>> > +<br>
>> > +   nir_foreach_ssa_def(instr, gcm_schedule_late_def, state);<br>
>> > +}<br>
>> > +<br>
>> > +static bool<br>
>> > +gcm_schedule_late_block(nir_block *block, void *void_state)<br>
>> > +{<br>
>> > +   struct gcm_state *state = void_state;<br>
>> > +<br>
>> > +   nir_foreach_instr_safe(block, instr) {<br>
>> > +      gcm_schedule_late_instr(instr, state);<br>
>> > +<br>
>> > +      if (!BITSET_TEST(state->pinned, instr->index)) {<br>
>> > +         /* If this is an instruction we can move, go ahead and pull it<br>
>> > out<br>
>> > +          * of the program and put it on the instrs list.  This keeps<br>
>> > us<br>
>> > +          * from causing linked list confusion when we're trying to put<br>
>> > +          * everything in its proper place.<br>
>> > +          *<br>
>> > +          * Note that we don't use nir_instr_remove here because that<br>
>> > also<br>
>> > +          * cleans up uses and defs and we want to keep that<br>
>> > information.<br>
>> > +          */<br>
>> > +         exec_node_remove(&instr->node);<br>
>> > +         exec_list_push_tail(&state->instrs, &instr->node);<br>
>> > +      }<br>
>> > +   }<br>
>> > +<br>
>> > +   return true;<br>
>> > +}<br>
>> > +<br>
>> > +static void<br>
>> > +gcm_place_instr(nir_instr *instr, struct gcm_state *state);<br>
>> > +<br>
>> > +static bool<br>
>> > +gcm_place_instr_def(nir_ssa_def *def, void *state)<br>
>> > +{<br>
>> > +   struct set_entry *entry;<br>
>> > +   set_foreach(def->uses, entry)<br>
>> > +      gcm_place_instr((nir_instr *)entry->key, state);<br>
>> > +<br>
>> > +   return false;<br>
>> > +}<br>
>> > +<br>
>> > +/** Places an instrution back into the program<br>
>> > + *<br>
>> > + * The earlier passes of GCM simply choose blocks for each instruction<br>
>> > and<br>
>> > + * otherwise leave them alone.  This pass actually places the<br>
>> > instructions<br>
>> > + * into their chosen blocks.<br>
>> > + *<br>
>> > + * To do so, we use a standard post-order depth-first search<br>
>> > linearization<br>
>> > + * algorithm.  We walk over the uses of the given instruction and<br>
>> > ensure<br>
>> > + * that they are placed and then place this instruction.  Because we<br>
>> > are<br>
>> > + * working on multiple blocks at a time, we keep track of the last<br>
>> > inserted<br>
>> > + * instruction per-block in the state structure's block_info array.<br>
>> > When<br>
>> > + * we insert an instruction in a block we insert it before the last<br>
>> > + * instruction inserted in that block rather than the last instruction<br>
>> > + * inserted globally.<br>
>> > + */<br>
>> > +static void<br>
>> > +gcm_place_instr(nir_instr *instr, struct gcm_state *state)<br>
>> > +{<br>
>> > +   if (BITSET_TEST(state->visited, instr->index))<br>
>> > +      return;<br>
>> > +<br>
>> > +   BITSET_SET(state->visited, instr->index);<br>
>> > +<br>
>> > +   /* Phi nodes are our once source of back-edges.  Since right now we<br>
>> > are<br>
>> > +    * only doing scheduling within blocks, we don't need to worry about<br>
>> > +    * them since they are always at the top.  Just skip them<br>
>> > completely.<br>
>> > +    */<br>
>> > +   if (instr->type == nir_instr_type_phi) {<br>
>> > +      assert(BITSET_TEST(state->pinned, instr->index));<br>
>> > +      return;<br>
>> > +   }<br>
>> > +<br>
>> > +   nir_foreach_ssa_def(instr, gcm_place_instr_def, state);<br>
>> > +<br>
>> > +   if (BITSET_TEST(state->pinned, instr->index)) {<br>
>> > +      /* Pinned instructions have an implicit dependence on the pinned<br>
>> > +       * instructions that come after them in the block.  Since the<br>
>> > pinned<br>
>> > +       * instructions will naturally "chain" together, we only need to<br>
>> > +       * explicitly visit one of them.<br>
>> > +       */<br>
>> > +      for (nir_instr *after = nir_instr_next(instr);<br>
>> > +           after;<br>
>> > +           after = nir_instr_next(after)) {<br>
>> > +         if (BITSET_TEST(state->pinned, after->index)) {<br>
>> > +            gcm_place_instr(after, state);<br>
>> > +            break;<br>
>> > +         }<br>
>> > +      }<br>
>> > +   }<br>
>> > +<br>
>> > +   struct gcm_block_info *block_info =<br>
>> > &state->blocks[instr->block->index];<br>
>> > +   if (!BITSET_TEST(state->pinned, instr->index)) {<br>
>> > +      exec_node_remove(&instr->node);<br>
>> > +<br>
>> > +      if (block_info->last_instr) {<br>
>> > +         exec_node_insert_node_before(&block_info->last_instr->node,<br>
>> > +                                      &instr->node);<br>
>> > +      } else {<br>
>> > +         /* Schedule it at the end of the block */<br>
>> > +         nir_instr *jump_instr = nir_block_last_instr(instr->block);<br>
>> > +         if (jump_instr && jump_instr->type == nir_instr_type_jump) {<br>
>> > +            exec_node_insert_node_before(&jump_instr->node,<br>
>> > &instr->node);<br>
>> > +         } else {<br>
>> > +            exec_list_push_tail(&instr->block->instr_list,<br>
>> > &instr->node);<br>
>> > +         }<br>
>> > +      }<br>
>> > +   }<br>
>> > +<br>
>> > +   block_info->last_instr = instr;<br>
>> > +}<br>
>> > +<br>
>> > +static void<br>
>> > +opt_gcm_impl(nir_function_impl *impl)<br>
>> > +{<br>
>> > +   struct gcm_state state;<br>
>> > +<br>
>> > +   unsigned num_instrs = nir_index_instrs(impl);<br>
>> > +   unsigned instr_words = BITSET_WORDS(num_instrs);<br>
>> > +<br>
>> > +   state.impl = impl;<br>
>> > +   state.instr = NULL;<br>
>> > +   state.visited = rzalloc_array(NULL, BITSET_WORD, instr_words);<br>
>> > +   state.pinned = rzalloc_array(NULL, BITSET_WORD, instr_words);<br>
>> > +   exec_list_make_empty(&state.instrs);<br>
>> > +   state.blocks = rzalloc_array(NULL, struct gcm_block_info,<br>
>> > impl->num_blocks);<br>
>> > +<br>
>> > +   nir_metadata_require(impl, nir_metadata_block_index |<br>
>> > +                              nir_metadata_dominance);<br>
>> > +<br>
>> > +   gcm_build_block_info(&impl->body, &state, 0);<br>
>> > +   nir_foreach_block(impl, gcm_pin_instructions_block, &state);<br>
>> > +<br>
>> > +   nir_foreach_block(impl, gcm_schedule_early_block, &state);<br>
>> > +<br>
>> > +   memset(state.visited, 0, instr_words * sizeof(*state.visited));<br>
>> > +   nir_foreach_block(impl, gcm_schedule_late_block, &state);<br>
>> > +<br>
>> > +   memset(state.visited, 0, instr_words * sizeof(*state.visited));<br>
>> > +   while (!exec_list_is_empty(&state.instrs)) {<br>
>> > +      nir_instr *instr = exec_node_data(nir_instr,<br>
>> > +                                        state.instrs.tail_pred, node);<br>
>> > +      gcm_place_instr(instr, &state);<br>
>> > +   }<br>
>> > +<br>
>> > +   ralloc_free(state.visited);<br>
>> > +   ralloc_free(state.blocks);<br>
>> > +}<br>
>> > +<br>
>> > +void<br>
>> > +nir_opt_gcm(nir_shader *shader)<br>
>> > +{<br>
>> > +   nir_foreach_overload(shader, overload) {<br>
>> > +      if (overload->impl)<br>
>> > +         opt_gcm_impl(overload->impl);<br>
>> > +   }<br>
>> > +}<br>
>> > --<br>
>> > 2.2.2<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>
><br>
><br>
</div></div></blockquote></div><br></div></div>