public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "obaines at nvidia dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug lto/114263] New: LTO generating incorrect code with -O2 -std=c++20 -fno-strict-aliasing flags in GCC13
Date: Thu, 07 Mar 2024 05:19:18 +0000	[thread overview]
Message-ID: <bug-114263-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2024-03-07  5:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07  5:19 obaines at nvidia dot com [this message]
2024-03-07  5:24 ` [Bug ipa/114263] " pinskia at gcc dot gnu.org
2024-03-07  5:25 ` pinskia at gcc dot gnu.org
2024-03-09 21:44 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-114263-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).