public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop.
@ 2024-04-02  6:24 nmodnation at gmail dot com
  2024-04-02  6:26 ` [Bug c/114554] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: nmodnation at gmail dot com @ 2024-04-02  6:24 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114554
           Summary: In O2-3 optimization, this code runs in an infinite
                    loop.
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nmodnation at gmail dot com
  Target Milestone: ---

#include <stdio.h>
#include <stdint.h>

static void wymum(uint64_t* p1, uint64_t* p2) {
  int64_t v1;
  int64_t v2;
  int64_t v3;
  int64_t v4;
  int64_t v5;
  int64_t v6;
  int64_t v7;
  int64_t v8;
  int64_t v9;
  int64_t v10;
  int64_t v11;
  int64_t t1;
  int32_t t2;
  int64_t t3;
  int64_t t4;
  t1 = *p1;
  t1 = (uint64_t)t1 >> (uint64_t)32;
  v1 = t1;
  t1 = *p2;
  t1 = (uint64_t)t1 >> (uint64_t)32;
  v2 = t1;
  t2 = (uint32_t)*p1;
  v3 = (uint64_t)(uint32_t)t2;
  t2 = (uint32_t)*p2;
  v4 = (uint64_t)(uint32_t)t2;
  t1 = v1;
  t1 *= v2;
  v5 = t1;
  t1 = v1;
  t1 *= v4;
  v6 = t1;
  t1 = v2;
  t1 *= v3;
  v7 = t1;
  t1 = v3;
  t1 *= v4;
  v8 = t1;
  t1 = v6;
  t1 = (uint64_t)t1 << (uint64_t)32;
  t3 = v8;
  t3 += t1;
  v9 = t3;
  t2 = (uint64_t)v9 < (uint64_t)v8 ? 1 : 0;
  v10 = (int64_t)t2;
  t1 = v7;
  t1 = (uint64_t)t1 << (uint64_t)32;
  t3 = v9;
  t3 += t1;
  v11 = t3;
  t2 = (uint64_t)v11 < (uint64_t)v9 ? 1 : 0;
  t1 = (int64_t)t2;
  v10 += t1;
  t1 = v6;
  t1 = (uint64_t)t1 >> (uint64_t)32;
  t3 = v7;
  t3 = (uint64_t)t3 >> (uint64_t)32;
  t4 = v5;
  t4 += t1;
  t4 += t3;
  t4 += v10;
  *p1 = v11;
  *p2 = t4;
  return;
}

static uint64_t wymix(uint64_t p1, uint64_t p2) {
  wymum(&p1, &p2);
  return p1 ^ p2;
}

static uint64_t wyrand(int64_t* p1) {
  *p1 += 3257665815644502181;
  return wymix(*p1, *p1 ^ -8378864009470890807);
}

int main(int argc, char **argv) {
  int64_t seed = 1;
  int i = 0;
  for (; i < 10; i++) {
    printf("%llx\n", wyrand(&seed));
  }
  return 0;
}

// gcc -O2 -o bug bug.c

It is not a solution, but it can be resolved by modifying it as shown below.

1. static uint64_t wyrand(int64_t* p1) => static uint64_t wyrand(uint64_t* p1)
2. int64_t seed = 1; => uint64_t seed = 1;

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

* [Bug c/114554] In O2-3 optimization, this code runs in an infinite loop.
  2024-04-02  6:24 [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop nmodnation at gmail dot com
@ 2024-04-02  6:26 ` pinskia at gcc dot gnu.org
  2024-04-02  6:28 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-02  6:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
With -fsanitize=undefined I get:
/app/example.cpp:52:6: runtime error: signed integer overflow:
8816734281700067322 + 4251162314662739968 cannot be represented in type 'long
int'
/app/example.cpp:45:6: runtime error: signed integer overflow:
4656174804089751318 + 5363955046469861376 cannot be represented in type 'long
int'
/app/example.cpp:76:7: runtime error: signed integer overflow:
6515331631289004363 + 3257665815644502181 cannot be represented in type 'long
int'
/app/example.cpp:34:6: runtime error: signed integer overflow: 3792422143 *
3257989107 cannot be represented in type 'long int'
/app/example.cpp:37:6: runtime error: signed integer overflow: 2230917464 *
4270511071 cannot be represented in type 'long int'

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

