r252814 - [TLS on Darwin] change how we handle globals with linkonce or weak linkage.

Manman Ren via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 14:42:31 PST 2015


Author: mren
Date: Wed Nov 11 16:42:31 2015
New Revision: 252814

URL: http://llvm.org/viewvc/llvm-project?rev=252814&view=rev
Log:
[TLS on Darwin] change how we handle globals with linkonce or weak linkage.

This is about how we handle static member of a template. Before this commit,
we use internal linkage for the IR thread-local variable, which is inefficient.
With this commit, we will start to follow Itanium C++ ABI.

rdar://problem/23415206

Reviewed by John McCall.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp
    cfe/trunk/test/CodeGenCXX/cxx11-thread-local-reference.cpp
    cfe/trunk/test/CodeGenCXX/cxx11-thread-local.cpp
    cfe/trunk/test/CodeGenCXX/tls-init-funcs.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=252814&r1=252813&r2=252814&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Wed Nov 11 16:42:31 2015
@@ -2315,12 +2315,17 @@ void CodeGenModule::EmitGlobalVarDefinit
   llvm::GlobalValue::LinkageTypes Linkage =
       getLLVMLinkageVarDefinition(D, GV->isConstant());
 
-  // On Darwin, the backing variable for a C++11 thread_local variable always
-  // has internal linkage; all accesses should just be calls to the
+  // On Darwin, if the normal linkage of a C++ thread_local variable is
+  // LinkOnce or Weak, we keep the normal linkage to prevent multiple
+  // copies within a linkage unit; otherwise, the backing variable has
+  // internal linkage and all accesses should just be calls to the
   // Itanium-specified entry point, which has the normal linkage of the
-  // variable.
+  // variable. This is to preserve the ability to change the implementation
+  // behind the scenes.
   if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
-      Context.getTargetInfo().getTriple().isMacOSX())
+      Context.getTargetInfo().getTriple().isMacOSX() &&
+      !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) &&
+      !llvm::GlobalVariable::isWeakLinkage(Linkage))
     Linkage = llvm::GlobalValue::InternalLinkage;
 
   GV->setLinkage(Linkage);

Modified: cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp?rev=252814&r1=252813&r2=252814&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp (original)
+++ cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp Wed Nov 11 16:42:31 2015
@@ -2163,12 +2163,10 @@ getThreadLocalWrapperLinkage(const VarDe
     return VarLinkage;
 
   // If the thread wrapper is replaceable, give it appropriate linkage.
-  if (isThreadWrapperReplaceable(VD, CGM)) {
-    if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) ||
-        llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
-      return llvm::GlobalVariable::WeakAnyLinkage;
-    return VarLinkage;
-  }
+  if (isThreadWrapperReplaceable(VD, CGM))
+    if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
+        !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
+      return VarLinkage;
   return llvm::GlobalValue::WeakODRLinkage;
 }
 
@@ -2194,7 +2192,9 @@ ItaniumCXXABI::getOrCreateThreadLocalWra
       llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
                              WrapperName.str(), &CGM.getModule());
   // Always resolve references to the wrapper at link time.
-  if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM))
+  if (!Wrapper->hasLocalLinkage() && !(isThreadWrapperReplaceable(VD, CGM) &&
+      !llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) &&
+      !llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage())))
     Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
   return Wrapper;
 }

Modified: cfe/trunk/test/CodeGenCXX/cxx11-thread-local-reference.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx11-thread-local-reference.cpp?rev=252814&r1=252813&r2=252814&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/cxx11-thread-local-reference.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/cxx11-thread-local-reference.cpp Wed Nov 11 16:42:31 2015
@@ -1,11 +1,14 @@
-// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-apple-darwin12 | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
 
 int &f();
 
-// CHECK: @r = thread_local global i32* null
+// LINUX: @r = thread_local global i32* null
+// DARWIN: @r = internal thread_local global i32* null
 thread_local int &r = f();
 
-// CHECK: @_ZTH1r = alias void (), void ()* @__tls_init
+// LINUX: @_ZTH1r = alias void (), void ()* @__tls_init
+// DARWIN: @_ZTH1r = internal alias void (), void ()* @__tls_init
 
 int &g() { return r; }
 
@@ -17,7 +20,8 @@ int &g() { return r; }
 // CHECK: call i32* @_ZTW1r()
 // CHECK: ret i32* %{{.*}}
 
-// CHECK: define weak_odr hidden i32* @_ZTW1r() {
+// LINUX: define weak_odr hidden i32* @_ZTW1r() {
+// DARWIN: define i32* @_ZTW1r() {
 // CHECK: call void @_ZTH1r()
 // CHECK: load i32*, i32** @r, align 8
 // CHECK: ret i32* %{{.*}}

Modified: cfe/trunk/test/CodeGenCXX/cxx11-thread-local.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx11-thread-local.cpp?rev=252814&r1=252813&r2=252814&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/cxx11-thread-local.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/cxx11-thread-local.cpp Wed Nov 11 16:42:31 2015
@@ -1,11 +1,13 @@
-// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
 // RUN: %clang_cc1 -std=c++11 -femulated-tls -emit-llvm %s -o - \
-// RUN:     -triple x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK %s
+// RUN:     -triple x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-apple-darwin12 | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
 
 int f();
 int g();
 
-// CHECK: @a = thread_local global i32 0
+// LINUX: @a = thread_local global i32 0
+// DARWIN: @a = internal thread_local global i32 0
 thread_local int a = f();
 extern thread_local int b;
 // CHECK: @c = global i32 0
@@ -14,7 +16,8 @@ int c = b;
 static thread_local int d = g();
 
 struct U { static thread_local int m; };
