llvm.org GIT mirror llvm / 665d42a
[pr19844] Add thread local mode to aliases. This matches gcc's behavior. It also seems natural given that aliases contain other properties that govern how it is accessed (linkage, visibility, dll storage). Clang still has to be updated to expose this feature to C. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209759 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
18 changed file(s) with 157 addition(s) and 125 deletion(s). Raw diff Collapse all Expand all
463463 exists for defining a dll interface, the compiler, assembler and linker know
464464 it is externally referenced and must refrain from deleting the symbol.
465465
466 .. _namedtypes:
467
468 Structure Types
469 ---------------
470
471 LLVM IR allows you to specify both "identified" and "literal" :ref:`structure
472 types `. Literal types are uniqued structurally, but identified types
473 are never uniqued. An :ref:`opaque structural type ` can also be used
474 to forward declare a type which is not yet available.
475
476 An example of a identified structure specification is:
477
478 .. code-block:: llvm
479
480 %mytype = type { %mytype*, i32 }
481
482 Prior to the LLVM 3.0 release, identified types were structurally uniqued. Only
483 literal types are uniqued in recent versions of LLVM.
484
485 .. _globalvars:
486
487 Global Variables
488 ----------------
489
490 Global variables define regions of memory allocated at compilation time
491 instead of run-time.
492
493 Global variables definitions must be initialized, may have an explicit section
494 to be placed in, and may have an optional explicit alignment specified.
495
496 Global variables in other translation units can also be declared, in which
497 case they don't have an initializer.
466 .. _tls_model:
467
468 Thread Local Storage Models
469 ---------------------------
498470
499471 A variable may be defined as ``thread_local``, which means that it will
500472 not be shared by threads (each thread will have a separated copy of the
508480 ``localexec``
509481 For variables defined in the executable and only used within it.
510482
483 If no explicit model is given, the "general dynamic" model is used.
484
511485 The models correspond to the ELF TLS models; see `ELF Handling For
512486 Thread-Local Storage `_ for
513487 more information on under which circumstances the different models may
514488 be used. The target may choose a different TLS model if the specified
515489 model is not supported, or if a better choice of model can be made.
490
491 A model can also be specified in a alias, but then it only governs how
492 the alias is accessed. It will not have any effect in the aliasee.
493
494 .. _namedtypes:
495
496 Structure Types
497 ---------------
498
499 LLVM IR allows you to specify both "identified" and "literal" :ref:`structure
500 types `. Literal types are uniqued structurally, but identified types
501 are never uniqued. An :ref:`opaque structural type ` can also be used
502 to forward declare a type which is not yet available.
503
504 An example of a identified structure specification is:
505
506 .. code-block:: llvm
507
508 %mytype = type { %mytype*, i32 }
509
510 Prior to the LLVM 3.0 release, identified types were structurally uniqued. Only
511 literal types are uniqued in recent versions of LLVM.
512
513 .. _globalvars:
514
515 Global Variables
516 ----------------
517
518 Global variables define regions of memory allocated at compilation time
519 instead of run-time.
520
521 Global variables definitions must be initialized, may have an explicit section
522 to be placed in, and may have an optional explicit alignment specified.
523
524 Global variables in other translation units can also be declared, in which
525 case they don't have an initializer.
516526
517527 A variable may be defined as a global ``constant``, which indicates that
518528 the contents of the variable will **never** be modified (enabling better
570580 iteration.
571581
572582 Globals can also have a :ref:`DLL storage class `.
583
584 Variables and aliasaes can have a
585 :ref:`Thread Local Storage Model `.
573586
574587 Syntax::
575588
673686
674687 Syntax::
675688
676 @ = [Visibility] [DLLStorageClass] alias [Linkage] @
689 @ = [Visibility] [DLLStorageClass] [ThreadLocal] alias [Linkage] @
677690
678691 The linkage must be one of ``private``, ``internal``, ``linkonce``, ``weak``,
679692 ``linkonce_odr``, ``weak_odr``, ``external``. Note that some system linkers
6262 LinkageTypes Linkage, const Twine &Name)
6363 : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
6464 Visibility(DefaultVisibility), UnnamedAddr(0),
65 DllStorageClass(DefaultStorageClass), Parent(nullptr) {
65 DllStorageClass(DefaultStorageClass),
66 ThreadLocal(NotThreadLocal), Parent(nullptr) {
6667 setName(Name);
6768 }
6869
7374 unsigned UnnamedAddr : 1; // This value's address is not significant
7475 unsigned DllStorageClass : 2; // DLL storage class
7576
77 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
78 // the desired model?
79
7680 private:
7781 // Give subclasses access to what otherwise would be wasted padding.
78 // (22 + 2 + 1 + 2 + 5) == 32.
79 unsigned SubClassData : 22;
82 // (19 + 3 + 2 + 1 + 2 + 5) == 32.
83 unsigned SubClassData : 19;
8084 protected:
8185 unsigned getGlobalValueSubClassData() const {
8286 return SubClassData;
8387 }
8488 void setGlobalValueSubClassData(unsigned V) {
85 assert(V < (1 << 22) && "It will not fit");
89 assert(V < (1 << 19) && "It will not fit");
8690 SubClassData = V;
8791 }
8892
8993 Module *Parent; // The containing module.
9094 public:
95 enum ThreadLocalMode {
96 NotThreadLocal = 0,
97 GeneralDynamicTLSModel,
98 LocalDynamicTLSModel,
99 InitialExecTLSModel,
100 LocalExecTLSModel
101 };
102
91103 ~GlobalValue() {
92104 removeDeadConstantUsers(); // remove any dead constants using this.
93105 }
107119 assert((!hasLocalLinkage() || V == DefaultVisibility) &&
108120 "local linkage requires default visibility");
109121 Visibility = V;
122 }
123
124 /// If the value is "Thread Local", its value isn't shared by the threads.
125 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
126 void setThreadLocal(bool Val) {
127 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
128 }
129 void setThreadLocalMode(ThreadLocalMode Val) {
130 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
131 ThreadLocal = Val;
132 }
133 ThreadLocalMode getThreadLocalMode() const {
134 return static_cast(ThreadLocal);
110135 }
111136
112137 DLLStorageClassTypes getDLLStorageClass() const {
4040 void setParent(Module *parent);
4141
4242 bool isConstantGlobal : 1; // Is this a global constant?
43 unsigned threadLocalMode : 3; // Is this symbol "Thread Local",
44 // if so, what is the desired
45 // model?
4643 bool isExternallyInitializedConstant : 1; // Is this a global whose value
4744 // can change from its initial
4845 // value before global
5350 void *operator new(size_t s) {
5451 return User::operator new(s, 1);
5552 }
56
57 enum ThreadLocalMode {
58 NotThreadLocal = 0,
59 GeneralDynamicTLSModel,
60 LocalDynamicTLSModel,
61 InitialExecTLSModel,
62 LocalExecTLSModel
63 };
6453
6554 /// GlobalVariable ctor - If a parent module is specified, the global is
6655 /// automatically inserted into the end of the specified modules global list.
154143 bool isConstant() const { return isConstantGlobal; }
155144 void setConstant(bool Val) { isConstantGlobal = Val; }
156145
157 /// If the value is "Thread Local", its value isn't shared by the threads.
158 bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
159 void setThreadLocal(bool Val) {
160 threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
161 }
162 void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
163 ThreadLocalMode getThreadLocalMode() const {
164 return static_cast(threadLocalMode);
165 }
166
167146 bool isExternallyInitialized() const {
168147 return isExternallyInitializedConstant;
169148 }
258258 case lltok::kw_extern_weak: // OptionalLinkage
259259 case lltok::kw_external: { // OptionalLinkage
260260 unsigned Linkage, Visibility, DLLStorageClass;
261 GlobalVariable::ThreadLocalMode TLM;
261262 if (ParseOptionalLinkage(Linkage) ||
262263 ParseOptionalVisibility(Visibility) ||
263264 ParseOptionalDLLStorageClass(DLLStorageClass) ||
264 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass))
265 ParseOptionalThreadLocal(TLM) ||
266 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass,
267 TLM))
265268 return true;
266269 break;
267270 }
269272 case lltok::kw_hidden: // OptionalVisibility
270273 case lltok::kw_protected: { // OptionalVisibility
271274 unsigned Visibility, DLLStorageClass;
275 GlobalVariable::ThreadLocalMode TLM;
272276 if (ParseOptionalVisibility(Visibility) ||
273277 ParseOptionalDLLStorageClass(DLLStorageClass) ||
274 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass))
278 ParseOptionalThreadLocal(TLM) ||
279 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass, TLM))
275280 return true;
276281 break;
277282 }
278283
279 case lltok::kw_thread_local: // OptionalThreadLocal
284 case lltok::kw_thread_local: { // OptionalThreadLocal
285 GlobalVariable::ThreadLocalMode TLM;
286 if (ParseOptionalThreadLocal(TLM) ||
287 ParseGlobal("", SMLoc(), 0, false, 0, 0, TLM))
288 return true;
289 break;
290 }
291
280292 case lltok::kw_addrspace: // OptionalAddrSpace
281293 case lltok::kw_constant: // GlobalType
282294 case lltok::kw_global: // GlobalType
283 if (ParseGlobal("", SMLoc(), 0, false, 0, 0)) return true;
295 if (ParseGlobal("", SMLoc(), 0, false, 0, 0, GlobalValue::NotThreadLocal))
296 return true;
284297 break;
285298
286299 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
469482
470483 bool HasLinkage;
471484 unsigned Linkage, Visibility, DLLStorageClass;
485 GlobalVariable::ThreadLocalMode TLM;
472486 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
473487 ParseOptionalVisibility(Visibility) ||
474 ParseOptionalDLLStorageClass(DLLStorageClass))
488 ParseOptionalDLLStorageClass(DLLStorageClass) ||
489 ParseOptionalThreadLocal(TLM))
475490 return true;
476491
477492 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
478493 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
479 DLLStorageClass);
480 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
494 DLLStorageClass, TLM);
495 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
481496 }
482497
483498 /// ParseNamedGlobal:
492507
493508 bool HasLinkage;
494509 unsigned Linkage, Visibility, DLLStorageClass;
510 GlobalVariable::ThreadLocalMode TLM;
495511 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
496512 ParseOptionalLinkage(Linkage, HasLinkage) ||
497513 ParseOptionalVisibility(Visibility) ||
498 ParseOptionalDLLStorageClass(DLLStorageClass))
514 ParseOptionalDLLStorageClass(DLLStorageClass) ||
515 ParseOptionalThreadLocal(TLM))
499516 return true;
500517
501518 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
502519 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
503 DLLStorageClass);
504 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
520 DLLStorageClass, TLM);
521 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
505522 }
506523
507524 // MDString:
638655 /// Everything through DLL storage class has already been parsed.
639656 ///
640657 bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
641 unsigned Visibility, unsigned DLLStorageClass) {
658 unsigned Visibility, unsigned DLLStorageClass,
659 GlobalVariable::ThreadLocalMode TLM) {
642660 assert(Lex.getKind() == lltok::kw_alias);
643661 Lex.Lex();
644662 LocTy LinkageLoc = Lex.getLoc();
698716 std::unique_ptr GA(
699717 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
700718 Name, Aliasee, /*Parent*/ nullptr));
719 GA->setThreadLocalMode(TLM);
701720 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
702721 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
703722
752771 ///
753772 bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
754773 unsigned Linkage, bool HasLinkage,
755 unsigned Visibility, unsigned DLLStorageClass) {
774 unsigned Visibility, unsigned DLLStorageClass,
775 GlobalVariable::ThreadLocalMode TLM) {
756776 if (!isValidVisibilityForLinkage(Visibility, Linkage))
757777 return Error(NameLoc,
758778 "symbol with local linkage must have default visibility");
759779
760780 unsigned AddrSpace;
761781 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
762 GlobalVariable::ThreadLocalMode TLM;
763782 LocTy UnnamedAddrLoc;
764783 LocTy IsExternallyInitializedLoc;
765784 LocTy TyLoc;
766785
767786 Type *Ty = nullptr;
768 if (ParseOptionalThreadLocal(TLM) ||
769 ParseOptionalAddrSpace(AddrSpace) ||
787 if (ParseOptionalAddrSpace(AddrSpace) ||
770788 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
771789 &UnnamedAddrLoc) ||
772790 ParseOptionalToken(lltok::kw_externally_initialized,
238238 bool ParseNamedGlobal();
239239 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
240240 bool HasLinkage, unsigned Visibility,
241 unsigned DLLStorageClass);
241 unsigned DLLStorageClass,
242 GlobalVariable::ThreadLocalMode TLM);
242243 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility,
243 unsigned DLLStorageClass);
244 unsigned DLLStorageClass,
245 GlobalVariable::ThreadLocalMode TLM);
244246 bool ParseStandaloneMetadata();
245247 bool ParseNamedMetadata();
246248 bool ParseMDString(MDString *&Result);
20162016 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
20172017 else
20182018 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
2019 if (Record.size() > 5)
2020 NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
20192021 ValueList.push_back(NewGA);
20202022 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
20212023 break;
510510 llvm_unreachable("Invalid DLL storage class");
511511 }
512512
513 static unsigned getEncodedThreadLocalMode(const GlobalVariable &GV) {
513 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
514514 switch (GV.getThreadLocalMode()) {
515515 case GlobalVariable::NotThreadLocal: return 0;
516516 case GlobalVariable::GeneralDynamicTLSModel: return 1;
667667 Vals.push_back(getEncodedLinkage(A));
668668 Vals.push_back(getEncodedVisibility(A));
669669 Vals.push_back(getEncodedDLLStorageClass(A));
670 if (A.isThreadLocal())
671 Vals.push_back(getEncodedThreadLocalMode(A));
670672 unsigned AbbrevToUse = 0;
671673 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
672674 Vals.clear();
11891189 if (BitWidth < 64)
11901190 Offset = SignExtend64(Offset, BitWidth);
11911191
1192 const GlobalVariable *GVar = dyn_cast(GV);
1193 if (!GVar) {
1194 // If GV is an alias then use the aliasee for determining thread-localness.
1195 if (const GlobalAlias *GA = dyn_cast(GV))
1196 GVar = dyn_cast_or_null(GA->getAliasee());
1197 }
1198
11991192 unsigned Opc;
1200 if (GVar && GVar->isThreadLocal())
1193 if (GV->isThreadLocal())
12011194 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
12021195 else
12031196 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
14871487 }
14881488 PrintVisibility(GA->getVisibility(), Out);
14891489 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
1490 PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
14901491
14911492 Out << "alias ";
14921493
112112 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
113113 OperandTraits::op_begin(this),
114114 InitVal != nullptr, Link, Name),
115 isConstantGlobal(constant), threadLocalMode(TLMode),
115 isConstantGlobal(constant),
116116 isExternallyInitializedConstant(isExternallyInitialized) {
117 setThreadLocalMode(TLMode);
117118 if (InitVal) {
118119 assert(InitVal->getType() == Ty &&
119120 "Initializer should be the same type as the GlobalVariable!");
131132 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
132133 OperandTraits::op_begin(this),
133134 InitVal != nullptr, Link, Name),
134 isConstantGlobal(constant), threadLocalMode(TLMode),
135 isConstantGlobal(constant),
135136 isExternallyInitializedConstant(isExternallyInitialized) {
137 setThreadLocalMode(TLMode);
136138 if (InitVal) {
137139 assert(InitVal->getType() == Ty &&
138140 "Initializer should be the same type as the GlobalVariable!");
239239 }
240240
241241 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
242 // We can't handle thread-local variables quickly yet. Unfortunately we have
243 // to peer through any aliases to find out if that rule applies.
244 const GlobalValue *TLSGV = GV;
245 if (const GlobalAlias *GA = dyn_cast(GV))
246 TLSGV = GA->getAliasee();
242 // We can't handle thread-local variables quickly yet.
243 if (GV->isThreadLocal())
244 return 0;
247245
248246 // MachO still uses GOT for large code-model accesses, but ELF requires
249247 // movz/movk sequences, which FastISel doesn't handle yet.
250248 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
251249 return 0;
252
253 if (const GlobalVariable *GVar = dyn_cast(TLSGV))
254 if (GVar->isThreadLocal())
255 return 0;
256250
257251 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
258252
18581858 // handle switches; if that changes, we need them as well. For now,
18591859 // what follows assumes everything's a generic (or TLS) global address.
18601860 const GlobalVariable *GVar = dyn_cast(GV);
1861 if (!GVar) {
1862 // If GV is an alias, use the aliasee for determining thread-locality.
1863 if (const GlobalAlias *GA = dyn_cast(GV))
1864 GVar = dyn_cast_or_null(GA->getAliasee());
1865 }
18661861
18671862 // FIXME: We don't yet handle the complexity of TLS.
1868 bool IsTLS = GVar && GVar->isThreadLocal();
1869 if (IsTLS)
1863 if (GV->isThreadLocal())
18701864 return 0;
18711865
18721866 // For small code model, generate a simple TOC load.
8787 }
8888
8989 /// Get the IR-specified TLS model for Var.
90 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
91 switch (Var->getThreadLocalMode()) {
90 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
91 switch (GV->getThreadLocalMode()) {
9292 case GlobalVariable::NotThreadLocal:
9393 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
9494 break;
126126 Model = TLSModel::InitialExec;
127127 }
128128
129 const GlobalVariable *Var = dyn_cast(GV);
130 if (Var) {
131 // If the user specified a more specific model, use that.
132 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
133 if (SelectedModel > Model)
134 return SelectedModel;
135 }
129 // If the user specified a more specific model, use that.
130 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
131 if (SelectedModel > Model)
132 return SelectedModel;
136133
137134 return Model;
138135 }
354354 return false;
355355
356356 // Can't handle TLS yet.
357 if (const GlobalVariable *GVar = dyn_cast(GV))
358 if (GVar->isThreadLocal())
359 return false;
360
361 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
362 // it works...).
363 if (const GlobalAlias *GA = dyn_cast(GV))
364 if (const GlobalVariable *GVar =
365 dyn_cast_or_null(GA->getAliasee()))
366 if (GVar->isThreadLocal())
367 return false;
357 if (GV->isThreadLocal())
358 return false;
368359
369360 // RIP-relative addresses can't have additional register operands, so if
370361 // we've already folded stuff into the addressing mode, just force the
0 ; RUN: llc -march=mipsel -relocation-model=pic -disable-mips-delay-filler < %s | FileCheck %s
11
22 @foo = thread_local global i32 42
3 @bar = hidden alias i32* @foo
3 @bar = hidden thread_local alias i32* @foo
44
55 define i32* @zed() {
66 ; CHECK-DAG: __tls_get_addr
77 @__resp = thread_local global %struct.__res_state* @_res ; <%struct.__res_state**> [#uses=1]
88 @_res = global %struct.__res_state zeroinitializer, section ".bss" ; <%struct.__res_state*> [#uses=1]
99
10 @__libc_resp = hidden alias %struct.__res_state** @__resp ; <%struct.__res_state**> [#uses=2]
10 @__libc_resp = hidden thread_local alias %struct.__res_state** @__resp ; <%struct.__res_state**> [#uses=2]
1111
1212 define i32 @foo() {
1313 ; CHECK-LABEL: foo:
None ; RUN: llc < %s -mtriple=i686-pc-linux-gnu -asm-verbose=false | FileCheck %s
0 ; RUN: llc < %s -mtriple=i686-pc-linux-gnu -asm-verbose=false \
1 ; RUN: -relocation-model=pic | FileCheck %s
2
3 @thread_var = thread_local global i32 42, align 4
4 @thread_alias = thread_local(localdynamic) alias i32* @thread_var
5
6 ; CHECK-LABEL: get_thread_var
7 define i32* @get_thread_var() {
8 ; CHECK: leal thread_var@TLSGD
9 ret i32* @thread_var
10 }
11
12 ; CHECK-LABEL: get_thread_alias
13 define i32* @get_thread_alias() {
14 ; CHECK: leal thread_alias@TLSLD
15 ret i32* @thread_alias
16 }
117
218 @bar = global i32 42
319
1616
1717 @v6 = alias i16, i32* @v1
1818 ; CHECK: @v6 = alias i16, i32* @v1
19
20 @v7 = thread_local(localdynamic) alias i32* @v1
21 ; CHECK: @v7 = thread_local(localdynamic) alias i32* @v1