public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond
@ 2023-01-05  9:09 daniel.lundin.mail at gmail dot com
  2023-01-05  9:18 ` [Bug c/108298] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: daniel.lundin.mail at gmail dot com @ 2023-01-05  9:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

            Bug ID: 108298
           Summary: Wrong optimization of volatile access from gcc 11 and
                    beyond
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daniel.lundin.mail at gmail dot com
  Target Milestone: ---

Created attachment 54194
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54194&action=edit
Example to reproduce incorrect optimizations

Before gcc 11.0.0, the attached example compiled under -O3 would result in the
whole function getting optimized into a single load instruction when volatile
was not used. Adding a volatile access would lead to instructions for fetching
the value from the stack getting generated.

This appears to be conforming behavior, references ISO/IEC 9899:2018 5.1.2.3,
particularly §2, §4 and §6. I believe gcc 10.4 or older behaves correctly.

After gcc 11.0.0, the function is optimized regardless of if volatile is used
or not. See attached example. With FREE_TO_OPTIMIZE defined, I would expect the
compiler to optimize the code under -O3. Without it, I would expect the
compiler to generate a read instruction since this is a "side effect".

For convenience, here is also an online compiler example for x86-64 Linux
https://godbolt.org/z/4qez1P746

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
@ 2023-01-05  9:18 ` pinskia at gcc dot gnu.org
  2023-01-05 11:25 ` segher at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-05  9:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 33053.
C2x changes things which might be the reason for change in gcc 12.

*** This bug has been marked as a duplicate of bug 33053 ***

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
  2023-01-05  9:18 ` [Bug c/108298] " pinskia at gcc dot gnu.org
@ 2023-01-05 11:25 ` segher at gcc dot gnu.org
  2023-01-05 11:29 ` daniel.lundin.mail at gmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: segher at gcc dot gnu.org @ 2023-01-05 11:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

Segher Boessenkool <segher at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|DUPLICATE                   |---
   Last reconfirmed|                            |2023-01-05
             Status|RESOLVED                    |NEW
     Ever confirmed|0                           |1
                 CC|                            |segher at gcc dot gnu.org

--- Comment #2 from Segher Boessenkool <segher at gcc dot gnu.org> ---
This is not a dup of 33053 (see PR33053#c5 and PR33053#c6).  Reopening, and
confirmed.  There should be a read from memory: that is a side effect, it has
to be executed in the real machine as in the abstract machine.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
  2023-01-05  9:18 ` [Bug c/108298] " pinskia at gcc dot gnu.org
  2023-01-05 11:25 ` segher at gcc dot gnu.org
@ 2023-01-05 11:29 ` daniel.lundin.mail at gmail dot com
  2023-01-05 14:55 ` segher at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel.lundin.mail at gmail dot com @ 2023-01-05 11:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #3 from Daniel Lundin <daniel.lundin.mail at gmail dot com> ---
(In reply to Segher Boessenkool from comment #2)
> This is not a dup of 33053 (see PR33053#c5 and PR33053#c6).  Reopening, and
> confirmed.  There should be a read from memory: that is a side effect, it has
> to be executed in the real machine as in the abstract machine.

This is however very much related to DR476 which is now implemented as per C23.
So conformance to DR476 should be on the C23 TODO-list.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (2 preceding siblings ...)
  2023-01-05 11:29 ` daniel.lundin.mail at gmail dot com
@ 2023-01-05 14:55 ` segher at gcc dot gnu.org
  2023-01-05 17:10 ` segher at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: segher at gcc dot gnu.org @ 2023-01-05 14:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #4 from Segher Boessenkool <segher at gcc dot gnu.org> ---
But please use PR33053 for that, or open a new PR?  Let's keep this one
for just this actual bug :-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (3 preceding siblings ...)
  2023-01-05 14:55 ` segher at gcc dot gnu.org
@ 2023-01-05 17:10 ` segher at gcc dot gnu.org
  2023-01-06 11:21 ` [Bug middle-end/108298] " marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: segher at gcc dot gnu.org @ 2023-01-05 17:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #5 from Segher Boessenkool <segher at gcc dot gnu.org> ---
This is not x86-specific.  Like on powerpc64 we get

        addi 3,3,3       # 11   [c=4 l=4]  *addsi3/1
        extsw 3,3        # 17   [c=4 l=4]  extendsidi2/1
        blr              # 25   [c=4 l=4]  simple_return

Before RTL all is fine still:

int foo (int a, int b, int c)
{
  int _1;
  int _2;

;;   basic block 2, loop depth 0
;;    pred:       ENTRY
  _1 = a_3(D) + 1;
  b = _1;
  _2 ={v} MEM[(volatile int *)&b];
  a_6 = _2 + 2;
  return a_6;
;;    succ:       EXIT

}

But it is expanded to something that is not going through memory:

;; _2 ={v} MEM[(volatile int *)&b];

(insn 10 9 0 (set (reg:SI 118 [ _2 ])
        (subreg/s/u:SI (reg/v:DI 121 [ b+-4 ]) 4)) "108298.c":10:9 -1
     (nil))

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug middle-end/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (4 preceding siblings ...)
  2023-01-05 17:10 ` segher at gcc dot gnu.org
@ 2023-01-06 11:21 ` marxin at gcc dot gnu.org
  2023-01-06 16:52 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: marxin at gcc dot gnu.org @ 2023-01-06 11:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marxin at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org

--- Comment #6 from Martin Liška <marxin at gcc dot gnu.org> ---
I think it started with r11-165-geb72dc663e9070b2.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug middle-end/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (5 preceding siblings ...)
  2023-01-06 11:21 ` [Bug middle-end/108298] " marxin at gcc dot gnu.org
@ 2023-01-06 16:52 ` pinskia at gcc dot gnu.org
  2023-01-09 11:50 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-06 16:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69482#c7 addresses the big issue
here I think.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug middle-end/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (6 preceding siblings ...)
  2023-01-06 16:52 ` pinskia at gcc dot gnu.org
@ 2023-01-09 11:50 ` rguenth at gcc dot gnu.org
  2023-01-09 13:53 ` segher at gcc dot gnu.org
  2023-01-09 13:55 ` rguenther at suse dot de
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-01-09 11:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Yes, and I think it's a duplicate in the end.

*** This bug has been marked as a duplicate of bug 69482 ***

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug middle-end/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (7 preceding siblings ...)
  2023-01-09 11:50 ` rguenth at gcc dot gnu.org
@ 2023-01-09 13:53 ` segher at gcc dot gnu.org
  2023-01-09 13:55 ` rguenther at suse dot de
  9 siblings, 0 replies; 11+ messages in thread
From: segher at gcc dot gnu.org @ 2023-01-09 13:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #9 from Segher Boessenkool <segher at gcc dot gnu.org> ---
It cannot be a duplicate: this bug was introduced much later than when
PR69482 was filed!

But glad the same patch seems to have fixed both, sure :-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug middle-end/108298] Wrong optimization of volatile access from gcc 11 and beyond
  2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
                   ` (8 preceding siblings ...)
  2023-01-09 13:53 ` segher at gcc dot gnu.org
@ 2023-01-09 13:55 ` rguenther at suse dot de
  9 siblings, 0 replies; 11+ messages in thread
From: rguenther at suse dot de @ 2023-01-09 13:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298

--- Comment #10 from rguenther at suse dot de <rguenther at suse dot de> ---
On Mon, 9 Jan 2023, segher at gcc dot gnu.org wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108298
> 
> --- Comment #9 from Segher Boessenkool <segher at gcc dot gnu.org> ---
> It cannot be a duplicate: this bug was introduced much later than when
> PR69482 was filed!

It wasn't introduced - the present issue was exposed for this case ;-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-01-09 13:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05  9:09 [Bug c/108298] New: Wrong optimization of volatile access from gcc 11 and beyond daniel.lundin.mail at gmail dot com
2023-01-05  9:18 ` [Bug c/108298] " pinskia at gcc dot gnu.org
2023-01-05 11:25 ` segher at gcc dot gnu.org
2023-01-05 11:29 ` daniel.lundin.mail at gmail dot com
2023-01-05 14:55 ` segher at gcc dot gnu.org
2023-01-05 17:10 ` segher at gcc dot gnu.org
2023-01-06 11:21 ` [Bug middle-end/108298] " marxin at gcc dot gnu.org
2023-01-06 16:52 ` pinskia at gcc dot gnu.org
2023-01-09 11:50 ` rguenth at gcc dot gnu.org
2023-01-09 13:53 ` segher at gcc dot gnu.org
2023-01-09 13:55 ` rguenther at suse dot de

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).