@@ -586,31 +586,50 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
586
586
PrioritizedCXXGlobalInits.size ());
587
587
PrioritizedCXXGlobalInits.push_back (std::make_pair (Key, Fn));
588
588
} else if (isTemplateInstantiation (D->getTemplateSpecializationKind ()) ||
589
- getContext ().GetGVALinkageForVariable (D) == GVA_DiscardableODR ||
589
+ ! isUniqueGVALinkage ( getContext ().GetGVALinkageForVariable (D)) ||
590
590
D->hasAttr <SelectAnyAttr>()) {
591
+ // For vague linkage globals, put the initializer into its own global_ctors
592
+ // entry with the global as a comdat key. This ensures at most one
593
+ // initializer per DSO runs during DSO dynamic initialization.
594
+ //
595
+ // For ELF platforms, this is an important code size and startup time
596
+ // optimization. For dynamic, non-hidden symbols, the weak guard variable
597
+ // remains to ensure that other DSOs do not re-initialize the global.
598
+ //
599
+ // For PE-COFF platforms, there is no guard variable, and COMDAT
600
+ // associativity is the only way to ensure vauge linkage globals are
601
+ // initialized exactly once.
602
+ //
603
+ // MachO is the only remaining platform with no comdats that doesn't
604
+ // benefit from this optimization. The rest are mainly modeled on ELF
605
+ // behavior.
606
+ //
607
+ // C++ requires that inline global variables are initialized in source
608
+ // order, but this requirement does not exist for templated entities.
609
+ // llvm.global_ctors does not guarantee initialization order, so in
610
+ // general, Clang does not fully conform to the ordering requirement.
611
+ // However, in practice, LLVM emits global_ctors in the provided order, and
612
+ // users typically don't rely on ordering between inline globals in
613
+ // different headers which are then transitively included in varying order.
614
+ // Clang's current behavior is a practical tradeoff, since dropping the
615
+ // comdat would lead to unacceptable impact on code size and startup time.
616
+ //
617
+ // FIXME: Find a solution to guarantee source-order initialization of
618
+ // inline variables.
619
+ //
591
620
// C++ [basic.start.init]p2:
592
621
// Definitions of explicitly specialized class template static data
593
622
// members have ordered initialization. Other class template static data
594
623
// members (i.e., implicitly or explicitly instantiated specializations)
595
624
// have unordered initialization.
596
625
//
597
- // As a consequence, we can put them into their own llvm.global_ctors entry.
598
- //
599
- // If the global is externally visible, put the initializer into a COMDAT
600
- // group with the global being initialized. On most platforms, this is a
601
- // minor startup time optimization. In the MS C++ ABI, there are no guard
602
- // variables, so this COMDAT key is required for correctness.
603
- //
604
- // SelectAny globals will be comdat-folded. Put the initializer into a
605
- // COMDAT group associated with the global, so the initializers get folded
606
- // too.
607
- I = DelayedCXXInitPosition.find (D);
608
626
// CXXGlobalInits.size() is the lex order number for the next deferred
609
627
// VarDecl. Use it when the current VarDecl is non-deferred. Although this
610
628
// lex order number is shared between current VarDecl and some following
611
629
// VarDecls, their order of insertion into `llvm.global_ctors` is the same
612
630
// as the lexing order and the following stable sort would preserve such
613
631
// order.
632
+ I = DelayedCXXInitPosition.find (D);
614
633
unsigned LexOrder =
615
634
I == DelayedCXXInitPosition.end () ? CXXGlobalInits.size () : I->second ;
616
635
AddGlobalCtor (Fn, 65535 , LexOrder, COMDATKey);
0 commit comments