[Mesa-dev] [PATCH 083/133] nir: Don't require a function in ssa_def_init

Jason Ekstrand jason at jlekstrand.net
Mon Dec 15 22:11:28 PST 2014


Instead, we give SSA definitions a temporary index of 0xFFFFFFFF if the
instruction does not have a block and a proper index when it actually gets
added to the list.
---
 src/glsl/nir/nir.c                     | 42 ++++++++++++++++++++++++++--------
 src/glsl/nir/nir.h                     |  5 ++--
 src/glsl/nir/nir_from_ssa.c            |  4 ++--
 src/glsl/nir/nir_opt_peephole_ffma.c   |  4 +---
 src/glsl/nir/nir_opt_peephole_select.c |  4 +---
 src/glsl/nir/nir_to_ssa.c              |  6 ++---
 6 files changed, 41 insertions(+), 24 deletions(-)

diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index 4c00248..07d7b94 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -1182,17 +1182,27 @@ add_use_cb(nir_src *src, void *state)
    return true;
 }
 
+static void
+add_ssa_def(nir_instr *instr, nir_ssa_def *def)
+{
+   if (instr->block && def->index == UINT_MAX) {
+      nir_function_impl *impl =
+         nir_cf_node_get_function(&instr->block->cf_node);
+
+      def->index = impl->ssa_alloc++;
+   }
+}
+
 static bool
 add_def_cb(nir_dest *dest, void *state)
 {
    nir_instr *instr = (nir_instr *) state;
 
-   if (dest->is_ssa)
-      return true;
-
-   nir_register *reg = dest->reg.reg;
-
-   _mesa_set_add(reg->defs, _mesa_hash_pointer(instr), instr);
+   if (dest->is_ssa) {
+      add_ssa_def(instr, &dest->ssa);
+   } else {
+      _mesa_set_add(dest->reg.reg->defs, _mesa_hash_pointer(instr), instr);
+   }
 
    return true;
 }
@@ -1200,8 +1210,12 @@ add_def_cb(nir_dest *dest, void *state)
 static void
 add_defs_uses(nir_instr *instr)
 {
-   nir_foreach_src(instr, add_use_cb, instr);
-   nir_foreach_dest(instr, add_def_cb, instr);
+   if (instr->type == nir_instr_type_ssa_undef) {
+      add_ssa_def(instr, &nir_instr_as_ssa_undef(instr)->def);
+   } else {
+      nir_foreach_src(instr, add_use_cb, instr);
+      nir_foreach_dest(instr, add_def_cb, instr);
+   }
 }
 
 void
@@ -1742,17 +1756,25 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
 }
 
 void
-nir_ssa_def_init(nir_function_impl *impl, nir_instr *instr, nir_ssa_def *def,
+nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
                  unsigned num_components, const char *name)
 {
    void *mem_ctx = ralloc_parent(instr);
 
    def->name = name;
-   def->index = impl->ssa_alloc++;
    def->parent_instr = instr;
    def->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
    def->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
    def->num_components = num_components;
+
+   if (instr->block) {
+      nir_function_impl *impl =
+         nir_cf_node_get_function(&instr->block->cf_node);
+
+      def->index = impl->ssa_alloc++;
+   } else {
+      def->index = UINT_MAX;
+   }
 }
 
 struct ssa_def_rewrite_state {
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 2bc8e4b..b04a137 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1318,9 +1318,8 @@ bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
 
-void nir_ssa_def_init(nir_function_impl *impl, nir_instr *instr,
-                      nir_ssa_def *def, unsigned num_components,
-                      const char *name);
+void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
+                      unsigned num_components, const char *name);
 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx);
 
 /* visits basic blocks in source-code order */
diff --git a/src/glsl/nir/nir_from_ssa.c b/src/glsl/nir/nir_from_ssa.c
index 9311bda..584609c 100644
--- a/src/glsl/nir/nir_from_ssa.c
+++ b/src/glsl/nir/nir_from_ssa.c
@@ -316,7 +316,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
                        _mesa_hash_pointer(&pcopy->instr), &pcopy->instr);
 
          copy->dest.is_ssa = true;
