From f917814c14b4910929da498aa5cce790fa2ec8ec Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Tue, 20 Mar 2018 14:16:06 -0700 Subject: [PATCH] nir: Support deref instructions in remove_dead_variables Reviewed-by: Caio Marcelo de Oliveira Filho Acked-by: Rob Clark Acked-by: Bas Nieuwenhuizen Acked-by: Dave Airlie Reviewed-by: Kenneth Graunke --- src/compiler/nir/nir_remove_dead_variables.c | 94 +++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/src/compiler/nir/nir_remove_dead_variables.c b/src/compiler/nir/nir_remove_dead_variables.c index 4a36ef9ba52..89e544f9e1f 100644 --- a/src/compiler/nir/nir_remove_dead_variables.c +++ b/src/compiler/nir/nir_remove_dead_variables.c @@ -27,6 +27,55 @@ #include "nir.h" +static bool +deref_used_for_not_store(nir_deref_instr *deref) +{ + nir_foreach_use(src, &deref->dest.ssa) { + switch (src->parent_instr->type) { + case nir_instr_type_deref: + if (deref_used_for_not_store(nir_instr_as_deref(src->parent_instr))) + return true; + break; + + case nir_instr_type_intrinsic: { + nir_intrinsic_instr *intrin = + nir_instr_as_intrinsic(src->parent_instr); + /* The first source of copy and store intrinsics is the deref to + * write. Don't record those. + */ + if ((intrin->intrinsic != nir_intrinsic_store_deref && + intrin->intrinsic != nir_intrinsic_copy_deref) || + src != &intrin->src[0]) + return true; + break; + } + + default: + /* If it's used by any other instruction type (most likely a texture + * instruction), consider it used. + */ + return true; + } + } + + return false; +} + +static void +add_var_use_deref(nir_deref_instr *deref, struct set *live) +{ + if (deref->deref_type != nir_deref_type_var) + return; + + /* If it's not a local that never escapes the shader, then any access at + * all means we need to keep it alive. + */ + assert(deref->mode == deref->var->data.mode); + if (!(deref->mode & (nir_var_local | nir_var_global | nir_var_shared)) || + deref_used_for_not_store(deref)) + _mesa_set_add(live, deref->var); +} + static void add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live, nir_variable_mode modes) @@ -100,6 +149,10 @@ add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes nir_foreach_block(block, function->impl) { nir_foreach_instr(instr, block) { switch(instr->type) { + case nir_instr_type_deref: + add_var_use_deref(nir_instr_as_deref(instr), live); + break; + case nir_instr_type_intrinsic: add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live, modes); @@ -144,6 +197,45 @@ remove_dead_var_writes(nir_shader *shader, struct set *live) nir_instr_remove(instr); } } + + nir_foreach_block(block, function->impl) { + nir_foreach_instr_safe(instr, block) { + switch (instr->type) { + case nir_instr_type_deref: { + nir_deref_instr *deref = nir_instr_as_deref(instr); + + nir_variable_mode parent_mode; + if (deref->deref_type == nir_deref_type_var) + parent_mode = deref->var->data.mode; + else + parent_mode = nir_deref_instr_parent(deref)->mode; + + /* If the parent mode is 0, then it references a dead variable. + * Flag this deref as dead and remove it. + */ + if (parent_mode == 0) { + deref->mode = 0; + nir_instr_remove(&deref->instr); + } + break; + } + + case nir_instr_type_intrinsic: { + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + if (intrin->intrinsic != nir_intrinsic_copy_deref && + intrin->intrinsic != nir_intrinsic_store_deref) + break; + + if (nir_src_as_deref(intrin->src[0])->mode == 0) + nir_instr_remove(instr); + break; + } + + default: + break; /* Nothing to do */ + } + } + } } } @@ -172,8 +264,6 @@ nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes) struct set *live = _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); - nir_assert_lowered_derefs(shader, nir_lower_all_derefs); - add_var_use_shader(shader, live, modes); if (modes & nir_var_uniform) -- 2.30.2