<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 9, 2016 at 12:34 PM, Rob Clark <span dir="ltr"><<a href="mailto:robdclark@gmail.com" target="_blank">robdclark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robclark@freedesktop.org" target="_blank">robclark@freedesktop.org</a>><br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org" target="_blank">robclark@freedesktop.org</a>><br>
---<br>
src/compiler/nir/nir.h | 3 +-<br>
src/compiler/nir/nir_lower_io_to_temporaries.c | 56 +++++++++++++++++++++-----<br>
src/mesa/drivers/dri/i965/brw_nir.c | 4 +-<br>
3 files changed, 52 insertions(+), 11 deletions(-)<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 5410f0b..c96eaf9 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2279,7 +2279,8 @@ bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes);<br>
<br>
bool nir_lower_locals_to_regs(nir_shader *shader);<br>
<br>
-void nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint);<br>
+void nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,<br>
+ bool outputs, bool inputs);<br>
<br>
void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);<br>
<br>
diff --git a/src/compiler/nir/nir_lower_io_to_temporaries.c b/src/compiler/nir/nir_lower_io_to_temporaries.c<br>
index 9df2ba0..34e7477 100644<br>
--- a/src/compiler/nir/nir_lower_io_to_temporaries.c<br>
+++ b/src/compiler/nir/nir_lower_io_to_temporaries.c<br>
@@ -22,9 +22,12 @@<br>
*/<br>
<br>
/*<br>
- * Implements a pass that lowers output variables to a temporary plus an<br>
- * output variable with a single copy at each exit point of the shader.<br>
- * This way the output variable is only ever written.<br>
+ * Implements a pass that lowers output and/or input variables to a<br>
+ * temporary plus an output variable with a single copy at each exit<br>
+ * point of the shader and/or an input variable with a single copy<br>
+ * at the entrance point of the shader. This way the output variable<br>
+ * is only ever written once and/or input is only read once, and there<br>
+ * are no indirect outut/input accesses.<br>
*/<br>
<br>
#include "nir.h"<br>
@@ -33,6 +36,7 @@ struct lower_io_state {<br>
nir_shader *shader;<br>
nir_function *entrypoint;<br>
struct exec_list old_outputs;<br>
+ struct exec_list old_inputs;<br>
};<br>
<br>
static void<br>
@@ -49,7 +53,6 @@ emit_copies(nir_cursor cursor, nir_shader *shader, struct exec_list *new_vars,<br>
nir_intrinsic_instr_create(shader, nir_intrinsic_copy_var);<br>
copy->variables[0] = nir_deref_var_create(copy, newv);<br>
copy->variables[1] = nir_deref_var_create(copy, temp);<br>
-<br></blockquote><div><br></div><div>I don't think this was intended.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
nir_instr_insert(cursor, ©->instr);<br>
}<br>
}<br>
@@ -90,6 +93,20 @@ emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)<br>
}<br>
}<br>
<br>
+static void<br>
+emit_input_copies(nir_cursor cursor, struct lower_io_state *state)<br>
+{<br>
+ emit_copies(cursor, state->shader, &state->old_inputs, &state->shader->inputs);<br>
+}<br>
+<br>
+static void<br>
+emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)<br>
+{<br>
+ if (impl->function == state->entrypoint) {<br>
+ emit_input_copies(nir_before_block(nir_start_block(impl)), state);<br>
+ }<br>
+}<br></blockquote><div><br></div><div>I was questioning the need for the wrapper before, but this is a bit silly :-)<br><br></div><div>I think I'd like to see the extra layer of wrappers go if you're not too attached to them. and I had one other trivial change above. Other than that the lower_io_to_temporaries patches are<br><br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
static nir_variable *<br>
create_shadow_temp(struct lower_io_state *state, nir_variable *var)<br>
{<br>
@@ -105,8 +122,8 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)<br>
/* Reparent the constant initializer (if any) */<br>
ralloc_steal(nvar, nvar->constant_initializer);<br>
<br>
- /* Give the output a new name with @out-temp appended */<br>
- const char *mode = "out";<br>
+ /* Give the original a new name with @<mode>-temp appended */<br>
+ const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";<br>
temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);<br>
temp->data.mode = nir_var_global;<br>
temp->constant_initializer = NULL;<br>
@@ -115,7 +132,8 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)<br>
}<br>
<br>
void<br>
-nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)<br>
+nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,<br>
+ bool outputs, bool inputs)<br>
{<br>
struct lower_io_state state;<br>
<br>
@@ -124,7 +142,16 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)<br>
<br>
state.shader = shader;<br>
state.entrypoint = entrypoint;<br>
- exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);<br>
+<br>
+ if (inputs)<br>
+ exec_list_move_nodes_to(&shader->inputs, &state.old_inputs);<br>
+ else<br>
+ exec_list_make_empty(&state.old_inputs);<br>
+<br>
+ if (outputs)<br>
+ exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);<br>
+ else<br>
+ exec_list_make_empty(&state.old_outputs);<br>
<br>
/* Walk over all of the outputs turn each output into a temporary and<br>
* make a new variable for the actual output.<br>
@@ -134,15 +161,26 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)<br>
exec_list_push_tail(&shader->outputs, &output->node);<br>
}<br>
<br>
+ /* and same for inputs: */<br>
+ nir_foreach_variable(var, &state.old_inputs) {<br>
+ nir_variable *input = create_shadow_temp(&state, var);<br>
+ exec_list_push_tail(&shader->inputs, &input->node);<br>
+ }<br>
+<br>
nir_foreach_function(function, shader) {<br>
if (function->impl == NULL)<br>
continue;<br>
<br>
- emit_output_copies_impl(&state, function->impl);<br>
+ if (inputs)<br>
+ emit_input_copies_impl(&state, function->impl);<br>
+<br>
+ if (outputs)<br>
+ emit_output_copies_impl(&state, function->impl);<br>
<br>
nir_metadata_preserve(function->impl, nir_metadata_block_index |<br>
nir_metadata_dominance);<br>
}<br>
<br>
+ exec_list_append(&shader->globals, &state.old_inputs);<br>
exec_list_append(&shader->globals, &state.old_outputs);<br>
}<br>
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c<br>
index 2ed46ca..c5cc07e 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_nir.c<br>
+++ b/src/mesa/drivers/dri/i965/brw_nir.c<br>
@@ -553,7 +553,9 @@ brw_create_nir(struct brw_context *brw,<br>
/* First, lower the GLSL IR or Mesa IR to NIR */<br>
if (shader_prog) {<br>
nir = glsl_to_nir(shader_prog, stage, options);<br>
- OPT_V(nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir));<br>
+ OPT_V(nir_lower_io_to_temporaries,<br>
+ nir_shader_get_entrypoint(nir),<br>
+ true, false);<br>
} else {<br>
nir = prog_to_nir(prog, options);<br>
OPT_V(nir_convert_to_ssa); /* turn registers into SSA */<br>
<span><font color="#888888">--<br>
2.5.5<br>
<br>
</font></span></blockquote></div><br></div></div>