https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128772

            Bug ID: 128772
           Summary: Classification of zero-length and flexible array
                    members: GCC and clang disagree
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ubizjak at gmail dot com
  Target Milestone: ---

GCC and clang classify structs ending in a zero-length array or a flexible
array member differently on x86-64, and I could not find wording in the psABI
(ABI 1.0, March 12, 2025) that says which is correct.

Test cases, each returning a struct by value:

    struct S5 { float x; int a[0]; };
    struct S5 f5 (float v) { return (struct S5) { v }; }

    struct S5 { float x; int a[]; };   /* flexible array member */
    struct S5 f5 (float v) { return (struct S5) { v }; }

In both cases sizeof(struct S5) is 4, so there is one eightbyte. Output at -O2:

    a[0]:
      GCC 16.2.1:    movd %xmm0, %eax ; ret    -> INTEGER, returned in %eax
      clang 22.1.8:  retq                      -> SSE, returned in %xmm0

    a[] (flexible array member):
      GCC 16.2.1:    ret                       -> SSE, returned in %xmm0
                     (with a note that the ABI for structs with a flexible
                     array member changed in GCC 4.4)
      clang 22.1.8:  movq %rdi, %rax ; movss %xmm0, (%rdi) ; retq
                                               -> MEMORY, returned via a hidden
pointer

So the three non-matching combinations return in %eax vs. %xmm0 vs. memory.
Code built by one compiler and called from the other reads the wrong location.

What the spec says: NO_CLASS is "used for padding and empty structures and
unions", and footnote 18 says zero-width bit-fields are NO_CLASS.
Classification rule 4 merges the classes of the fields within an eightbyte,
where NO_CLASS yields the other class (4b) and INTEGER beats SSE (4d). I found
nothing about zero-length arrays or flexible array members, so the result
depends on whether such a member contributes NO_CLASS, its element class, or
forces MEMORY.

>From the output, GCC seems to classify a zero-length array by its element type
but ignore a flexible array member, while clang ignores a zero-length array but
treats a struct with a flexible array member as MEMORY.

The flexible array member is standard C99, so this is not only a GNU-extension
problem.

Reply via email to