-         nir_ssa_def_init(state->impl, &pcopy->instr, &copy->dest.ssa,
+         nir_ssa_def_init(&pcopy->instr, &copy->dest.ssa,
                           phi->dest.ssa.num_components, src->src.ssa->name);
 
          struct set_entry *entry = _mesa_set_search(src->src.ssa->uses,
@@ -341,7 +341,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
       exec_list_push_tail(&block_pcopy->copies, &copy->node);
 
       copy->dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &block_pcopy->instr, &copy->dest.ssa,
+      nir_ssa_def_init(&block_pcopy->instr, &copy->dest.ssa,
                        phi->dest.ssa.num_components, phi->dest.ssa.name);
 
       nir_src copy_dest_src = {
diff --git a/src/glsl/nir/nir_opt_peephole_ffma.c b/src/glsl/nir/nir_opt_peephole_ffma.c
index 2c9b8e5..1165d7a 100644
--- a/src/glsl/nir/nir_opt_peephole_ffma.c
+++ b/src/glsl/nir/nir_opt_peephole_ffma.c
@@ -34,7 +34,6 @@
 
 struct peephole_ffma_state {
    void *mem_ctx;
-   nir_function_impl *impl;
    bool progress;
 };
 
@@ -135,7 +134,7 @@ nir_opt_peephole_ffma_block(nir_block *block, void *void_state)
 
       if (add->dest.dest.is_ssa) {
          ffma->dest.dest.is_ssa = true;
-         nir_ssa_def_init(state->impl, &ffma->instr, &ffma->dest.dest.ssa,
+         nir_ssa_def_init(&ffma->instr, &ffma->dest.dest.ssa,
                           add->dest.dest.ssa.num_components,
                           add->dest.dest.ssa.name);
 
@@ -165,7 +164,6 @@ nir_opt_peephole_ffma_impl(nir_function_impl *impl)
    struct peephole_ffma_state state;
 
    state.mem_ctx = ralloc_parent(impl);
-   state.impl = impl;
    state.progress = false;
 
    nir_foreach_block(impl, nir_opt_peephole_ffma_block, &state);
diff --git a/src/glsl/nir/nir_opt_peephole_select.c b/src/glsl/nir/nir_opt_peephole_select.c
index b3b54c1..4fc7726 100644
--- a/src/glsl/nir/nir_opt_peephole_select.c
+++ b/src/glsl/nir/nir_opt_peephole_select.c
@@ -48,7 +48,6 @@
 
 struct peephole_select_state {
    void *mem_ctx;
-   nir_function_impl *impl;
    bool progress;
 };
 
@@ -163,7 +162,7 @@ nir_opt_peephole_select_block(nir_block *block, void *void_state)
       }
 
       sel->dest.dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &sel->instr, &sel->dest.dest.ssa,
+      nir_ssa_def_init(&sel->instr, &sel->dest.dest.ssa,
                        phi->dest.ssa.num_components, phi->dest.ssa.name);
       sel->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
 
@@ -190,7 +189,6 @@ nir_opt_peephole_select_impl(nir_function_impl *impl)
 
    state.mem_ctx = ralloc_parent(impl);
    state.progress = false;
-   state.impl = impl;
 
    nir_foreach_block(impl, nir_opt_peephole_select_block, &state);
 
diff --git a/src/glsl/nir/nir_to_ssa.c b/src/glsl/nir/nir_to_ssa.c
index 7148733..ed12750 100644
--- a/src/glsl/nir/nir_to_ssa.c
+++ b/src/glsl/nir/nir_to_ssa.c
@@ -163,7 +163,7 @@ static nir_ssa_def *get_ssa_src(nir_register *reg, rewrite_state *state)
        * to preserve the information that this source is undefined
        */
       nir_ssa_undef_instr *instr = nir_ssa_undef_instr_create(state->mem_ctx);
-      nir_ssa_def_init(state->impl, &instr->instr, &instr->def,
+      nir_ssa_def_init(&instr->instr, &instr->def,
                        reg->num_components, NULL);
 
       /*
@@ -246,7 +246,7 @@ rewrite_def_forwards(nir_dest *dest, void *_state)
       name = ralloc_asprintf(state->mem_ctx, "%s_%u", dest->reg.reg->name,
                              state->states[index].num_defs);
 
-   nir_ssa_def_init(state->impl, state->parent_instr, &dest->ssa,
+   nir_ssa_def_init(state->parent_instr, &dest->ssa,
                     reg->num_components, name);
 
    /* push our SSA destination on the stack */
@@ -313,7 +313,7 @@ rewrite_alu_instr_forward(nir_alu_instr *instr, rewrite_state *state)
 
       instr->dest.write_mask = (1 << num_components) - 1;
       instr->dest.dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &instr->instr, &instr->dest.dest.ssa,
+      nir_ssa_def_init(&instr->instr, &instr->dest.dest.ssa,
                        num_components, name);
 
       if (nir_op_infos[instr->op].output_size == 0) {
-- 
2.2.0



More information about the mesa-dev mailing list