[Mesa-dev] [PATCH 11/17] nir/lower-io: split out some helper fxns

Rob Clark robdclark at gmail.com
Fri Apr 15 14:49:46 UTC 2016


From: Rob Clark <robclark at freedesktop.org>

Prep work to reduce the noise in the next patch.

Signed-off-by: Rob Clark <robclark at freedesktop.org>
---
 src/compiler/nir/nir_lower_io_to_temporaries.c | 103 +++++++++++++++----------
 1 file changed, 62 insertions(+), 41 deletions(-)

diff --git a/src/compiler/nir/nir_lower_io_to_temporaries.c b/src/compiler/nir/nir_lower_io_to_temporaries.c
index 84fba0e..817d0cc 100644
--- a/src/compiler/nir/nir_lower_io_to_temporaries.c
+++ b/src/compiler/nir/nir_lower_io_to_temporaries.c
@@ -31,29 +31,35 @@
 
 struct lower_io_state {
    nir_shader *shader;
+   nir_function *entrypoint;
    struct exec_list old_outputs;
 };
 
 static void
-emit_output_copies(nir_cursor cursor, struct lower_io_state *state)
+emit_copies(nir_cursor cursor, nir_shader *shader, struct exec_list *new_vars,
+          struct exec_list *old_vars)
 {
-   assert(exec_list_length(&state->shader->outputs) ==
-          exec_list_length(&state->old_outputs));
+   assert(exec_list_length(new_vars) == exec_list_length(old_vars));
 
-   foreach_two_lists(out_node, &state->shader->outputs,
-                     temp_node, &state->old_outputs) {
-      nir_variable *output = exec_node_data(nir_variable, out_node, node);
-      nir_variable *temp = exec_node_data(nir_variable, temp_node, node);
+   foreach_two_lists(new_node, new_vars, old_node, old_vars) {
+      nir_variable *newv = exec_node_data(nir_variable, new_node, node);
+      nir_variable *temp = exec_node_data(nir_variable, old_node, node);
 
       nir_intrinsic_instr *copy =
-         nir_intrinsic_instr_create(state->shader, nir_intrinsic_copy_var);
-      copy->variables[0] = nir_deref_var_create(copy, output);
+         nir_intrinsic_instr_create(shader, nir_intrinsic_copy_var);
+      copy->variables[0] = nir_deref_var_create(copy, newv);
       copy->variables[1] = nir_deref_var_create(copy, temp);
 
       nir_instr_insert(cursor, &copy->instr);
    }
 }
 
+static void
+emit_output_copies(nir_cursor cursor, struct lower_io_state *state)
+{
+   emit_copies(cursor, state->shader, &state->shader->outputs, &state->old_outputs);
+}
+
 static bool
 emit_output_copies_block(nir_block *block, void *state)
 {
@@ -69,6 +75,50 @@ emit_output_copies_block(nir_block *block, void *state)
    return true;
 }
 
+static void
+emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
+{
+   if (state->shader->stage == MESA_SHADER_GEOMETRY) {
+      /* For geometry shaders, we have to emit the output copies right
+       * before each EmitVertex call.
+       */
+      nir_foreach_block(impl, emit_output_copies_block, state);
+   } else if (impl->function == state->entrypoint) {
+      /* For all other shader types, we need to do the copies right before
+       * the jumps to the end block.
+       */
+      struct set_entry *block_entry;
+      set_foreach(impl->end_block->predecessors, block_entry) {
+         struct nir_block *block = (void *)block_entry->key;
+         emit_output_copies(nir_after_block_before_jump(block), state);
+      }
+   }
+}
+
+static nir_variable *
+create_shadow_temp(struct lower_io_state *state, nir_variable *var)
+{
+   nir_variable *nvar = ralloc(state->shader, nir_variable);
+   memcpy(nvar, var, sizeof *nvar);
+
+   /* The original is now the temporary */
+   nir_variable *temp = var;
+
+   /* Reparent the name to the new variable */
+   ralloc_steal(nvar, nvar->name);
+
+   /* Reparent the constant initializer (if any) */
+   ralloc_steal(nvar, nvar->constant_initializer);
+
+   /* Give the output a new name with @out-temp appended */
+   const char *mode = "out";
+   temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
+   temp->data.mode = nir_var_global;
+   temp->constant_initializer = NULL;
+
+   return nvar;
+}
+
 void
 nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
 {
@@ -78,29 +128,14 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
       return;
 
    state.shader = shader;
+   state.entrypoint = entrypoint;
    exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
 
    /* Walk over all of the outputs turn each output into a temporary and
     * make a new variable for the actual output.
     */
    nir_foreach_variable(var, &state.old_outputs) {
-      nir_variable *output = ralloc(shader, nir_variable);
-      memcpy(output, var, sizeof *output);
-
-      /* The orignal is now the temporary */
-      nir_variable *temp = var;
-
-      /* Reparent the name to the new variable */
-      ralloc_steal(output, output->name);
-
-      /* Reparent the constant initializer (if any) */
-      ralloc_steal(output, output->constant_initializer);
-
-      /* Give the output a new name with @out-temp appended */
-      temp->name = ralloc_asprintf(var, "%s at out-temp", output->name);
-      temp->data.mode = nir_var_global;
-      temp->constant_initializer = NULL;
-
+      nir_variable *output = create_shadow_temp(&state, var);
       exec_list_push_tail(&shader->outputs, &output->node);
    }
 
@@ -108,21 +143,7 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
       if (function->impl == NULL)
          continue;
 
-      if (shader->stage == MESA_SHADER_GEOMETRY) {
-         /* For geometry shaders, we have to emit the output copies right
-          * before each EmitVertex call.
-          */
-         nir_foreach_block(function->impl, emit_output_copies_block, &state);
-      } else if (function == entrypoint) {
-         /* For all other shader types, we need to do the copies right before
-          * the jumps to the end block.
-          */
-         struct set_entry *block_entry;
-         set_foreach(function->impl->end_block->predecessors, block_entry) {
-            struct nir_block *block = (void *)block_entry->key;
-            emit_output_copies(nir_after_block_before_jump(block), &state);
-         }
-      }
+      emit_output_copies_impl(&state, function->impl);
 
       nir_metadata_preserve(function->impl, nir_metadata_block_index |
                                             nir_metadata_dominance);
-- 
2.5.5



More information about the mesa-dev mailing list