nir: add nir_variable_{set,append,steal}_name{f}() to modify nir_variable names

Setting variable names currently always uses ralloc, but the new
nir_variable_* helpers will mostly eliminate ralloc/malloc in a later
commit.

This just updates all places that touch nir_variable names to use the new
helpers.

Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36538>
This commit is contained in:
Marek Olšák 2025-08-01 18:43:32 -04:00 committed by Marge Bot
parent 05749922b0
commit 96ffc24e4e
21 changed files with 79 additions and 42 deletions

View file

@ -218,7 +218,7 @@ add_temp_var(nir_builder *b, char *name, const struct glsl_type *type)
{
nir_variable *var = rzalloc(b->shader, nir_variable);
var->type = type;
var->name = ralloc_strdup(var, name);
nir_variable_set_name(var, name);
var->data.mode = nir_var_function_temp;
nir_function_impl_add_variable(b->impl, var);

View file

@ -136,7 +136,7 @@ gl_nir_lower_discard_flow(nir_shader *shader)
nir_function_impl *main = nir_shader_get_entrypoint(shader);
nir_variable *discarded = rzalloc(shader, nir_variable);
discarded->name = ralloc_strdup(discarded, "discarded");
nir_variable_set_name(discarded, "discarded");
discarded->type = glsl_bool_type();
discarded->data.mode = nir_var_shader_temp;

View file

@ -270,7 +270,7 @@ lower_named_interface_blocks(struct gl_linked_shader *sh)
glsl_get_struct_field_data(iface_t, i);
nir_variable *new_var = rzalloc(sh->Program->nir, nir_variable);
new_var->name = ralloc_strdup(new_var, field_name);
nir_variable_set_name(new_var, field_name);
if (!glsl_type_is_array(var->type)) {
new_var->type = glsl_get_struct_field(iface_t, i);
} else {

View file

@ -271,7 +271,7 @@ create_or_update_packed_varying(struct lower_packed_varyings_state *state,
assert(name);
nir_variable *packed_var = rzalloc(state->shader, nir_variable);
packed_var->name = ralloc_asprintf(packed_var, "packed:%s", name);
nir_variable_set_namef(packed_var, "packed:%s", name);
packed_var->data.mode = state->mode;
bool is_interpolation_flat =
@ -316,7 +316,7 @@ create_or_update_packed_varying(struct lower_packed_varyings_state *state,
*/
if (state->gs_input_vertices == 0 || vertex_index == 0) {
assert(name);
ralloc_asprintf_append((char **) &var->name, ",%s", name);
nir_variable_append_namef(var, ",%s", name);
}
}
}

View file

@ -447,7 +447,7 @@ nir_visitor::visit(ir_variable *ir)
nir_variable *var = rzalloc(shader, nir_variable);
var->type = ir->type;
var->name = ralloc_strdup(var, ir->name);
nir_variable_set_name(var, ir->name);
var->data.assigned = ir->data.assigned;
var->data.read_only = ir->data.read_only;

View file

@ -267,12 +267,50 @@ nir_shader_add_variable(nir_shader *shader, nir_variable *var)
exec_list_push_tail(&shader->variables, &var->node);
}
void
nir_variable_set_name(nir_variable *var, const char *name)
{
if (var->name)
ralloc_free(var->name);
var->name = ralloc_strdup(var, name);
}
void
nir_variable_set_namef(nir_variable *var, const char *fmt, ...)
{
if (var->name)
ralloc_free(var->name);
va_list args;
va_start(args, fmt);
var->name = ralloc_vasprintf(var, fmt, args);
va_end(args);
}
void
nir_variable_append_namef(nir_variable *var, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
ralloc_vasprintf_append(&var->name, fmt, args);
va_end(args);
}
void
nir_variable_steal_name(nir_variable *dst, nir_variable *src)
{
ralloc_steal(dst, src->name);
dst->name = src->name;
src->name = NULL;
}
nir_variable *
nir_variable_create(nir_shader *shader, nir_variable_mode mode,
const struct glsl_type *type, const char *name)
{
nir_variable *var = rzalloc(shader, nir_variable);
var->name = ralloc_strdup(var, name);
nir_variable_set_name(var, name);
var->type = type;
var->data.mode = mode;
var->data.how_declared = nir_var_declared_normally;
@ -297,7 +335,7 @@ nir_local_variable_create(nir_function_impl *impl,
const struct glsl_type *type, const char *name)
{
nir_variable *var = rzalloc(impl->function->shader, nir_variable);
var->name = ralloc_strdup(var, name);
nir_variable_set_name(var, name);
var->type = type;
var->data.mode = nir_var_function_temp;

View file

@ -3942,6 +3942,11 @@ nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
exec_list_push_tail(&impl->locals, &var->node);
}
void nir_variable_set_name(nir_variable *var, const char *name);
void nir_variable_set_namef(nir_variable *var, const char *fmt, ...) PRINTFLIKE(2, 3);
void nir_variable_append_namef(nir_variable *var, const char *fmt, ...) PRINTFLIKE(2, 3);
void nir_variable_steal_name(nir_variable *dst, nir_variable *src);
/** creates a variable, sets a few defaults, and adds it to the list */
nir_variable *nir_variable_create(nir_shader *shader,
nir_variable_mode mode,

View file

@ -154,7 +154,7 @@ nir_variable_clone(const nir_variable *var, nir_shader *shader)
nir_variable *nvar = rzalloc(shader, nir_variable);
nvar->type = var->type;
nvar->name = ralloc_strdup(nvar, var->name);
nir_variable_set_name(nvar, var->name);
nvar->data = var->data;
nvar->num_state_slots = var->num_state_slots;
if (var->num_state_slots) {

View file

@ -54,7 +54,7 @@ create_clipdist_var(nir_shader *shader,
var->data.mode = nir_var_shader_in;
shader->num_inputs += MAX2(1, DIV_ROUND_UP(array_size, 4));
}
var->name = ralloc_asprintf(var, "clipdist_%d", slot - VARYING_SLOT_CLIP_DIST0);
nir_variable_set_namef(var, "clipdist_%d", slot - VARYING_SLOT_CLIP_DIST0);
var->data.index = 0;
var->data.location = slot;

View file

@ -147,7 +147,7 @@ replace_var_declaration(struct lower_distance_state *state, nir_shader *sh,
unsigned new_size = (state->total_size + 3) / 4;
*new_var = rzalloc(sh, nir_variable);
(*new_var)->name = ralloc_strdup(*new_var, GLSL_CLIP_VAR_NAME);
nir_variable_set_name(*new_var, GLSL_CLIP_VAR_NAME);
(*new_var)->data.mode = var->data.mode;
(*new_var)->data.location = VARYING_SLOT_CLIP_DIST0;
(*new_var)->data.assigned = true;

View file

@ -207,8 +207,8 @@ lower_const_array_to_uniform(nir_shader *shader, struct var_info *info,
uni->data.read_only = true;
uni->data.mode = nir_var_uniform;
uni->type = info->var->type;
uni->name = ralloc_asprintf(uni, "constarray_%x_%u",
*const_count, shader->info.stage);
nir_variable_set_namef(uni,"constarray_%x_%u",
*const_count, shader->info.stage);
nir_shader_add_variable(shader, uni);

View file

@ -62,12 +62,11 @@ lower_fragcolor_intrin(nir_builder *b, nir_intrinsic_instr *instr, void *data)
b->cursor = nir_after_instr(&instr->instr);
nir_def *frag_color = instr->src[1].ssa;
ralloc_free(out->name);
const char *name = out->data.index == 0 ? "gl_FragData[0]" : "gl_SecondaryFragDataEXT[0]";
const char *name_tmpl = out->data.index == 0 ? "gl_FragData[%u]" : "gl_SecondaryFragDataEXT[%u]";
out->name = ralloc_strdup(out, name);
nir_variable_set_name(out, name);
/* translate gl_FragColor -> gl_FragData since this is already handled */
out->data.location = FRAG_RESULT_DATA0;

View file

@ -305,13 +305,13 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
nir_variable *temp = var;
/* Reparent the name to the new variable */
ralloc_steal(nvar, nvar->name);
nir_variable_steal_name(nvar, var);
assert(nvar->constant_initializer == NULL && nvar->pointer_initializer == NULL);
/* Give the original a new name with @<mode>-temp appended */
const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
nir_variable_set_namef(temp, "%s@%s-temp", mode, nvar->name);
temp->data.mode = nir_var_shader_temp;
temp->data.read_only = false;
temp->data.fb_fetch_output = false;

View file

@ -174,8 +174,7 @@ nir_create_passthrough_gs(const nir_shader_compiler_options *options,
snprintf(name, sizeof(name), "in_%d", var->data.driver_location);
nir_variable *in = nir_variable_clone(var, nir);
ralloc_free(in->name);
in->name = ralloc_strdup(in, name);
nir_variable_set_name(in, name);
in->type = glsl_array_type(var->type, 6, false);
in->data.mode = nir_var_shader_in;
nir_shader_add_variable(nir, in);
@ -195,8 +194,7 @@ nir_create_passthrough_gs(const nir_shader_compiler_options *options,
snprintf(name, sizeof(name), "out_%d", var->data.driver_location);
nir_variable *out = nir_variable_clone(var, nir);
ralloc_free(out->name);
out->name = ralloc_strdup(out, name);
nir_variable_set_name(out, name);
out->data.mode = nir_var_shader_out;
nir_shader_add_variable(nir, out);

View file

@ -366,7 +366,7 @@ read_variable(read_ctx *ctx)
if (flags.u.has_name) {
const char *name = blob_read_string(ctx->blob);
var->name = ralloc_strdup(var, name);
nir_variable_set_name(var, name);
} else {
var->name = NULL;
}

View file

@ -2183,7 +2183,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_node_payload:
/* For these, we create the variable normally */
var->var = rzalloc(b->shader, nir_variable);
var->var->name = ralloc_strdup(var->var, val->name);
nir_variable_set_name(var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
/* This is a total hack but we need some way to flag variables which are
@ -2205,7 +2205,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_accel_struct:
case vtn_variable_mode_shader_record:
var->var = rzalloc(b->shader, nir_variable);
var->var->name = ralloc_strdup(var->var, val->name);
nir_variable_set_name(var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
var->var->interface_type = var->var->type;
@ -2221,7 +2221,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_task_payload:
/* Create the variable normally */
var->var = rzalloc(b->shader, nir_variable);
var->var->name = ralloc_strdup(var->var, val->name);
nir_variable_set_name(var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
var->var->data.mode = nir_mode;
if (var->mode == vtn_variable_mode_workgroup &&
@ -2232,7 +2232,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_input:
case vtn_variable_mode_output: {
var->var = rzalloc(b->shader, nir_variable);
var->var->name = ralloc_strdup(var->var, val->name);
nir_variable_set_name(var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
var->var->data.mode = nir_mode;

View file

@ -1030,14 +1030,14 @@ ir3_nir_lower_gs(nir_shader *shader)
exec_list_push_tail(&state.new_outputs, &output->node);
/* Rewrite the original output to be a shadow variable. */
var->name = ralloc_asprintf(var, "%s@gs-temp", output->name);
nir_variable_set_namef(var, "%s@gs-temp", output->name);
var->data.mode = nir_var_shader_temp;
/* Clone the shadow variable to create the emit shadow variable that
* we'll assign in the emit conditionals.
*/
nir_variable *emit_output = nir_variable_clone(var, shader);
emit_output->name = ralloc_asprintf(var, "%s@emit-temp", output->name);
nir_variable_set_namef(emit_output, "%s@emit-temp", output->name);
exec_list_push_tail(&state.emit_outputs, &emit_output->node);
}

View file

@ -301,7 +301,7 @@ ttn_emit_declaration(struct ttn_compile *c)
case TGSI_FILE_INPUT:
var->data.read_only = true;
var->data.mode = nir_var_shader_in;
var->name = ralloc_asprintf(var, "in_%d", idx);
nir_variable_set_namef(var, "in_%d", idx);
if (c->scan->processor == PIPE_SHADER_FRAGMENT) {
if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
@ -359,7 +359,7 @@ ttn_emit_declaration(struct ttn_compile *c)
is_array ? array_size : 0);
var->data.mode = nir_var_shader_out;
var->name = ralloc_asprintf(var, "out_%d", idx);
nir_variable_set_namef(var, "out_%d", idx);
var->data.index = 0;
var->data.interpolation =
ttn_translate_interp_mode(decl->Interp.Interpolate);
@ -446,7 +446,7 @@ ttn_emit_declaration(struct ttn_compile *c)
break;
case TGSI_FILE_CONSTANT:
var->data.mode = nir_var_uniform;
var->name = ralloc_asprintf(var, "uniform_%d", idx);
nir_variable_set_namef(var, "uniform_%d", idx);
var->data.location = idx;
break;
default:

View file

@ -1111,8 +1111,7 @@ zink_create_quads_emulation_gs(const nir_shader_compiler_options *options,
snprintf(name, sizeof(name), "in_%d", var->data.driver_location);
nir_variable *in = nir_variable_clone(var, nir);
ralloc_free(in->name);
in->name = ralloc_strdup(in, name);
nir_variable_set_name(in, name);
in->type = glsl_array_type(var->type, 4, false);
in->data.mode = nir_var_shader_in;
nir_shader_add_variable(nir, in);
@ -1123,8 +1122,7 @@ zink_create_quads_emulation_gs(const nir_shader_compiler_options *options,
snprintf(name, sizeof(name), "out_%d", var->data.driver_location);
nir_variable *out = nir_variable_clone(var, nir);
ralloc_free(out->name);
out->name = ralloc_strdup(out, name);
nir_variable_set_name(out, name);
out->data.mode = nir_var_shader_out;
nir_shader_add_variable(nir, out);
@ -2063,7 +2061,7 @@ decompose_attribs(nir_shader *nir, uint32_t decomposed_attrs, uint32_t decompose
state.needs_w = (decomposed_attrs_without_w & BITFIELD_BIT(location)) != 0 && num_components == 4;
for (unsigned i = 0; i < (state.needs_w ? num_components - 1 : num_components); i++) {
split[i+1] = nir_variable_clone(var, nir);
split[i+1]->name = ralloc_asprintf(nir, "%s_split%u", var->name, i);
nir_variable_set_namef(split[i+1], "%s_split%u", var->name, i);
if (decomposed_attrs_without_w & BITFIELD_BIT(location))
split[i+1]->type = !i && num_components == 4 ? var->type : new_type;
else
@ -2222,9 +2220,9 @@ get_bo_var(nir_shader *shader, struct bo_vars *bo, bool ssbo, nir_src *src, unsi
}
var = nir_variable_clone(var, shader);
if (ssbo)
var->name = ralloc_asprintf(shader, "%s@%u", "ssbos", bit_size);
nir_variable_set_namef(var, "%s@%u", "ssbos", bit_size);
else
var->name = ralloc_asprintf(shader, "%s@%u", idx ? "ubos" : "uniform_0", bit_size);
nir_variable_set_namef(var, "%s@%u", idx ? "ubos" : "uniform_0", bit_size);
*ptr = var;
nir_shader_add_variable(shader, var);

View file

@ -269,8 +269,7 @@ brw_nir_lower_mesh_primitive_count(nir_shader *nir)
nir_create_variable_with_location(nir, nir_var_shader_out,
VARYING_SLOT_PRIMITIVE_COUNT,
glsl_uint_type());
final_primitive_count->name = ralloc_strdup(final_primitive_count,
"gl_PrimitiveCountNV");
nir_variable_set_name(final_primitive_count, "gl_PrimitiveCountNV");
final_primitive_count->data.interpolation = INTERP_MODE_NONE;
nir_store_var(b, final_primitive_count,

View file

@ -1494,7 +1494,7 @@ emit_global_consts(struct ntd_context *ctx)
nir_foreach_variable_with_modes(var, ctx->shader, nir_var_mem_constant) {
if (!var->name)
var->name = ralloc_asprintf(var, "const_%d", var->data.driver_location);
nir_variable_set_namef(var, "const_%d", var->data.driver_location);
const struct dxil_value *agg_vals =
get_value_for_const_aggregate(&ctx->mod, var->constant_initializer, var->type);
@ -1525,7 +1525,7 @@ emit_shared_vars(struct ntd_context *ctx)
nir_foreach_variable_with_modes(var, ctx->shader, nir_var_mem_shared) {
if (!var->name)
var->name = ralloc_asprintf(var, "shared_%d", var->data.driver_location);
nir_variable_set_namef(var, "shared_%d", var->data.driver_location);
const struct dxil_value *gvar = dxil_add_global_ptr_var(&ctx->mod, var->name,
get_type_for_glsl_type(&ctx->mod, var->type),
DXIL_AS_GROUPSHARED, 16,