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

[clang] static constexpr variable initialized inside consteval function is loosing its value #92690

Closed
hanickadot opened this issue May 19, 2024 · 4 comments
Labels
clang:codegen confirmed Verified by a second party consteval C++20 consteval constexpr Anything related to constant evaluation duplicate Resolved as duplicate

Comments

@hanickadot
Copy link
Contributor

Any variable initialized as static constexpr variable inside consteval is not keeping its value in runtime:

consteval const int & get_value() {
    constexpr static int value = 42;
    return value;
}

int foo() {
  return get_value();
}

generates assembly (with clang 18.1):

foo():                                # @foo()
        push    rbp
        mov     rbp, rsp
        mov     eax, dword ptr [rip + get_value()::value]
        pop     rbp
        ret
get_value()::value:
        .long   0    

confirmed with clang 16+ to current main branch (597ac471cc7da97ccf957362a7e9f7a52d6910ee at compiler explorer)

link to example: https://compiler-explorer.com/z/fj6zco3d5

same thing happens to strings and arrays ... their storage is all zeros

@github-actions github-actions bot added the clang Clang issues not falling into any other category label May 19, 2024
@EugeneZelenko EugeneZelenko added clang:codegen and removed clang Clang issues not falling into any other category labels May 19, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 19, 2024

@llvm/issue-subscribers-clang-codegen

Author: Hana Dusíková (hanickadot)

Any variable initialized as `static constexpr` variable inside `consteval` is not keeping its value in runtime:
consteval const int & get_value() {
    constexpr static int value = 42;
    return value;
}

int foo() {
  return get_value();
}

generates assembly (with clang 18.1):

foo():                                # @<!-- -->foo()
        push    rbp
        mov     rbp, rsp
        mov     eax, dword ptr [rip + get_value()::value]
        pop     rbp
        ret
get_value()::value:
        .long   0    

confirmed with clang 16+ to current main branch (597ac471cc7da97ccf957362a7e9f7a52d6910ee at compiler explorer)

link to example: https://compiler-explorer.com/z/fj6zco3d5

same thing happens to strings and arrays ... their storage is all zeros

@EugeneZelenko EugeneZelenko added consteval C++20 consteval constexpr Anything related to constant evaluation labels May 19, 2024
@hanickadot
Copy link
Contributor Author

A little bit better example: https://compiler-explorer.com/z/vTE8WYMGz

@shafik
Copy link
Collaborator

shafik commented May 19, 2024

I believe this is a duplicate of: #82994

Using constexpr causes the problem to go away: https://compiler-explorer.com/z/hTaTTGsv9

CC @cor3ntin @Fznamznon

@hanickadot
Copy link
Contributor Author

Yes, you are correct, this seem as the same problem.

@shafik shafik added the confirmed Verified by a second party label May 19, 2024
@shafik shafik closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2024
@shafik shafik added the duplicate Resolved as duplicate label Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen confirmed Verified by a second party consteval C++20 consteval constexpr Anything related to constant evaluation duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

5 participants
@shafik @hanickadot @EugeneZelenko @llvmbot and others