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

Author: Karol Herbst <[email protected]>
Date:   Sat Jun 17 08:50:12 2023 +0200

compiler/types: fix size of padded OpenCL Structs

In C the size of a struct { uin32_t a; uint8_t b; } is 8, not 5, so we have
to account for the biggest alignment across all struct members.

Funny that the OpenCL CTS doesn't catch that.

Fixes: 44d32e62fb8 ("glsl: add cl_size and cl_alignment")
Signed-off-by: Karol Herbst <[email protected]>
Reviewed-by: Jesse Natalie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23701>

---

 src/compiler/glsl_types.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/compiler/glsl_types.cpp b/src/compiler/glsl_types.cpp
index 2cff223a984..64dd8caa71f 100644
--- a/src/compiler/glsl_types.cpp
+++ b/src/compiler/glsl_types.cpp
@@ -3310,13 +3310,20 @@ glsl_type::cl_size() const
       return size * this->length;
    } else if (this->is_struct()) {
       unsigned size = 0;
+      unsigned max_alignment = 1;
       for (unsigned i = 0; i < this->length; ++i) {
          struct glsl_struct_field &field = this->fields.structure[i];
          /* if a struct is packed, members don't get aligned */
-         if (!this->packed)
-            size = align(size, field.type->cl_alignment());
+         if (!this->packed) {
+            unsigned alignment = field.type->cl_alignment();
+            max_alignment = MAX2(max_alignment, alignment);
+            size = align(size, alignment);
+         }
          size += field.type->cl_size();
       }
+
+      /* Size of C structs are aligned to the biggest alignment of its fields 
*/
+      size = align(size, max_alignment);
       return size;
    }
    return 1;

Reply via email to