Commit 04dbf709 authored by Timothy Arceri's avatar Timothy Arceri Committed by Marge Bot

nir: add callback to nir_remove_dead_variables()

This allows us to do API specific checks before removing variable
without filling nir_remove_dead_variables() with API specific code.

In the following patches we will use this to support the removal
of dead uniforms in GLSL.
Reviewed-by: Kenneth Graunke's avatarKenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt's avatarEric Anholt <eric@anholt.net>
Part-of: <!4797>
parent bc79442f
......@@ -2230,9 +2230,9 @@ radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders)
radv_optimize_nir(ordered_shaders[i - 1], false, false);
nir_remove_dead_variables(ordered_shaders[i],
nir_var_shader_out);
nir_var_shader_out, NULL);
nir_remove_dead_variables(ordered_shaders[i - 1],
nir_var_shader_in);
nir_var_shader_in, NULL);
bool progress = nir_remove_unused_varyings(ordered_shaders[i],
ordered_shaders[i - 1]);
......
......@@ -226,7 +226,8 @@ radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively,
NIR_PASS(progress, shader, nir_opt_copy_prop_vars);
NIR_PASS(progress, shader, nir_opt_dead_write_vars);
NIR_PASS(progress, shader, nir_remove_dead_variables,
nir_var_function_temp | nir_var_shader_in | nir_var_shader_out);
nir_var_function_temp | nir_var_shader_in | nir_var_shader_out,
NULL);
NIR_PASS_V(shader, nir_lower_alu_to_scalar, NULL, NULL);
NIR_PASS_V(shader, nir_lower_phis_to_scalar);
......@@ -458,7 +459,8 @@ radv_shader_compile_to_nir(struct radv_device *device,
NIR_PASS_V(nir, nir_lower_input_attachments, true);
NIR_PASS_V(nir, nir_remove_dead_variables,
nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared);
nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared,
NULL);
NIR_PASS_V(nir, nir_propagate_invariant);
......@@ -499,7 +501,7 @@ radv_shader_compile_to_nir(struct radv_device *device,
nir_split_var_copies(nir);
nir_lower_global_vars_to_local(nir);
nir_remove_dead_variables(nir, nir_var_function_temp);
nir_remove_dead_variables(nir, nir_var_function_temp, NULL);
bool gfx7minus = device->physical_device->rad_info.chip_class <= GFX7;
nir_lower_subgroups(nir, &(struct nir_lower_subgroups_options) {
.subgroup_size = subgroup_size,
......
......@@ -808,7 +808,7 @@ v3d_nir_lower_vs_early(struct v3d_compile *c)
&c->s->outputs, used_outputs, NULL); /* demotes to globals */
NIR_PASS_V(c->s, nir_lower_global_vars_to_local);
v3d_optimize_nir(c->s);
NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in, NULL);
/* This must go before nir_lower_io */
if (c->vs_key->per_vertex_point_size)
......@@ -839,7 +839,7 @@ v3d_nir_lower_gs_early(struct v3d_compile *c)
&c->s->outputs, used_outputs, NULL); /* demotes to globals */
NIR_PASS_V(c->s, nir_lower_global_vars_to_local);
v3d_optimize_nir(c->s);
NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in, NULL);
/* This must go before nir_lower_io */
if (c->gs_key->per_vertex_point_size)
......
......@@ -4083,7 +4083,8 @@ bool nir_lower_vars_to_ssa(nir_shader *shader);
bool nir_remove_dead_derefs(nir_shader *shader);
bool nir_remove_dead_derefs_impl(nir_function_impl *impl);
bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes);
bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
bool (*can_remove_var)(nir_variable *var));
bool nir_lower_variable_initializers(nir_shader *shader,
nir_variable_mode modes);
......
......@@ -143,11 +143,15 @@ remove_dead_var_writes(nir_shader *shader, struct set *live)
}
static bool
remove_dead_vars(struct exec_list *var_list, struct set *live)
remove_dead_vars(struct exec_list *var_list, struct set *live,
bool (*can_remove_var)(nir_variable *var))
{
bool progress = false;
foreach_list_typed_safe(nir_variable, var, node, var_list) {
if (can_remove_var && !can_remove_var(var))
continue;
struct set_entry *entry = _mesa_set_search(live, var);
if (entry == NULL) {
/* Mark this variable as used by setting the mode to 0 */
......@@ -161,35 +165,49 @@ remove_dead_vars(struct exec_list *var_list, struct set *live)
}
bool
nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
bool (*can_remove_var)(nir_variable *var))
{
bool progress = false;
struct set *live = _mesa_pointer_set_create(NULL);
add_var_use_shader(shader, live, modes);
if (modes & nir_var_uniform)
progress = remove_dead_vars(&shader->uniforms, live) || progress;
if (modes & nir_var_uniform) {
progress = remove_dead_vars(&shader->uniforms, live, can_remove_var) ||
progress;
}
if (modes & nir_var_shader_in)
progress = remove_dead_vars(&shader->inputs, live) || progress;
if (modes & nir_var_shader_in) {
progress = remove_dead_vars(&shader->inputs, live, can_remove_var) ||
progress;
}
if (modes & nir_var_shader_out)
progress = remove_dead_vars(&shader->outputs, live) || progress;
if (modes & nir_var_shader_out) {
progress = remove_dead_vars(&shader->outputs, live, can_remove_var) ||
progress;
}
if (modes & nir_var_shader_temp)
progress = remove_dead_vars(&shader->globals, live) || progress;
if (modes & nir_var_shader_temp) {
progress = remove_dead_vars(&shader->globals, live, can_remove_var) ||
progress;
}
if (modes & nir_var_system_value)
progress = remove_dead_vars(&shader->system_values, live) || progress;
if (modes & nir_var_system_value) {
progress = remove_dead_vars(&shader->system_values, live,
can_remove_var) || progress;
}
if (modes & nir_var_mem_shared)
progress = remove_dead_vars(&shader->shared, live) || progress;
if (modes & nir_var_mem_shared) {
progress = remove_dead_vars(&shader->shared, live, can_remove_var) ||
progress;
}
if (modes & nir_var_function_temp) {
nir_foreach_function(function, shader) {
if (function->impl) {
if (remove_dead_vars(&function->impl->locals, live))
if (remove_dead_vars(&function->impl->locals, live,
can_remove_var))
progress = true;
}
}
......
......@@ -5324,7 +5324,7 @@ spirv_to_nir(const uint32_t *words, size_t word_count,
*/
nir_lower_variable_initializers(b->shader, nir_var_shader_out);
nir_remove_dead_variables(b->shader,
nir_var_shader_in | nir_var_shader_out);
nir_var_shader_in | nir_var_shader_out, NULL);
/* We sometimes generate bogus derefs that, while never used, give the
* validator a bit of heartburn. Run dead code to get rid of them.
......
......@@ -352,7 +352,7 @@ ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
OPT_V(s, nir_opt_cse);
}
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp);
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
OPT_V(s, nir_opt_sink, nir_move_const_undef);
......
......@@ -590,7 +590,8 @@ tu_shader_create(struct tu_device *dev,
NIR_PASS_V(nir, nir_split_per_member_structs);
NIR_PASS_V(nir, nir_remove_dead_variables,
nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared);
nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared,
NULL);
/* Gather information for transform feedback.
* This should be called after nir_split_per_member_structs.
......
......@@ -1909,7 +1909,7 @@ bool lp_build_nir_llvm(
nir_convert_from_ssa(nir, true);
nir_lower_locals_to_regs(nir);
nir_remove_dead_derefs(nir);
nir_remove_dead_variables(nir, nir_var_function_temp);
nir_remove_dead_variables(nir, nir_var_function_temp, NULL);
nir_foreach_variable(variable, &nir->outputs)
handle_shader_output_decl(bld_base, nir, variable);
......
......@@ -839,7 +839,7 @@ etna_compile_shader_nir(struct etna_shader_variant *v)
while( OPT(s, nir_opt_vectorize) );
OPT_V(s, nir_lower_alu_to_scalar, etna_alu_to_scalar_filter_cb, specs);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
NIR_PASS_V(s, nir_opt_algebraic_late);
NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
......
......@@ -122,7 +122,7 @@ ir2_optimize_nir(nir_shader *s, bool lower)
ir2_optimize_loop(s);
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp);
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
OPT_V(s, nir_opt_sink, nir_move_const_undef);
/* TODO we dont want to get shaders writing to depth for depth textures */
......
......@@ -138,7 +138,7 @@ lima_program_optimize_vs_nir(struct nir_shader *s)
NIR_PASS_V(s, nir_opt_dce);
NIR_PASS_V(s, nir_lower_locals_to_regs);
NIR_PASS_V(s, nir_convert_from_ssa, true);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
nir_sweep(s);
}
......@@ -243,7 +243,7 @@ lima_program_optimize_fs_nir(struct nir_shader *s,
NIR_PASS_V(s, nir_lower_locals_to_regs);
NIR_PASS_V(s, nir_convert_from_ssa, true);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
NIR_PASS_V(s, nir_lower_vec_to_movs);
......
......@@ -3239,7 +3239,7 @@ Converter::run()
NIR_PASS_V(nir, nir_lower_bool_to_int32);
NIR_PASS_V(nir, nir_lower_locals_to_regs);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
NIR_PASS_V(nir, nir_convert_from_ssa, true);
// Garbage collect dead instructions
......
......@@ -635,8 +635,8 @@ int r600_shader_from_nir(struct r600_context *rctx,
if (optimize)
while(optimize_once(sel->nir));
NIR_PASS_V(sel->nir, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(sel->nir, nir_remove_dead_variables, nir_var_shader_out);
NIR_PASS_V(sel->nir, nir_remove_dead_variables, nir_var_shader_in, NULL);
NIR_PASS_V(sel->nir, nir_remove_dead_variables, nir_var_shader_out, NULL);
NIR_PASS_V(sel->nir, nir_lower_vars_to_scratch,
......
......@@ -934,7 +934,7 @@ static void si_lower_nir(struct si_screen *sscreen, struct nir_shader *nir)
si_nir_opts(nir);
NIR_PASS_V(nir, nir_lower_bool_to_int32);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
if (sscreen->debug_flags & DBG(FS_CORRECT_DERIVS_AFTER_KILL))
NIR_PASS_V(nir, nir_lower_discard_to_demote);
......
......@@ -322,7 +322,7 @@ v3d_uncompiled_shader_create(struct pipe_context *pctx,
v3d_optimize_nir(s);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
/* Garbage collect dead instructions */
nir_sweep(s);
......
......@@ -2482,7 +2482,7 @@ vc4_shader_state_create(struct pipe_context *pctx,
vc4_optimize_nir(s);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
/* Garbage collect dead instructions */
nir_sweep(s);
......
......@@ -140,7 +140,7 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
NIR_PASS_V(nir, nir_lower_clip_halfz);
NIR_PASS_V(nir, nir_lower_regs_to_ssa);
optimize_nir(nir);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
NIR_PASS_V(nir, lower_discard_if);
NIR_PASS_V(nir, nir_convert_from_ssa, true);
......
......@@ -191,7 +191,7 @@ blorp_compile_fs(struct blorp_context *blorp, void *mem_ctx,
wm_prog_data->base.binding_table.texture_start = BLORP_TEXTURE_BT_INDEX;
brw_preprocess_nir(compiler, nir, NULL);
nir_remove_dead_variables(nir, nir_var_shader_in);
nir_remove_dead_variables(nir, nir_var_shader_in, NULL);
nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
if (blorp->compiler->devinfo->gen < 6) {
......
......@@ -597,7 +597,7 @@ brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
/* Workaround Gfxbench unused local sampler variable which will trigger an
* assert in the opt_large_constants pass.
*/
OPT(nir_remove_dead_variables, nir_var_function_temp);
OPT(nir_remove_dead_variables, nir_var_function_temp, NULL);
}
static unsigned
......@@ -785,8 +785,8 @@ brw_nir_link_shaders(const struct brw_compiler *compiler,
if (nir_link_opt_varyings(producer, consumer))
brw_nir_optimize(consumer, compiler, c_is_scalar, false);
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
if (nir_remove_unused_varyings(producer, consumer)) {
NIR_PASS_V(producer, nir_lower_global_vars_to_local);
......
......@@ -282,7 +282,8 @@ anv_shader_compile_to_nir(struct anv_device *device,
NIR_PASS_V(nir, nir_split_per_member_structs);
NIR_PASS_V(nir, nir_remove_dead_variables,
nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
nir_var_shader_in | nir_var_shader_out | nir_var_system_value,
NULL);
NIR_PASS_V(nir, nir_propagate_invariant);
NIR_PASS_V(nir, nir_lower_io_to_temporaries,
......
......@@ -106,7 +106,8 @@ brw_create_nir(struct brw_context *brw,
}
assert (nir);
nir_remove_dead_variables(nir, nir_var_shader_in | nir_var_shader_out);
nir_remove_dead_variables(nir, nir_var_shader_in | nir_var_shader_out,
NULL);
nir_validate_shader(nir, "after glsl_to_nir or spirv_to_nir");
NIR_PASS_V(nir, nir_lower_io_to_temporaries,
nir_shader_get_entrypoint(nir), true, false);
......
......@@ -260,7 +260,8 @@ st_nir_opts(nir_shader *nir)
NIR_PASS(progress, nir, nir_remove_dead_variables,
(nir_variable_mode)(nir_var_function_temp |
nir_var_shader_temp |
nir_var_mem_shared));
nir_var_mem_shared),
NULL);
NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
NIR_PASS(progress, nir, nir_opt_dead_write_vars);
......@@ -378,7 +379,7 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
if (!_mesa_is_gles(st->ctx) || !nir->info.separate_shader) {
nir_variable_mode mask =
(nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
nir_remove_dead_variables(nir, mask);
nir_remove_dead_variables(nir, mask, NULL);
}
if (options->lower_all_io_to_temps ||
......@@ -503,7 +504,7 @@ st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
nir_variable_mode mask = (nir_variable_mode)
(nir_var_shader_in | nir_var_shader_out | nir_var_function_temp );
nir_remove_dead_variables(nir, mask);
nir_remove_dead_variables(nir, mask, NULL);
if (!st->has_hw_atomics)
NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);
......@@ -561,8 +562,8 @@ st_nir_link_shaders(nir_shader *producer, nir_shader *consumer)
if (nir_link_opt_varyings(producer, consumer))
st_nir_opts(consumer);
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
if (nir_remove_unused_varyings(producer, consumer)) {
NIR_PASS_V(producer, nir_lower_global_vars_to_local);
......@@ -575,8 +576,10 @@ st_nir_link_shaders(nir_shader *producer, nir_shader *consumer)
* nir_compact_varyings() depends on all dead varyings being removed so
* we need to call nir_remove_dead_variables() again here.
*/
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out,
NULL);
NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in,
NULL);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment