Hi! Clear padding in unions is special, we basically compute bitmask which bits aren't padding (0s) and which are padding (1s) for each union member individually and using clear_padding_flush then and it together because we don't know which union member is active, so only padding bits for all the union members are cleared. This PR reports that I forgot to handle bit-fields in there. It calls clear_padding_type with fldsz which is DECL_SIZE_UNIT value, so the function can clear up to 7 bits more than it should. This patch just ors in the padding bits in the last byte back.
Bootstrapped/regtested on x86_64-linux and i686-linux, eyeballed what it does on powerpc64-linux. Ok for trunk? 2026-11-19 Jakub Jelinek <[email protected]> PR middle-end/128302 * gimple-fold.cc (clear_padding_union): Handle DECL_BIT_FIELD inside of a union. * gcc.dg/builtin-clear-padding-3.c: New test. --- a/gcc/gimple-fold.cc 2026-11-17 22:41:32.369250305 +0100 +++ b/gcc/gimple-fold.cc 2026-11-18 08:33:00.754346919 +0100 @@ -4781,6 +4781,22 @@ clear_padding_union (clear_padding_struc union_buf->size = start_size; memset (union_buf->buf, ~0, start_size); clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init); + if (DECL_BIT_FIELD (field) + && (TYPE_PRECISION (TREE_TYPE (field)) % BITS_PER_UNIT) != 0) + { + HOST_WIDE_INT bits + = TYPE_PRECISION (TREE_TYPE (field)) % BITS_PER_UNIT; + if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) + sorry_at (buf->loc, "PDP11 bit-field handling unsupported" + " in %qs", "__builtin_clear_padding"); + else if (BYTES_BIG_ENDIAN) + /* Big endian. */ + union_buf->buf[union_buf->size - 1] + |= ((1U << BITS_PER_UNIT) - 1) >> bits; + else + /* Little endian. */ + union_buf->buf[union_buf->size - 1] |= ~((1 << bits) - 1); + } clear_padding_add_padding (union_buf, sz - fldsz); clear_padding_flush (union_buf, true); } --- a/gcc/testsuite/gcc.dg/builtin-clear-padding-3.c 2026-11-18 08:07:12.713850640 +0100 +++ b/gcc/testsuite/gcc.dg/builtin-clear-padding-3.c 2026-11-18 08:24:29.520123788 +0100 @@ -0,0 +1,45 @@ +/* PR middle-end/128302 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +union A { int a : 15; unsigned b : 15; } a1, a2; +#if __BITINT_MAXWIDTH__ >= 951 +union B { _BitInt(951) a : 739; unsigned _BitInt(951) b : 739; } b1, b2; +#else +union B { int a : 5; unsigned b : 5; } b1, b2; +#endif +#if __BITINT_MAXWIDTH__ >= 65535 +union C { _BitInt(65535) a : 65531; unsigned _BitInt(65535) b : 65531; } c1, c2; +#else +union C { int a : 5; unsigned b : 5; } c1, c2; +#endif + +__attribute__((noipa)) void +foo (union A *a, union B *b, union C *c) +{ + a->a = -1361; +#if __BITINT_MAXWIDTH__ >= 951 + b->a = 878212441949088910277769330824589426446069476594189768023517947696484604789122065870316179286548943333278002443986694487060345108557457062615946090733223961247635344029067055511695459419265707468087255861705908585308874670wb; +#else + b->a = 9; +#endif + c->a = 0; +} + +int +main () +{ + __builtin_memset (&a2, -1, sizeof (a2)); + __builtin_memset (&b2, -1, sizeof (b2)); + __builtin_memset (&c2, -1, sizeof (c2)); + foo (&a1, &b1, &c1); + foo (&a2, &b2, &c2); + __builtin_clear_padding (&a2); + __builtin_clear_padding (&b2); + __builtin_clear_padding (&c2); + if (__builtin_memcmp (&a1, &a2, sizeof (a1)) + || __builtin_memcmp (&b1, &b2, sizeof (b1)) + || __builtin_memcmp (&c1, &c2, sizeof (c1))) + __builtin_abort (); + return 0; +} Jakub
