| Issue |
233752
|
| Summary |
[clang-cl] Inline variable is initialized after a later global of the same TU (.CRT$XCU section order)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
AnatolyKalin
|
On `x86_64-pc-windows-msvc`, the dynamic initializer of an inline variable runs after the initializer of a later non-inline global of the same translation unit. [basic.start.dynamic]/3 requires `V` to be initialized before `W`; cl.exe and clang on ELF do it.
```cpp
#include <cstdio>
int makeV() {
std::puts("init V");
return 42;
}
inline const int V = makeV();
static const int W = (std::printf("init W, V = %d\n", V), V);
int main() {
std::printf("main: W = %d (expected 42)\n", W);
return W == 42 ? 0 : 1;
}
```
```
> clang-cl /std:c++20 /O2 single.cpp && single.exe
init W, V = 0
init V
main: W = 0 (expected 42)
```
cl.exe 19.42 prints `init V`, then `init W, V = 42`. Reproduced with clang-cl 19.1.5 and 23.1.3, with link.exe and lld-link.
`llvm.global_ctors` is in the right order: the entry of `??__EV@@YAXXZ` (keyed on the comdat `?V@@3HB`) comes before `_GLOBAL__sub_I_single.cpp`, as fixed in #55804. In the object file, however, the comdat-associative `.CRT$XCU` section of `V` gets a higher section number than the `.CRT$XCU` section of the TU initializer (23 and 14 in `llvm-objdump -h`), and the linker keeps sections of the same name in that order. cl.exe emits the `.CRT$XCU` section of `V` first.
The same happens across TUs: a TU that includes the definition of `V` and is first on the link line runs its own globals before `V`.
Related: #55804, #116950, and the FIXME in `CodeGenModule::EmitCXXGlobalVarDeclInitFunc` (clang/lib/CodeGen/CGDeclCXX.cpp).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs