public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Joseph Myers <joseph@codesourcery.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: <gcc-patches@gcc.gnu.org>, Richard Biener <rguenther@suse.de>
Subject: Re: _BitInt vs. _Atomic
Date: Fri, 28 Jul 2023 18:32:32 +0000	[thread overview]
Message-ID: <ba9b91a5-d5e7-8c20-5c21-4e9de5253c61@codesourcery.com> (raw)
In-Reply-To: <ZMOp7aGbHVg/Ba4H@tucnak>

On Fri, 28 Jul 2023, Jakub Jelinek via Gcc-patches wrote:

> The C++ way of dealing with this is using __builtin_clear_padding,
> done on atomic stores/updates of the atomic memory (padding is cleared
> if any on the value to be stored, or on the expected and desired values).
> 
> I don't know enough about the C atomic requirements whether that is feasible
> for it as well, or whether it is possible to make the padding bits partially
> or fully set somehow non-atomically without invoking UB and then make it
> never match.

If padding bits not being reliably preserved causes problems for the 
compare-exchange loops in C in practice, then it would seem reasonable to 
use __builtin_clear_padding internally as part of implementing those cases 
of atomic compound assignment.

> And another issue is that while __atomic_load, __atomic_store,
> __atomic_exchange and __atomic_compare_exchange work on arbitrary _BitInt
> sizes, others like __atomic_fetch_add only support _BitInt or other integral
> types which have size of 1, 2, 4, 8 or 16 bytes, others emit an error
> in c-family/c-common.cc (sync_resolve_size).  So, either
> resolve_overloaded_builtin should for the case when pointer is pointer to
> _BitInt which doesn't have 1, 2, 4, 8 or 16 bytes size lower those into
> a loop using __atomic_compare_exchange (or perhaps also if there is
> padding), or <stdatomic.h> should do that.

The <stdatomic.h> interfaces definitely need to work with _BitInt.  My 
guess is that doing this with the built-in expansion would be more robust 
than putting more complicated definitions in the header that choose which 
built-in functions to use depending on properties of the type (and keeping 
the built-in functions limited to certain widths), but I don't know.

Note also that those <stdatomic.h> operations have no undefined behavior 
on signed integer overflow.

If any ABIs require sign / zero extension of _BitInt values in memory, 
care would also be needed in the case of (size of 1, 2, 4, 8 or 16 bytes, 
but also has high bits required to be sign / zero extended) to ensure that 
the operations are implemented so as to leave the high bits with the 
expected values in case of overflow, which wouldn't result from simply 
using the underlying operation for a type with the full precision of its 
memory size.

-- 
Joseph S. Myers
joseph@codesourcery.com

      parent reply	other threads:[~2023-07-28 18:32 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27 17:16 [PATCH 5/5] testsuite part 2 for _BitInt support [PR102989] Jakub Jelinek
2023-07-27 19:06 ` Joseph Myers
2023-07-28 11:43   ` _BitInt vs. _Atomic Jakub Jelinek
2023-07-28 14:03     ` Martin Uecker
2023-07-28 14:06       ` Martin Uecker
2023-07-28 14:26       ` Jakub Jelinek
2023-07-28 14:53         ` Martin Uecker
2023-07-28 15:10           ` Jakub Jelinek
2023-07-28 16:01             ` Martin Uecker
2023-07-31 14:33               ` Michael Matz
2023-07-31 15:37                 ` Martin Uecker
2023-08-01 15:54                   ` Michael Matz
2023-08-01 16:54                     ` Joseph Myers
2023-08-02 12:25                       ` Michael Matz
2023-08-01 19:03                     ` Martin Uecker
2023-07-28 18:32     ` Joseph Myers [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba9b91a5-d5e7-8c20-5c21-4e9de5253c61@codesourcery.com \
    --to=joseph@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).