Skip to content

Miscompile with C++23 constexpr-unknown constant evaluation #129844

@efriedma-quic

Description

@efriedma-quic

Testcase:

int &ff();
int &x = ff();
struct A { int& x; };
struct B { A x[20]; };
B f() { return {x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x}; }

Generates the following, which is wrong because the references point to x itself, not the referenced lvalue.

[...]
@constinit = private constant [20 x %struct.A] [%struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }, %struct.A { ptr @x }], align 8
[...]
define dso_local void @_Z1fv(ptr dead_on_unwind noalias writable sret(%struct.B) align 8 %agg.result) #2 {
entry:
  %x = getelementptr inbounds nuw %struct.B, ptr %agg.result, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %x, ptr align 8 @constinit, i64 160, i1 false)
  ret void
}

This is synthetic; I have no idea how likely it is for anyone to actually run into this. Filing as a followup to discussion on #128409.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++23clang:codegenIR generation bugs: mangling, exceptions, etc.constexprAnything related to constant evaluationmiscompilation

    Type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions