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

--- Comment #1 from Uroš Bizjak <ubizjak at gmail dot com> ---
Unnamed bit fields are under-specified in x86_64 psABI.

Section 3.2.3 says NO_CLASS "will be used for padding and empty structures and
unions", and the footnote on rule 4 says "Zero-width bit-fields are NO_CLASS".
Nothing is said about unnamed bit-fields with a non-zero width (for example
"unsigned char : 5;"). In C they are padding, but they are still declared
fields.

GCC and clang compilers disagree:

  - GCC classifies them as INTEGER, like named bit-fields. Only zero-width
bit-fields are skipped.
  - clang ignores all unnamed bit-fields.

Two small examples, checked with GCC 16.2.1 and clang 22.1.8 (-O2):

  struct A { unsigned long f; unsigned char : 5; };   /* sizeof 16 */
  int ga (struct A a, int x) { return x; }
  /* GCC: A in %rdi:%rsi, x in %edx. clang: A in %rdi, x in %esi. */

  struct B { long : 17; };                            /* sizeof 3 */
  int gb (struct B b, int x) { return x; }
  /* GCC: B in %rdi, x in %esi. clang: B not passed in a register, x in %edi.
*/

So the same prototype gets different register assignments depending on the
compiler, which silently breaks mixed GCC/clang code.

In the psABI, the only bit-field classification rule is footnote 18:
“Zero-width bit-fields are NO_CLASS.” The bullet at Page 20 only says unnamed
bit-fields don’t affect struct alignment. It says nothing that makes non-zero
unnamed bit-fields NO_CLASS. GCC classifies them as INTEGER and clang 18
ignores them. GCC’s reading is closer to the literal text, but the text doesn’t
settle it.

Reply via email to