Skip to content

[RFC] [clang] [CodeGen] Avoid creating global variable repeatedly when type are not specified #114948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5233,11 +5233,18 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
llvm::Type *Ty,
ForDefinition_t IsForDefinition) {
assert(D->hasGlobalStorage() && "Not a global variable");

StringRef MangledName = getMangledName(D);
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
QualType ASTTy = D->getType();
LangAS AddrSpace = ASTTy.getAddressSpace();

if (Entry && !Ty && Entry->getAddressSpace() == getContext().getTargetAddressSpace(AddrSpace))
return Entry;

if (!Ty)
Ty = getTypes().ConvertTypeForMem(ASTTy);

StringRef MangledName = getMangledName(D);
return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
IsForDefinition);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/attr-weakref2.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int test4_h(void) {
}
int test4_f;

// CHECK: @test5_f = external global i32
// CHECK: @test5_f = extern_weak global i32
extern int test5_f;
static int test5_g __attribute__((weakref("test5_f")));
int test5_h(void) {
Expand Down
Loading