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

Clang/LLVM 7.0.1 lld-link.exe initializes inline static data member multiple times

Open KarstenH opened this issue 8 years ago • 4 comments

Bugzilla Link 38171
Resolution DUPLICATE
Resolved on Apr 22, 2019 13:10
Version 7.0
OS Windows NT
CC @zygoloid,@rnk

Extended Description

Clang/LLVM 6.0.0 lld-link.exe initializes an inline static data member once per TU. As far as I understand C++17 this is not correct.

Although an inline variable may be defined in multiple TUs the compiler and/or linker must ensure that it exists only once in a program and hence is initialized exactly once. Just like multiply defined inline functions exist only once in a program. See the following little program:

// header.h #include

struct S { inline static int i = ((std::cout << "i "), 0); // C++17 inline variable, thus also a definition };

// TU1.cpp #include "header.h"

int main() { std::cin.get(); }

// TU2.cpp include "header.h"

// TU3.cpp include "header.h"

// TU4.cpp include "header.h"

Now S::i should be initialized exactly once. Thus the output should be "i ". But in fact it is "i i i i ". That's four initializations, one for each TU.

MSVC's link.exe behaves a little different. In debug mode it is incorrect, too. But in release mode it correctly initializes exactly once.

This is a bug in lld-link.exe, right?

If so I consider this a serious bug. Assume S::i is not a simple int but an object of class type that does a bunch of work in its constructor. For example, it might create a window. Now you end up having four windows instead of one and wonder what happened.

KarstenH avatar Jul 15 '18 06:07 KarstenH

OK, thanks, that explains it. Weird somehow. Did I do something wrong when I reported the bug and/or changed the details? I'm almost sure I never touched the CC list, at least not consciously. Maybe Firefox does something wrong?

Maybe, but it's also possible that llvm bugzilla was just misconfigured at the time, and then it was fixed. I don't know how to audit for configuration changes.

rnk avatar Apr 22 '19 20:04 rnk

I noticed this also did not have llvm-bugs CC'd on it, so this only went to Richard. I double checked in the bugzilla admin console, and llvm-bugs is on the default CC list. Essentially, nobody except Richard ever received email about this bug, which is why no action has been taken. Similarly, your follow up comment went only to Richard.

*** This bug has been marked as a duplicate of bug llvm/llvm-project#37251 *** OK, thanks, that explains it. Weird somehow. Did I do something wrong when I reported the bug and/or changed the details? I'm almost sure I never touched the CC list, at least not consciously. Maybe Firefox does something wrong?

KarstenH avatar Apr 20 '19 06:04 KarstenH

I noticed this also did not have llvm-bugs CC'd on it, so this only went to Richard. I double checked in the bugzilla admin console, and llvm-bugs is on the default CC list. Essentially, nobody except Richard ever received email about this bug, which is why no action has been taken. Similarly, your follow up comment went only to Richard.

*** This bug has been marked as a duplicate of bug llvm/llvm-project#37251 ***

rnk avatar Apr 19 '19 16:04 rnk

It's still an error in 7.0.1.

Please Richard, see also here: https://stackoverflow.com/q/50982390/8731417 This is an example with an object of class type.

KarstenH avatar Dec 25 '18 09:12 KarstenH