Bug 108298 - Wrong optimization of volatile access from gcc 11 and beyond
Summary: Wrong optimization of volatile access from gcc 11 and beyond
Status: RESOLVED DUPLICATE of bug 69482
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2023-01-05 09:09 UTC by Daniel Lundin
Modified: 2023-01-09 13:55 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-01-05 00:00:00


Attachments
Example to reproduce incorrect optimizations (143 bytes, text/plain)
2023-01-05 09:09 UTC, Daniel Lundin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Lundin 2023-01-05 09:09:22 UTC
Created attachment 54194 [details]
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
Comment 1 Andrew Pinski 2023-01-05 09:18:39 UTC
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 ***
Comment 2 Segher Boessenkool 2023-01-05 11:25:29 UTC
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.
Comment 3 Daniel Lundin 2023-01-05 11:29:02 UTC
(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.
Comment 4 Segher Boessenkool 2023-01-05 14:55:53 UTC
But please use PR33053 for that, or open a new PR?  Let's keep this one
for just this actual bug :-)
Comment 5 Segher Boessenkool 2023-01-05 17:10:05 UTC
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))
Comment 6 Martin Liška 2023-01-06 11:21:59 UTC
I think it started with r11-165-geb72dc663e9070b2.
Comment 7 Andrew Pinski 2023-01-06 16:52:47 UTC
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69482#c7 addresses the big issue here I think.
Comment 8 Richard Biener 2023-01-09 11:50:14 UTC
Yes, and I think it's a duplicate in the end.

*** This bug has been marked as a duplicate of bug 69482 ***
Comment 9 Segher Boessenkool 2023-01-09 13:53:31 UTC
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 :-)
Comment 10 rguenther@suse.de 2023-01-09 13:55:47 UTC
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 ;-)