From: Manman Ren Date: Wed, 11 Nov 2015 22:42:31 +0000 (+0000) Subject: [TLS on Darwin] change how we handle globals with linkonce or weak linkage. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ff855dea215a5fdb7478c49bf0ab183a420deb59;p=clang [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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@252814 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 5d7794e4a3..d36a85df54 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2315,12 +2315,17 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 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); diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp index 0eea014b3d..a6402f233c 100644 --- a/lib/CodeGen/ItaniumCXXABI.cpp +++ b/lib/CodeGen/ItaniumCXXABI.cpp @@ -2163,12 +2163,10 @@ getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { 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::getOrCreateThreadLocalWrapper(const VarDecl *VD, 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; } diff --git a/test/CodeGenCXX/cxx11-thread-local-reference.cpp b/test/CodeGenCXX/cxx11-thread-local-reference.cpp index 2d74120000..a1efd1adb3 100644 --- a/test/CodeGenCXX/cxx11-thread-local-reference.cpp +++ b/test/CodeGenCXX/cxx11-thread-local-reference.cpp @@ -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* %{{.*}} diff --git a/test/CodeGenCXX/cxx11-thread-local.cpp b/test/CodeGenCXX/cxx11-thread-local.cpp index 9d9d255dcb..b96b027bb8 100644 --- a/test/CodeGenCXX/cxx11-thread-local.cpp +++ b/test/CodeGenCXX/cxx11-thread-local.cpp @@ -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 struct V { static thread_local int m; }; @@ -45,9 +48,11 @@ int e = V::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 diff --git a/test/CodeGenCXX/tls-init-funcs.cpp b/test/CodeGenCXX/tls-init-funcs.cpp index d47329cdc2..8188f214df 100644 --- a/test/CodeGenCXX/tls-init-funcs.cpp +++ b/test/CodeGenCXX/tls-init-funcs.cpp @@ -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 {