https://gcc.gnu.org/g:728d0bebb85d72e59ee8836e82ee19fa0bef8b14
commit r17-6486-g728d0bebb85d72e59ee8836e82ee19fa0bef8b14 Author: Jakub Jelinek <[email protected]> Date: Fri Nov 20 09:14:52 2026 +0100 gimple-fold: Fix up handling of large _BitInt bit-fields in __builtin_clear_padding [PR128302] While working on the previous patch, I've noticed that the bit-field handling code in clear_padding_type just assumed smallish bit-fields. The way it is implemented is that it first calls clear_padding_add_padding which skipped the padding before the bit-field (if any) and including the bit-field itself, with all bits in there not already handled before set to all ones (padding bits) and then for really small bit-fields cleared just a few bits in the first and at the same time last partial byte or for larger cleared some bits in first partial byte, then perhaps lots of fully cleared bytes and finally handled last partial byte if any according to endianess. Now, clear_padding_add_padding has an conditional flush first and then special code if the skipped size is still too large for the working buffer we have (which is 33 clear_padding_units, so on e.g. x86 roughly 4KB) and the code was asserting that after the clear_padding_add_padding call everything we need to tweak for the bit-field is still in the working buffer. Without _BitInt, it is questionable if we ever trigger the assert, perhaps we might if we have to skip too large number of bytes (on x86 over 4KB), so perhaps some 8KB aligned :0 unnamed union member (which is just padding), some small member before it and some __int128 bit-field after it?). With _BitInt, obviously we have to trigger the assert sometimes, e.g. for _BitInt(65535) : 65531 etc. (anything larger than the buffer). The following patch fixes it by detecting these very large skip cases, and in that case don't do clear_padding_add_padding to pos + end, but only to pos + epsilon (so that we can handle there the first partial byte and perhaps clear a few bytes after it), then have special case which in a loop keeps flushing and clearing further bytes we've previously cleared in one single memset and finally clear the rest and continue with what we did. And, if needed does a separate clear_padding_add_padding to pos. 2026-11-20 Jakub Jelinek <[email protected]> PR middle-end/128302 * gimple-fold.cc (clear_padding_type): Handle very large _BitInt bit-fields in RECORD_TYPEs. * gcc.dg/builtin-clear-padding-4.c: New test. Reviewed-by: Richard Biener <[email protected]> Diff: --- gcc/gimple-fold.cc | 65 +++++++++++++++++++++++--- gcc/testsuite/gcc.dg/builtin-clear-padding-4.c | 40 ++++++++++++++++ 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 9bc88da352b0..04c585303270 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -5004,15 +5004,62 @@ clear_padding_type (clear_padding_struct *buf, tree type, bpos %= BITS_PER_UNIT; HOST_WIDE_INT end = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; + if (pos > cur_pos) + { + clear_padding_add_padding (buf, pos - cur_pos); + cur_pos = pos; + } if (pos + end > cur_pos) { - clear_padding_add_padding (buf, pos + end - cur_pos); - cur_pos = pos + end; + if ((unsigned HOST_WIDE_INT) end + > (unsigned HOST_WIDE_INT) clear_padding_buf_size / 2) + { + if ((unsigned HOST_WIDE_INT) pos + clear_padding_unit + > (unsigned HOST_WIDE_INT) cur_pos) + { + clear_padding_add_padding (buf, + pos + clear_padding_unit + - cur_pos); + cur_pos = pos + clear_padding_unit; + } + } + else + { + clear_padding_add_padding (buf, pos + end - cur_pos); + cur_pos = pos + end; + } } gcc_assert (cur_pos > pos && ((unsigned HOST_WIDE_INT) buf->size >= (unsigned HOST_WIDE_INT) cur_pos - pos)); unsigned char *p = buf->buf + buf->size - (cur_pos - pos); + HOST_WIDE_INT fldszu = 0; + auto bitfld_adjust = [&] { + memset (p, 0, buf->buf + buf->size - p); + fldszu -= buf->buf + buf->size - p; + clear_padding_flush (buf, false); + while ((unsigned HOST_WIDE_INT) fldszu + > (unsigned HOST_WIDE_INT) 2 * clear_padding_unit) + { + HOST_WIDE_INT this_sz + = MIN ((HOST_WIDE_INT) (clear_padding_buf_size + - buf->size), + (HOST_WIDE_INT) (fldszu + - 2 * clear_padding_unit)); + memset (buf->buf + buf->size, 0, this_sz); + fldszu -= this_sz; + cur_pos += this_sz; + buf->size += this_sz; + clear_padding_flush (buf, false); + } + clear_padding_add_padding (buf, pos + end - cur_pos); + cur_pos = pos + end; + pos = cur_pos - fldszu - ((fldsz % BITS_PER_UNIT) != 0); + gcc_assert (cur_pos > pos + && ((unsigned HOST_WIDE_INT) buf->size + >= (unsigned HOST_WIDE_INT) cur_pos - pos)); + p = buf->buf + buf->size - (cur_pos - pos); + }; if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) sorry_at (buf->loc, "PDP11 bit-field handling unsupported" " in %qs", "__builtin_clear_padding"); @@ -5030,8 +5077,11 @@ clear_padding_type (clear_padding_struct *buf, tree type, p++; fldsz -= BITS_PER_UNIT - bpos; } - memset (p, 0, fldsz / BITS_PER_UNIT); - p += fldsz / BITS_PER_UNIT; + fldszu = fldsz / BITS_PER_UNIT; + if (cur_pos < pos + end) + bitfld_adjust (); + memset (p, 0, fldszu); + p += fldszu; fldsz %= BITS_PER_UNIT; if (fldsz) *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; @@ -5050,8 +5100,11 @@ clear_padding_type (clear_padding_struct *buf, tree type, p++; fldsz -= BITS_PER_UNIT - bpos; } - memset (p, 0, fldsz / BITS_PER_UNIT); - p += fldsz / BITS_PER_UNIT; + fldszu = fldsz / BITS_PER_UNIT; + if (cur_pos < pos + end) + bitfld_adjust (); + memset (p, 0, fldszu); + p += fldszu; fldsz %= BITS_PER_UNIT; if (fldsz) *p &= ~((1 << fldsz) - 1); diff --git a/gcc/testsuite/gcc.dg/builtin-clear-padding-4.c b/gcc/testsuite/gcc.dg/builtin-clear-padding-4.c new file mode 100644 index 000000000000..55a722b18a27 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-clear-padding-4.c @@ -0,0 +1,40 @@ +/* PR middle-end/128302 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#if __BITINT_MAXWIDTH__ >= 65535 +struct C { _BitInt(65535) a : 65531; unsigned _BitInt(65535) b : 65531; } c1, c2, c3, c4; +#else +struct C { int a : 5; unsigned b : 5; } c1, c2; +#endif + +__attribute__((noipa)) void +foo (struct C *c) +{ + c->a = 0; + c->b = 0; +} + +__attribute__((noipa)) void +bar (struct C *c) +{ + c->a = -1; + c->b = -1; +} + +int +main () +{ + __builtin_memset (&c2, -1, sizeof (c2)); + __builtin_memset (&c4, -1, sizeof (c4)); + foo (&c1); + foo (&c2); + bar (&c3); + bar (&c4); + __builtin_clear_padding (&c2); + __builtin_clear_padding (&c4); + if (__builtin_memcmp (&c1, &c2, sizeof (c1)) + || __builtin_memcmp (&c3, &c4, sizeof (c3))) + __builtin_abort (); + return 0; +}