* [Bug c/114554] In O2-3 optimization, this code runs in an infinite loop.
  2024-04-02  6:24 [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop nmodnation at gmail dot com
  2024-04-02  6:26 ` [Bug c/114554] " pinskia at gcc dot gnu.org
@ 2024-04-02  6:28 ` pinskia at gcc dot gnu.org
  2024-04-02  6:35 ` pinskia at gcc dot gnu.org
  2024-04-02  6:45 ` nmodnation at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-02  6:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Plus I get the following warnings at -O2:
In function 'wyrand',
    inlined from 'main' at <source>:84:5:
<source>:76:7: warning: iteration 2 invokes undefined behavior
[-Waggressive-loop-optimizations]
   76 |   *p1 += 3257665815644502181;
      |       ^~
<source>: In function 'main':
<source>:83:12: note: within this loop
   83 |   for (; i < 10; i++) {
      |          ~~^~~~
ASM generation compiler returned: 0
In function 'wyrand',
    inlined from 'main' at <source>:84:5:
<source>:76:7: warning: iteration 2 invokes undefined behavior
[-Waggressive-loop-optimizations]
   76 |   *p1 += 3257665815644502181;
      |       ^~
<source>: In function 'main':
<source>:83:12: note: within this loop
   83 |   for (; i < 10; i++) {
      |          ~~^~~~


With -fwrapv, the code "works" but that is because signed integer overflow has
changed from undefined behavior to being defined as wrapping.

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

* [Bug c/114554] In O2-3 optimization, this code runs in an infinite loop.
  2024-04-02  6:24 [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop nmodnation at gmail dot com
  2024-04-02  6:26 ` [Bug c/114554] " pinskia at gcc dot gnu.org
  2024-04-02  6:28 ` pinskia at gcc dot gnu.org
@ 2024-04-02  6:35 ` pinskia at gcc dot gnu.org
  2024-04-02  6:45 ` nmodnation at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-02  6:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also didn't you read https://gcc.gnu.org/bugs/ or even what is mentioned on the
bug entry web page?
https://gcc.gnu.org/bugs/ says:
Before reporting that GCC compiles your code incorrectly, compile it with gcc
-Wall -Wextra and see whether this shows anything wrong with your code.
Similarly, if compiling with -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations makes a difference, or if compiling with
-fsanitize=undefined produces any run-time errors, then your code is probably
not correct.

bug entry web page (IN RED AND BOLD):
Before reporting that GCC compiles your code incorrectly, compile it with gcc
-Wall -Wextra and see whether this shows anything wrong with your code.
Similarly, if compiling with -fno-strict-aliasing -fwrapv makes a difference,
your code probably is not correct.

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

* [Bug c/114554] In O2-3 optimization, this code runs in an infinite loop.
  2024-04-02  6:24 [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop nmodnation at gmail dot com
                   ` (2 preceding siblings ...)
  2024-04-02  6:35 ` pinskia at gcc dot gnu.org
@ 2024-04-02  6:45 ` nmodnation at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: nmodnation at gmail dot com @ 2024-04-02  6:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from nmodnation n <nmodnation at gmail dot com> ---
I apologize for taking up your time because of my mistake. I will be
careful from next time. thank you

2024년 4월 2일 (화) 오후 3:35, pinskia at gcc dot gnu.org <
gcc-bugzilla@gcc.gnu.org>님이 작성:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114554
>
> --- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> Also didn't you read https://gcc.gnu.org/bugs/ or even what is mentioned
> on the
> bug entry web page?
> https://gcc.gnu.org/bugs/ says:
> Before reporting that GCC compiles your code incorrectly, compile it with
> gcc
> -Wall -Wextra and see whether this shows anything wrong with your code.
> Similarly, if compiling with -fno-strict-aliasing -fwrapv
> -fno-aggressive-loop-optimizations makes a difference, or if compiling with
> -fsanitize=undefined produces any run-time errors, then your code is
> probably
> not correct.
>
> bug entry web page (IN RED AND BOLD):
> Before reporting that GCC compiles your code incorrectly, compile it with
> gcc
> -Wall -Wextra and see whether this shows anything wrong with your code.
> Similarly, if compiling with -fno-strict-aliasing -fwrapv makes a
> difference,
> your code probably is not correct.
>
> --
> You are receiving this mail because:
> You reported the bug.

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

end of thread, other threads:[~2024-04-02  6:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-02  6:24 [Bug c/114554] New: In O2-3 optimization, this code runs in an infinite loop nmodnation at gmail dot com
2024-04-02  6:26 ` [Bug c/114554] " pinskia at gcc dot gnu.org
2024-04-02  6:28 ` pinskia at gcc dot gnu.org
2024-04-02  6:35 ` pinskia at gcc dot gnu.org
2024-04-02  6:45 ` nmodnation at gmail dot com

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