From 595f9d5476764ba29272549bac7ccd0000f459fe Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Fri, 25 Mar 2016 15:03:40 -0400 Subject: [PATCH] nir/lower-io: split out some helper fxns Prep work to reduce the noise in the next patch. Signed-off-by: Rob Clark Reviewed-by: Jason Ekstrand --- .../nir/nir_lower_io_to_temporaries.c | 122 ++++++++++-------- 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/src/compiler/nir/nir_lower_io_to_temporaries.c b/src/compiler/nir/nir_lower_io_to_temporaries.c index bf16aecfcf1..4b4363b8cde 100644 --- a/src/compiler/nir/nir_lower_io_to_temporaries.c +++ b/src/compiler/nir/nir_lower_io_to_temporaries.c @@ -31,29 +31,87 @@ 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, ©->instr); } } +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(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + if (intrin->intrinsic == nir_intrinsic_emit_vertex) { + nir_cursor cursor = nir_before_instr(&intrin->instr); + emit_copies(cursor, state->shader, &state->shader->outputs, + &state->old_outputs); + } + } + } + } 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; + nir_cursor cursor = nir_after_block_before_jump(block); + emit_copies(cursor, state->shader, &state->shader->outputs, + &state->old_outputs); + } + } +} + +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) { @@ -63,29 +121,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@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); } @@ -93,32 +136,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(block, function->impl) { - nir_foreach_instr(instr, block) { - if (instr->type != nir_instr_type_intrinsic) - continue; - - nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); - if (intrin->intrinsic == nir_intrinsic_emit_vertex) { - emit_output_copies(nir_before_instr(&intrin->instr), - &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);