public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marc Glisse <marc.glisse@inria.fr>
To: Martin Uecker <ma.uecker@gmail.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>,
	 Richard Biener <richard.guenther@gmail.com>
Subject: Re: reordering of trapping operations and volatile
Date: Sat, 8 Jan 2022 15:13:02 +0100 (CET)	[thread overview]
Message-ID: <f416c5cd-cc3a-e856-4f24-9f66a749b3e1@hippo.saclay.inria.fr> (raw)
In-Reply-To: <30b077c2231d31e0fb345584adef27ebe233c418.camel@gmail.com>

On Sat, 8 Jan 2022, Martin Uecker via Gcc wrote:

> Am Samstag, den 08.01.2022, 13:41 +0100 schrieb Richard Biener:
>> On January 8, 2022 9:32:24 AM GMT+01:00, Martin Uecker <ma.uecker@gmail.com> wrote:
>>> Hi Richard,
>
> thank you for your quick response!
>
>>> 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
>>>
>>> In this example a division is hoisted
>>> before the volatile store. (the division
>>> by zero which could trap is UB, of course).
>>>
>>> As Martin Sebor pointed out this is done
>>> as part of redundancy elimination
>>> in tree-ssa-pre.c and that this might
>>> simply be an oversight (and could then be
>>> fixed with a small change).
>>>
>>> Could you clarify whether such reordering
>>> is intentional and could be exploited in
>>> general also in other optimizations or
>>> confirm that this is an oversight that
>>> affects only this specific case?
>>>
>>> If this is intentional, are there examples
>>> where this is important for optimization?
>>
>> In general there is no data flow information that
>> prevents traps from being reordered with respect
>> to volatile accesses.
>
> Yes, although I think potentially trapping ops
> are not moved before calls (as this would be
> incorrect).  So do you think it would be feasable
> to prevent this for volatile too?
>
>> The specific case could be
>> easily mitigated in PRE. Another case would be
>>
>> A = c / d;
>> X = 1;
>> If (use_a)
>>   Bar (a);
>>
>> Where we'd sink a across x into the guarded Bb I suspect.
>
> Yes. Related example:
>
> https://godbolt.org/z/5WGhadre3
>
> volatile int x;
> void bar(int a);
>
> void foo(int c, int d)
> {
>  int a = c / d;
>  x = 1;
>  if (d)
>    bar(a);
> }
>
> foo:
>        mov     DWORD PTR x[rip], 1
>        test    esi, esi
>        jne     .L4
>        ret
> .L4:
>        mov     eax, edi
>        cdq
>        idiv    esi
>        mov     edi, eax
>        jmp     bar
>
>
> It would be nice to prevent this too, although
> I am less concerned about this direction, as
> the UB has already happened so there is not
> much we could guarantee about this anyway.
>
> In the other case, it could affect correct
> code before the trap.

-fnon-call-exceptions helps with the first testcase but not with the 
second one. I don't know if that's by accident, but the flag seems 
possibly relevant.

-- 
Marc Glisse

  reply	other threads:[~2022-01-08 14:13 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 [this message]
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
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=f416c5cd-cc3a-e856-4f24-9f66a749b3e1@hippo.saclay.inria.fr \
    --to=marc.glisse@inria.fr \
    --cc=gcc@gcc.gnu.org \
    --cc=ma.uecker@gmail.com \
    --cc=richard.guenther@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).