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

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