Issue 107570
Summary Global Variable Not Updated Despite Being Included in Return Statement
Labels new issue
Assignees
Reporter edumoot
    The global variable `counter` in the return statement at line 34 does not update to `9 or any non-zero value` when the binary is compiled with optimization levels "-O0", "-O1", "-O2", and "-O3." Goldbot link is [here.](https://godbolt.org/). Additionally, the same issue occurs with another global variable,  `counter_shorter`.

As a result, the main function's return code remains unaffected, and all compiled binaries return 0 consistently.

We can reproduce this issues in LLVM18.1.8 and 16.0.3.

```
clang -g -O0 -o 435_O0.out 435.c
clang -g -O1 -o 435_O1.out 435.c
clang -g -O2 -o 435_O2.out 435.c
clang -g -O3 -o 435_O3.out 435.c

./435_O0.out
echo $?
0

./435_O1.out
echo $?
0

./435_O2.out
echo $?
0

./435_O3.out
echo $?
0
```

For LLVM17.0.6, it is slight different. The -O0 binary works correctly while the other binaries keep the same issue.
```
clang -g -O0 -o 435_O0.out 435.c
./435_O0.out
echo $?
1

```


`cat 435.c`
```
#include <stdint.h>
union Data {
   uint32_t f0;
 unsigned f1 : 6;
   const uint16_t f2;
   int64_t f3;
   volatile int32_t f4;
};

static int counter = 1;
static unsigned short counter_short = 0;
static union Data data_union = {0xD3D8E1CE};
static union Data data_matrix[8][3] = {
    {{4}, {4}, {0xD757520C}}, {{0x0C01EF4F}, {1}, {0x0C01EF4F}},
    {{4}, {0xD757520C}, {0xD757520C}}, {{0x7DB40437}, {1}, {0x7DB40437}},
    {{4}, {4}, {0xD757520C}}, {{0x0C01EF4F}, {1}, {0x0C01EF4F}},
    {{4}, {0xD757520C}, {0xD757520C}}, {{0x7DB40437}, {1}, {0x7DB40437}}
};

static union Data calculate_data(void) {
    char temp = -1;
    int temp_array[2];
 for (int i = 0; i < 2; i++)
        temp_array[i] = -7;
    for (counter = 0; counter <= 9; counter++) {
        temp_array[1] &= 0x116E074F;
        counter_short++;
        return data_union;
 }
    return data_matrix[6][2];
}

int main(void) {
 calculate_data();
    return counter;
}

```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to