glsl: validate more stuff
authorMarek Olšák <marek.olsak@amd.com>
Wed, 24 Jun 2020 06:39:38 +0000 (02:39 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Wed, 8 Jul 2020 02:02:06 +0000 (22:02 -0400)
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5746>

src/compiler/glsl/ir_validate.cpp

index 64e469917536dfbc51b037892667a5d8c07fdbfc..4f96ef0031b5fbba602cff37cf3b2032afb40143 100644 (file)
@@ -69,11 +69,13 @@ public:
    virtual ir_visitor_status visit_enter(ir_function *ir);
    virtual ir_visitor_status visit_leave(ir_function *ir);
    virtual ir_visitor_status visit_enter(ir_function_signature *ir);
+   virtual ir_visitor_status visit_enter(ir_return *ir);
 
    virtual ir_visitor_status visit_leave(ir_expression *ir);
    virtual ir_visitor_status visit_leave(ir_swizzle *ir);
 
    virtual ir_visitor_status visit_enter(class ir_dereference_array *);
+   virtual ir_visitor_status visit_enter(class ir_dereference_record *);
 
    virtual ir_visitor_status visit_enter(ir_assignment *ir);
    virtual ir_visitor_status visit_enter(ir_call *ir);
@@ -96,6 +98,13 @@ ir_validate::visit(ir_dereference_variable *ir)
       abort();
    }
 
+   if (ir->var->type != ir->type) {
+      printf("ir_dereference_variable type is not equal to variable type: ");
+      ir->print();
+      printf("\n");
+      abort();
+   }
+
    if (_mesa_set_search(ir_set, ir->var) == NULL) {
       printf("ir_dereference_variable @ %p specifies undeclared variable "
             "`%s' @ %p\n",
@@ -121,6 +130,21 @@ ir_validate::visit_enter(class ir_dereference_array *ir)
       abort();
    }
 
+   if (ir->array->type->is_array()) {
+      if (ir->array->type->fields.array != ir->type) {
+         printf("ir_dereference_array type is not equal to the array "
+                "element type: ");
+         ir->print();
+         printf("\n");
+         abort();
+      }
+   } else if (ir->array->type->base_type != ir->type->base_type) {
+      printf("ir_dereference_array base types are not equal: ");
+      ir->print();
+      printf("\n");
+      abort();
+   }
+
    if (!ir->array_index->type->is_scalar()) {
       printf("ir_dereference_array @ %p does not have scalar index: %s\n",
              (void *) ir, ir->array_index->type->name);
@@ -136,6 +160,28 @@ ir_validate::visit_enter(class ir_dereference_array *ir)
    return visit_continue;
 }
 
+ir_visitor_status
+ir_validate::visit_enter(class ir_dereference_record *ir)
+{
+   if (!ir->record->type->is_struct() && !ir->record->type->is_interface()) {
+      printf("ir_dereference_record @ %p does not specify a record\n",
+             (void *) ir);
+      ir->print();
+      printf("\n");
+      abort();
+   }
+
+   if (ir->record->type->fields.structure[ir->field_idx].type != ir->type) {
+      printf("ir_dereference_record type is not equal to the record "
+             "field type: ");
+      ir->print();
+      printf("\n");
+      abort();
+   }
+
+   return visit_continue;
+}
+
 ir_visitor_status
 ir_validate::visit_enter(ir_discard *ir)
 {
@@ -234,6 +280,17 @@ ir_validate::visit_enter(ir_function_signature *ir)
    return visit_continue;
 }
 
+ir_visitor_status
+ir_validate::visit_enter(ir_return *ir)
+{
+   if (!this->current_function) {
+      printf("Return statement outside of a function\n");
+      abort();
+   }
+
+   return visit_continue;
+}
+
 ir_visitor_status
 ir_validate::visit_leave(ir_expression *ir)
 {
@@ -1032,6 +1089,15 @@ ir_validate::visit_enter(ir_assignment *ir)
       }
    }
 
+   if (lhs->type->base_type != ir->rhs->type->base_type) {
+      printf("Assignment LHS and RHS base types are different:\n");
+      lhs->print();
+      printf("\n");
+      ir->rhs->print();
+      printf("\n");
+      abort();
+   }
+
    this->validate_ir(ir, this->data_enter);
 
    return visit_continue;