diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ba376f9ecfacd..9566cfb8d6e79 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -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); } diff --git a/clang/test/CodeGen/attr-weakref2.c b/clang/test/CodeGen/attr-weakref2.c index 114f048a85183..a67f906810faf 100644 --- a/clang/test/CodeGen/attr-weakref2.c +++ b/clang/test/CodeGen/attr-weakref2.c @@ -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) {