Skip to content

Commit

Permalink
[CodeGen] Clean up access to EmittedDeferredDecls, NFCI.
Browse files Browse the repository at this point in the history
GlobalDecls should only be added to EmittedDeferredDecls if they
need reemission. This is checked in addEmittedDeferredDecl, which
is called via addDeferredDeclToEmit. Extend these checks to also
handle VarDecls (for lambdas, as tested in Interpreter/lambda.cpp)
and remove the direct access of EmittedDeferredDecls in EmitGlobal
that may actually end up duplicating FunctionDecls.

Differential Revision: https://reviews.llvm.org/D156897
  • Loading branch information
hahnjo committed Aug 17, 2023
1 parent 91af0d0 commit b719e41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 0 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3677,7 +3677,6 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
// The value must be emitted, but cannot be emitted eagerly.
assert(!MayBeEmittedEagerly(Global));
addDeferredDeclToEmit(GD);
EmittedDeferredDecls[MangledName] = GD;
} else {
// Otherwise, remember that we saw a deferred decl with this name. The
// first use of the mangled name will cause it to move into
Expand Down Expand Up @@ -4417,7 +4416,6 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
// DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
// don't need it anymore).
addDeferredDeclToEmit(DDI->second);
EmittedDeferredDecls[DDI->first] = DDI->second;
DeferredDecls.erase(DDI);

// Otherwise, there are cases we have to worry about where we're
Expand Down Expand Up @@ -4677,7 +4675,6 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
// Move the potentially referenced deferred decl to the DeferredDeclsToEmit
// list, and remove it from DeferredDecls (since we don't need it anymore).
addDeferredDeclToEmit(DDI->second);
EmittedDeferredDecls[DDI->first] = DDI->second;
DeferredDecls.erase(DDI);
}

Expand Down
13 changes: 9 additions & 4 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,15 @@ class CodeGenModule : public CodeGenTypeCache {
llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;

void addEmittedDeferredDecl(GlobalDecl GD) {
if (!llvm::isa<FunctionDecl>(GD.getDecl()))
return;
llvm::GlobalVariable::LinkageTypes L = getFunctionLinkage(GD);
if (llvm::GlobalValue::isLinkOnceLinkage(L) ||
// Assume a linkage by default that does not need reemission.
auto L = llvm::GlobalValue::ExternalLinkage;
if (llvm::isa<FunctionDecl>(GD.getDecl()))
L = getFunctionLinkage(GD);
else if (auto *VD = llvm::dyn_cast<VarDecl>(GD.getDecl()))
L = getLLVMLinkageVarDefinition(VD);

if (llvm::GlobalValue::isInternalLinkage(L) ||
llvm::GlobalValue::isLinkOnceLinkage(L) ||
llvm::GlobalValue::isWeakLinkage(L)) {
EmittedDeferredDecls[getMangledName(GD)] = GD;
}
Expand Down

0 comments on commit b719e41

Please sign in to comment.