spirv: Refactor variable initializer code

Pass the vtn_value and let vtn_create_variable do the validation.

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8708>
This commit is contained in:
Caio Marcelo de Oliveira Filho
2021-01-28 11:47:24 -08:00
committed by Marge Bot
parent 696b0ab2c9
commit 378eca1394
2 changed files with 26 additions and 26 deletions

View File

@@ -750,6 +750,15 @@ vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
return &b->values[value_id];
}
static inline uint32_t
vtn_id_for_value(struct vtn_builder *b, struct vtn_value *value)
{
vtn_fail_if(value <= b->values, "vtn_value pointer outside the range of valid values");
uint32_t value_id = value - b->values;
vtn_fail_if(value_id >= b->value_id_bound, "vtn_value pointer outside the range of valid values");
return value_id;
}
/* Consider not using this function directly and instead use
* vtn_push_ssa/vtn_push_pointer so that appropriate applying of
* decorations is handled by common code.

View File

@@ -1752,7 +1752,7 @@ vtn_get_call_payload_for_location(struct vtn_builder *b, uint32_t location_id)
static void
vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
struct vtn_type *ptr_type, SpvStorageClass storage_class,
nir_constant *const_initializer, nir_variable *var_initializer)
struct vtn_value *initializer)
{
vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
struct vtn_type *type = ptr_type->deref;
@@ -1976,14 +1976,20 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
unreachable("Should have been caught before");
}
/* We can only have one type of initializer */
assert(!(const_initializer && var_initializer));
if (const_initializer) {
var->var->constant_initializer =
nir_constant_clone(const_initializer, var->var);
if (initializer) {
switch (initializer->value_type) {
case vtn_value_type_constant:
var->var->constant_initializer =
nir_constant_clone(initializer->constant, var->var);
break;
case vtn_value_type_pointer:
var->var->pointer_initializer = initializer->pointer->var->var;
break;
default:
vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
vtn_id_for_value(b, initializer));
}
}
if (var_initializer)
var->var->pointer_initializer = var_initializer;
if (var->mode == vtn_variable_mode_uniform ||
var->mode == vtn_variable_mode_ssbo) {
@@ -2220,24 +2226,9 @@ vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
SpvStorageClass storage_class = w[3];
nir_constant *const_initializer = NULL;
nir_variable *var_initializer = NULL;
if (count > 4) {
struct vtn_value *init = vtn_untyped_value(b, w[4]);
switch (init->value_type) {
case vtn_value_type_constant:
const_initializer = init->constant;
break;
case vtn_value_type_pointer:
var_initializer = init->pointer->var->var;
break;
default:
vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
w[4]);
}
}
struct vtn_value *initializer = count > 4 ? vtn_untyped_value(b, w[4]) : NULL;
vtn_create_variable(b, val, ptr_type, storage_class, const_initializer, var_initializer);
vtn_create_variable(b, val, ptr_type, storage_class, initializer);
break;
}
@@ -2257,7 +2248,7 @@ vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
ptr_type->type = nir_address_format_to_glsl_type(
vtn_mode_to_address_format(b, vtn_variable_mode_function));
vtn_create_variable(b, val, ptr_type, ptr_type->storage_class, NULL, NULL);
vtn_create_variable(b, val, ptr_type, ptr_type->storage_class, NULL);
nir_variable *nir_var = val->pointer->var->var;
nir_var->data.sampler.is_inline_sampler = true;