-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Static initialization order of variables in a single TU #55804
Comments
@llvm/issue-subscribers-clang-codegen |
Based on the discussion https://reviews.llvm.org/D126341/new/#3541259, presume the test case here is valid (which I think so), this is due to our using COMDAT for inline variables (and we can't rely on the order in |
It's not clear to me if static data member is an inline variable which has the same ordering requirements from the standard, but essentially, yes, that's the issue. |
Thanks for the confirmation. I'll take a look at this. |
I don't think the use of COMDATs is relevant here. That might cause similar problems in multi-translation-unit cases if the linker selects COMDATs inconsistently (but I don't think we know whether any real linkers do so). The entries in Certainly switching away from using separate initialization functions and COMDATs for inline variables would solve this. I'm still optimistic that we can avoid doing so. |
If not, I think we should assign mangled names to the initialization functions for inline variables so we can at least deduplicate them across TUs, even if we're going to end up invoking them more than once each. |
Thanks for the suggestions. I've put up a fix here https://reviews.llvm.org/D127233. |
Fixes llvm#55804 The lexing order is already bookkept in DelayedCXXInitPosition but we were not using it based on the wrong assumption that inline variable is unordered. This patch fixes it by ordering entries in llvm.global_ctors by orders in DelayedCXXInitPosition. for llvm.global_ctors entries without a lexing order, ordering them by the insertion order. (This *mostly* orders the template instantiation in https://reviews.llvm.org/D126341 intuitively, minus one tweak for which I'll submit a separate patch.) Differential Revision: https://reviews.llvm.org/D127233
Fixes llvm/llvm-project#55804 The lexing order is already bookkept in DelayedCXXInitPosition but we were not using it based on the wrong assumption that inline variable is unordered. This patch fixes it by ordering entries in llvm.global_ctors by orders in DelayedCXXInitPosition. for llvm.global_ctors entries without a lexing order, ordering them by the insertion order. (This *mostly* orders the template instantiation in https://reviews.llvm.org/D126341 intuitively, minus one tweak for which I'll submit a separate patch.) Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D127233
I left a question on stack overflow detailing the issue: https://stackoverflow.com/q/72286507/5336393, but no answers yet. I'm not sure whether it is a Clang bug or if there is no initialization order guarantee.
The example is:
Surprisingly,
ct
is initialized beforev1
Using Clang 14.0.3, but this issue goes back further. Is this a bug or are there no guarantees about the initialization order?
https://clang.godbolt.org/z/M9e757r46
The text was updated successfully, but these errors were encountered: