Hi! The comments about _Atomic _BitInt made me figure out I forgot (although earlier was planning to do that) to implement __builtin_clear_padding support for _BitInt.
The following patch (incremental to the _BitInt series) does that. 2023-07-28 Jakub Jelinek <ja...@redhat.com> PR c/102989 * gimple-fold.cc (clear_padding_unit): Mention in comment that _BitInt types don't need to fit either. (clear_padding_bitint_needs_padding_p): New function. (clear_padding_type_may_have_padding_p): Handle BITINT_TYPE. (clear_padding_type): Likewise. * gcc.dg/bitint-16.c: New test. --- gcc/gimple-fold.cc.jj 2023-07-11 15:28:54.704679510 +0200 +++ gcc/gimple-fold.cc 2023-07-28 12:37:18.971789595 +0200 @@ -4103,8 +4103,8 @@ gimple_fold_builtin_realloc (gimple_stmt return false; } -/* Number of bytes into which any type but aggregate or vector types - should fit. */ +/* Number of bytes into which any type but aggregate, vector or + _BitInt types should fit. */ static constexpr size_t clear_padding_unit = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT; /* Buffer size on which __builtin_clear_padding folding code works. */ @@ -4595,6 +4595,26 @@ clear_padding_real_needs_padding_p (tree && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95)); } +/* _BitInt has padding bits if it isn't extended in the ABI and has smaller + precision than bits in limb or corresponding number of limbs. */ + +static bool +clear_padding_bitint_needs_padding_p (tree type) +{ + struct bitint_info info; + gcc_assert (targetm.c.bitint_type_info (TYPE_PRECISION (type), &info)); + if (info.extended) + return false; + scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode); + if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode)) + return true; + else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode)) + return false; + else + return (((unsigned) TYPE_PRECISION (type)) + % GET_MODE_PRECISION (limb_mode)) != 0; +} + /* Return true if TYPE might contain any padding bits. */ bool @@ -4611,6 +4631,8 @@ clear_padding_type_may_have_padding_p (t return clear_padding_type_may_have_padding_p (TREE_TYPE (type)); case REAL_TYPE: return clear_padding_real_needs_padding_p (type); + case BITINT_TYPE: + return clear_padding_bitint_needs_padding_p (type); default: return false; } @@ -4855,6 +4877,57 @@ clear_padding_type (clear_padding_struct memset (buf->buf + buf->size, ~0, sz); buf->size += sz; break; + case BITINT_TYPE: + { + struct bitint_info info; + gcc_assert (targetm.c.bitint_type_info (TYPE_PRECISION (type), &info)); + scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode); + if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode)) + { + gcc_assert ((size_t) sz <= clear_padding_unit); + if ((unsigned HOST_WIDE_INT) sz + buf->size + > clear_padding_buf_size) + clear_padding_flush (buf, false); + if (!info.extended + && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode)) + { + int tprec = GET_MODE_PRECISION (limb_mode); + int prec = TYPE_PRECISION (type); + tree t = build_nonstandard_integer_type (tprec, 1); + tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec)); + int len = native_encode_expr (cst, buf->buf + buf->size, sz); + gcc_assert (len > 0 && (size_t) len == (size_t) sz); + } + else + memset (buf->buf + buf->size, 0, sz); + buf->size += sz; + break; + } + tree limbtype + = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1); + fldsz = int_size_in_bytes (limbtype); + nelts = int_size_in_bytes (type) / fldsz; + for (HOST_WIDE_INT i = 0; i < nelts; i++) + { + if (!info.extended + && i == (info.big_endian ? 0 : nelts - 1) + && (((unsigned) TYPE_PRECISION (type)) + % TYPE_PRECISION (limbtype)) != 0) + { + int tprec = GET_MODE_PRECISION (limb_mode); + int prec = (((unsigned) TYPE_PRECISION (type)) % tprec); + tree cst = wide_int_to_tree (limbtype, + wi::mask (prec, true, tprec)); + int len = native_encode_expr (cst, buf->buf + buf->size, + fldsz); + gcc_assert (len > 0 && (size_t) len == (size_t) fldsz); + buf->size += fldsz; + } + else + clear_padding_type (buf, limbtype, fldsz, for_auto_init); + } + break; + } default: gcc_assert ((size_t) sz <= clear_padding_unit); if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size) --- gcc/testsuite/gcc.dg/bitint-16.c.jj 2023-07-28 12:47:37.641180784 +0200 +++ gcc/testsuite/gcc.dg/bitint-16.c 2023-07-28 12:47:30.488280320 +0200 @@ -0,0 +1,31 @@ +/* PR c/102989 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2 -std=c2x -pedantic-errors" } */ + +_BitInt(15) a; +_BitInt(42) b; +#if __BITINT_MAXWIDTH__ >= 115 +_BitInt(115) c; +#endif +#if __BITINT_MAXWIDTH__ >= 192 +_BitInt(192) d; +#endif +#if __BITINT_MAXWIDTH__ >= 575 +_BitInt(575) e; +#endif + +int +main () +{ + __builtin_clear_padding (&a); + __builtin_clear_padding (&b); +#if __BITINT_MAXWIDTH__ >= 115 + __builtin_clear_padding (&c); +#endif +#if __BITINT_MAXWIDTH__ >= 192 + __builtin_clear_padding (&d); +#endif +#if __BITINT_MAXWIDTH__ >= 575 + __builtin_clear_padding (&e); +#endif +} Jakub