public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result
@ 2021-05-18 15:23 jan.smets at nokia dot com
  2021-05-19  1:14 ` [Bug tree-optimization/100653] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: jan.smets at nokia dot com @ 2021-05-18 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100653
           Summary: usage of scalar_storage_order produces incorrect
                    result
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jan.smets at nokia dot com
  Target Milestone: ---

Created attachment 50840
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50840&action=edit
scalar_storage_order test case

Attached test case produces invalid results with -O2, good with -O1.

Known to fail : 11.1 10.3 9.3 8.5 7.5
Known to work: 6.5 
Target: x86_64-linux-gnu

$ gcc test.c -o /tmp/test -O2 -Wall -Wextra && /tmp/test
a0a1e01 EQ? 11e0a0a 0
$ gcc test.c -o /tmp/test -O1 -Wall -Wextra && /tmp/test
a0a1e01 EQ? a0a1e01 1

Thank you

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

* [Bug tree-optimization/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
@ 2021-05-19  1:14 ` pinskia at gcc dot gnu.org
  2021-05-26 10:49 ` ebotcazou at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-05-19  1:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.1.0, 11.1.0, 12.0, 7.3.0
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-05-19
           Keywords|                            |wrong-code

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.  It looks like some pass loses REF_REVERSE_STORAGE_ORDER.
As far as I can tell it has never worked.

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

* [Bug tree-optimization/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
  2021-05-19  1:14 ` [Bug tree-optimization/100653] " pinskia at gcc dot gnu.org
@ 2021-05-26 10:49 ` ebotcazou at gcc dot gnu.org
  2021-05-26 12:36 ` jan.smets at nokia dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2021-05-26 10:49 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

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

--- Comment #2 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> Attached test case produces invalid results with -O2, good with -O1.

See the manual:

     Moreover, the use of type punning or aliasing to toggle the storage
     order is not supported; that is to say, a given scalar object
     cannot be accessed through distinct types that assign a different
     storage order to it.

typedef struct tIp6Addr
{
    unsigned int    s6_addr32[4];
} tIp6Addr;

struct _tBeTimNetAddr
{
    unsigned char isIPv4;
    union
    {
        unsigned int addr;
        tIp6Addr addr6;
    } BB_MSG_ENDIANNESS u;
} BB_MSG_ENDIANNESS;

_tBeTimNetAddr.u.addr6.s6_addr32[0] is accessed in little-endian and
_tBeTimNetAddr.u.addr is accessed in big-endian, but they are the same scalar.

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

