[MS] Implement on-demand TLS initialization for Microsoft CXX ABI
authorMaurice Heumann <MauriceHeumann@gmail.com>
Thu, 13 Jan 2022 02:26:30 +0000 (18:26 -0800)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 14 Jan 2022 05:23:23 +0000 (21:23 -0800)
commit072e2a7c67b74b8bde35f78ac0b0b13a269380f8
tree25cdecf4a92725c0b8e0ec4034003c7e0d2ae49b
parent11067d711bca10ce740d0673073576bb81f50e06
[MS] Implement on-demand TLS initialization for Microsoft CXX ABI

TLS initializers, for example constructors of thread-local variables, don't necessarily get called. If a thread was created before a module is loaded, the module's TLS initializers are not executed for this particular thread.

This is why Microsoft added support for dynamic TLS initialization. Before every use of thread-local variables, a check is added that runs the module's TLS initializers on-demand.

To do this, the method `__dyn_tls_on_demand_init` gets called. Internally, it simply calls `__dyn_tls_init`.

No additional TLS initializer that sets the guard needs to be emitted, as the guard always gets set by `__dyn_tls_init`.
The guard is also checked again within `__dyn_tls_init`. This makes our check redundant, however, as Microsoft's compiler also emits this check, the behaviour is adopted here.

Reviewed By: majnemer

Differential Revision: https://reviews.llvm.org/D115456
clang/include/clang/Basic/LangOptions.h
clang/lib/CodeGen/CGCXXABI.cpp
clang/lib/CodeGen/CGCXXABI.h
clang/lib/CodeGen/ItaniumCXXABI.cpp
clang/lib/CodeGen/MicrosoftCXXABI.cpp
clang/test/CodeGenCXX/ms-thread_local.cpp