nir/lower-io: add support for lowering inputs
Signed-off-by: Rob Clark <robclark@freedesktop.org> Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
@@ -2279,7 +2279,8 @@ bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes);
|
|||||||
|
|
||||||
bool nir_lower_locals_to_regs(nir_shader *shader);
|
bool nir_lower_locals_to_regs(nir_shader *shader);
|
||||||
|
|
||||||
void nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint);
|
void nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,
|
||||||
|
bool outputs, bool inputs);
|
||||||
|
|
||||||
void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
|
void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
|
||||||
|
|
||||||
|
@@ -22,9 +22,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Implements a pass that lowers output variables to a temporary plus an
|
* Implements a pass that lowers output and/or input variables to a
|
||||||
* output variable with a single copy at each exit point of the shader.
|
* temporary plus an output variable with a single copy at each exit
|
||||||
* This way the output variable is only ever written.
|
* 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"
|
#include "nir.h"
|
||||||
@@ -33,6 +36,7 @@ struct lower_io_state {
|
|||||||
nir_shader *shader;
|
nir_shader *shader;
|
||||||
nir_function *entrypoint;
|
nir_function *entrypoint;
|
||||||
struct exec_list old_outputs;
|
struct exec_list old_outputs;
|
||||||
|
struct exec_list old_inputs;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -88,6 +92,16 @@ emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
|
||||||
|
{
|
||||||
|
if (impl->function == state->entrypoint) {
|
||||||
|
nir_cursor cursor = nir_before_block(nir_start_block(impl));
|
||||||
|
emit_copies(cursor, state->shader, &state->old_inputs,
|
||||||
|
&state->shader->inputs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static nir_variable *
|
static nir_variable *
|
||||||
create_shadow_temp(struct lower_io_state *state, nir_variable *var)
|
create_shadow_temp(struct lower_io_state *state, nir_variable *var)
|
||||||
{
|
{
|
||||||
@@ -103,8 +117,8 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
|
|||||||
/* Reparent the constant initializer (if any) */
|
/* Reparent the constant initializer (if any) */
|
||||||
ralloc_steal(nvar, nvar->constant_initializer);
|
ralloc_steal(nvar, nvar->constant_initializer);
|
||||||
|
|
||||||
/* Give the output a new name with @out-temp appended */
|
/* Give the original a new name with @<mode>-temp appended */
|
||||||
const char *mode = "out";
|
const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
|
||||||
temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
|
temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
|
||||||
temp->data.mode = nir_var_global;
|
temp->data.mode = nir_var_global;
|
||||||
temp->constant_initializer = NULL;
|
temp->constant_initializer = NULL;
|
||||||
@@ -113,7 +127,8 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
|
nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,
|
||||||
|
bool outputs, bool inputs)
|
||||||
{
|
{
|
||||||
struct lower_io_state state;
|
struct lower_io_state state;
|
||||||
|
|
||||||
@@ -122,7 +137,16 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
|
|||||||
|
|
||||||
state.shader = shader;
|
state.shader = shader;
|
||||||
state.entrypoint = entrypoint;
|
state.entrypoint = entrypoint;
|
||||||
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
|
/* Walk over all of the outputs turn each output into a temporary and
|
||||||
* make a new variable for the actual output.
|
* make a new variable for the actual output.
|
||||||
@@ -132,15 +156,26 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint)
|
|||||||
exec_list_push_tail(&shader->outputs, &output->node);
|
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(function, shader) {
|
nir_foreach_function(function, shader) {
|
||||||
if (function->impl == NULL)
|
if (function->impl == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
emit_output_copies_impl(&state, function->impl);
|
if (inputs)
|
||||||
|
emit_input_copies_impl(&state, function->impl);
|
||||||
|
|
||||||
|
if (outputs)
|
||||||
|
emit_output_copies_impl(&state, function->impl);
|
||||||
|
|
||||||
nir_metadata_preserve(function->impl, nir_metadata_block_index |
|
nir_metadata_preserve(function->impl, nir_metadata_block_index |
|
||||||
nir_metadata_dominance);
|
nir_metadata_dominance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exec_list_append(&shader->globals, &state.old_inputs);
|
||||||
exec_list_append(&shader->globals, &state.old_outputs);
|
exec_list_append(&shader->globals, &state.old_outputs);
|
||||||
}
|
}
|
||||||
|
@@ -563,7 +563,9 @@ brw_create_nir(struct brw_context *brw,
|
|||||||
/* First, lower the GLSL IR or Mesa IR to NIR */
|
/* First, lower the GLSL IR or Mesa IR to NIR */
|
||||||
if (shader_prog) {
|
if (shader_prog) {
|
||||||
nir = glsl_to_nir(shader_prog, stage, options);
|
nir = glsl_to_nir(shader_prog, stage, options);
|
||||||
OPT_V(nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir));
|
OPT_V(nir_lower_io_to_temporaries,
|
||||||
|
nir_shader_get_entrypoint(nir),
|
||||||
|
true, false);
|
||||||
} else {
|
} else {
|
||||||
nir = prog_to_nir(prog, options);
|
nir = prog_to_nir(prog, options);
|
||||||
OPT_V(nir_convert_to_ssa); /* turn registers into SSA */
|
OPT_V(nir_convert_to_ssa); /* turn registers into SSA */
|
||||||
|
Reference in New Issue
Block a user