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

Thomas Helland thomashelland90 at gmail.com
Tue Jul 28 10:52:43 PDT 2015


V2: Do a "depth first search" to convert to LCSSA

I'm still a bit unsure if the way I'm inserting
the phi nodes is correct, but the flow of the pass
seems to be correct. Unfortunately it keeps getting
rerun time and time again. Most likely due to some optimizations
removing the phi-nodes or other dead code that is left behind.

Signed-off-by: Thomas Helland <thomashelland90 at gmail.com>
---
 src/glsl/Makefile.sources     |   1 +
 src/glsl/nir/nir.h            |   4 +
 src/glsl/nir/nir_form_LCSSA.c | 227 ++++++++++++++++++++++++++++++++++++++++++
 src/glsl/nir/nir_metadata.c   |   2 +
 4 files changed, 234 insertions(+)
 create mode 100644 src/glsl/nir/nir_form_LCSSA.c

diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index d784a81..9abaf2d 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..772fa9a 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1299,6 +1299,7 @@ typedef enum {
    nir_metadata_block_index = 0x1,
    nir_metadata_dominance = 0x2,
    nir_metadata_live_variables = 0x4,
+   nir_metadata_LCSSA_form = 0x5,
 } nir_metadata;
 
 typedef struct {
@@ -1677,6 +1678,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..76279f5
--- /dev/null
+++ b/src/glsl/nir/nir_form_LCSSA.c
@@ -0,0 +1,227 @@
+/*
+ * 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,
+ * 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.
+ */
+
+
+/* Method for getting LCSSA:
+ *
+ * Take one loop from the list and initialize it.
+ * We should have already initialized all the values to "outside_loop"
+ * and then we set those values that are inside the loop to "inside_loop".
+ *
+ * We run through all of the variables inside the loop to check if they
+ * are used outside the loop. If they are we insert an intermediate phi
+ * just outside the loop. Inserting phi's should be safe to do without
+ * regenerating all our information. However, we end up messing up
+ * our array of loop_variables, as there our now more variables
+ * outside the loop. These are however not "sources" in our loop, and so
+ * we do not need their information to calculate invariance information.
+ * We can therefore safely proceed with calculating invariance information.
+ */
+
+#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;
+} lcssa_state;
+
+/* Inserts a phi node between the ssa-def and its use */
+static void
+insert_phi_for_def(nir_ssa_def *def_in_loop, nir_src *use_outside_loop,
+                   lcssa_state *state)
+{
+   nir_phi_instr *phi = nir_phi_instr_create(state->shader);
+
+   nir_block *succ = nir_cf_node_as_block(
+                        nir_cf_node_next(&state->loop->cf_node));
+
+   nir_instr_insert_before_block(succ, &phi->instr);
+
+   nir_ssa_def *phi_def;
+   nir_ssa_def_init(&phi->instr, phi_def,
+                    def_in_loop->num_components, "LCSSA-phi");
+
+   nir_src phi_src = nir_src_for_ssa(phi_def);
+   nir_ssa_def_rewrite_uses(def_in_loop, phi_src, state->shader);
+
+   /* Remove the source of our user.
+    * Set the ssa-dest as a source for our user
+    * Move the destination of the def_in_loop to the phi
+    */
+}
+
+static bool
+convert_loop_exit_for_ssa(nir_ssa_def *def, void *state)
+{
+   lcssa_state *state_cast = (lcssa_state *) state;
+   nir_foreach_use_safe(def, src) {
+      nir_ssa_def *dest = src->ssa;
+      if (!BITSET_TEST(state_cast->is_in_loop, dest->index))
+         insert_phi_for_def(def, src, state);
+   }
+   nir_foreach_if_use_safe(def, src) {
+      nir_ssa_def *dest = src->ssa;
+      if (!BITSET_TEST(state_cast->is_in_loop, dest->index))
+         insert_phi_for_def(def, src, state);
+   }
+   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 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 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)
+{
+   nir_foreach_overload(shader, overload) {
+      if (overload->impl)
+         nir_form_LCSSA_impl(overload->impl);
+   }
+}
diff --git a/src/glsl/nir/nir_metadata.c b/src/glsl/nir/nir_metadata.c
index a03e124..fe1a9b2 100644
--- a/src/glsl/nir/nir_metadata.c
+++ b/src/glsl/nir/nir_metadata.c
@@ -37,6 +37,8 @@ nir_metadata_require(nir_function_impl *impl, nir_metadata required)
 
    if (NEEDS_UPDATE(nir_metadata_block_index))
       nir_index_blocks(impl);
+   if (NEEDS_UPDATE(nir_metadata_LCSSA_form))
+      nir_form_LCSSA_impl(impl);
    if (NEEDS_UPDATE(nir_metadata_dominance))
       nir_calc_dominance_impl(impl);
    if (NEEDS_UPDATE(nir_metadata_live_variables))
-- 
2.4.6



More information about the mesa-dev mailing list