Skip to content

Commit b19d3ee

Browse files
committed
Revert "[C++20][Modules] Build module static initializers per P1874R1."
This reverts commit ac50710. reverting while we figuere out why one of the green dragon lldb test fails.
1 parent e7c8ded commit b19d3ee

File tree

10 files changed

+11
-517
lines changed

10 files changed

+11
-517
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
472472
};
473473
llvm::DenseMap<Module*, PerModuleInitializers*> ModuleInitializers;
474474

475-
/// For module code-gen cases, this is the top-level module we are building.
476-
Module *TopLevelModule = nullptr;
477-
478475
static constexpr unsigned ConstantArrayTypesLog2InitSize = 8;
479476
static constexpr unsigned GeneralTypesLog2InitSize = 9;
480477
static constexpr unsigned FunctionProtoTypesLog2InitSize = 12;
@@ -1078,12 +1075,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
10781075
/// Get the initializations to perform when importing a module, if any.
10791076
ArrayRef<Decl*> getModuleInitializers(Module *M);
10801077

1081-
/// Set the (C++20) module we are building.
1082-
void setModuleForCodeGen(Module *M) { TopLevelModule = M; }
1083-
1084-
/// Get module under construction, nullptr if this is not a C++20 module.
1085-
Module *getModuleForCodeGen() const { return TopLevelModule; }
1086-
10871078
TranslationUnitDecl *getTranslationUnitDecl() const {
10881079
return TUDecl->getMostRecentDecl();
10891080
}

clang/include/clang/Basic/Module.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -665,18 +665,6 @@ class Module {
665665
Module *findSubmodule(StringRef Name) const;
666666
Module *findOrInferSubmodule(StringRef Name);
667667

668-
/// Get the Global Module Fragment (sub-module) for this module, it there is
669-
/// one.
670-
///
671-
/// \returns The GMF sub-module if found, or NULL otherwise.
672-
Module *getGlobalModuleFragment() { return findSubmodule("<global>"); }
673-
674-
/// Get the Private Module Fragment (sub-module) for this module, it there is
675-
/// one.
676-
///
677-
/// \returns The PMF sub-module if found, or NULL otherwise.
678-
Module *getPrivateModuleFragment() { return findSubmodule("<private>"); }
679-
680668
/// Determine whether the specified module would be visible to
681669
/// a lookup at the end of this module.
682670
///

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,11 +2281,6 @@ class Sema final {
22812281
return ModuleScopes.empty() ? nullptr : ModuleScopes.back().Module;
22822282
}
22832283

2284-
/// Is the module scope we are an interface?
2285-
bool currentModuleIsInterface() const {
2286-
return ModuleScopes.empty() ? false : ModuleScopes.back().ModuleInterface;
2287-
}
2288-
22892284
/// Get the module owning an entity.
22902285
Module *getOwningModule(const Decl *Entity) {
22912286
return Entity->getOwningModule();

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 8 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -618,127 +618,6 @@ void CodeGenModule::EmitCXXThreadLocalInitFunc() {
618618
CXXThreadLocals.clear();
619619
}
620620

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-
742621
static SmallString<128> getTransformedFileName(llvm::Module &M) {
743622
SmallString<128> FileName = llvm::sys::path::filename(M.getName());
744623

@@ -771,26 +650,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
771650
while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
772651
CXXGlobalInits.pop_back();
773652

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())
794654
return;
795655

796656
llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
@@ -816,13 +676,6 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
816676
llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
817677
FTy, "_GLOBAL__I_" + getPrioritySuffix(Priority), FI);
818678

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-
826679
for (; I < PrioE; ++I)
827680
LocalCXXGlobalInits.push_back(I->second);
828681

@@ -832,33 +685,17 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
832685
PrioritizedCXXGlobalInits.clear();
833686
}
834687

835-
if (getCXXABI().useSinitAndSterm() && ModuleInits.empty() &&
836-
CXXGlobalInits.empty())
688+
if (getCXXABI().useSinitAndSterm() && CXXGlobalInits.empty())
837689
return;
838690

839-
for (auto F : CXXGlobalInits)
840-
ModuleInits.push_back(F);
841-
CXXGlobalInits.clear();
842-
843691
// Include the filename in the symbol name. Including "sub_" matches gcc
844692
// and makes sure these symbols appear lexicographically behind the symbols
845693
// 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);
862699
AddGlobalCtor(Fn);
863700

864701
// In OpenCL global init functions must be converted to kernels in order to
@@ -881,7 +718,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
881718
Fn->addFnAttr("device-init");
882719
}
883720

