From: Martin Uecker <ma.uecker@gmail.com>
To: Andrew Pinski <pinskia@gmail.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: reordering of trapping operations and volatile
Date: Sat, 08 Jan 2022 22:07:57 +0100 [thread overview]
Message-ID: <bc09552c7b4b6b0c7e9d9f5b3bbff1275dc412b1.camel@gmail.com> (raw)
In-Reply-To: <CA+=Sn1ni3nAq3aGBJkV_=ie56EeeO2zXZK3gkq0nGM6f=-Jd_A@mail.gmail.com>
Am Samstag, den 08.01.2022, 10:35 -0800 schrieb Andrew Pinski:
> On Sat, Jan 8, 2022 at 12:33 AM Martin Uecker via Gcc <gcc@gcc.gnu.org> wrote:
> >
> > Hi Richard,
> >
> > I have a question regarding reodering of volatile
> > accesses and trapping operations. My initial
> > assumption (and hope) was that compilers take
> > care to avoid creating traps that are incorrectly
> > ordered relative to observable behavior.
> >
> > I had trouble finding examples, and my cursory
> > glace at the code seemed to confirm that GCC
> > carefully avoids this. But then someone showed
> > me this example, where this can happen in GCC:
> >
> >
> > volatile int x;
> >
> > int foo(int a, int b, _Bool store_to_x)
> > {
> > if (!store_to_x)
> > return a / b;
> > x = b;
> > return a / b;
> > }
> >
> >
> > https://godbolt.org/z/vq3r8vjxr
>
> The question becomes what is a trapping instruction vs an undefined
> instruction?
> For floating point types, it is well defined what is a trapping
> instruction while for integer types it is not well defined.
> On some (many?) targets dividing by 0 is just undefined and does not
> trap (powerpc, aarch64, arm and many others; MIPS it depends on the
> options passed to GCC if the conditional trap should be inserted or
> not).
> The other side is if there is undefined code on the path, should
> observable results happen first (stores to volatile/atomics, etc.)?
I think for volatile stores and I/O, I think it would be
nice of we could guarantee that those happen before the UB
ruins the day. (I am not sure about atomics, those are
not directly obsevable)
For I/O this is probably already the case (?).
For volatile, it seems this would need some tweaks.
I am trying to figure out whether this is feasible.
> GCC assumes by default that divide is trappable but stores not are not
> observable. This is where -fnon-call-exceptions come into play.
Ok, thanks! I will look at this!
> In the second case, GCC assumes reducing trappable instructions are
> fine.
-fnon-call-exceptions would treat trapping instructions
as defined (and trapping) instead of UB? This is
then probably even stronger than the requirement above.
> Note I thought -fno-delete-dead-exceptions would fix the sink
> but it didn't.
Martin
next prev parent reply other threads:[~2022-01-08 21:07 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-08 8:32 Martin Uecker
2022-01-08 12:41 ` Richard Biener
2022-01-08 13:50 ` Martin Uecker
2022-01-08 14:13 ` Marc Glisse
2022-01-08 14:41 ` Eric Botcazou
2022-01-08 15:27 ` Martin Uecker
2022-01-08 17:33 ` Eric Botcazou
2022-01-08 15:03 ` David Brown
2022-01-08 16:42 ` Martin Uecker
2022-01-08 18:35 ` Andrew Pinski
2022-01-08 21:07 ` Martin Uecker [this message]
2022-01-10 9:04 ` Richard Biener
2022-01-10 17:36 ` Martin Uecker
2022-01-11 7:11 ` Richard Biener
2022-01-11 8:17 ` Martin Uecker
2022-01-11 9:13 ` Richard Biener
2022-01-11 20:01 ` Martin Uecker
2022-01-13 16:45 ` Michael Matz
2022-01-13 19:17 ` Martin Uecker
2022-01-14 14:15 ` Michael Matz
2022-01-14 14:58 ` Paul Koning
2022-01-15 21:28 ` Martin Sebor
2022-01-15 21:38 ` Paul Koning
2022-01-16 12:37 ` Martin Uecker
2022-01-14 15:46 ` Martin Uecker
2022-01-14 19:54 ` Jonathan Wakely
2022-01-15 9:00 ` Martin Uecker
2022-01-15 16:33 ` Jonathan Wakely
2022-01-15 18:48 ` Martin Uecker
2022-01-17 14:10 ` Michael Matz
2022-01-18 8:31 ` Richard Biener
2022-01-21 16:21 ` Martin Uecker
2022-01-11 18:17 ` David Brown
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=bc09552c7b4b6b0c7e9d9f5b3bbff1275dc412b1.camel@gmail.com \
--to=ma.uecker@gmail.com \
--cc=gcc@gcc.gnu.org \
--cc=pinskia@gmail.com \
/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).