mesa/st: Use nir_shader_instructions_pass for st_nir_lower_builtin

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16482>
This commit is contained in:
Jason Ekstrand 2022-05-12 14:04:14 -05:00 committed by Marge Bot
parent e16197c46e
commit 627d58099c
1 changed files with 59 additions and 93 deletions

View File

@ -64,12 +64,6 @@
#include "uniforms.h"
#include "program/prog_instruction.h"
typedef struct {
nir_shader *shader;
nir_builder builder;
void *mem_ctx;
} lower_builtin_state;
static const struct gl_builtin_uniform_element *
get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
{
@ -97,10 +91,10 @@ get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
}
static nir_variable *
get_variable(lower_builtin_state *state, nir_deref_path *path,
get_variable(nir_builder *b, nir_deref_path *path,
const struct gl_builtin_uniform_element *element)
{
nir_shader *shader = state->shader;
nir_shader *shader = b->shader;
gl_state_index16 tokens[STATE_LENGTH];
int idx = 1;
@ -163,35 +157,30 @@ get_variable(lower_builtin_state *state, nir_deref_path *path,
}
static bool
lower_builtin_block(lower_builtin_state *state, nir_block *block)
lower_builtin_instr(nir_builder *b, nir_instr *instr, UNUSED void *_data)
{
nir_builder *b = &state->builder;
bool progress = false;
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
return false;
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
if (intrin->intrinsic != nir_intrinsic_load_deref)
continue;
return false;
nir_variable *var =
nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
if (var->data.mode != nir_var_uniform)
continue;
return false;
/* built-in's will always start with "gl_" */
if (strncmp(var->name, "gl_", 3) != 0)
continue;
return false;
const struct gl_builtin_uniform_desc *desc =
_mesa_glsl_get_builtin_uniform_desc(var->name);
/* if no descriptor, it isn't something we need to handle specially: */
if (!desc)
continue;
return false;
nir_deref_path path;
nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
@ -201,7 +190,7 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
/* matrix elements (array_deref) do not need special handling: */
if (!element) {
nir_deref_path_finish(&path);
continue;
return false;
}
/* remove existing var from uniform list: */
@ -211,7 +200,7 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
*/
exec_node_self_link(&var->node);
nir_variable *new_var = get_variable(state, &path, element);
nir_variable *new_var = get_variable(b, &path, element);
nir_deref_path_finish(&path);
b->cursor = nir_before_instr(instr);
@ -237,37 +226,14 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
*/
nir_instr_remove(&intrin->instr);
progress = true;
}
return progress;
}
static void
lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
{
nir_builder_init(&state->builder, impl);
state->mem_ctx = ralloc_parent(impl);
bool progress = false;
nir_foreach_block(block, impl) {
progress |= lower_builtin_block(state, block);
}
if (progress)
nir_remove_dead_derefs_impl(impl);
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
return true;
}
void
st_nir_lower_builtin(nir_shader *shader)
{
lower_builtin_state state;
state.shader = shader;
nir_foreach_function(function, shader) {
if (function->impl)
lower_builtin_impl(&state, function->impl);
}
if (nir_shader_instructions_pass(shader, lower_builtin_instr,
nir_metadata_block_index |
nir_metadata_dominance, NULL))
nir_remove_dead_derefs(shader);
}