[Mesa-dev] [RFC] [PATCH 2/3 v2] nir: Add a LCSAA-pass

Thomas Helland thomashelland90 at gmail.com
Wed Jul 29 15:07:19 PDT 2015


The pass is now working (it does the right thing) but something
ends up crashing after the pass is run, for some reason.
I haven't gotten to debug this yet, but thought I'd replace
the incredibly faulty patch that was on the list with a better one.
Sorry for the noise.

I've confirmed that phi's get inserted for the uses that is outside
the loop only, and not for the uses inside the loop.

It does not yet handle if-statements; I'm a bit unsure how to detect
that the if-statement is outside the loop. I guess the dominance
information can be used for that, in one way or the other?
Or I can do a "foreach in cf node" and compare pointers.
It might be a bit hacky, but it will do the job.

TODO items:
I haven't fixed up the casting in the visitor-functions,
and I should probably rename some of them.

Signed-off-by: Thomas Helland <thomashelland90 at gmail.com>
---
 src/glsl/Makefile.sources           |   1 +
 src/glsl/nir/nir.h                  |   3 +
 src/glsl/nir/nir_form_LCSSA.c       | 233 ++++++++++++++++++++++++++++++++++++
 src/mesa/drivers/dri/i965/brw_nir.c |   7 ++
 4 files changed, 244 insertions(+)
 create mode 100644 src/glsl/nir/nir_form_LCSSA.c

diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 93f4e48..5740de9 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -27,6 +27,7 @@ NIR_FILES = \
 	nir/nir_constant_expressions.h \
 	nir/nir_dominance.c \
 	nir/nir_from_ssa.c \
+        nir/nir_form_LCSSA.c \
 	nir/nir_intrinsics.c \
 	nir/nir_intrinsics.h \
 	nir/nir_live_variables.c \
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 36024b0..ee5de11 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1677,6 +1677,9 @@ bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
 void nir_convert_to_ssa_impl(nir_function_impl *impl);
 void nir_convert_to_ssa(nir_shader *shader);
 
+void nir_form_LCSSA_impl(nir_function_impl *impl);
+void nir_form_LCSSA(nir_shader *shader);
+
 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
  * registers.  If false, convert all values (even those not involved in a phi
  * node) to registers.
