zink: preserve/merge variable names when generating new variables

in the case where multiple variables get merged into one, try to use
all the names when creating new vars

Acked-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Timur Kristóf <timur.kristof@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28814>
This commit is contained in:
Mike Blumenkrantz 2024-04-18 10:56:50 -04:00 committed by Marge Bot
parent cb597cb85e
commit 588c762936
1 changed files with 32 additions and 3 deletions

View File

@ -5234,6 +5234,7 @@ struct rework_io_state {
nir_variable_mode mode; nir_variable_mode mode;
gl_shader_stage stage; gl_shader_stage stage;
nir_shader *nir; nir_shader *nir;
const char *name;
/* these are found by scanning */ /* these are found by scanning */
bool arrayed_io; bool arrayed_io;
@ -5246,6 +5247,8 @@ struct rework_io_state {
unsigned bit_size; unsigned bit_size;
unsigned base; unsigned base;
nir_alu_type type; nir_alu_type type;
/* must be last */
char *newname;
}; };
/* match an existing variable against the rework state */ /* match an existing variable against the rework state */
@ -5272,6 +5275,26 @@ find_rework_var(nir_shader *nir, struct rework_io_state *ris)
return NULL; return NULL;
} }
static void
update_io_var_name(struct rework_io_state *ris, const char *name)
{
if (!(zink_debug & (ZINK_DEBUG_NIR | ZINK_DEBUG_SPIRV)))
return;
if (!name)
return;
if (ris->name && !strcmp(ris->name, name))
return;
if (ris->newname && !strcmp(ris->newname, name))
return;
if (ris->newname) {
ris->newname = ralloc_asprintf(ris->nir, "%s_%s", ris->newname, name);
} else if (ris->name) {
ris->newname = ralloc_asprintf(ris->nir, "%s_%s", ris->name, name);
} else {
ris->newname = ralloc_strdup(ris->nir, name);
}
}
/* check/update tracking state for variable info */ /* check/update tracking state for variable info */
static void static void
update_io_var_state(nir_intrinsic_instr *intr, struct rework_io_state *ris) update_io_var_state(nir_intrinsic_instr *intr, struct rework_io_state *ris)
@ -5340,6 +5363,8 @@ update_io_var_state(nir_intrinsic_instr *intr, struct rework_io_state *ris)
ris->type = type; ris->type = type;
} }
update_io_var_name(ris, intr->name);
ris->medium_precision |= sem.medium_precision; ris->medium_precision |= sem.medium_precision;
ris->fb_fetch_output |= sem.fb_fetch_output; ris->fb_fetch_output |= sem.fb_fetch_output;
ris->dual_source_blend_index |= sem.dual_source_blend_index; ris->dual_source_blend_index |= sem.dual_source_blend_index;
@ -5400,6 +5425,7 @@ scan_io_var_slot(nir_shader *nir, nir_variable_mode mode, unsigned location, boo
struct rework_io_state test; struct rework_io_state test;
do { do {
update_io_var_name(&test, ris.newname ? ris.newname : ris.name);
test = ris; test = ris;
/* always run indirect scan first to detect potential overlaps */ /* always run indirect scan first to detect potential overlaps */
if (scan_indirects) { if (scan_indirects) {
@ -5409,7 +5435,7 @@ scan_io_var_slot(nir_shader *nir, nir_variable_mode mode, unsigned location, boo
ris.indirect_only = false; ris.indirect_only = false;
nir_shader_intrinsics_pass(nir, scan_io_var_usage, nir_metadata_all, &ris); nir_shader_intrinsics_pass(nir, scan_io_var_usage, nir_metadata_all, &ris);
/* keep scanning until no changes found */ /* keep scanning until no changes found */
} while (memcmp(&ris, &test, sizeof(ris))); } while (memcmp(&ris, &test, offsetof(struct rework_io_state, newname)));
return ris; return ris;
} }
@ -5419,8 +5445,10 @@ create_io_var(nir_shader *nir, struct rework_io_state *ris)
{ {
char name[1024]; char name[1024];
assert(ris->component_mask); assert(ris->component_mask);
if (ris->newname || ris->name) {
snprintf(name, sizeof(name), "%s", ris->newname ? ris->newname : ris->name);
/* always use builtin name where possible */ /* always use builtin name where possible */
if (nir->info.stage == MESA_SHADER_VERTEX && ris->mode == nir_var_shader_in) { } else if (nir->info.stage == MESA_SHADER_VERTEX && ris->mode == nir_var_shader_in) {
snprintf(name, sizeof(name), "%s", gl_vert_attrib_name(ris->location)); snprintf(name, sizeof(name), "%s", gl_vert_attrib_name(ris->location));
} else if (nir->info.stage == MESA_SHADER_FRAGMENT && ris->mode == nir_var_shader_out) { } else if (nir->info.stage == MESA_SHADER_FRAGMENT && ris->mode == nir_var_shader_out) {
snprintf(name, sizeof(name), "%s", gl_frag_result_name(ris->location)); snprintf(name, sizeof(name), "%s", gl_frag_result_name(ris->location));
@ -5562,7 +5590,8 @@ rework_io_vars(nir_shader *nir, nir_variable_mode mode, struct zink_shader *zs)
.stage = nir->info.stage, .stage = nir->info.stage,
.bit_size = 32, .bit_size = 32,
.component_mask = component_mask, .component_mask = component_mask,
.type = nir_type_float32 .type = nir_type_float32,
.newname = scan_io_var_slot(nir, nir_var_shader_in, slot, false).newname,
}; };
create_io_var(nir, &ris); create_io_var(nir, &ris);
inputs_read &= ~BITFIELD64_BIT(slot); inputs_read &= ~BITFIELD64_BIT(slot);