Module: Mesa
Branch: main
Commit: 629af540ca1811b67587051151bff3d2a66130ca
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=629af540ca1811b67587051151bff3d2a66130ca

Author: Faith Ekstrand <[email protected]>
Date:   Tue Dec  5 10:11:46 2023 -0600

spirv: Plumb variable alignments through to NIR

Reviewed-by: Karol Herbst <[email protected]>
Reviewed-by: Jesse Natalie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26522>

---

 src/compiler/nir/nir.h             |  3 +++
 src/compiler/spirv/vtn_variables.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 47d1795b02d..b5eb056184e 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -750,6 +750,9 @@ typedef struct nir_variable {
        */
       int location;
 
+      /** Required alignment of this variable */
+      unsigned alignment;
+
       /**
        * The actual location of the variable in the IR. Only valid for inputs,
        * outputs, uniforms (including samplers and images), and for UBO and 
SSBO
diff --git a/src/compiler/spirv/vtn_variables.c 
b/src/compiler/spirv/vtn_variables.c
index 0b6f69145e8..f5cdba013b3 100644
--- a/src/compiler/spirv/vtn_variables.c
+++ b/src/compiler/spirv/vtn_variables.c
@@ -1512,6 +1512,28 @@ gather_var_kind_cb(struct vtn_builder *b, struct 
vtn_value *val, int member,
    }
 }
 
+static void
+var_set_alignment(struct vtn_builder *b, struct vtn_variable *vtn_var,
+                  uint32_t alignment)
+{
+   if (alignment == 0) {
+      vtn_warn("Specified alignment is zero, ignoring");
+      return;
+   }
+
+   if (!util_is_power_of_two_or_zero(alignment)) {
+      /* This isn't actually a requirement anywhere in any spec but it seems
+       * reasonable to enforce.
+       */
+      unsigned real_align = 1 << (ffs(alignment) - 1);
+      vtn_warn("Alignment of %u specified, which not a power of two, "
+               "using %u instead", alignment, real_align);
+      alignment = real_align;
+   }
+
+   vtn_var->var->data.alignment = alignment;
+}
+
 static void
 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
                   const struct vtn_decoration *dec, void *void_var)
@@ -1531,6 +1553,12 @@ var_decoration_cb(struct vtn_builder *b, struct 
vtn_value *val, int member,
       vtn_var->input_attachment_index = dec->operands[0];
       vtn_var->access |= ACCESS_NON_WRITEABLE;
       return;
+   case SpvDecorationAlignment:
+      var_set_alignment(b, vtn_var, dec->operands[0]);
+      break;
+   case SpvDecorationAlignmentId:
+      var_set_alignment(b, vtn_var, vtn_constant_uint(b, dec->operands[0]));
+      break;
    case SpvDecorationPatch:
       vtn_var->var->data.patch = true;
       break;

Reply via email to