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

            Bug ID: 114263
           Summary: LTO generating incorrect code with -O2 -std=c++20
                    -fno-strict-aliasing flags in GCC13
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: lto
          Assignee: unassigned at gcc dot gnu.org
          Reporter: obaines at nvidia dot com
  Target Milestone: ---

I have a minimized test case where LTO seems to be generating incorrect code.

### Sources ###
A.cpp
```
#include <vector>
#include <cstdint>

struct A {
    A(uint64_t _x, uint8_t _y) : x(_x), y(_y) {}
    uint64_t x;
    uint8_t y;
};

void doA() {
    std::vector<A> v;
    v.emplace_back(0x1111111111111111ULL, 0x2);
    v.emplace_back(0x3333333333333333ULL, 0x4);
}
```

B.cpp
```
#include <iostream>
#include <vector>
#include <cstdint>

struct B {
    B(uint64_t _x, uint64_t _y) : x(_x), y(_y) {}
    uint64_t x;
    uint64_t y;
};

void doB() {
    std::vector<B> v;
    v.emplace_back(0x5555555555555555ULL, 0x6666666666666666ULL);
    v.emplace_back(0x7777777777777777ULL, 0x8888888888888888ULL);
    std::cout << std::hex << "expected: " << 0x6666666666666666ULL << " actual:
" << v[0].y << std::endl;
}
```

AB.cpp
```
void doA();
void doB();

int main() {
    doA();
    doB();
}
```

### Build Command ###
g++ -flto -g -O2 -std=c++20 -fno-strict-aliasing -Wall -Wextra -c *.cpp
g++ -flto -g -O2 -std=c++20 -fno-strict-aliasing -Wall -Wextra *.o

### Notes ###
The code doesn't generate any warnings during compilation.
The output of the program is usually `expected: 6666666666666666 actual:
6666666666666666`, but with these optimization flags the output is `expected:
6666666666666666 actual: 66`. The bug won't reproduce if I specify C++ standard
before C++20, or if I don't disable strict-aliasing optimization.
Changing the size of A::y affects the number of bytes that are correctly copied
in the output. The bug reproduces in gcc 13.2.0 and from the latest
releases/gcc-13 branch (g++ (GCC) 13.2.1 20240306), but does not reproduce in
gcc-12.3.0 or with the latest trunk branch (g++ (GCC) 14.0.1 20240306
(experimental)). 
So, as far as I can tell the bug is exclusive to GCC13.

Reply via email to