incorrect static object initialization on Windows
| Bugzilla Link | 40323 |
| Resolution | FIXED |
| Resolved on | Apr 26, 2019 10:35 |
| Version | trunk |
| OS | Windows NT |
| Depends On | llvm/llvm-project#40816 |
| CC | @DougGregor,@jyu2-git,@nico,@zygoloid,@rnk |
| Fixed by commit(s) | rG0b28b8b09be7 |
Extended Description
This test case passes on Linux but fails on Windows, with -O0. When compiled with the Microsoft compiler, the test case passes.
Output: Fail hist is 0 4
$ cat bug.cpp
#include <stdio.h>
long hist_ = 0;
template
int main(int argc, char *argv[])
{
X_
mentioned in issue llvm/llvm-project#40816
Rechecked in
The fix caused bug 41471, so maybe we'll have to revert it.
This is fix by: D58160: MS ABI: adding template static member in the linker directive section to make sure init function can be called before main.
What's happening here is that the VC++ generates an extra -include:?i@?$X_@H@@2HA directive to ensure that the template static data member global is retained when /OPT:REF is enabled.
We can fix this in clang by adding @"?i@?$X_@H@@2HA" to the llvm.used list, which will cause us to emit a similar -include:?i@?$X_@H@@2HA directive into the .drective section.
There might be an issue filed about this somewhere else in the tracker, since I do remember encountering it before. At the time, I thought to myself, "what kind of person would use side-effects in an initializer of a static data member of a class template?", and dismissed it as not important. It goes to show that these things always come back around.
I work for Intel on the C++ compiler --Melanie Blower
assigned to @jyu2-git