@@ -618,127 +618,6 @@ void CodeGenModule::EmitCXXThreadLocalInitFunc() {
618
618
CXXThreadLocals.clear ();
619
619
}
620
620
621
- /* Build the initializer for a C++20 module:
622
- This is arranged to be run only once regardless of how many times the module
623
- might be included transitively. This arranged by using a control variable.
624
-
625
- First we call any initializers for imported modules.
626
- We then call initializers for the Global Module Fragment (if present)
627
- We then call initializers for the current module.
628
- We then call initializers for the Private Module Fragment (if present)
629
- */
630
-
631
- void CodeGenModule::EmitCXXModuleInitFunc (Module *Primary) {
632
- while (!CXXGlobalInits.empty () && !CXXGlobalInits.back ())
633
- CXXGlobalInits.pop_back ();
634
-
635
- // We create the function, even if it is empty, since an importer of this
636
- // module will refer to it unconditionally (for the current implementation
637
- // there is no way for the importer to know that an importee does not need
638
- // an initializer to be run).
639
-
640
- // Module initializers for imported modules are emitted first.
641
- // Collect the modules that we import
642
- SmallVector<Module *> AllImports;
643
- // Ones that we export
644
- for (auto I : Primary->Exports )
645
- AllImports.push_back (I.getPointer ());
646
- // Ones that we only import.
647
- for (Module *M : Primary->Imports )
648
- AllImports.push_back (M);
649
-
650
- SmallVector<llvm::Function *, 8 > ModuleInits;
651
- for (Module *M : AllImports) {
652
- llvm::FunctionType *FTy = llvm::FunctionType::get (VoidTy, false );
653
- SmallString<256 > FnName;
654
- {
655
- llvm::raw_svector_ostream Out (FnName);
656
- cast<ItaniumMangleContext>(getCXXABI ().getMangleContext ())
657
- .mangleModuleInitializer (M, Out);
658
- }
659
- assert (!GetGlobalValue (FnName.str ()) &&
660
- " We should only have one use of the initializer call" );
661
- llvm::Function *Fn = llvm::Function::Create (
662
- FTy, llvm::Function::ExternalLinkage, FnName.str (), &getModule ());
663
- ModuleInits.push_back (Fn);
664
- }
665
- AllImports.clear ();
666
-
667
- // Add any initializers with specified priority; this uses the same approach
668
- // as EmitCXXGlobalInitFunc().
669
- if (!PrioritizedCXXGlobalInits.empty ()) {
670
- SmallVector<llvm::Function *, 8 > LocalCXXGlobalInits;
671
- llvm::array_pod_sort (PrioritizedCXXGlobalInits.begin (),
672
- PrioritizedCXXGlobalInits.end ());
673
- for (SmallVectorImpl<GlobalInitData>::iterator
674
- I = PrioritizedCXXGlobalInits.begin (),
675
- E = PrioritizedCXXGlobalInits.end ();
676
- I != E;) {
677
- SmallVectorImpl<GlobalInitData>::iterator PrioE =
678
- std::upper_bound (I + 1 , E, *I, GlobalInitPriorityCmp ());
679
-
680
- for (; I < PrioE; ++I)
681
- ModuleInits.push_back (I->second );
682
- }
683
- PrioritizedCXXGlobalInits.clear ();
684
- }
685
-
686
- // Now append the ones without specified priority.
687
- for (auto F : CXXGlobalInits)
688
- ModuleInits.push_back (F);
689
- CXXGlobalInits.clear ();
690
-
691
- llvm::FunctionType *FTy = llvm::FunctionType::get (VoidTy, false );
692
- const CGFunctionInfo &FI = getTypes ().arrangeNullaryFunction ();
693
-
694
- // We now build the initializer for this module, which has a mangled name
695
- // as per the Itanium ABI . The action of the initializer is guarded so that
696
- // each init is run just once (even though a module might be imported
697
- // multiple times via nested use).
698
- llvm::Function *Fn;
699
- llvm::GlobalVariable *Guard = nullptr ;
700
- {
701
- SmallString<256 > InitFnName;
702
- llvm::raw_svector_ostream Out (InitFnName);
703
- cast<ItaniumMangleContext>(getCXXABI ().getMangleContext ())
704
- .mangleModuleInitializer (Primary, Out);
705
- Fn = CreateGlobalInitOrCleanUpFunction (
706
- FTy, llvm::Twine (InitFnName), FI, SourceLocation (), false ,
707
- llvm::GlobalVariable::ExternalLinkage);
708
-
709
- Guard = new llvm::GlobalVariable (getModule (), Int8Ty, /* isConstant=*/ false ,
710
- llvm::GlobalVariable::InternalLinkage,
711
- llvm::ConstantInt::get (Int8Ty, 0 ),
712
- InitFnName.str () + " __in_chrg" );
713
- }
714
- CharUnits GuardAlign = CharUnits::One ();
715
- Guard->setAlignment (GuardAlign.getAsAlign ());
716
-
717
- CodeGenFunction (*this ).GenerateCXXGlobalInitFunc (
718
- Fn, ModuleInits, ConstantAddress (Guard, Int8Ty, GuardAlign));
719
- // We allow for the case that a module object is added to a linked binary
720
- // without a specific call to the the initializer. This also ensure that
721
- // implementation partition initializers are called when the partition
722
- // is not imported as an interface.
723
- AddGlobalCtor (Fn);
724
-
725
- // See the comment in EmitCXXGlobalInitFunc about OpenCL global init
726
- // functions.
727
- if (getLangOpts ().OpenCL ) {
728
- GenKernelArgMetadata (Fn);
729
- Fn->setCallingConv (llvm::CallingConv::SPIR_KERNEL);
730
- }
731
-
732
- assert (!getLangOpts ().CUDA || !getLangOpts ().CUDAIsDevice ||
733
- getLangOpts ().GPUAllowDeviceInit );
734
- if (getLangOpts ().HIP && getLangOpts ().CUDAIsDevice ) {
735
- Fn->setCallingConv (llvm::CallingConv::AMDGPU_KERNEL);
736
- Fn->addFnAttr (" device-init" );
737
- }
738
-
739
- ModuleInits.clear ();
740
- }
741
-
742
621
static SmallString<128 > getTransformedFileName (llvm::Module &M) {
743
622
SmallString<128 > FileName = llvm::sys::path::filename (M.getName ());
744
623
@@ -771,26 +650,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
771
650
while (!CXXGlobalInits.empty () && !CXXGlobalInits.back ())
772
651
CXXGlobalInits.pop_back ();
773
652
774
- // When we import C++20 modules, we must run their initializers first.
775
- SmallVector<llvm::Function *, 8 > ModuleInits;
776
- if (CXX20ModuleInits)
777
- for (Module *M : ImportedModules) {
778
- llvm::FunctionType *FTy = llvm::FunctionType::get (VoidTy, false );
779
- SmallString<256 > FnName;
780
- {
781
- llvm::raw_svector_ostream Out (FnName);
782
- cast<ItaniumMangleContext>(getCXXABI ().getMangleContext ())
783
- .mangleModuleInitializer (M, Out);
784
- }
785
- assert (!GetGlobalValue (FnName.str ()) &&
786
- " We should only have one use of the initializer call" );
787
- llvm::Function *Fn = llvm::Function::Create (
788
- FTy, llvm::Function::ExternalLinkage, FnName.str (), &getModule ());
789
- ModuleInits.push_back (Fn);
790
- }
791
-
792
- if (ModuleInits.empty () && CXXGlobalInits.empty () &&
793
- PrioritizedCXXGlobalInits.empty ())
653
+ if (CXXGlobalInits.empty () && PrioritizedCXXGlobalInits.empty ())
794
654
return ;
795
655
796
656
llvm::FunctionType *FTy = llvm::FunctionType::get (VoidTy, false );
@@ -816,13 +676,6 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
816
676
llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction (
817
677
FTy, " _GLOBAL__I_" + getPrioritySuffix (Priority), FI);
818
678
819
- // Prepend the module inits to the highest priority set.
820
- if (!ModuleInits.empty ()) {
821
- for (auto F : ModuleInits)
822
- LocalCXXGlobalInits.push_back (F);
823
- ModuleInits.clear ();
824
- }
825
-
826
679
for (; I < PrioE; ++I)
827
680
LocalCXXGlobalInits.push_back (I->second );
828
681
@@ -832,33 +685,17 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
832
685
PrioritizedCXXGlobalInits.clear ();
833
686
}
834
687
835
- if (getCXXABI ().useSinitAndSterm () && ModuleInits.empty () &&
836
- CXXGlobalInits.empty ())
688
+ if (getCXXABI ().useSinitAndSterm () && CXXGlobalInits.empty ())
837
689
return ;
838
690
839
- for (auto F : CXXGlobalInits)
840
- ModuleInits.push_back (F);
841
- CXXGlobalInits.clear ();
842
-
843
691
// Include the filename in the symbol name. Including "sub_" matches gcc
844
692
// and makes sure these symbols appear lexicographically behind the symbols
845
693
// with priority emitted above.
846
- llvm::Function *Fn;
847
- if (CXX20ModuleInits && getContext ().getModuleForCodeGen ()) {
848
- SmallString<256 > InitFnName;
849
- llvm::raw_svector_ostream Out (InitFnName);
850
- cast<ItaniumMangleContext>(getCXXABI ().getMangleContext ())
851
- .mangleModuleInitializer (getContext ().getModuleForCodeGen (), Out);
852
- Fn = CreateGlobalInitOrCleanUpFunction (
853
- FTy, llvm::Twine (InitFnName), FI, SourceLocation (), false ,
854
- llvm::GlobalVariable::ExternalLinkage);
855
- } else
856
- Fn = CreateGlobalInitOrCleanUpFunction (
857
- FTy,
858
- llvm::Twine (" _GLOBAL__sub_I_" , getTransformedFileName (getModule ())),
859
- FI);
860
-
861
- CodeGenFunction (*this ).GenerateCXXGlobalInitFunc (Fn, ModuleInits);
694
+ llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction (
695
+ FTy, llvm::Twine (" _GLOBAL__sub_I_" , getTransformedFileName (getModule ())),
696
+ FI);
697
+
698
+ CodeGenFunction (*this ).GenerateCXXGlobalInitFunc (Fn, CXXGlobalInits);
862
699
AddGlobalCtor (Fn);
863
700
864
701
// In OpenCL global init functions must be converted to kernels in order to
@@ -881,7 +718,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
881
718
Fn->addFnAttr (" device-init" );
882
719
}
883
720
884
- ModuleInits .clear ();
721
+ CXXGlobalInits .clear ();
885
722
}
886
723
887
724
void CodeGenModule::EmitCXXGlobalCleanUpFunc () {
0 commit comments