Bug 101370 - miscompile of constexpr array with initializers to self
Summary: miscompile of constexpr array with initializers to self
Status: RESOLVED DUPLICATE of bug 64989
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2021-07-08 01:41 UTC by Richard Smith
Modified: 2021-12-07 12:27 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Smith 2021-07-08 01:41:06 UTC
Testcase (reduced from tcmalloc):


struct Elem {
    Elem* next_ = this;
    Elem* prev_ = this;
};

constinit Elem qs[3];


GCC generates a zero initializer for qs:


qs:
        .zero   48


If the `constinit` is removed, GCC instead incorrectly generates a dynamic initializer, so presumably GCC knows that this variable *should* have a constant initializer but can't actually form one.

The same problem can be observed without constinit:


constexpr Elem rs[3];
const void *p = rs;


... again results in a zero-initialized global and no dynamic initializer.
Comment 1 Marek Polacek 2021-07-08 01:55:01 UTC
Confirmed.
Comment 2 Andrew Pinski 2021-12-07 12:19:59 UTC
Another testcase:
struct Elem {
    Elem* next_ = this;
};
constexpr Elem rs[1];
const void *p = rs[0].next_;

Which shows it is just an array issue; changing rs to be a non-array works that is:

struct Elem {
    Elem* next_ = this;
};
constexpr Elem rs;
const void *p = rs.next_;
Comment 3 Andrew Pinski 2021-12-07 12:22:18 UTC
Here is a simple compile time testcase:
struct Elem {
    Elem* next_ = this;
};

constexpr Elem rs[1];
static_assert (rs[0].next_ == rs);
Comment 4 Andrew Pinski 2021-12-07 12:27:39 UTC
This is a dup of bug 64989 really.
My reduced testcase in comment #3 shows it is.  Sometimes we reject the constexpr as the array used in its own initializer and other times just produce wrong code.

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