nir: add nir_variable_create_zeroed helper

This will allow us to switch nir_variable from ralloc to gc_ctx,
which uses a slab allocator.

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 22:59:07 -04:00
committed by Marge Bot
parent b769d5dcde
commit 44350bce1f
18 changed files with 28 additions and 21 deletions

View File

@@ -216,7 +216,7 @@ satv3(nir_builder *b, nir_def *c)
static nir_variable *
add_temp_var(nir_builder *b, char *name, const struct glsl_type *type)
{
nir_variable *var = rzalloc(b->shader, nir_variable);
nir_variable *var = nir_variable_create_zeroed(b->shader);
var->type = type;
nir_variable_set_name(b->shader, var, name);
var->data.mode = nir_var_function_temp;

View File

@@ -135,7 +135,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);
nir_variable *discarded = nir_variable_create_zeroed(shader);
nir_variable_set_name(shader, discarded, "discarded");
discarded->type = glsl_bool_type();
discarded->data.mode = nir_var_shader_temp;

View File

@@ -269,7 +269,7 @@ lower_named_interface_blocks(struct gl_linked_shader *sh)
const struct glsl_struct_field *field_data =
glsl_get_struct_field_data(iface_t, i);
nir_variable *new_var = rzalloc(sh->Program->nir, nir_variable);
nir_variable *new_var = nir_variable_create_zeroed(sh->Program->nir);
nir_variable_set_name(sh->Program->nir, new_var, field_name);
if (!glsl_type_is_array(var->type)) {
new_var->type = glsl_get_struct_field(iface_t, i);

View File

@@ -270,7 +270,7 @@ create_or_update_packed_varying(struct lower_packed_varyings_state *state,
assert(state->components[slot] != 0);
assert(name);
nir_variable *packed_var = rzalloc(state->shader, nir_variable);
nir_variable *packed_var = nir_variable_create_zeroed(state->shader);
nir_variable_set_namef(state->shader, packed_var, "packed:%s", name);
packed_var->data.mode = state->mode;

View File

@@ -162,7 +162,7 @@ gl_nir_lower_xfb_varying(nir_shader *shader, const char *old_var_name,
if (!get_deref(&b, old_var_name, toplevel_var, &deref, &type))
return NULL;
nir_variable *new_variable = rzalloc(shader, nir_variable);
nir_variable *new_variable = nir_variable_create_zeroed(shader);
new_variable->name = generate_new_name(shader, old_var_name);
new_variable->type = type;
new_variable->data.mode = nir_var_shader_out;

View File

@@ -445,7 +445,7 @@ nir_visitor::visit(ir_variable *ir)
if (ir->data.mode == ir_var_function_out)
return;
nir_variable *var = rzalloc(shader, nir_variable);
nir_variable *var = nir_variable_create_zeroed(shader);
var->type = ir->type;
nir_variable_set_name(shader, var, ir->name);

View File

@@ -268,6 +268,12 @@ nir_shader_add_variable(nir_shader *shader, nir_variable *var)
exec_list_push_tail(&shader->variables, &var->node);
}
nir_variable *
nir_variable_create_zeroed(nir_shader *nir)
{
return rzalloc(nir, nir_variable);
}
void
nir_variable_set_name(nir_shader *nir, nir_variable *var, const char *name)
{
@@ -383,7 +389,7 @@ 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);
nir_variable *var = nir_variable_create_zeroed(shader);
nir_variable_set_name(shader, var, name);
var->type = type;
var->data.mode = mode;
@@ -408,7 +414,7 @@ nir_variable *
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);
nir_variable *var = nir_variable_create_zeroed(impl->function->shader);
nir_variable_set_name(impl->function->shader, var, name);
var->type = type;
var->data.mode = nir_var_function_temp;

View File

@@ -3951,6 +3951,7 @@ nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
exec_list_push_tail(&impl->locals, &var->node);
}
nir_variable *nir_variable_create_zeroed(nir_shader *nir);
void nir_variable_set_name(nir_shader *nir, nir_variable *var,
const char *name);
void nir_variable_set_namef(nir_shader *nir, nir_variable *var,

View File

@@ -151,7 +151,7 @@ nir_constant_clone(const nir_constant *c, void *mem_ctx)
nir_variable *
nir_variable_clone(const nir_variable *var, nir_shader *shader)
{
nir_variable *nvar = rzalloc(shader, nir_variable);
nir_variable *nvar = nir_variable_create_zeroed(shader);
nvar->type = var->type;
nir_variable_set_name(shader, nvar, var->name);

View File

@@ -43,7 +43,7 @@ static nir_variable *
create_clipdist_var(nir_shader *shader,
bool output, gl_varying_slot slot, unsigned array_size)
{
nir_variable *var = rzalloc(shader, nir_variable);
nir_variable *var = nir_variable_create_zeroed(shader);
if (output) {
var->data.driver_location = shader->num_outputs;

View File

@@ -146,7 +146,7 @@ replace_var_declaration(struct lower_distance_state *state, nir_shader *sh,
if (!(*new_var)) {
unsigned new_size = (state->total_size + 3) / 4;
*new_var = rzalloc(sh, nir_variable);
*new_var = nir_variable_create_zeroed(sh);
nir_variable_set_name(sh, *new_var, GLSL_CLIP_VAR_NAME);
(*new_var)->data.mode = var->data.mode;
(*new_var)->data.location = VARYING_SLOT_CLIP_DIST0;

View File

@@ -168,7 +168,7 @@ lower_const_array_to_uniform(nir_shader *shader, struct var_info *info,
if (*const_count == limit)
return false;
nir_variable *uni = rzalloc(shader, nir_variable);
nir_variable *uni = nir_variable_create_zeroed(shader);
/* Rebuild constant initialiser */
nir_constant *const_init = rebuild_const_array_initialiser(var->type, shader);

View File

@@ -297,7 +297,7 @@ emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
static nir_variable *
create_shadow_temp(struct lower_io_state *state, nir_variable *var)
{
nir_variable *nvar = ralloc(state->shader, nir_variable);
nir_variable *nvar = nir_variable_create_zeroed(state->shader);
memcpy(nvar, var, sizeof *nvar);
nvar->data.cannot_coalesce = true;

View File

@@ -342,7 +342,7 @@ write_variable(write_ctx *ctx, const nir_variable *var)
static nir_variable *
read_variable(read_ctx *ctx)
{
nir_variable *var = rzalloc(ctx->nir, nir_variable);
nir_variable *var = nir_variable_create_zeroed(ctx->nir);
read_add_object(ctx, var);
union packed_var flags;

View File

@@ -7130,7 +7130,7 @@ vtn_emit_kernel_entry_point_wrapper(struct vtn_builder *b,
param_type->storage_class == SpvStorageClassFunction;
/* input variable */
nir_variable *in_var = rzalloc(b->nb.shader, nir_variable);
nir_variable *in_var = nir_variable_create_zeroed(b->nb.shader);
if (is_by_val) {
in_var->data.mode = nir_var_uniform;

View File

@@ -2182,7 +2182,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_hit_attrib:
case vtn_variable_mode_node_payload:
/* For these, we create the variable normally */
var->var = rzalloc(b->shader, nir_variable);
var->var = nir_variable_create_zeroed(b->shader);
nir_variable_set_name(b->shader, var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
@@ -2204,7 +2204,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_push_constant:
case vtn_variable_mode_accel_struct:
case vtn_variable_mode_shader_record:
var->var = rzalloc(b->shader, nir_variable);
var->var = nir_variable_create_zeroed(b->shader);
nir_variable_set_name(b->shader, var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
@@ -2220,7 +2220,7 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
case vtn_variable_mode_cross_workgroup:
case vtn_variable_mode_task_payload:
/* Create the variable normally */
var->var = rzalloc(b->shader, nir_variable);
var->var = nir_variable_create_zeroed(b->shader);
nir_variable_set_name(b->shader, var->var, val->name);
var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
var->var->data.mode = nir_mode;
@@ -2231,7 +2231,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 = nir_variable_create_zeroed(b->shader);
nir_variable_set_name(b->shader, 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

@@ -289,7 +289,7 @@ ttn_emit_declaration(struct ttn_compile *c)
for (i = 0; i < array_size; i++) {
unsigned idx = decl->Range.First + i;
nir_variable *var = rzalloc(b->shader, nir_variable);
nir_variable *var = nir_variable_create_zeroed(b->shader);
var->data.driver_location = idx;

View File

@@ -6437,7 +6437,7 @@ add_sysvalue(struct ntd_context *ctx,
int driver_location)
{
nir_variable *var = rzalloc(ctx->shader, nir_variable);
nir_variable *var = nir_variable_create_zeroed(ctx->shader);
if (!var)
return NULL;
var->data.driver_location = driver_location;