This is an archive of the discontinued LLVM Phabricator instance.

MS ABI: Implement thread_local for global variables
ClosedPublic

Authored by majnemer on Oct 3 2014, 12:47 AM.

Details

Summary

This add support for the C++11 feature, thread_local global variables.
The ABI Clang implements is an improvement of the MSVC ABI. Sadly,
further improvements could be made but not without sacrificing ABI
compatibility.

The feature is implemented as follows:

  • All thread_local initialization routines are pointed to from the .CRT$XDU section.
  • All non-weak thread_local variables have their initialization routines call from a single function instead of getting their own .CRT$XDU section entry. This is done to open up optimization opportunities to the compiler.
  • All weak thread_local variables have their own .CRT$XDU section entry. This entry is in a COMDAT with the global variable it is initializing; this ensures that we will initialize the global exactly once.
  • Destructors are registered in the initialization function using __tlregdtor.

Diff Detail

Event Timeline

majnemer updated this revision to Diff 14369.Oct 3 2014, 12:47 AM
majnemer retitled this revision from to MS ABI: Implement thread_local for global variables.
majnemer updated this object.
majnemer added reviewers: rnk, rsmith, rjmccall.
majnemer added a subscriber: Unknown Object (MLST).
rnk added inline comments.Oct 3 2014, 1:43 PM
lib/CodeGen/CGCXXABI.h
449–450

This method appears dead now.

490–491

Make this pure and delete the generic CGCXXABI implementation now?

497–498

Can you update or delete the doxygen? IIRC there are clang warnings that detect parameter name mismatches like this.

lib/CodeGen/ItaniumCXXABI.cpp
237–238

dead

lib/CodeGen/MicrosoftCXXABI.cpp
274

dead

1778

"Nondiscardable" seems like the wrong name. Perhaps "NonComdatInits" or "TUOrderedInits"?

1799–1801

One possible alternative is to build a single global in the .CRT$XDU section that is an ordered array of all the thread local initializers that need to be run. I guess you considered and rejected this, because the single function allows for more optimization?

test/CodeGenCXX/ms-thread_local.cpp
22

Maybe add some code CHECKs like:

// CHECK-LABEL: define internal void @__tls_init()
// CHECK: call void @"\01??__E?b@@3UA@@A"()
majnemer updated this revision to Diff 14421.Oct 3 2014, 10:41 PM
  • Address review comments.
majnemer updated this revision to Diff 14422.Oct 3 2014, 11:05 PM
  • Fix a bug involving references.
rnk accepted this revision.Oct 4 2014, 10:45 AM
rnk edited edge metadata.

lgtm

This revision is now accepted and ready to land.Oct 4 2014, 10:45 AM
majnemer closed this revision.Oct 4 2014, 10:15 PM
majnemer updated this revision to Diff 14430.

Closed by commit rL219074 (authored by @majnemer).