nir/validate: Require unused bits of nir_const_value to be zero
authorJason Ekstrand <jason@jlekstrand.net>
Tue, 2 Apr 2019 02:42:37 +0000 (21:42 -0500)
committerKarol Herbst <kherbst@redhat.com>
Sun, 14 Apr 2019 20:25:56 +0000 (22:25 +0200)
Reviewed-by: Karol Herbst <kherbst@redhat.com>
src/compiler/nir/nir_instr_set.c
src/compiler/nir/nir_validate.c

index 7dfd3ef1a3e873c0b341d34ce97eb8fd864ec3c3..d53e044c9017d271f43b0b5de993e84e6b9c4c21 100644 (file)
@@ -628,29 +628,15 @@ nir_instrs_equal(const nir_instr *instr1, const nir_instr *instr2)
       if (load1->def.bit_size != load2->def.bit_size)
          return false;
 
-      for (unsigned i = 0; i < load1->def.num_components; ++i) {
-         switch (load1->def.bit_size) {
-         case 1:
+      if (load1->def.bit_size == 1) {
+         for (unsigned i = 0; i < load1->def.num_components; ++i) {
             if (load1->value[i].b != load2->value[i].b)
                return false;
-            break;
-         case 8:
-            if (load1->value[i].u8 != load2->value[i].u8)
-               return false;
-            break;
-         case 16:
-            if (load1->value[i].u16 != load2->value[i].u16)
-               return false;
-            break;
-         case 32:
-            if (load1->value[i].u32 != load2->value[i].u32)
-               return false;
-            break;
-         case 64:
-            if (load1->value[i].u64 != load2->value[i].u64)
-               return false;
-            break;
          }
+      } else {
+         unsigned size = load1->def.num_components * sizeof(*load1->value);
+         if (memcmp(load1->value, load2->value, size) != 0)
+            return false;
       }
       return true;
    }
index 9a950218208e08415fc9c0d8b904008dabbd2bd2..7746c391abcab9f3e1b288501a3a80d256c535e1 100644 (file)
@@ -619,10 +619,45 @@ validate_call_instr(nir_call_instr *instr, validate_state *state)
    }
 }
 
+static void
+validate_const_value(nir_const_value *val, unsigned bit_size,
+                     validate_state *state)
+{
+   /* In order for block copies to work properly for things like instruction
+    * comparisons and [de]serialization, we require the unused bits of the
+    * nir_const_value to be zero.
+    */
+   nir_const_value cmp_val;
+   memset(&cmp_val, 0, sizeof(cmp_val));
+   switch (bit_size) {
+   case 1:
+      cmp_val.b = val->b;
+      break;
+   case 8:
+      cmp_val.u8 = val->u8;
+      break;
+   case 16:
+      cmp_val.u16 = val->u16;
+      break;
+   case 32:
+      cmp_val.u32 = val->u32;
+      break;
+   case 64:
+      cmp_val.u64 = val->u64;
+      break;
+   default:
+      validate_assert(state, !"Invalid load_const bit size");
+   }
+   validate_assert(state, memcmp(val, &cmp_val, sizeof(cmp_val)) == 0);
+}
+
 static void
 validate_load_const_instr(nir_load_const_instr *instr, validate_state *state)
 {
    validate_ssa_def(&instr->def, state);
+
+   for (unsigned i = 0; i < instr->def.num_components; i++)
+      validate_const_value(&instr->value[i], instr->def.bit_size, state);
 }
 
 static void