diff --git a/src/glsl/nir/nir_form_LCSSA.c b/src/glsl/nir/nir_form_LCSSA.c
new file mode 100644
index 0000000..cdb97ee
--- /dev/null
+++ b/src/glsl/nir/nir_form_LCSSA.c
@@ -0,0 +1,233 @@
+/*
+ * Copyright © 2015 Thomas Helland (for Google's Summer of Code)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * This pass converts the ssa-graph into "Loop Closed SSA form". This is
+ * done by placing phi nodes at the exits of the loop for all values
+ * that are used outside the loop. The result is it transforms:
+ *
+ * loop {                    ->          loop {
+ *    ssa2 = ....            ->          ssa2 = ...
+ *    if (cond)              ->          if (cond)
+ *       break;              ->             ssa5 = phi(ssa2)
+ *    ssa3 = ssa2 * ssa4     ->             break;
+ * }                         ->          ssa3 = ssa2 * ssa4
+ * ssa6 = ssa2 + 4           ->          }
+ *                                    ssa6 = ssa5 + 4
+ *
+ * This will make a lot of other passes like loop unrolling and LICM simpler.
+ * It is also beneficial as it makes it trivial to keep control of which
+ * ssa-defs are live across the loop-boundary. It will also simplify doing
+ * loop-unswitching a lot, as one just needs to copy the conditional out of
+ * the loop, put one copy of the loop into the then branch, and the other
+ * into the else branch, set the condition true and false respectively in
+ * these loops, and rewrite the LCSSA-phi's to have both the "true"-loop
+ * and the "false"-loop versions of the ssa-def as source.
+ */
+
+#include "nir.h"
+
+typedef struct {
+   /* The nir_shader we are transforming */
+   nir_shader *shader;
+
+   /* The loop we store information for */
+   nir_loop *loop;
+
+   /* Keep track of which defs are in the loop */
+   BITSET_WORD *is_in_loop;
+
+   /* General purpose bool */
+   bool flag;
+} lcssa_state;
+
+static inline bool
+mark_ssa_def_as_in_loop(nir_ssa_def *def, void *state)
+{
+   lcssa_state *state_cast = (lcssa_state *) state;
+   BITSET_SET(state_cast->is_in_loop, def->index);
+   return true;
+}
+
+static bool
+mark_block_as_in_loop(nir_block *block, void *state)
+{
+   nir_foreach_instr(block, instr)
+      nir_foreach_ssa_def(instr, mark_ssa_def_as_in_loop, state);
+   return true;
+}
+
+static bool
+is_outside_loop(nir_ssa_def *def, void *void_state)
+{
+   lcssa_state *state = void_state;
+   if (!BITSET_TEST(state->is_in_loop, def->index))
+      state->flag = true;
+   return true;
+}
+
+static bool
+convert_loop_exit_for_ssa(nir_ssa_def *def, void *state)
+{
+   lcssa_state *state_cast = (lcssa_state *) state;
+
+   state_cast->flag = false;
+
+   nir_foreach_use_safe(def, src)
+      nir_foreach_ssa_def(src->parent_instr, is_outside_loop, state);
+
+   nir_foreach_if_use_safe(def, src) {
+      /* Here we should eventually add something to fix up if-uses */
+   }
+
+   /* There was no sources that had defs outside the loop */
+   if (!state_cast->flag)
+      return true;
+
+   nir_phi_instr *phi = nir_phi_instr_create(state_cast->shader);
+
+   nir_block *succ = nir_cf_node_as_block(
+                        nir_cf_node_next(&state_cast->loop->cf_node));
+   phi->instr.block = succ;
+
+   nir_ssa_dest_init(&phi->instr, &phi->dest,
+                    def->num_components, "LCSSA-phi");
+
+   state_cast->flag = false;
+   nir_foreach_use_safe(def, src) {
+      nir_foreach_ssa_def(src->parent_instr, is_outside_loop, state);
+
+      if (!state_cast->flag)
+         continue;
+
+      nir_instr_rewrite_src(src->parent_instr, src, nir_src_for_ssa(&phi->dest.ssa));
+      state_cast->flag = false;
+   }
+
+   nir_phi_src *phi_src = ralloc(phi, nir_phi_src);
+   phi_src->pred = def->parent_instr->block;
+   phi_src->src = nir_src_for_ssa(def);
+
+   exec_list_push_tail(&phi->srcs, &phi_src->node);
+
+   nir_instr_insert_before_block(succ, &phi->instr);
+
+   nir_foreach_if_use_safe(def, src) {
+      /* If the if is outside the loop then run the below */
+      //nir_if_rewrite_condition(src->parent_if, nir_src_for_ssa(phi->dest.ssa));
+   }
+   return true;
+}
+
+static bool
+convert_loop_exits_for_block(nir_block *block, lcssa_state *state)
+{
+   nir_foreach_instr(block, instr)
+      nir_foreach_ssa_def(instr, convert_loop_exit_for_ssa, state);
+   return true;
+}
+
+static void
+convert_to_lcssa(nir_cf_node *cf_node, lcssa_state *state)
+{
+   switch (cf_node->type) {
+   case nir_cf_node_block:
+      convert_loop_exits_for_block(nir_cf_node_as_block(cf_node), state);
+      return;
+   case nir_cf_node_if: {
+      nir_if *if_stmt = nir_cf_node_as_if(cf_node);
+      foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->then_list)
+         convert_to_lcssa(nested_node, state);
+      foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->else_list)
+         convert_to_lcssa(nested_node, state);
+      return;
+   }
+   case nir_cf_node_loop:
+      /* This should have no uses except for the LCSSA-phis, so there should
+       * be no need to rewrite anything for it
+       */
+      return;
+   default:
+      unreachable("unknown cf node type");
+   }
+}
+
+static void
+compute_lcssa(nir_cf_node *cf_node)
+{
+   switch (cf_node->type) {
+   case nir_cf_node_block:
+      return;
+   case nir_cf_node_if: {
+      nir_if *if_stmt = nir_cf_node_as_if(cf_node);
+      foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->then_list)
+         compute_lcssa(nested_node);
+      foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->else_list)
+         compute_lcssa(nested_node);
+      return;
+   }
+   case nir_cf_node_loop: {
+      nir_loop *loop = nir_cf_node_as_loop(cf_node);
+      foreach_list_typed(nir_cf_node, nested_node, node, &loop->body)
+         compute_lcssa(nested_node);
+      break;
+   }
+   default:
+      unreachable("unknown cf node type");
+   }
+
+   nir_loop *loop = nir_cf_node_as_loop(cf_node);
+   nir_function_impl *impl = nir_cf_node_get_function(cf_node);
+
+   lcssa_state *state = rzalloc(NULL, lcssa_state);
+
+   state->is_in_loop = rzalloc_array(state, BITSET_WORD,
+                                      BITSET_WORDS(impl->ssa_alloc));
+
+   state->loop = loop;
+
+   nir_foreach_block_in_cf_node(cf_node, mark_block_as_in_loop, state);
+
+   foreach_list_typed(nir_cf_node, node, node, &loop->body)
+      convert_to_lcssa(node, state);
+}
+
+void
+nir_form_LCSSA_impl(nir_function_impl *impl)
+{
+   foreach_list_typed(nir_cf_node, node, node, &impl->body)
+      compute_lcssa(node);
+}
+
+void
+nir_form_LCSSA(nir_shader *shader)
+{
+   fprintf(stderr, "--------------Pre-LCSSA shader -------------- \n \n");
+   nir_print_shader(shader, stderr);
+   nir_foreach_overload(shader, overload) {
+      if (overload->impl)
+         nir_form_LCSSA_impl(overload->impl);
+   }
+   fprintf(stderr, "--------------Post-LCSSA shader ------------- \n \n");
+   nir_print_shader(shader, stderr);
+}
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c
index 3e154c1..b906fa9 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.c
+++ b/src/mesa/drivers/dri/i965/brw_nir.c
@@ -54,6 +54,13 @@ nir_optimize(nir_shader *nir)
       nir_validate_shader(nir);
       progress |= nir_opt_remove_phis(nir);
       nir_validate_shader(nir);
+      nir_form_LCSSA(nir);
+      nir_validate_shader(nir);
+      /* Loop optimizations start here */
+
+      /* Loop optimizations end here */
+      nir_opt_remove_phis(nir);
+      nir_validate_shader(nir);
    } while (progress);
 }
 
-- 
2.4.6



More information about the mesa-dev mailing list