[PATCH] CodeGen: Don't emit a thread-wrapper if we can't touch the backing variable

David Majnemer david.majnemer at gmail.com
Thu Jun 12 19:29:08 PDT 2014


================
Comment at: lib/CodeGen/ItaniumCXXABI.cpp:1599
@@ +1598,3 @@
+  if (!isThreadLocalBackingVariableAlwaysReferenceable(VD, CGM))
+    return VD->hasDefinition() ? llvm::GlobalValue::WeakAnyLinkage
+                               : llvm::GlobalValue::ExternalLinkage;
----------------
Richard Smith wrote:
> David Majnemer wrote:
> > Richard Smith wrote:
> > > Hmm, what's the motivation for making this weak? Should it be strong if the `thread_local` variable is a strong definition?
> > > 
> > > (I'm also not completely sure whether `WeakAny` or `WeakODR` is appropriate for the case where the variable *is* weak; I *think* the intent is for the underlying mechanism to be replaceable, which implies using `WeakAny`.)
> > It's a strong definition but an internal one.
> > 
> > `WeakODR` permits inlining of the thread-wrapper. This, in turn, allows references to the internal backing store of the variable to be proliferated if the thread-wrapper gets inlined.
> I feel like I'm missing something here. Why doesn't this:
> 
>   thread_local int n = f();
> 
> ... get a non-weak, inlineable definition for its thread wrapper (in the translation unit with the definition)?
True but we still need to handle cases like:
  template <typename T>
  thread_local int n = f();

I'll try to make it more conservative.

================
Comment at: test/CodeGenCXX/tls-init-funcs.cpp:6-7
@@ -5,2 +5,4 @@
+// CHECK: call {{.*}} @_ZTW3ext()
+// CHECK: declare hidden {{.*}} @_ZTW3ext()
 // CHECK: define weak hidden {{.*}} @_ZTW1a
 
----------------
Richard Smith wrote:
> David Majnemer wrote:
> > Richard Smith wrote:
> > > Why are these marked as `hidden`? Don't they need to be visible to support cross-DSO linking of `thread_local` variables?
> > ToT clang makes the thread-wrapper hidden. ItaniumCXXABI.cpp has the following comment in it:
> > > Always resolve references to the wrapper at link time.
> > 
> > It was added by you in r179858.
> That works on the basis that every translation unit which references a TW symbol provides a (weak) definition of that symbol. That's not true in the Mac OS case (where they don't follow the Itanium ABI).
Then we can skip the `hidden`.

http://reviews.llvm.org/D4109






More information about the cfe-commits mailing list