Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
efriedma-quic opened this issue Mar 5, 2025 · 3 comments · Fixed by #129952
Closed

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

efriedma-quic opened this issue Mar 5, 2025 · 3 comments · Fixed by #129952
Labels
c++23 clang:codegen IR generation bugs: mangling, exceptions, etc. constexpr Anything related to constant evaluation miscompilation

Comments

@efriedma-quic
Copy link
Collaborator

efriedma-quic commented Mar 5, 2025

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.

@efriedma-quic efriedma-quic added c++23 clang:codegen IR generation bugs: mangling, exceptions, etc. miscompilation labels Mar 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 5, 2025

@llvm/issue-subscribers-clang-codegen

Author: Eli Friedman (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}; }

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.

@tbaederr
Copy link
Contributor

tbaederr commented Mar 5, 2025

This is also accepted by gcc and clang 19, why should it be rejected?

@efriedma-quic
Copy link
Collaborator Author

Updated description to be more clear.

@zyn0217 zyn0217 added this to the LLVM 20.X Release milestone Mar 5, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Mar 5, 2025
efriedma-quic added a commit to efriedma-quic/llvm-project that referenced this issue Mar 5, 2025
…consistently.

Perform the check for constexpr-unknown values in the same place we
perform checks for other values which don't count as constant
expressions.

While I'm here, also fix a rejects-valid with a reference that doesn't
have an initializer.  This diagnostic was also covering up some of the
bugs here.

The existing behavior with -fexperimental-new-constant-interpreter seems
to be correct, but the diagnostics are slightly different; it would be
helpful if someone could check on that as a followup.

Followup to llvm#128409.

Fixes llvm#129844. Fixes llvm#129845.
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in LLVM Release Status Mar 10, 2025
efriedma-quic added a commit to efriedma-quic/llvm-project that referenced this issue Mar 10, 2025
…consistently (llvm#129952)

Perform the check for constexpr-unknown values in the same place we
perform checks for other values which don't count as constant
expressions.

While I'm here, also fix a rejects-valid with a reference that doesn't
have an initializer. This diagnostic was also covering up some of the
bugs here.

The existing behavior with -fexperimental-new-constant-interpreter seems
to be correct, but the diagnostics are slightly different; it would be
helpful if someone could check on that as a followup.

Followup to llvm#128409.

Fixes llvm#129844. Fixes llvm#129845.
swift-ci pushed a commit to swiftlang/llvm-project that referenced this issue Mar 11, 2025
…consistently (llvm#129952)

Perform the check for constexpr-unknown values in the same place we
perform checks for other values which don't count as constant
expressions.

While I'm here, also fix a rejects-valid with a reference that doesn't
have an initializer. This diagnostic was also covering up some of the
bugs here.

The existing behavior with -fexperimental-new-constant-interpreter seems
to be correct, but the diagnostics are slightly different; it would be
helpful if someone could check on that as a followup.

Followup to llvm#128409.

Fixes llvm#129844. Fixes llvm#129845.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++23 clang:codegen IR generation bugs: mangling, exceptions, etc. constexpr Anything related to constant evaluation miscompilation
Projects
4 participants