[Mesa-dev] [PATCH 140/133] nir: Rename parallel_copy_copy to parallel_copy_entry and add a foreach macro

Jason Ekstrand jason at jlekstrand.net
Wed Dec 17 17:04:33 PST 2014


parallel_copy_copy was a silly name.  Also, things were getting long and
annoying, so I added a foreach macro.  For historical reasons, several of
the original iterations over parallel copy entries in from_ssa used the
_safe variants of the loop.  However, all of these no longer ever remove an
entry so it's ok to make them all use the normal iterator.
---
 src/glsl/nir/nir.c          | 10 +++----
 src/glsl/nir/nir.h          | 13 ++++++--
 src/glsl/nir/nir_from_ssa.c | 72 ++++++++++++++++++++++-----------------------
 src/glsl/nir/nir_print.c    |  6 ++--
 4 files changed, 55 insertions(+), 46 deletions(-)

diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index 17c1efe..fd18903 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -471,7 +471,7 @@ nir_parallel_copy_instr_create(void *mem_ctx)
    nir_parallel_copy_instr *instr = ralloc(mem_ctx, nir_parallel_copy_instr);
    instr_init(&instr->instr, nir_instr_type_parallel_copy);
 
-   exec_list_make_empty(&instr->copies);
+   exec_list_make_empty(&instr->entries);
 
    return instr;
 }
