From b80c5f51a40a9c8c1c6a61e250b8197f74806b8c Mon Sep 17 00:00:00 2001 From: Karl Schimpf Date: Thu, 3 Sep 2015 18:06:44 +0000 Subject: [PATCH] Allow global address space forward decls using IDs in .ll files. Summary: This fixes bugzilla bug 24656. Fixes the case where there is a forward reference to a global variable using an ID (i.e. @0). It does this by passing the address space of the initializer pointer for which the forward referenced global is used. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246788 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLParser.cpp | 29 +++++++++---------- test/Assembler/global-addrspace-forwardref.ll | 9 ++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index ac7207f99c4..8ec5c6a4aa6 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -1040,6 +1040,17 @@ bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, // GlobalValue Reference/Resolution Routines. //===----------------------------------------------------------------------===// +static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy, + const std::string &Name) { + if (auto *FT = dyn_cast(PTy->getElementType())) + return Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); + else + return new GlobalVariable(*M, PTy->getElementType(), false, + GlobalValue::ExternalWeakLinkage, nullptr, Name, + nullptr, GlobalVariable::NotThreadLocal, + PTy->getAddressSpace()); +} + /// GetGlobalVal - Get a value with the specified name or ID, creating a /// forward reference record if needed. This can return null if the value /// exists but does not have the right type. @@ -1073,15 +1084,7 @@ GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty, } // Otherwise, create a new forward reference for this value and remember it. - GlobalValue *FwdVal; - if (FunctionType *FT = dyn_cast(PTy->getElementType())) - FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); - else - FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, - GlobalValue::ExternalWeakLinkage, nullptr, Name, - nullptr, GlobalVariable::NotThreadLocal, - PTy->getAddressSpace()); - + GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, Name); ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); return FwdVal; } @@ -1113,13 +1116,7 @@ GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { } // Otherwise, create a new forward reference for this value and remember it. - GlobalValue *FwdVal; - if (FunctionType *FT = dyn_cast(PTy->getElementType())) - FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); - else - FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, - GlobalValue::ExternalWeakLinkage, nullptr, ""); - + GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, ""); ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); return FwdVal; } diff --git a/test/Assembler/global-addrspace-forwardref.ll b/test/Assembler/global-addrspace-forwardref.ll index 4a036e08be6..c66b5b73066 100644 --- a/test/Assembler/global-addrspace-forwardref.ll +++ b/test/Assembler/global-addrspace-forwardref.ll @@ -7,3 +7,12 @@ ; CHECK: @a = addrspace(1) global i8 0 @a2 = global i8 addrspace(1)* @a @a = addrspace(1) global i8 0 + +; Now test with global IDs instead of global names. + +; CHECK: @a3 = global i8 addrspace(1)* @0 +; CHECK: @0 = addrspace(1) global i8 0 + +@a3 = global i8 addrspace(1)* @0 +@0 = addrspace(1) global i8 0 + -- 2.34.1