public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/116316] New: incorrect code with -O2
@ 2024-08-09 18:42 qinzhao at gcc dot gnu.org
  2024-08-09 18:51 ` [Bug tree-optimization/116316] " pinskia at gcc dot gnu.org
  2024-08-09 19:15 ` qinzhao at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: qinzhao at gcc dot gnu.org @ 2024-08-09 18:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116316
           Summary: incorrect code with -O2
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: qinzhao at gcc dot gnu.org
  Target Milestone: ---

the following testing case failed on both x86 and aarch64 with -O2, but passed
with -O1:

#include <stdio.h>
struct annotated {
  int b;
  int c[];
} *array_annotated;

int main(int argc, char *argv[])
{
  size_t __size = sizeof(struct annotated) + sizeof(*array_annotated->c) * 10;
  array_annotated = (struct annotated *) __builtin_malloc(__size);
  __builtin_memset(array_annotated, 0, __size);
  if (&(array_annotated->b))
    *(size_t *)(&(array_annotated->b)) = 10;
  if (array_annotated->b != 10)
    __builtin_abort ();

  printf("pass\n");
  return 0;
}


adding -fno-tree-fre -fno-tree-pre -fno-code-hoisting cures the issue

[opc@qinzhao-aarch64-ol8 ~]$ ./Install/latest-d/bin/gcc t.c -O2
[opc@qinzhao-aarch64-ol8 ~]$ ./a.out
Aborted (core dumped)
[opc@qinzhao-aarch64-ol8 ~]$ ./Install/latest-d/bin/gcc t.c -O2 -fno-tree-fre
-fno-tree-pre -fno-code-hoisting
[opc@qinzhao-aarch64-ol8 ~]$ ./a.out
pass

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

* [Bug tree-optimization/116316] incorrect code with -O2
  2024-08-09 18:42 [Bug tree-optimization/116316] New: incorrect code with -O2 qinzhao at gcc dot gnu.org
@ 2024-08-09 18:51 ` pinskia at gcc dot gnu.org
  2024-08-09 19:15 ` qinzhao at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-08-09 18:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---

<source>:13:6: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
   13 |     *(size_t *)(&(array_annotated->b)) = 10;
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-fno-strict-aliasing  fixes it.

You access `array_annotated->b` via size_t (which is most likely `unsigned
long`) and via int. Since long and int are distant types (and not signed
versions of each other) they don't alias.

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

* [Bug tree-optimization/116316] incorrect code with -O2
  2024-08-09 18:42 [Bug tree-optimization/116316] New: incorrect code with -O2 qinzhao at gcc dot gnu.org
  2024-08-09 18:51 ` [Bug tree-optimization/116316] " pinskia at gcc dot gnu.org
@ 2024-08-09 19:15 ` qinzhao at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: qinzhao at gcc dot gnu.org @ 2024-08-09 19:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from qinzhao at gcc dot gnu.org ---
(In reply to Andrew Pinski from comment #1)
> <source>:13:6: warning: dereferencing type-punned pointer will break
> strict-aliasing rules [-Wstrict-aliasing]
>    13 |     *(size_t *)(&(array_annotated->b)) = 10;
>       |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> -fno-strict-aliasing  fixes it.
> 
> You access `array_annotated->b` via size_t (which is most likely `unsigned
> long`) and via int. Since long and int are distant types (and not signed
> versions of each other) they don't alias.

Okay, I see. thanks a lot.
I did suspect that this is a source code issue, now confirmed. -:)

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

end of thread, other threads:[~2024-08-09 19:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-09 18:42 [Bug tree-optimization/116316] New: incorrect code with -O2 qinzhao at gcc dot gnu.org
2024-08-09 18:51 ` [Bug tree-optimization/116316] " pinskia at gcc dot gnu.org
2024-08-09 19:15 ` qinzhao 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).