nir: Allow casts in nir_deref_instr_get[_const]_offset()

Allow casts in a deref chain so we can calculate an offset from
a base pointer dereference or have pointer type casts in the
middle of the chain (both are pretty common in CL).

Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5682>
diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c
index 83c4012..dca7621 100644
--- a/src/compiler/nir/nir_deref.c
+++ b/src/compiler/nir/nir_deref.c
@@ -279,8 +279,6 @@
    nir_deref_path path;
    nir_deref_path_init(&path, deref, NULL);
 
-   assert(path.path[0]->deref_type == nir_deref_type_var);
-
    unsigned offset = 0;
    for (nir_deref_instr **p = &path.path[1]; *p; p++) {
       switch ((*p)->deref_type) {
@@ -295,6 +293,9 @@
                                                 (*p)->strct.index);
 	 break;
       }
+      case nir_deref_type_cast:
+         /* A cast doesn't contribute to the offset */
+         break;
       default:
          unreachable("Unsupported deref type");
       }
@@ -312,8 +313,6 @@
    nir_deref_path path;
    nir_deref_path_init(&path, deref, NULL);
 
-   assert(path.path[0]->deref_type == nir_deref_type_var);
-
    nir_ssa_def *offset = nir_imm_intN_t(b, 0, deref->dest.ssa.bit_size);
    for (nir_deref_instr **p = &path.path[1]; *p; p++) {
       switch ((*p)->deref_type) {
@@ -332,6 +331,9 @@
          offset = nir_iadd_imm(b, offset, field_offset);
          break;
       }
+      case nir_deref_type_cast:
+         /* A cast doesn't contribute to the offset */
+         break;
       default:
          unreachable("Unsupported deref type");
       }