-// CHECK: @_ZN1U1mE = thread_local global i32 0
+// LINUX: @_ZN1U1mE = thread_local global i32 0
+// DARWIN: @_ZN1U1mE = internal thread_local global i32 0
 thread_local int U::m = f();
 
 template<typename T> struct V { static thread_local int m; };
@@ -45,9 +48,11 @@ int e = V<int>::m;
 
 // CHECK: @llvm.global_ctors = appending global {{.*}} @[[GLOBAL_INIT:[^ ]*]]
 
-// CHECK: @_ZTH1a = alias void (), void ()* @__tls_init
+// LINUX: @_ZTH1a = alias void (), void ()* @__tls_init
+// DARWIN: @_ZTH1a = internal alias void (), void ()* @__tls_init
 // CHECK: @_ZTHL1d = internal alias void (), void ()* @__tls_init
-// CHECK: @_ZTHN1U1mE = alias void (), void ()* @__tls_init
+// LINUX: @_ZTHN1U1mE = alias void (), void ()* @__tls_init
+// DARWIN: @_ZTHN1U1mE = internal alias void (), void ()* @__tls_init
 // CHECK: @_ZTHN1VIiE1mE = linkonce_odr alias void (), void ()* @__tls_init
 
 
@@ -78,13 +83,15 @@ int f() {
 // CHECK-NEXT: load i32, i32* %{{.*}}, align 4
 // CHECK-NEXT: store i32 %{{.*}}, i32* @c, align 4
 
-// CHECK-LABEL: define weak_odr hidden i32* @_ZTW1b()
-// CHECK: br i1 icmp ne (void ()* @_ZTH1b, void ()* null),
+// LINUX-LABEL: define weak_odr hidden i32* @_ZTW1b()
+// LINUX: br i1 icmp ne (void ()* @_ZTH1b, void ()* null),
 // not null:
-// CHECK: call void @_ZTH1b()
-// CHECK: br label
+// LINUX: call void @_ZTH1b()
+// LINUX: br label
 // finally:
-// CHECK: ret i32* @b
+// LINUX: ret i32* @b
+// DARWIN-LABEL: declare i32* @_ZTW1b()
+// There is no definition of the thread wrapper on Darwin for external TLV.
 
 // CHECK: define {{.*}} @[[D_INIT:.*]]()
 // CHECK: call i32 @_Z1gv()
@@ -111,24 +118,28 @@ struct T { ~T(); };
 void tls_dtor() {
   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1s
   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZZ8tls_dtorvE1s)
-  // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
+  // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
+  // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1s
   static thread_local S s;
 
   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1t
   // CHECK-NOT: _ZN1T
-  // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
+  // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
+  // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1t
   static thread_local T t;
 
   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1u
   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZGRZ8tls_dtorvE1u_)
-  // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
+  // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
+  // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1u
   static thread_local const S &u = S();
 }
 
-// CHECK: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
+// LINUX: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
+// DARWIN: declare i32 @_tlv_atexit(void (i8*)*, i8*, i8*)
 
 // CHECK: define {{.*}} @_Z7PR15991v(
 int PR15991() {
@@ -143,7 +154,8 @@ struct PR19254 {
 };
 // CHECK: define {{.*}} @_ZN7PR192541fEv(
 int PR19254::f() {
-  // CHECK: call void @_ZTHN7PR192541nE(
+  // LINUX: call void @_ZTHN7PR192541nE(
+  // DARWIN: call i32* @_ZTWN7PR192541nE(
   return this->n;
 }
 
@@ -182,19 +194,21 @@ void set_anon_i() {
 // CHECK: call void @[[V_M_INIT]]()
 
 
-// CHECK: define weak_odr hidden i32* @_ZTW1a() {
+// LIUNX: define weak_odr hidden i32* @_ZTW1a() {
+// DARWIN: define i32* @_ZTW1a() {
 // CHECK:   call void @_ZTH1a()
 // CHECK:   ret i32* @a
 // CHECK: }
 
 
-// CHECK: declare extern_weak void @_ZTH1b()
+// LINUX: declare extern_weak void @_ZTH1b()
 
 
 // CHECK-LABEL: define internal i32* @_ZTWL1d()
 // CHECK: call void @_ZTHL1d()
 // CHECK: ret i32* @_ZL1d
 
-// CHECK-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
+// LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
+// DARWIN-LABEL: define i32* @_ZTWN1U1mE()
 // CHECK: call void @_ZTHN1U1mE()
 // CHECK: ret i32* @_ZN1U1mE

Modified: cfe/trunk/test/CodeGenCXX/tls-init-funcs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/tls-init-funcs.cpp?rev=252814&r1=252813&r2=252814&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/tls-init-funcs.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/tls-init-funcs.cpp Wed Nov 11 16:42:31 2015
@@ -1,13 +1,13 @@
 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.8 -std=c++1y -S -emit-llvm %s -o - | FileCheck %s
 
 // CHECK: @a = internal thread_local global
-// CHECK: @_Z2vtIiE = internal thread_local global i32 5
+// CHECK: @_Z2vtIiE = linkonce_odr thread_local global i32 5
 // CHECK: @_ZZ3inlvE3loc = linkonce_odr thread_local global i32 0
 // CHECK: @_tlv_atexit({{.*}}@_ZN1AD1Ev
 // CHECK: call i32* @_ZTW3ext()
 // CHECK: declare i32* @_ZTW3ext()
-// CHECK: define weak i32* @_ZTW2vtIiE()
-// CHECK: define weak i32* @_ZTW2vtIvE()
+// CHECK: define weak_odr hidden i32* @_ZTW2vtIiE()
+// CHECK: define weak_odr hidden i32* @_ZTW2vtIvE()
 // CHECK: define {{.*}} @_ZTW1a
 
 struct A {




More information about the cfe-commits mailing list