Allow global address space forward decls using IDs in .ll files.
authorKarl Schimpf <kschimpf@google.com>
Thu, 3 Sep 2015 18:06:44 +0000 (18:06 +0000)
committerKarl Schimpf <kschimpf@google.com>
Thu, 3 Sep 2015 18:06:44 +0000 (18:06 +0000)
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
test/Assembler/global-addrspace-forwardref.ll

index ac7207f99c4f3417d0549ce0d199cb72e7b1235c..8ec5c6a4aa676f3dedfc36e022f31fdef4011668 100644 (file)
@@ -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<FunctionType>(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<FunctionType>(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<FunctionType>(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;
 }
index 4a036e08be6a262a3068bb66fbbf384811e16613..c66b5b73066f0621ebc8bb37479766bd575f0216 100644 (file)
@@ -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
+