From 7c6b7ba5f5410d4b552ee53c60d0690522c99959 Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Mon, 29 Apr 2013 22:27:16 +0000 Subject: [PATCH] Emit the TLS intialization functions into a list. Add the TLS initialization functions to a list of initialization functions. The back-end takes this list and places the function pointers into the correct section. This way they're called before `main().' git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180739 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDeclCXX.cpp | 1 + lib/CodeGen/CodeGenModule.cpp | 26 ++++++++++++++++++++++++++ lib/CodeGen/CodeGenModule.h | 11 +++++++++++ test/CodeGenCXX/tls-init-funcs.cpp | 21 +++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 test/CodeGenCXX/tls-init-funcs.cpp diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp index 9ffcff2766..4d78ea8f3a 100644 --- a/lib/CodeGen/CGDeclCXX.cpp +++ b/lib/CodeGen/CGDeclCXX.cpp @@ -304,6 +304,7 @@ void CodeGenModule::EmitCXXThreadLocalInitFunc() { Guard->setThreadLocal(true); CodeGenFunction(*this) .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard); + AddTLSInitFunc(InitFn); } getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn); diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index bf67bd1007..6247b15ce9 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -182,6 +182,7 @@ void CodeGenModule::Release() { AddGlobalCtor(ObjCInitFunction); EmitCtorList(GlobalCtors, "llvm.global_ctors"); EmitCtorList(GlobalDtors, "llvm.global_dtors"); + EmitTLSList(TLSInitFuncs); EmitGlobalAnnotations(); EmitStaticExternCAliases(); EmitLLVMUsed(); @@ -479,6 +480,12 @@ void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { GlobalDtors.push_back(std::make_pair(Dtor, Priority)); } +/// AddTLSInitFunc - Add a function to the list that will initialize TLS +/// variables before main() runs. +void CodeGenModule::AddTLSInitFunc(llvm::Function *Init) { + TLSInitFuncs.push_back(Init); +} + void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { // Ctor function type is void()*. llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); @@ -507,6 +514,25 @@ void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { } } +void CodeGenModule::EmitTLSList(ArrayRef Fns) { + if (Fns.empty()) return; + + // TLS init function types are void()*. + llvm::FunctionType* TLSFTy = llvm::FunctionType::get(VoidTy, false); + llvm::Type *TLSPFTy = llvm::PointerType::getUnqual(TLSFTy); + + SmallVector Inits; + for (ArrayRef::iterator I = Fns.begin(), + E = Fns.end(); I != E; ++I) + Inits.push_back(llvm::ConstantExpr::getBitCast(*I, TLSPFTy)); + + llvm::ArrayType *AT = llvm::ArrayType::get(TLSPFTy, Inits.size()); + new llvm::GlobalVariable(TheModule, AT, false, + llvm::GlobalValue::AppendingLinkage, + llvm::ConstantArray::get(AT, Inits), + "llvm.tls_init_funcs"); +} + llvm::GlobalValue::LinkageTypes CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 0f4fe8ae51..ca30096dcb 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -293,6 +293,10 @@ class CodeGenModule : public CodeGenTypeCache { /// priorities to be emitted when the translation unit is complete. CtorList GlobalDtors; + /// TLSInitFuncs - Store the list of TLS initialization functions toe be + /// emitted with the translation unit is complete. + std::vector TLSInitFuncs; + /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names. llvm::DenseMap MangledDeclNames; llvm::BumpPtrAllocator MangledNamesAllocator; @@ -1053,11 +1057,18 @@ private: void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535); void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535); + void AddTLSInitFunc(llvm::Function *Init); + /// EmitCtorList - Generates a global array of functions and priorities using /// the given list and name. This array will have appending linkage and is /// suitable for use as a LLVM constructor or destructor array. void EmitCtorList(const CtorList &Fns, const char *GlobalName); + /// EmitTLSList - Generates a global array of functions with the name + /// `llvm.tls_init_funcs'. This array will have appending linkage and is meant + /// to hold initialization functions for TLS variables. + void EmitTLSList(ArrayRef Fns); + /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the /// given type. void EmitFundamentalRTTIDescriptor(QualType Type); diff --git a/test/CodeGenCXX/tls-init-funcs.cpp b/test/CodeGenCXX/tls-init-funcs.cpp new file mode 100644 index 0000000000..79b0ea4319 --- /dev/null +++ b/test/CodeGenCXX/tls-init-funcs.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -std=c++11 -S -emit-llvm %s -o - | FileCheck -check-prefix=BITCODE %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -std=c++11 -S %s -o - | FileCheck -check-prefix=ASM %s + +// BITCODE: @llvm.tls_init_funcs = appending global [1 x void ()*] [void ()* @__tls_init] + +struct A { + A(); +}; + +struct B { + int i; + B(int i); +}; + +thread_local int i = 37; +thread_local A a; +thread_local B b(927); + +// ASM: .section __DATA,__thread_init,thread_local_init_function_pointers +// ASM: .align 3 +// ASM: .quad ___tls_init -- 2.40.0