From 44350bce1fcd55f0d74b1c2cc815be3984ab2646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Fri, 1 Aug 2025 22:59:07 -0400 Subject: [PATCH] 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 Part-of: --- .../glsl/gl_nir_lower_blend_equation_advanced.c | 2 +- src/compiler/glsl/gl_nir_lower_discard_flow.c | 2 +- .../glsl/gl_nir_lower_named_interface_blocks.c | 2 +- src/compiler/glsl/gl_nir_lower_packed_varyings.c | 2 +- src/compiler/glsl/gl_nir_lower_xfb_varying.c | 2 +- src/compiler/glsl/glsl_to_nir.cpp | 2 +- src/compiler/nir/nir.c | 10 ++++++++-- src/compiler/nir/nir.h | 1 + src/compiler/nir/nir_clone.c | 2 +- src/compiler/nir/nir_lower_clip.c | 2 +- .../nir/nir_lower_clip_cull_distance_array_vars.c | 2 +- src/compiler/nir/nir_lower_const_arrays_to_uniforms.c | 2 +- src/compiler/nir/nir_lower_io_vars_to_temporaries.c | 2 +- src/compiler/nir/nir_serialize.c | 2 +- src/compiler/spirv/spirv_to_nir.c | 2 +- src/compiler/spirv/vtn_variables.c | 8 ++++---- src/gallium/auxiliary/nir/tgsi_to_nir.c | 2 +- src/microsoft/compiler/nir_to_dxil.c | 2 +- 18 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/compiler/glsl/gl_nir_lower_blend_equation_advanced.c b/src/compiler/glsl/gl_nir_lower_blend_equation_advanced.c index 999eb7907f7..e7cd2ba9ab2 100644 --- a/src/compiler/glsl/gl_nir_lower_blend_equation_advanced.c +++ b/src/compiler/glsl/gl_nir_lower_blend_equation_advanced.c @@ -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; diff --git a/src/compiler/glsl/gl_nir_lower_discard_flow.c b/src/compiler/glsl/gl_nir_lower_discard_flow.c index 8f542af687e..f4f9f63fa00 100644 --- a/src/compiler/glsl/gl_nir_lower_discard_flow.c +++ b/src/compiler/glsl/gl_nir_lower_discard_flow.c @@ -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; diff --git a/src/compiler/glsl/gl_nir_lower_named_interface_blocks.c b/src/compiler/glsl/gl_nir_lower_named_interface_blocks.c index 129e3425192..00222adde37 100644 --- a/src/compiler/glsl/gl_nir_lower_named_interface_blocks.c +++ b/src/compiler/glsl/gl_nir_lower_named_interface_blocks.c @@ -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); diff --git a/src/compiler/glsl/gl_nir_lower_packed_varyings.c b/src/compiler/glsl/gl_nir_lower_packed_varyings.c index 8534440897c..659eb58efb4 100644 --- a/src/compiler/glsl/gl_nir_lower_packed_varyings.c +++ b/src/compiler/glsl/gl_nir_lower_packed_varyings.c @@ -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; diff --git a/src/compiler/glsl/gl_nir_lower_xfb_varying.c b/src/compiler/glsl/gl_nir_lower_xfb_varying.c index ef87b1ecc0c..ca2662d08bf 100644 --- a/src/compiler/glsl/gl_nir_lower_xfb_varying.c +++ b/src/compiler/glsl/gl_nir_lower_xfb_varying.c @@ -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; diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp index fe5337d7521..a2e6f42d4e4 100644 --- a/src/compiler/glsl/glsl_to_nir.cpp +++ b/src/compiler/glsl/glsl_to_nir.cpp @@ -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); diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index 24e721a071d..d49f4f2c2ed 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -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; diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 1c2ce687c36..918a740216e 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -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, diff --git a/src/compiler/nir/nir_clone.c b/src/compiler/nir/nir_clone.c index cda8e701e88..9e1819c8ec3 100644 --- a/src/compiler/nir/nir_clone.c +++ b/src/compiler/nir/nir_clone.c @@ -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); diff --git a/src/compiler/nir/nir_lower_clip.c b/src/compiler/nir/nir_lower_clip.c index c4076f547dd..04a83bcb2c4 100644 --- a/src/compiler/nir/nir_lower_clip.c +++ b/src/compiler/nir/nir_lower_clip.c @@ -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; diff --git a/src/compiler/nir/nir_lower_clip_cull_distance_array_vars.c b/src/compiler/nir/nir_lower_clip_cull_distance_array_vars.c index 562c27b3d6e..941908b8c06 100644 --- a/src/compiler/nir/nir_lower_clip_cull_distance_array_vars.c +++ b/src/compiler/nir/nir_lower_clip_cull_distance_array_vars.c @@ -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; diff --git a/src/compiler/nir/nir_lower_const_arrays_to_uniforms.c b/src/compiler/nir/nir_lower_const_arrays_to_uniforms.c index 7700b400fc4..8faf867d0aa 100644 --- a/src/compiler/nir/nir_lower_const_arrays_to_uniforms.c +++ b/src/compiler/nir/nir_lower_const_arrays_to_uniforms.c @@ -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); diff --git a/src/compiler/nir/nir_lower_io_vars_to_temporaries.c b/src/compiler/nir/nir_lower_io_vars_to_temporaries.c index 9e64c502ae0..a455d5cdda2 100644 --- a/src/compiler/nir/nir_lower_io_vars_to_temporaries.c +++ b/src/compiler/nir/nir_lower_io_vars_to_temporaries.c @@ -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; diff --git a/src/compiler/nir/nir_serialize.c b/src/compiler/nir/nir_serialize.c index 6a2ecb6e486..fd76d6a1051 100644 --- a/src/compiler/nir/nir_serialize.c +++ b/src/compiler/nir/nir_serialize.c @@ -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; diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c index 9f94bdd882b..612cf539fb9 100644 --- a/src/compiler/spirv/spirv_to_nir.c +++ b/src/compiler/spirv/spirv_to_nir.c @@ -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; diff --git a/src/compiler/spirv/vtn_variables.c b/src/compiler/spirv/vtn_variables.c index 717b390882f..8c072ff9fe8 100644 --- a/src/compiler/spirv/vtn_variables.c +++ b/src/compiler/spirv/vtn_variables.c @@ -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; diff --git a/src/gallium/auxiliary/nir/tgsi_to_nir.c b/src/gallium/auxiliary/nir/tgsi_to_nir.c index 81999005890..a69e39e66f7 100644 --- a/src/gallium/auxiliary/nir/tgsi_to_nir.c +++ b/src/gallium/auxiliary/nir/tgsi_to_nir.c @@ -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; diff --git a/src/microsoft/compiler/nir_to_dxil.c b/src/microsoft/compiler/nir_to_dxil.c index 8a193d29784..525f8819261 100644 --- a/src/microsoft/compiler/nir_to_dxil.c +++ b/src/microsoft/compiler/nir_to_dxil.c @@ -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;