public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/99258] New: volatile struct access optimized away
@ 2021-02-24 18:17 borysp at invisiblethingslab dot com
  2021-02-24 18:58 ` [Bug tree-optimization/99258] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: borysp at invisiblethingslab dot com @ 2021-02-24 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99258
           Summary: volatile struct access optimized away
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: borysp at invisiblethingslab dot com
  Target Milestone: ---

Access to a volatile struct is optimized away, if the size of the struct is
>16B (at least on x64 cpu with sse). Small repro:
[borys@mowmiwuju test]$ cat a.c
struct A {
    long a;
    long b;
};

struct B {
    long a;
    long b;
    long c;
};

void f(volatile struct A* x) {
    *x;
}

void g(volatile struct B* x) {
    *x;
}
[borys@mowmiwuju test]$ gcc --version 
gcc (GCC) 10.2.0
[borys@mowmiwuju test]$ gcc -Wall -Wextra -std=c11 -O2 -c a.c
[borys@mowmiwuju test]$ objdump -d -Mintel a.o

a.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <f>:
   0:   f3 0f 6f 07             movdqu xmm0,XMMWORD PTR [rdi]
   4:   c3                      ret    
   5:   66 66 2e 0f 1f 84 00    data16 cs nop WORD PTR [rax+rax*1+0x0]
   c:   00 00 00 00 

0000000000000010 <g>:
  10:   c3                      ret 


Testing on https://godbolt.org/ indicates this issue is present in all gcc
versions. clang emits correct code for this test case.

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

* [Bug tree-optimization/99258] volatile struct access optimized away
  2021-02-24 18:17 [Bug tree-optimization/99258] New: volatile struct access optimized away borysp at invisiblethingslab dot com
@ 2021-02-24 18:58 ` pinskia at gcc dot gnu.org
  2021-02-25  8:19 ` [Bug middle-end/99258] " rguenth at gcc dot gnu.org
  2021-02-25  9:15 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-02-24 18:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Related to PR 47409 and PR 84699.

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

* [Bug middle-end/99258] volatile struct access optimized away
  2021-02-24 18:17 [Bug tree-optimization/99258] New: volatile struct access optimized away borysp at invisiblethingslab dot com
  2021-02-24 18:58 ` [Bug tree-optimization/99258] " pinskia at gcc dot gnu.org
@ 2021-02-25  8:19 ` rguenth at gcc dot gnu.org
  2021-02-25  9:15 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-25  8:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jsm28 at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
           Keywords|                            |wrong-code
   Last reconfirmed|                            |2021-02-25
     Ever confirmed|0                           |1
          Component|tree-optimization           |middle-end
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  Happens already during gimplification which does

      else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
               && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
        {
          /* Historically, the compiler has treated a bare reference
             to a non-BLKmode volatile lvalue as forcing a load.  */
...
        }
      else
        /* We can't do anything useful with a volatile reference to
           an incomplete type, so just throw it away.  Likewise for
           a BLKmode type, since any implicit inner load should
           already have been turned into an explicit one by the
           gimplification process.  */
        *expr_p = NULL;

throwing away the load.  GIMPLE indeed requires a LHS for a load and
we'd need to introduce a temporary aggregate here.  Which should be
possible for non-VLA, non TREE_ADDRESSABLE types.  Thus the condition
could be amended to not look at the types mode but verify its size is
constant and the type is not addressable (though the code explicitely
says a TREE_ADDRESSABLE type is OK).  Note relaxing the mode check
to constant size can cause arbitrarily big temporaries being generated
on the stack so I'm not sure that's good.  Alternatively open-coding
elementwise reads would be a possibility (for a load into a temporary
we might emit a memcpy call in the end).

Alternatively we could invent some internal function taking an aggregate
argument and emit

  .USE (*x);

and leave the actual generation of the load to RTL expansion.

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

* [Bug middle-end/99258] volatile struct access optimized away
  2021-02-24 18:17 [Bug tree-optimization/99258] New: volatile struct access optimized away borysp at invisiblethingslab dot com
  2021-02-24 18:58 ` [Bug tree-optimization/99258] " pinskia at gcc dot gnu.org
  2021-02-25  8:19 ` [Bug middle-end/99258] " rguenth at gcc dot gnu.org
@ 2021-02-25  9:15 ` jakub at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-02-25  9:15 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
We still need to gimplify volatile aggregate copies or reads (or aggregate
which contain at least one volatile member) into per-element accesses (at least
for the volatile portions; see the other PRs for details) and the scalar reads
can just have dummy scalar temporaries on the lhs if needed.

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

end of thread, other threads:[~2021-02-25  9:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-24 18:17 [Bug tree-optimization/99258] New: volatile struct access optimized away borysp at invisiblethingslab dot com
2021-02-24 18:58 ` [Bug tree-optimization/99258] " pinskia at gcc dot gnu.org
2021-02-25  8:19 ` [Bug middle-end/99258] " rguenth at gcc dot gnu.org
2021-02-25  9:15 ` jakub at gcc dot gnu.org

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