<div dir="ltr"><div><div><div><div><div>After talking with Kristian a bit on IRC, I think there is a bit more explanation needed.  I would be happy to add something like this to the commit message or as a comment upon request.<br><br></div>There is an interesting edge case in the variable pointers extension when you do an OpPtrAccessChain on a pointer to a struct decorated block.  In this case there is not entirely clear if you should look for an array stride and treat it as an implicit array of such structs or if you should treat it as a single block in an array of blocks which would imply incrementing the index portion of the descriptor tuple.  We choose the later approach and assume that it's an array of blocks and therefore an array of SSBOs.  This has two advantages:<br><br></div> 1) The SPIR-V spec for the OpPtrAccessChain instruction says "<em>Base</em> is treated as the address of the first element of an array, and the <em>Element</em> element’s address is computed to be the base for the <em>Indexes</em>, as per <a><strong>OpAccessChain</strong></a>."  Taken literally, that would mean that your struct type is supposed to be treated as an array of such a struct and, since it's decorated block, that means an array of blocks which corresponds to an array descriptor.<br><br></div> 2) Taking this approach ensures that any array dereference in an OpAccessChain can be replaced with an OpAccessChain that selects element 0 of the array together with an OpPtrAccessChain taking that result and adding to the index.  In particular, it ensures that this works when the array dereference is on an array of SSBOs.<br><br></div>The downside (there always is one) is that this might be somewhat surprising behavior to apps.  I can easily see an app slapping an ArrayStride on the pointer and expecting that struct to implicitly turn into an array of structs and being a bit shocked when they get GPU hangs because of trying to access some random texture as an SSBO.  We could attempt to be a bit "smarter" and go looking for an ArrayStride decoration and, if we find one, use that instead of incrementing the block index.  However, that seems like a recipe for disaster because the behavior suddenly depends on adding a decoration.<br><br>Really, this whole mess is an unfortunate complication arising from attempting to add variable pointers onto a description of resource descriptors that's based on GLSL syntax.  I'm in the process of trying to get things clarified within the SPIR-V working group in Khronos.  In the mean time, I believe this to be the best and most reasonable interpretation of the spec as-written today.<br><br></div>--Jason<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 30, 2017 at 7:06 PM, Jason Ekstrand <span dir="ltr"><<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The SPIR-V spec is a bit underspecified when it comes to exactly how<br>
you're allowed to use OpPtrAccessChain and what it means in certain edge<br>
cases.  In particular, what if the base pointer of the OpPtrAccessChain<br>
points to the base struct of an SSBO instead of an element in that SSBO.<br>
The original variable pointers implementation in mesa assumed that you<br>
weren't allowed to do an OpPtrAccessChain that adjusted the block index<br>
and asserted such.  However, there are some CTS tests that do this and,<br>
if the CTS does it, someone will do it in the wild so we should probably<br>
handle it.  With this commit, we significantly reduce our assumptions<br>
and should be able to handle more-or-less anything.<br>
<br>
The one assumption we still make for correctness is that if we see an<br>
OpPtrAccessChain on a pointer to a struct decorated block that the block<br>
index should be adjusted.  In theory, someone could try to put an array<br>
stride on such a pointer and try to make the SSBO an implicit array of<br>
the base struct and we would not give them what they want.  That said,<br>
any index other than 0 would count as an out-of-bounds access which is<br>
invalid.<br>
---<br>
 src/compiler/spirv/vtn_<wbr>variables.c | 128 ++++++++++++++++++++++++------<wbr>-------<br>
 1 file changed, 83 insertions(+), 45 deletions(-)<br>
<br>
diff --git a/src/compiler/spirv/vtn_<wbr>variables.c b/src/compiler/spirv/vtn_<wbr>variables.c<br>
index 09b3d35..89ce939 100644<br>
--- a/src/compiler/spirv/vtn_<wbr>variables.c<br>
+++ b/src/compiler/spirv/vtn_<wbr>variables.c<br>
@@ -157,6 +157,22 @@ vtn_variable_resource_index(<wbr>struct vtn_builder *b, struct vtn_variable *var,<br>
    return &instr->dest.ssa;<br>
 }<br>