@@ -1410,8 +1410,8 @@ static bool
 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
                          nir_foreach_dest_cb cb, void *state)
 {
-   foreach_list_typed(nir_parallel_copy_copy, copy, node, &instr->copies) {
-      if (!cb(&copy->dest, state))
+   nir_foreach_parallel_copy_entry(instr, entry) {
+      if (!cb(&entry->dest, state))
          return false;
    }
 
@@ -1598,8 +1598,8 @@ static bool
 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
                         nir_foreach_src_cb cb, void *state)
 {
-   foreach_list_typed(nir_parallel_copy_copy, copy, node, &instr->copies) {
-      if (!visit_src(&copy->src, cb, state))
+   nir_foreach_parallel_copy_entry(instr, entry) {
+      if (!visit_src(&entry->src, cb, state))
          return false;
    }
 
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index a23bc5f..a30f619 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -963,11 +963,20 @@ typedef struct {
    struct exec_node node;
    nir_src src;
    nir_dest dest;
-} nir_parallel_copy_copy;
+} nir_parallel_copy_entry;
+
+#define nir_foreach_parallel_copy_entry(pcopy, entry) \
+   foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
 
 typedef struct {
    nir_instr instr;
-   struct exec_list copies;
+
+   /* A list of nir_parallel_copy_entry's.  The sources of all of the
+    * entries are copied to the corresponding destinations "in parallel".
+    * In other words, if we have two entries: a -> b and b -> a, the values
+    * get swapped.
+    */
+   struct exec_list entries;
 } nir_parallel_copy_instr;
 
 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr)
diff --git a/src/glsl/nir/nir_from_ssa.c b/src/glsl/nir/nir_from_ssa.c
index 29fcc64..11f3ecd 100644
--- a/src/glsl/nir/nir_from_ssa.c
+++ b/src/glsl/nir/nir_from_ssa.c
@@ -350,22 +350,22 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
             get_parallel_copy_at_end_of_block(src->pred);
          assert(pcopy);
 
-         nir_parallel_copy_copy *copy = ralloc(state->dead_ctx,
-                                               nir_parallel_copy_copy);
-         exec_list_push_tail(&pcopy->copies, &copy->node);
+         nir_parallel_copy_entry *entry = ralloc(state->dead_ctx,
+                                                 nir_parallel_copy_entry);
+         exec_list_push_tail(&pcopy->entries, &entry->node);
 
-         copy->src = nir_src_copy(src->src, state->dead_ctx);
+         entry->src = nir_src_copy(src->src, state->dead_ctx);
          _mesa_set_add(src->src.ssa->uses,
                        _mesa_hash_pointer(&pcopy->instr), &pcopy->instr);
 
-         copy->dest.is_ssa = true;
-         nir_ssa_def_init(&pcopy->instr, &copy->dest.ssa,
+         entry->dest.is_ssa = true;
+         nir_ssa_def_init(&pcopy->instr, &entry->dest.ssa,
                           phi->dest.ssa.num_components, src->src.ssa->name);
 
-         struct set_entry *entry = _mesa_set_search(src->src.ssa->uses,
-                                                    _mesa_hash_pointer(instr),
-                                                    instr);
-         if (entry)
+         struct set_entry *use_entry =
+            _mesa_set_search(src->src.ssa->uses,
+                             _mesa_hash_pointer(instr), instr);
+         if (use_entry)
             /* It is possible that a phi node can use the same source twice
              * but for different basic blocks.  If that happens, entry will
              * be NULL because we already deleted it.  This is safe
@@ -373,28 +373,28 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
              * all of the sources of the phi from their respective use sets
              * and moved them to the parallel copy definitions.
              */
-            _mesa_set_remove(src->src.ssa->uses, entry);
+            _mesa_set_remove(src->src.ssa->uses, use_entry);
 
-         src->src.ssa = &copy->dest.ssa;
-         _mesa_set_add(copy->dest.ssa.uses, _mesa_hash_pointer(instr), instr);
+         src->src.ssa = &entry->dest.ssa;
+         _mesa_set_add(entry->dest.ssa.uses, _mesa_hash_pointer(instr), instr);
       }
 
-      nir_parallel_copy_copy *copy = ralloc(state->dead_ctx,
-                                            nir_parallel_copy_copy);
-      exec_list_push_tail(&block_pcopy->copies, &copy->node);
+      nir_parallel_copy_entry *entry = ralloc(state->dead_ctx,
+                                              nir_parallel_copy_entry);
+      exec_list_push_tail(&block_pcopy->entries, &entry->node);
 
-      copy->dest.is_ssa = true;
-      nir_ssa_def_init(&block_pcopy->instr, &copy->dest.ssa,
+      entry->dest.is_ssa = true;
+      nir_ssa_def_init(&block_pcopy->instr, &entry->dest.ssa,
                        phi->dest.ssa.num_components, phi->dest.ssa.name);
 
-      nir_src copy_dest_src = {
-         .ssa = &copy->dest.ssa,
+      nir_src entry_dest_src = {
+         .ssa = &entry->dest.ssa,
          .is_ssa = true,
       };
-      nir_ssa_def_rewrite_uses(&phi->dest.ssa, copy_dest_src, state->mem_ctx);
+      nir_ssa_def_rewrite_uses(&phi->dest.ssa, entry_dest_src, state->mem_ctx);
 
-      copy->src.is_ssa = true;
-      copy->src.ssa = &phi->dest.ssa;
+      entry->src.is_ssa = true;
+      entry->src.ssa = &phi->dest.ssa;
       _mesa_set_add(phi->dest.ssa.uses,
                     _mesa_hash_pointer(&block_pcopy->instr),
                     &block_pcopy->instr);
@@ -433,22 +433,22 @@ static void
 agressive_coalesce_parallel_copy(nir_parallel_copy_instr *pcopy,
                                  struct from_ssa_state *state)
 {
-   foreach_list_typed_safe(nir_parallel_copy_copy, copy, node, &pcopy->copies) {
-      if (!copy->src.is_ssa)
+   nir_foreach_parallel_copy_entry(pcopy, entry) {
+      if (!entry->src.is_ssa)
          continue;
 
       /* Since load_const instructions are SSA only, we can't replace their
        * destinations with registers and, therefore, can't coalesce them.
        */
-      if (copy->src.ssa->parent_instr->type == nir_instr_type_load_const)
+      if (entry->src.ssa->parent_instr->type == nir_instr_type_load_const)
          continue;
 
       /* Don't try and coalesce these */
-      if (copy->dest.ssa.num_components != copy->src.ssa->num_components)
+      if (entry->dest.ssa.num_components != entry->src.ssa->num_components)
          continue;
 
-      merge_node *src_node = get_merge_node(copy->src.ssa, state);
-      merge_node *dest_node = get_merge_node(&copy->dest.ssa, state);
+      merge_node *src_node = get_merge_node(entry->src.ssa, state);
+      merge_node *dest_node = get_merge_node(&entry->dest.ssa, state);
 
       if (src_node->set == dest_node->set)
          continue;
@@ -677,9 +677,9 @@ resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
                       struct from_ssa_state *state)
 {
    unsigned num_copies = 0;
-   foreach_list_typed_safe(nir_parallel_copy_copy, copy, node, &pcopy->copies) {
+   nir_foreach_parallel_copy_entry(pcopy, entry) {
       /* Sources may be SSA */
-      if (!copy->src.is_ssa && copy->src.reg.reg == copy->dest.reg.reg)
+      if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
          continue;
 
       num_copies++;
@@ -715,23 +715,23 @@ resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
     *  - Predicessors are recorded from sources and destinations
     */
    int num_vals = 0;
-   foreach_list_typed(nir_parallel_copy_copy, copy, node, &pcopy->copies) {
+   nir_foreach_parallel_copy_entry(pcopy, entry) {
       /* Sources may be SSA */
-      if (!copy->src.is_ssa && copy->src.reg.reg == copy->dest.reg.reg)
+      if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
          continue;
 
       int src_idx = -1;
       for (int i = 0; i < num_vals; ++i) {
-         if (nir_srcs_equal(values[i], copy->src))
+         if (nir_srcs_equal(values[i], entry->src))
             src_idx = i;
       }
       if (src_idx < 0) {
          src_idx = num_vals++;
-         values[src_idx] = copy->src;
+         values[src_idx] = entry->src;
       }
 
       nir_src dest_src = {
-         .reg.reg = copy->dest.reg.reg,
+         .reg.reg = entry->dest.reg.reg,
          .reg.indirect = NULL,
          .reg.base_offset = 0,
          .is_ssa = false,
diff --git a/src/glsl/nir/nir_print.c b/src/glsl/nir/nir_print.c
index 4b0fb23..e9d528f 100644
--- a/src/glsl/nir/nir_print.c
+++ b/src/glsl/nir/nir_print.c
@@ -580,13 +580,13 @@ print_parallel_copy_instr(nir_parallel_copy_instr *instr, FILE *fp)
 {
    bool first = true;
    fprintf(fp, "pcopy: ");
-   foreach_list_typed(nir_parallel_copy_copy, copy, node, &instr->copies) {
+   nir_foreach_parallel_copy_entry(instr, entry) {
       if (!first)
          fprintf(fp, "; ");
 
-      print_dest(&copy->dest, fp);
+      print_dest(&entry->dest, fp);
       fprintf(fp, " = ");
-      print_src(&copy->src, fp);
+      print_src(&entry->src, fp);
 
       first = false;
    }
-- 
2.2.0



More information about the mesa-dev mailing list