884-
ModuleInits.clear();
721+
CXXGlobalInits.clear();
885722
}
886723

887724
void CodeGenModule::EmitCXXGlobalCleanUpFunc() {

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
136136
GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
137137
ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
138138

139-
// Build C++20 Module initializers.
140-
// TODO: Add Microsoft here once we know the mangling required for the
141-
// initializers.
142-
CXX20ModuleInits =
143-
LangOpts.CPlusPlusModules && getCXXABI().getMangleContext().getKind() ==
144-
ItaniumMangleContext::MK_Itanium;
145-
146139
RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
147140

148141
if (LangOpts.ObjC)
@@ -516,18 +509,12 @@ static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
516509
}
517510

518511
void CodeGenModule::Release() {
519-
Module *Primary = getContext().getModuleForCodeGen();
520-
if (CXX20ModuleInits && Primary)
521-
EmitModuleInitializers(Primary);
522512
EmitDeferred();
523513
EmitVTablesOpportunistically();
524514
applyGlobalValReplacements();
525515
applyReplacements();
526516
emitMultiVersionFunctions();
527-
if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition())
528-
EmitCXXModuleInitFunc(Primary);
529-
else
530-
EmitCXXGlobalInitFunc();
517+
EmitCXXGlobalInitFunc();
531518
EmitCXXGlobalCleanUpFunc();
532519
registerGlobalDtorsWithAtExit();
533520
EmitCXXThreadLocalInitFunc();
@@ -2504,31 +2491,6 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
25042491
}
25052492
}
25062493

2507-
void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
2508-
// Emit the initializers in the order that sub-modules appear in the
2509-
// source, first Global Module Fragments, if present.
2510-
if (auto GMF = Primary->getGlobalModuleFragment()) {
2511-
for (Decl *D : getContext().getModuleInitializers(GMF)) {
2512-
assert(D->getKind() == Decl::Var && "GMF initializer decl is not a var?");
2513-
EmitTopLevelDecl(D);
2514-
}
2515-
}
2516-
// Second any associated with the module, itself.
2517-
for (Decl *D : getContext().getModuleInitializers(Primary)) {
2518-
// Skip import decls, the inits for those are called explicitly.
2519-
if (D->getKind() == Decl::Import)
2520-
continue;
2521-
EmitTopLevelDecl(D);
2522-
}
2523-
// Third any associated with the Privat eMOdule Fragment, if present.
2524-
if (auto PMF = Primary->getPrivateModuleFragment()) {
2525-
for (Decl *D : getContext().getModuleInitializers(PMF)) {
2526-
assert(D->getKind() == Decl::Var && "PMF initializer decl is not a var?");
2527-
EmitTopLevelDecl(D);
2528-
}
2529-
}
2530-
}
2531-
25322494
void CodeGenModule::EmitModuleLinkOptions() {
25332495
// Collect the set of all of the modules we want to visit to emit link
25342496
// options, which is essentially the imported modules and all of their
@@ -2934,19 +2896,12 @@ bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
29342896
// explicitly instantiated, so they should not be emitted eagerly.
29352897
return false;
29362898
}
2937-
if (const auto *VD = dyn_cast<VarDecl>(Global)) {
2899+
if (const auto *VD = dyn_cast<VarDecl>(Global))
29382900
if (Context.getInlineVariableDefinitionKind(VD) ==
29392901
ASTContext::InlineVariableDefinitionKind::WeakUnknown)
29402902
// A definition of an inline constexpr static data member may change
29412903
// linkage later if it's redeclared outside the class.
29422904
return false;
2943-
if (CXX20ModuleInits && VD->getOwningModule()) {
2944-
// For CXX20, module-owned initializers need to be deferred, since it is
2945-
// not known at this point if they will be run for the current module or
2946-
// as part of the initializer for an imported one.
2947-
return false;
2948-
}
2949-
}
29502905
// If OpenMP is enabled and threadprivates must be generated like TLS, delay
29512906
// codegen for global variables, because they may be marked as threadprivate.
29522907
if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
@@ -6243,12 +6198,6 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
62436198
DI->EmitImportDecl(*Import);
62446199
}
62456200

6246-
// For CXX20 we are done - we will call the module initializer for the
6247-
// imported module, and that will likewise call those for any imports it
6248-
// has.
6249-
if (CXX20ModuleInits)
6250-
break;
6251-
62526201
// Find all of the submodules and emit the module initializers.
62536202
llvm::SmallPtrSet<clang::Module *, 16> Visited;
62546203
SmallVector<clang::Module *, 16> Stack;

0 commit comments

Comments
 (0)