llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

incorrect static object initialization on Windows

Open mibintc opened this issue 7 years ago • 7 comments

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 struct X_ { static T i; static T init() { hist_ |= sizeof(T); return T(); } }; template T X_::i = X_::init(); template struct X_;

int main(int argc, char *argv[]) { X_ my_var2; if (hist_ != sizeof(int)) { printf("Fail hist is %d %d\n", hist_, sizeof(int)); // On Windows, this exit is taken return 4; } else printf("Pass hist is %d %d\n", hist_, sizeof(int)); return 0; }

mibintc avatar Jan 15 '19 19:01 mibintc

mentioned in issue llvm/llvm-project#40816

nico avatar Nov 27 '21 01:11 nico

Rechecked in

jyu2-git avatar Apr 26 '19 17:04 jyu2-git

The fix caused bug 41471, so maybe we'll have to revert it.

nico avatar Apr 11 '19 21:04 nico

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.

jyu2-git avatar Apr 03 '19 17:04 jyu2-git

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.

rnk avatar Jan 15 '19 19:01 rnk

I work for Intel on the C++ compiler --Melanie Blower

mibintc avatar Jan 15 '19 19:01 mibintc

assigned to @jyu2-git

mibintc avatar Jan 15 '19 19:01 mibintc