From 0f41da9160252d65319a58e1f34083ef8aaf01fd Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Thu, 31 Aug 2023 23:45:02 -0700 Subject: [PATCH] compiler/types: Constify a couple of pointers in glsl_type This will allow builtins to initialize those with read-only data in the future. Reviewed-by: Emma Anholt Part-of: --- src/compiler/glsl/ir.h | 2 +- src/compiler/glsl/link_varyings.cpp | 2 +- src/compiler/glsl_types.cpp | 47 +++++++++++++++++------------ src/compiler/glsl_types.h | 4 +-- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/compiler/glsl/ir.h b/src/compiler/glsl/ir.h index 1a3721838c6..eda56692b9a 100644 --- a/src/compiler/glsl/ir.h +++ b/src/compiler/glsl/ir.h @@ -2154,7 +2154,7 @@ public: virtual int precision() const { - glsl_struct_field *field = record->type->fields.structure + field_idx; + const glsl_struct_field *field = record->type->fields.structure + field_idx; return field->precision; } diff --git a/src/compiler/glsl/link_varyings.cpp b/src/compiler/glsl/link_varyings.cpp index 0ef618ac114..9e173ff823e 100644 --- a/src/compiler/glsl/link_varyings.cpp +++ b/src/compiler/glsl/link_varyings.cpp @@ -523,7 +523,7 @@ validate_explicit_variable_location(const struct gl_constants *consts, const glsl_type *type_without_array = type->without_array(); if (type_without_array->is_interface()) { for (unsigned i = 0; i < type_without_array->length; i++) { - glsl_struct_field *field = &type_without_array->fields.structure[i]; + const glsl_struct_field *field = &type_without_array->fields.structure[i]; unsigned field_location = field->location - (field->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0); unsigned field_slots = field->type->count_attribute_slots(false); diff --git a/src/compiler/glsl_types.cpp b/src/compiler/glsl_types.cpp index 7172ca146ce..464e1edcf5c 100644 --- a/src/compiler/glsl_types.cpp +++ b/src/compiler/glsl_types.cpp @@ -125,16 +125,18 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, assert(name != NULL); this->name = ralloc_strdup(this->mem_ctx, name); + /* Zero-fill to prevent spurious Valgrind errors when serializing NIR * due to uninitialized unused bits in bit fields. */ - this->fields.structure = rzalloc_array(this->mem_ctx, - glsl_struct_field, length); + struct glsl_struct_field *copied_struct = + rzalloc_array(this->mem_ctx, glsl_struct_field, length); for (i = 0; i < length; i++) { - this->fields.structure[i] = fields[i]; - this->fields.structure[i].name = ralloc_strdup(this->fields.structure, - fields[i].name); + copied_struct[i] = fields[i]; + copied_struct[i].name = ralloc_strdup(copied_struct, fields[i].name); } + + this->fields.structure = copied_struct; } glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, @@ -155,13 +157,16 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, assert(name != NULL); this->name = ralloc_strdup(this->mem_ctx, name); - this->fields.structure = rzalloc_array(this->mem_ctx, - glsl_struct_field, length); + + struct glsl_struct_field *copied_struct = + rzalloc_array(this->mem_ctx, glsl_struct_field, length); + for (i = 0; i < length; i++) { - this->fields.structure[i] = fields[i]; - this->fields.structure[i].name = ralloc_strdup(this->fields.structure, - fields[i].name); + copied_struct[i] = fields[i]; + copied_struct[i].name = ralloc_strdup(copied_struct, fields[i].name); } + + this->fields.structure = copied_struct; } glsl_type::glsl_type(const glsl_type *return_type, @@ -180,20 +185,22 @@ glsl_type::glsl_type(const glsl_type *return_type, this->name = ""; - this->fields.parameters = rzalloc_array(this->mem_ctx, - glsl_function_param, num_params + 1); + struct glsl_function_param *copied_params = + rzalloc_array(this->mem_ctx, glsl_function_param, num_params + 1); /* We store the return type as the first parameter */ - this->fields.parameters[0].type = return_type; - this->fields.parameters[0].in = false; - this->fields.parameters[0].out = true; + copied_params[0].type = return_type; + copied_params[0].in = false; + copied_params[0].out = true; /* We store the i'th parameter in slot i+1 */ for (i = 0; i < length; i++) { - this->fields.parameters[i + 1].type = params[i].type; - this->fields.parameters[i + 1].in = params[i].in; - this->fields.parameters[i + 1].out = params[i].out; + copied_params[i + 1].type = params[i].type; + copied_params[i + 1].in = params[i].in; + copied_params[i + 1].out = params[i].out; } + + this->fields.parameters = copied_params; } glsl_type::glsl_type(const char *subroutine_name) : @@ -3320,7 +3327,7 @@ glsl_type::cl_alignment() const unsigned res = 1; for (unsigned i = 0; i < this->length; ++i) { - struct glsl_struct_field &field = this->fields.structure[i]; + const struct glsl_struct_field &field = this->fields.structure[i]; res = MAX2(res, field.type->cl_alignment()); } return res; @@ -3341,7 +3348,7 @@ glsl_type::cl_size() const unsigned size = 0; unsigned max_alignment = 1; for (unsigned i = 0; i < this->length; ++i) { - struct glsl_struct_field &field = this->fields.structure[i]; + const struct glsl_struct_field &field = this->fields.structure[i]; /* if a struct is packed, members don't get aligned */ if (!this->packed) { unsigned alignment = field.type->cl_alignment(); diff --git a/src/compiler/glsl_types.h b/src/compiler/glsl_types.h index 51e233c8a88..a5c4fff1659 100644 --- a/src/compiler/glsl_types.h +++ b/src/compiler/glsl_types.h @@ -363,8 +363,8 @@ public: */ union { const struct glsl_type *array; /**< Type of array elements. */ - struct glsl_function_param *parameters; /**< Parameters to function. */ - struct glsl_struct_field *structure; /**< List of struct fields. */ + const struct glsl_function_param *parameters; /**< Parameters to function. */ + const struct glsl_struct_field *structure; /**< List of struct fields. */ } fields; /**