[Mesa-dev] [PATCH 12/16] nir/lower-io: add support for lowering inputs

Rob Clark robdclark at gmail.com
Sat Mar 26 21:02:14 UTC 2016


From: Rob Clark <robclark at freedesktop.org>

Signed-off-by: Rob Clark <robclark at freedesktop.org>
---
 src/compiler/nir/nir.h                         |  2 +-
 src/compiler/nir/nir_lower_io_to_temporaries.c | 55 +++++++++++++++++++++-----
 src/mesa/drivers/dri/i965/brw_nir.c            |  2 +-
 3 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index ab73068..f12bc68 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2160,7 +2160,7 @@ bool nir_lower_indirect_derefs(nir_shader *shader, uint32_t mode_mask);
 
 bool nir_lower_locals_to_regs(nir_shader *shader);
 
-void nir_lower_io_to_temporaries(nir_shader *shader);
+void nir_lower_io_to_temporaries(nir_shader *shader, bool outputs, bool inputs);
 
 void nir_assign_var_locations(struct exec_list *var_list,
                               unsigned *size,
diff --git a/src/compiler/nir/nir_lower_io_to_temporaries.c b/src/compiler/nir/nir_lower_io_to_temporaries.c
index 8e9df99..c25b6e7 100644
--- a/src/compiler/nir/nir_lower_io_to_temporaries.c
+++ b/src/compiler/nir/nir_lower_io_to_temporaries.c
@@ -22,9 +22,12 @@
  */
 
 /*
- * Implements a pass that lowers output variables to a temporary plus an
- * output variable with a single copy at each exit point of the shader.
- * This way the output variable is only ever written.
+ * Implements a pass that lowers output and/or input variables to a
+ * temporary plus an output variable with a single copy at each exit
+ * point of the shader and/or an input variable with a single copy
+ * at the entrance point of the shader.  This way the output variable
+ * is only ever written once and/or input is only read once, and there
+ * are no indirect outut/input accesses.
  */
 
 #include "nir.h"
@@ -32,6 +35,7 @@
 struct lower_io_state {
    nir_shader *shader;
    struct exec_list old_outputs;
+   struct exec_list old_inputs;
 };
 
 static void
@@ -48,7 +52,6 @@ emit_copies(nir_cursor cursor, nir_shader *shader, struct exec_list *new_vars,
          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);
    }
 }
@@ -94,6 +97,20 @@ emit_output_copies_impl(nir_function_impl *impl, struct lower_io_state *state)
    }
 }
 
+static void
+emit_input_copies(nir_cursor cursor, struct lower_io_state *state)
+{
+   emit_copies(cursor, state->shader, &state->old_inputs, &state->shader->inputs);
+}
+
+static void
+emit_input_copies_impl(nir_function_impl *impl, struct lower_io_state *state)
+{
+   if (strcmp(impl->function->name, "main") == 0) {
+      emit_input_copies(nir_before_block(nir_start_block(impl)), state);
+   }
+}
+
 static nir_variable *
 create_shadow_temp(struct lower_io_state *state, nir_variable *var)
 {
@@ -106,8 +123,8 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
    /* Reparent the name to the new variable */
    ralloc_steal(nvar, nvar->name);
 
-   /* Give the output a new name with @out-temp appended */
-   const char *mode = "out";
+   /* Give the original a new name with @<mode>-temp appended */
+   const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
    temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
    temp->data.mode = nir_var_global;
    temp->constant_initializer = NULL;
@@ -116,7 +133,7 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
 }
 
 void
-nir_lower_io_to_temporaries(nir_shader *shader)
+nir_lower_io_to_temporaries(nir_shader *shader, bool outputs, bool inputs)
 {
    struct lower_io_state state;
 
@@ -124,7 +141,16 @@ nir_lower_io_to_temporaries(nir_shader *shader)
       return;
 
    state.shader = shader;
-   exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
+
+   if (inputs)
+      exec_list_move_nodes_to(&shader->inputs, &state.old_inputs);
+   else
+      exec_list_make_empty(&state.old_inputs);
+
+   if (outputs)
+      exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
+   else
+      exec_list_make_empty(&state.old_outputs);
 
    /* Walk over all of the outputs turn each output into a temporary and
     * make a new variable for the actual output.
@@ -134,15 +160,26 @@ nir_lower_io_to_temporaries(nir_shader *shader)
       exec_list_push_tail(&shader->outputs, &output->node);
    }
 
+   /* and same for inputs: */
+   nir_foreach_variable(var, &state.old_inputs) {
+      nir_variable *input = create_shadow_temp(&state, var);
+      exec_list_push_tail(&shader->inputs, &input->node);
+   }
+
    nir_foreach_function(shader, function) {
       if (function->impl == NULL)
          continue;
 
-      emit_output_copies_impl(function->impl, &state);
+      if (inputs)
+         emit_input_copies_impl(function->impl, &state);
+
+      if (outputs)
+         emit_output_copies_impl(function->impl, &state);
 
       nir_metadata_preserve(function->impl, nir_metadata_block_index |
                                             nir_metadata_dominance);
    }
 
+   exec_list_append(&shader->globals, &state.old_inputs);
    exec_list_append(&shader->globals, &state.old_outputs);
 }
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c
index 10f4ae1..d7c9764 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.c
+++ b/src/mesa/drivers/dri/i965/brw_nir.c
@@ -560,7 +560,7 @@ brw_create_nir(struct brw_context *brw,
    /* First, lower the GLSL IR or Mesa IR to NIR */
    if (shader_prog) {
       nir = glsl_to_nir(shader_prog, stage, options);
-      nir_lower_io_to_temporaries(nir);
+      nir_lower_io_to_temporaries(nir, true, false);
    } else {
       nir = prog_to_nir(prog, options);
       OPT_V(nir_convert_to_ssa); /* turn registers into SSA */
-- 
2.5.5



More information about the mesa-dev mailing list