<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 9, 2015 at 12:01 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">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></blockquote><div><br></div><div>Sorry for not addressing this comment.  A couple of things:<br><br></div><div>1) We need the "is this instruction pinned" metadata for other things than just not iterating over them so we need to stash it somehow anyway.<br><br></div><div>2) I think we want value numbering to be its own pass.  We just won't run the validator in between that and GCM.  These passes are tricky and complex enough that I don't want to make it even more confusing by saying "Oh, now we value-number".  Obviously, GVN and GCM will have to be kept in sync so we don't combine two things that can't be code-motioned but that's not a big deal.<br><br></div><div>As far as pulling them as we pin and then only ever walking over non-pinned instructions.  I thought about it..  Then I didn't for some historical reason but I don't remember what that was anymore.  We could go back and do that and it would be a bit fasater.  I can do that if you'd like but I don't think it'll matter much.<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"><br>
On Mon, Feb 9, 2015 at 3:19 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> 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>
>  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 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>
> + * 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 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 we<br>
> +    * traverse the instructions and insert them back into the program 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>
> +   BITSET_WORD *visited;<br>
> +<br>
> +   /* Marks instructions that are "pinned", i.e. cannot be moved from their<br>
> +    * basic block by code motion */<br>
> +   BITSET_WORD *pinned;<br>
<br>
</div></div>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>
<div><div class="h5"><br>
> +<br>
> +   /* The list of non-pinned instructions.  As we do the late scheduling,<br>
> +    * we pull non-pinned instructions out of their blocks and place them in<br>
> +    * this list.  This saves us from having linked-list problems when 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 *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 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 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>
</div></div>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>
<div><div class="h5"><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>
> +            &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 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 value-numbering),<br>
> +    * all of the sources must lie on the same branch of the dominance 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 we<br>
> +    * may still be iterating through sources and future calls to<br>
> +    * gcm_schedule_early_src for the same instruction will still need 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 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 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 SSA<br>
> + * definition, ensuring that they are scheduled, and then computing the LCA<br>
> + * (least common ancestor) of its uses.  It then schedules this 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 have to<br>
> +       * dominate the sources of the phi nodes that use them; instead, they<br>
> +       * have to dominate the predecessor block corresponding to the phi<br>
> +       * source.  We handle this by looking through the sources, finding<br>
> +       * any that are usingg this SSA def, and using those blocks 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 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 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 early.<br>
> +    * We now walk up the dominance tree and pick the lowest block that 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>
</div></div>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>
<span class="">   while (lca != def->parent_instr->block) {<br>
</span>      lca = lca->imm_dom;<br>
      assert(lca);<br>
<span class="">      if (state->blocks[lca->index].loop_depth <<br>
</span><span class="">          state->blocks[best->index].loop_depth)<br>
</span>         best = lca;<br>
<div><div class="h5">   }<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 as<br>
> + * late as they can reasonably go in the dominance tree.  The 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 doesn't<br>
> + * schedule them "as late as possible" as the paper implies.  Instead, it<br>
> + * first finds the lates possible place it can schedule the instruction and<br>
> + * then possibly schedules it earlier than that.  The actual location 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 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 out<br>
> +          * of the program and put it on the instrs list.  This keeps 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 also<br>
> +          * cleans up uses and defs and we want to keep that 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 and<br>
> + * otherwise leave them alone.  This pass actually places the instructions<br>
> + * into their chosen blocks.<br>
> + *<br>
> + * To do so, we use a standard post-order depth-first search linearization<br>
> + * algorithm.  We walk over the uses of the given instruction and ensure<br>
> + * that they are placed and then place this instruction.  Because we are<br>
> + * working on multiple blocks at a time, we keep track of the last inserted<br>
> + * instruction per-block in the state structure's block_info array.  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 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 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 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 = &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, &instr->node);<br>
> +         } else {<br>
> +            exec_list_push_tail(&instr->block->instr_list, &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, 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>
</div></div>> _______________________________________________<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>
</blockquote></div><br></div></div>