nir: Validate constant initializers
authorJason Ekstrand <jason@jlekstrand.net>
Fri, 2 Oct 2020 07:14:36 +0000 (02:14 -0500)
committerMarge Bot <eric+marge@anholt.net>
Tue, 6 Oct 2020 15:42:03 +0000 (15:42 +0000)
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6974>

src/compiler/nir/nir_validate.c

index 706ddd2..962d7ae 100644 (file)
@@ -1227,6 +1227,34 @@ postvalidate_reg_decl(nir_register *reg, validate_state *state)
 }
 
 static void
+validate_constant(nir_constant *c, const struct glsl_type *type,
+                  validate_state *state)
+{
+   if (glsl_type_is_vector_or_scalar(type)) {
+      unsigned num_components = glsl_get_vector_elements(type);
+      unsigned bit_size = glsl_get_bit_size(type);
+      for (unsigned i = 0; i < num_components; i++)
+         validate_const_value(&c->values[i], bit_size, state);
+      for (unsigned i = num_components; i < NIR_MAX_VEC_COMPONENTS; i++)
+         validate_assert(state, c->values[i].u64 == 0);
+   } else {
+      validate_assert(state, c->num_elements == glsl_get_length(type));
+      if (glsl_type_is_struct_or_ifc(type)) {
+         for (unsigned i = 0; i < c->num_elements; i++) {
+            const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
+            validate_constant(c->elements[i], elem_type, state);
+         }
+      } else if (glsl_type_is_array_or_matrix(type)) {
+         const struct glsl_type *elem_type = glsl_get_array_element(type);
+         for (unsigned i = 0; i < c->num_elements; i++)
+            validate_constant(c->elements[i], elem_type, state);
+      } else {
+         validate_assert(state, !"Invalid type for nir_constant");
+      }
+   }
+}
+
+static void
 validate_var_decl(nir_variable *var, nir_variable_mode valid_modes,
                   validate_state *state)
 {
@@ -1259,6 +1287,9 @@ validate_var_decl(nir_variable *var, nir_variable_mode valid_modes,
    if (var->data.per_view)
       validate_assert(state, glsl_type_is_array(var->type));
 
+   if (var->constant_initializer)
+      validate_constant(var->constant_initializer, var->type, state);
+
    /*
     * TODO validate some things ir_validate.cpp does (requires more GLSL type
     * support)