<br>
+static nir_ssa_def *<br>
+vtn_resource_reindex(struct vtn_builder *b, nir_ssa_def *base_index,<br>
+                     nir_ssa_def *offset_index)<br>
+{<br>
+   nir_intrinsic_instr *instr =<br>
+      nir_intrinsic_instr_create(b-><wbr>nb.shader,<br>
+                                 nir_intrinsic_vulkan_resource_<wbr>reindex);<br>
+   instr->src[0] = nir_src_for_ssa(base_index);<br>
+   instr->src[1] = nir_src_for_ssa(offset_index);<br>
+<br>
+   nir_ssa_dest_init(&instr-><wbr>instr, &instr->dest, 1, 32, NULL);<br>
+   nir_builder_instr_insert(&b-><wbr>nb, &instr->instr);<br>
+<br>
+   return &instr->dest.ssa;<br>
+}<br>
+<br>
 static struct vtn_pointer *<br>
 vtn_ssa_offset_pointer_<wbr>dereference(struct vtn_builder *b,<br>
                                    struct vtn_pointer *base,<br>
@@ -167,47 +183,61 @@ vtn_ssa_offset_pointer_<wbr>dereference(struct vtn_builder *b,<br>
    struct vtn_type *type = base->type;<br>
<br>
    unsigned idx = 0;<br>
-   if (deref_chain->ptr_as_array) {<br>
-      /* We need ptr_type for the stride */<br>
-      assert(base->ptr_type);<br>
-      /* This must be a pointer to an actual element somewhere */<br>
-      assert(offset);<br>
-      assert(block_index || base->mode == vtn_variable_mode_workgroup);<br>
-      /* We need at least one element in the chain */<br>
-      assert(deref_chain->length >= 1);<br>
+   if (base->mode == vtn_variable_mode_ubo ||<br>
+       base->mode == vtn_variable_mode_ssbo) {<br>
+      if (!block_index) {<br>
+         assert(base->var && base->type);<br>
+         nir_ssa_def *desc_arr_idx;<br>
+         if (glsl_type_is_array(type-><wbr>type)) {<br>
+            if (deref_chain->length >= 1) {<br>
+               desc_arr_idx =<br>
+                  vtn_access_link_as_ssa(b, deref_chain->link[0], 1);<br>
+               idx++;<br>
+               /* This consumes a level of type */<br>
+               type = type->array_element;<br>
+            } else {<br>
+               /* This is annoying.  We've been asked for a pointer to the<br>
+                * array of UBOs/SSBOs and not a specifc buffer.  Return a<br>
+                * pointer with a descriptor index of 0 and we'll have to do<br>
+                * a reindex later to adjust it to the right thing.<br>
+                */<br>
+               desc_arr_idx = nir_imm_int(&b->nb, 0);<br>
+            }<br>
+         } else if (deref_chain->ptr_as_array) {<br>
+            /* You can't have a zero-length OpPtrAccessChain */<br>
+            assert(deref_chain->length >= 1);<br>
+            desc_arr_idx = vtn_access_link_as_ssa(b, deref_chain->link[0], 1);<br>
+         } else {<br>
+            /* We have a regular non-array SSBO. */<br>
+            desc_arr_idx = NULL;<br>
+         }<br>
+         block_index = vtn_variable_resource_index(b, base->var, desc_arr_idx);<br>
+      } else if (deref_chain->ptr_as_array &&<br>
+                 type->base_type == vtn_base_type_struct && type->block) {<br>
+         /* We are doing an OpPtrAccessChain on a pointer to a struct<br>
+          * decorated block.  Do a reindex to add the first link in the<br>
+          * OpPtrAccessChain to the index we received.<br>
+          */<br>
+         assert(deref_chain->length >= 1);<br>
+         nir_ssa_def *offset_index =<br>
+            vtn_access_link_as_ssa(b, deref_chain->link[0], 1);<br>
+         idx++;<br>
<br>
-      nir_ssa_def *elem_offset =<br>
-         vtn_access_link_as_ssa(b, deref_chain->link[idx],<br>
-                                base->ptr_type->stride);<br>
-      offset = nir_iadd(&b->nb, offset, elem_offset);<br>
-      idx++;<br>
+         block_index = vtn_resource_reindex(b, block_index, offset_index);<br>
+      }<br>
    }<br>
<br>
    if (!offset) {<br>
-      /* This is the first access chain so we don't have a block index */<br>
-      assert(!block_index);<br>
+      if (base->mode == vtn_variable_mode_workgroup) {<br>
+         /* SLM doesn't need nor have a block index */<br>
+         assert(!block_index);<br>
<br>
-      assert(base->var);<br>
-      assert(base->ptr_type);<br>
-      switch (base->mode) {<br>
-      case vtn_variable_mode_ubo:<br>
-      case vtn_variable_mode_ssbo:<br>
-         if (glsl_type_is_array(type-><wbr>type)) {<br>
-            /* We need at least one element in the chain */<br>
-            assert(deref_chain->length >= 1);<br>
+         /* We need the variable for the base offset */<br>
+         assert(base->var);<br>
<br>
-            nir_ssa_def *desc_arr_idx =<br>
-               vtn_access_link_as_ssa(b, deref_chain->link[0], 1);<br>
-            block_index = vtn_variable_resource_index(b, base->var, desc_arr_idx);<br>
-            type = type->array_element;<br>
-            idx++;<br>
-         } else {<br>
-            block_index = vtn_variable_resource_index(b, base->var, NULL);<br>
-         }<br>
-         offset = nir_imm_int(&b->nb, 0);<br>
-         break;<br>
+         /* We need ptr_type for size and alignment */<br>
+         assert(base->ptr_type);<br>
<br>
-      case vtn_variable_mode_workgroup:<br>
          /* Assign location on first use so that we don't end up bloating SLM<br>
           * address space for variables which are never statically used.<br>
           */<br>
@@ -219,15 +249,29 @@ vtn_ssa_offset_pointer_<wbr>dereference(struct vtn_builder *b,<br>
             b->shader->num_shared += base->ptr_type->length;<br>
          }<br>
<br>
-         block_index = NULL;<br>
          offset = nir_imm_int(&b->nb, base->var->shared_location);<br>
-         break;<br>
+      } else {<br>
+         /* The code above should have ensured a block_index when needed. */<br>
+         assert(block_index);<br>
<br>
-      default:<br>
-         unreachable("Invalid offset pointer mode");<br>
+         /* Start off with at the start of the buffer. */<br>
+         offset = nir_imm_int(&b->nb, 0);<br>
       }<br>
    }<br>
-   assert(offset);<br>
+<br>
+   if (deref_chain->ptr_as_array && idx == 0) {<br>
+      /* We need ptr_type for the stride */<br>
+      assert(base->ptr_type);<br>
+<br>
+      /* We need at least one element in the chain */<br>
+      assert(deref_chain->length >= 1);<br>
+<br>
+      nir_ssa_def *elem_offset =<br>
+         vtn_access_link_as_ssa(b, deref_chain->link[idx],<br>
+                                base->ptr_type->stride);<br>
+      offset = nir_iadd(&b->nb, offset, elem_offset);<br>
+      idx++;<br>
+   }<br>
<br>
    for (; idx < deref_chain->length; idx++) {<br>
       switch (glsl_get_base_type(type-><wbr>type)) {<br>
@@ -1535,12 +1579,6 @@ vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)<br>
        */<br>
       assert(!ptr->offset && !ptr->block_index);<br>
<br>
-      /* We can't handle a pointer to an array of descriptors because we have<br>
-       * no way of knowing later on that we need to add to update the block<br>
-       * index when dereferencing.<br>
-       */<br>
-      assert(ptr->var && ptr->var->type->base_type == vtn_base_type_struct);<br>
-<br>
       struct vtn_access_chain chain = {<br>
          .length = 0,<br>
       };<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.0.400.gff86faf<br>
<br>
</font></span></blockquote></div><br></div>