* [Bug tree-optimization/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
  2021-05-19  1:14 ` [Bug tree-optimization/100653] " pinskia at gcc dot gnu.org
  2021-05-26 10:49 ` ebotcazou at gcc dot gnu.org
@ 2021-05-26 12:36 ` jan.smets at nokia dot com
  2021-05-26 12:45 ` ebotcazou at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jan.smets at nokia dot com @ 2021-05-26 12:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jan Smets <jan.smets at nokia dot com> ---
Is there some way there can be warned against such invalid usages? Because
these things are really hard to see on a 'macro' level.

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

* [Bug tree-optimization/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (2 preceding siblings ...)
  2021-05-26 12:36 ` jan.smets at nokia dot com
@ 2021-05-26 12:45 ` ebotcazou at gcc dot gnu.org
  2021-05-26 13:04 ` ebotcazou at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2021-05-26 12:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> Is there some way there can be warned against such invalid usages? Because
> these things are really hard to see on a 'macro' level.

Not at the moment, although this could probably be implemented.  But a simple
rule of thumb is to avoid using scalar_storage_order with unions at all.

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

* [Bug tree-optimization/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (3 preceding siblings ...)
  2021-05-26 12:45 ` ebotcazou at gcc dot gnu.org
@ 2021-05-26 13:04 ` ebotcazou at gcc dot gnu.org
  2021-05-26 17:14 ` [Bug c/100653] " cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2021-05-26 13:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> Not at the moment, although this could probably be implemented.  But a
> simple rule of thumb is to avoid using scalar_storage_order with unions at
> all.

I'm going to implement a simple warning for unions.

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

* [Bug c/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (4 preceding siblings ...)
  2021-05-26 13:04 ` ebotcazou at gcc dot gnu.org
@ 2021-05-26 17:14 ` cvs-commit at gcc dot gnu.org
  2021-05-26 22:26 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-26 17:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:401bd4adcfda9965363b1ac3ba7e1580f15d6883

commit r12-1072-g401bd4adcfda9965363b1ac3ba7e1580f15d6883
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed May 26 19:12:05 2021 +0200

    Warn on type punning that toggles scalar storage order

    As documented in the manual, we do not support type punning that toggles
    the scalar storage order, so this adds a warning for the case of unions.

    gcc/c/
            PR c/100653
            * c-decl.c (finish_struct): Warn for a union containing an
aggregate
            field with a differing scalar storage order.
    gcc/testsuite/
            * gcc.dg/sso-13.c: New test.

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

* [Bug c/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (5 preceding siblings ...)
  2021-05-26 17:14 ` [Bug c/100653] " cvs-commit at gcc dot gnu.org
@ 2021-05-26 22:26 ` cvs-commit at gcc dot gnu.org
  2021-05-27 19:29 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-26 22:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:58523f23fefcef0850841e7055d75d4309f0453e

commit r12-1083-g58523f23fefcef0850841e7055d75d4309f0453e
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Thu May 27 00:24:20 2021 +0200

    Small tweak to documentation of scalar_storage_order

    gcc/
            PR c/100653
            * doc/extend.texi (scalar_storage_order): Rephrase slightly.

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

* [Bug c/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (6 preceding siblings ...)
  2021-05-26 22:26 ` cvs-commit at gcc dot gnu.org
@ 2021-05-27 19:29 ` pinskia at gcc dot gnu.org
  2021-06-15 15:49 ` george.thopas at gmail dot com
  2021-06-15 16:42 ` ebotcazou at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-05-27 19:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |george.thopas at gmail dot com

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 100804 has been marked as a duplicate of this bug. ***

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

* [Bug c/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (7 preceding siblings ...)
  2021-05-27 19:29 ` pinskia at gcc dot gnu.org
@ 2021-06-15 15:49 ` george.thopas at gmail dot com
  2021-06-15 16:42 ` ebotcazou at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: george.thopas at gmail dot com @ 2021-06-15 15:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from George Thopas <george.thopas at gmail dot com> ---
Created attachment 51025
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51025&action=edit
avoid eliminating fields with different endianess as equal

Short story :
Ran a bisect to find why this always works in the gcc-6 branch and has
different behaviour outside. Found out what happens (see below) and attached a
2-line patch for what I assume is missing.  Works for me but is it the 'right
thing'(tm) todo ?  

Thanks.

Long story:
I started digging into this in the hope to get a better understanding
In the last 3 years I've never seen this on the gcc-6 branch. 
Even though there's a tons of code and mixed endianess structures and 
unions validation. If I missed it, I at least needed to know at least why. 

The bisect ended up giving commit e7a3e0c653be4bd32f116dae06438896b7dc915b.
Reverting it for test purposes in gcc-7/gcc-8 just confirmed it is the 
right trigger.

There's obviously nothing wrong with that commit but at least it gave me 
a clue what to look for. To my understanding of the code :
The RPO change affects evaluation order when merging and eliminating blocks.
In the case of the example, the compiler ends up checking if both fields 
of the union are the same and can be merged/eliminated. Their both in the same
location. The code currently considers different signedness and bails out, but
not different  endianness and eliminates one depending one evaluation order. 
Whatever one that is.

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

* [Bug c/100653] usage of scalar_storage_order produces incorrect result
  2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
                   ` (8 preceding siblings ...)
  2021-06-15 15:49 ` george.thopas at gmail dot com
@ 2021-06-15 16:42 ` ebotcazou at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2021-06-15 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> The bisect ended up giving commit e7a3e0c653be4bd32f116dae06438896b7dc915b.
> Reverting it for test purposes in gcc-7/gcc-8 just confirmed it is the 
> right trigger.
> 
> There's obviously nothing wrong with that commit but at least it gave me 
> a clue what to look for. To my understanding of the code :
> The RPO change affects evaluation order when merging and eliminating blocks.
> In the case of the example, the compiler ends up checking if both fields 
> of the union are the same and can be merged/eliminated. Their both in the
> same location. The code currently considers different signedness and bails
> out, but not different endianness and eliminates one depending one
> evaluation order. 

That's by design, the entire implementation assumes that every scalar in memory
is accessed using the same storage order throughout the entire program.  If you
want to overcome this limitation, you need to reimplement it from scratch.

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

end of thread, other threads:[~2021-06-15 16:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-18 15:23 [Bug tree-optimization/100653] New: usage of scalar_storage_order produces incorrect result jan.smets at nokia dot com
2021-05-19  1:14 ` [Bug tree-optimization/100653] " pinskia at gcc dot gnu.org
2021-05-26 10:49 ` ebotcazou at gcc dot gnu.org
2021-05-26 12:36 ` jan.smets at nokia dot com
2021-05-26 12:45 ` ebotcazou at gcc dot gnu.org
2021-05-26 13:04 ` ebotcazou at gcc dot gnu.org
2021-05-26 17:14 ` [Bug c/100653] " cvs-commit at gcc dot gnu.org
2021-05-26 22:26 ` cvs-commit at gcc dot gnu.org
2021-05-27 19:29 ` pinskia at gcc dot gnu.org
2021-06-15 15:49 ` george.thopas at gmail dot com
2021-06-15 16:42 ` ebotcazou 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).