On Thu, 19 Nov 2026, Jakub Jelinek wrote:

> Hi!
> 
> 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.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

LGTM.  Looks like sashiko has suggestions to fix up more corner cases
(and to the previous patch it finds some serious issues).

Thanks,
Richard.

> 2026-11-19  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.
> 
> --- a/gcc/gimple-fold.cc      2026-11-18 08:33:00.754346919 +0100
> +++ b/gcc/gimple-fold.cc      2026-11-18 12:17:25.558751272 +0100
> @@ -5022,13 +5022,55 @@ clear_padding_type (clear_padding_struct
>                 = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
>               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");
> @@ -5046,8 +5088,11 @@ clear_padding_type (clear_padding_struct
>                           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;
> @@ -5066,8 +5111,11 @@ clear_padding_type (clear_padding_struct
>                           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);
> --- a/gcc/testsuite/gcc.dg/builtin-clear-padding-4.c  2026-11-18 
> 10:22:54.167885478 +0100
> +++ b/gcc/testsuite/gcc.dg/builtin-clear-padding-4.c  2026-11-18 
> 12:27:23.439818433 +0100
> @@ -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;
> +}
> 
>       Jakub
> 
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to