public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture.
@ 2023-11-22  7:43 shinwogud12 at gmail dot com
  2023-11-22  7:45 ` [Bug c/112665] " shinwogud12 at gmail dot com
  2023-12-14  7:52 ` [Bug target/112665] " krebbel at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: shinwogud12 at gmail dot com @ 2023-11-22  7:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112665
           Summary: I am getting incorrect output values at optimization
                    level 2 in GCC for the s390x architecture.
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shinwogud12 at gmail dot com
  Target Milestone: ---

The provided C code includes a main function and a helper function i(), along
with a struct definition and several global variables. The code primarily
involves conditional logic and a loop to manipulate these global variables.

###PoC(Proof of Concept)
```
#include <stdint.h>
#include <stdio.h>

struct a {
  uint64_t b;
};
int c = 1;
int d, e, h, f, g, l = 0;
volatile struct a k[1];

void i() {
  for (; l < 1; l++)
    f = d <= 0;
}

int main() {
  for (e = 9; e; --e){
    i();
    c && (g = ((int16_t)(k[0], f)) <= e);
  }
  printf("g_200 value %d\n", g);
  return 0;
}
```

**Struct Definition**

```
struct a {
  uint64_t b;
};
```
- Defines a struct **`a`** with a single member **`b`** of type **`uint64_t`**.

**Global Variables**

```c
int c = 1;
int d, e, h, f, g, l = 0;
volatile struct a k[1];
```

- A loop that runs once, setting **`f`** to 1 if **`d`** is less than or equal
to 0.

**Main Function**

```c
cCopy code
int main() {
  for (e = 9; e; --e){
    i();
    c && (g = ((int16_t)(k[0], f)) <= e);
  }
  printf("g_200 value %d\n", g);
  return 0;
}

```

- A loop starts with **`e`** at 9, decrementing until it reaches 0.
- Calls function **`i()`**, which sets **`f`** based on the condition **`d <=
0`**.
- The expression **`c && (g = ((int16_t)(k[0], f)) <= e)`** uses the comma
operator, resulting in **`f`** being evaluated and cast to **`int16_t`**. Since
**`c`** is always 1, **`g`** is set to 1 if **`f`** is less than or equal to
**`e`**.
- The final value of **`g`** is printed.

### Expected Result

- Since **`d`** is initialized to 0 and never modified, **`f`** will always be
set to 1 in the **`i()`** function.
- In the main function, **`g`** is set to 1 in each iteration of the loop
because **`f`** (which is 1) is always less than or equal to **`e`** (which
decrements from 9 to 1).
- The final output of the program will be **`g_200 value 1`**.

Why does optimization level 2 in GCC output 0?

Godbolt Link: https://www.godbolt.org/z/r6c4oo18a

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

* [Bug c/112665] I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture.
  2023-11-22  7:43 [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture shinwogud12 at gmail dot com
@ 2023-11-22  7:45 ` shinwogud12 at gmail dot com
  2023-12-14  7:52 ` [Bug target/112665] " krebbel at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: shinwogud12 at gmail dot com @ 2023-11-22  7:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from 신재형 <shinwogud12 at gmail dot com> ---
(In reply to 신재형 from comment #0)
> The provided C code includes a main function and a helper function i(),
> along with a struct definition and several global variables. The code
> primarily involves conditional logic and a loop to manipulate these global
> variables.
> 
> PoC(Proof of Concept)
> 
> #include <stdint.h>
> #include <stdio.h>
> 
> struct a {
>   uint64_t b;
> };
> int c = 1;
> int d, e, h, f, g, l = 0;
> volatile struct a k[1];
> 
> void i() {
>   for (; l < 1; l++)
>     f = d <= 0;
> }
> 
> int main() {
>   for (e = 9; e; --e){
>     i();
>     c && (g = ((int16_t)(k[0], f)) <= e);
>   }
>   printf("g_200 value %d\n", g);
>   return 0;
> }
> 
> 
> Struct Definition
> 
> 
> struct a {
>   uint64_t b;
> };
> 
> - Defines a struct a with a single member b of type
> uint64_t.
> 
> Global Variables
> 
> 
> int c = 1;
> int d, e, h, f, g, l = 0;
> volatile struct a k[1];
> 
> 
> - A loop that runs once, setting **`f`** to 1 if **`d`** is less than or
> equal to 0.
> 
> Main Function
> 
> 
> cCopy code
> int main() {
>   for (e = 9; e; --e){
>     i();
>     c && (g = ((int16_t)(k[0], f)) <= e);
>   }
>   printf("g_200 value %d\n", g);
>   return 0;
> }
>
> 
> 
> - A loop starts with e at 9, decrementing until it reaches 0.
> - Calls function i(), which sets f based on the condition d
> <= 0
> - The expression c && (g = ((int16_t)(k[0], f)) <= e) uses the comma
> operator, resulting in f being evaluated and cast to int16_t.
> Since c is always 1, g is set to 1 if f is less than or
> equal to e.
> - The final value of g is printed.
> 
> ### Expected Result
> 
> - Since d is initialized to 0 and never modified, f will always
> be set to 1 in the i() function.
> - In the main function, g is set to 1 in each iteration of the loop
> because f (which is 1) is always less than or equal to e (which
> decrements from 9 to 1).
> - The final output of the program will be g_200 value 1.
> 
> Why does optimization level 2 in GCC output 0?
> 
> Godbolt Link: https://www.godbolt.org/z/r6c4oo18a

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

* [Bug target/112665] I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture.
  2023-11-22  7:43 [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture shinwogud12 at gmail dot com
  2023-11-22  7:45 ` [Bug c/112665] " shinwogud12 at gmail dot com
@ 2023-12-14  7:52 ` krebbel at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: krebbel at gcc dot gnu.org @ 2023-12-14  7:52 UTC (permalink / raw)
  To: gcc-bugs

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

Andreas Krebbel <krebbel at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE
                 CC|                            |krebbel at gcc dot gnu.org

--- Comment #2 from Andreas Krebbel <krebbel at gcc dot gnu.org> ---
I can confirm this when running the program with qemu but not on real hardware.
The code is also using the chrl instruction so I guess this is another instance
of PR112986

*** This bug has been marked as a duplicate of bug 112986 ***

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

end of thread, other threads:[~2023-12-14  7:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-22  7:43 [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture shinwogud12 at gmail dot com
2023-11-22  7:45 ` [Bug c/112665] " shinwogud12 at gmail dot com
2023-12-14  7:52 ` [Bug target/112665] " krebbel 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).