<div dir="ltr">For some reason this appear to break CFI test on windows bot. Is this a problem in the change or somewhere else?<div><br></div><div><a href="http://lab.llvm.org:8011/builders/sanitizer-windows/builds/13706" class="cremed">http://lab.llvm.org:8011/builders/sanitizer-windows/builds/13706</a></div><div><br></div><div><pre style="font-family:'Courier New',courier,monotype,monospace;color:rgb(0,0,0);font-size:medium;line-height:normal"><span class="stdout">Command 7: "C:/b/slave/sanitizer-windows/build/./bin/clang.exe" "-fuse-ld=lld" "-flto" "-fsanitize=cfi" "-o" "C:\b\slave\sanitizer-windows\build\projects\compiler-rt\test\cfi\Output\anon-namespace.cpp.tmp2" "C:\b\slave\sanitizer-windows\build\projects\compiler-rt\test\cfi\Output\anon-namespace.cpp.tmp1.o" "C:\b\slave\sanitizer-windows\build\projects\compiler-rt\test\cfi\Output\anon-namespace.cpp.tmp2.o"

Command 7 Result: 1

Command 7 Output:





Command 7 Stderr:

Alias must point to a definition


i8** @"\01??_7B@?A@@6B@.2"


LLVM ERROR: Broken module found, compilation aborted!


clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)






</span></pre><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 2, 2015 at 11:33 AM Rafael Espindola via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rafael<br>
Date: Wed Dec  2 13:30:52 2015<br>
New Revision: 254538<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=254538&view=rev" rel="noreferrer" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=254538&view=rev</a><br>
Log:<br>
Fix linking when we copy over only a decl.<br>
<br>
We were failing to copy the fact that the GV is weak and in the case of<br>
an alias, producing invalid IR.<br>
<br>
Added:<br>
    llvm/trunk/test/Linker/Inputs/comdat14.ll<br>
    llvm/trunk/test/Linker/comdat14.ll<br>
Modified:<br>
    llvm/trunk/lib/Linker/LinkModules.cpp<br>
<br>
Modified: llvm/trunk/lib/Linker/LinkModules.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=254538&r1=254537&r2=254538&view=diff" rel="noreferrer" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=254538&r1=254537&r2=254538&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)<br>
+++ llvm/trunk/lib/Linker/LinkModules.cpp Wed Dec  2 13:30:52 2015<br>
@@ -453,7 +453,7 @@ private:<br>
   /// Handles cloning of a global values from the source module into<br>
   /// the destination module, including setting the attributes and visibility.<br>
   GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, const GlobalValue *SGV,<br>
-                                    const GlobalValue *DGV = nullptr);<br>
+                                    const GlobalValue *DGV, bool ForDefinition);<br>
<br>
   /// Check if we should promote the given local value to global scope.<br>
   bool doPromoteLocalToGlobal(const GlobalValue *SGV);<br>
@@ -594,14 +594,8 @@ static void forceRenaming(GlobalValue *G<br>
 void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,<br>
                                     const GlobalValue *SrcGV) {<br>
   auto *GA = dyn_cast<GlobalAlias>(SrcGV);<br>
-  // Check for the special case of converting an alias (definition) to a<br>
-  // non-alias (declaration). This can happen when we are importing and<br>
-  // encounter a weak_any alias (weak_any defs may not be imported, see<br>
-  // comments in ModuleLinker::getLinkage) or an alias whose base object is<br>
-  // being imported as a declaration. In that case copy the attributes from the<br>
-  // base object.<br>
   if (GA && !dyn_cast<GlobalAlias>(NewGV)) {<br>
-    assert(isPerformingImport() && !doImportAsDefinition(GA));<br>
+    // FIXME: this is likelly bogus:<br>
     NewGV->copyAttributesFrom(GA->getBaseObject());<br>
   } else<br>
     NewGV->copyAttributesFrom(SrcGV);<br>
@@ -779,11 +773,12 @@ ModuleLinker::copyGlobalVariableProto(Ty<br>
   // No linking to be performed or linking from the source: simply create an<br>
   // identical version of the symbol over in the dest module... the<br>
   // initializer will be filled in later by LinkGlobalInits.<br>
-  GlobalVariable *NewDGV = new GlobalVariable(<br>
-      DstM, TypeMap.get(SGVar->getType()->getElementType()),<br>
-      SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),<br>
-      /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),<br>
-      SGVar->getType()->getAddressSpace());<br>
+  GlobalVariable *NewDGV =<br>
+      new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),<br>
+                         SGVar->isConstant(), GlobalValue::ExternalLinkage,<br>
+                         /*init*/ nullptr, getName(SGVar),<br>
+                         /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),<br>
+                         SGVar->getType()->getAddressSpace());<br>
<br>
   return NewDGV;<br>
 }<br>
@@ -794,8 +789,8 @@ Function *ModuleLinker::copyFunctionProt<br>
                                           const Function *SF) {<br>
   // If there is no linkage to be performed or we are linking from the source,<br>
   // bring SF over.<br>
-  return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),<br>
-                          getName(SF), &DstM);<br>
+  return Function::Create(TypeMap.get(SF->getFunctionType()),<br>
+                          GlobalValue::ExternalLinkage, getName(SF), &DstM);<br>
 }<br>
<br>
 /// Set up prototypes for any aliases that come over from the source module.<br>
@@ -829,7 +824,7 @@ GlobalValue *ModuleLinker::copyGlobalAli<br>
   // bring over SGA.<br>
   auto *Ty = TypeMap.get(SGA->getValueType());<br>
   return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),<br>
-                             getLinkage(SGA), getName(SGA), &DstM);<br>
+                             GlobalValue::ExternalLinkage, getName(SGA), &DstM);<br>
 }<br>
<br>
 static GlobalValue::VisibilityTypes<br>
@@ -857,14 +852,31 @@ void ModuleLinker::setVisibility(GlobalV<br>
<br>
 GlobalValue *ModuleLinker::copyGlobalValueProto(TypeMapTy &TypeMap,<br>
                                                 const GlobalValue *SGV,<br>
-                                                const GlobalValue *DGV) {<br>
+                                                const GlobalValue *DGV,<br>
+                                                bool ForDefinition) {<br>
   GlobalValue *NewGV;<br>
-  if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))<br>
+  if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {<br>
     NewGV = copyGlobalVariableProto(TypeMap, SGVar);<br>
-  else if (auto *SF = dyn_cast<Function>(SGV))<br>
+  } else if (auto *SF = dyn_cast<Function>(SGV)) {<br>
     NewGV = copyFunctionProto(TypeMap, SF);<br>
-  else<br>
-    NewGV = copyGlobalAliasProto(TypeMap, cast<GlobalAlias>(SGV));<br>
+  } else {<br>
+    if (ForDefinition)<br>
+      NewGV = copyGlobalAliasProto(TypeMap, cast<GlobalAlias>(SGV));<br>
+    else<br>
+      NewGV = new GlobalVariable(<br>
+          DstM, TypeMap.get(SGV->getType()->getElementType()),<br>
+          /*isConstant*/ false, GlobalValue::ExternalLinkage,<br>
+          /*init*/ nullptr, getName(SGV),<br>
+          /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),<br>
+          SGV->getType()->getAddressSpace());<br>
+  }<br>
+<br>
+  if (ForDefinition)<br>
+    NewGV->setLinkage(getLinkage(SGV));<br>
+  else if (SGV->hasAvailableExternallyLinkage() || SGV->hasWeakLinkage() ||<br>
+           SGV->hasLinkOnceLinkage())<br>
+    NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);<br>
+<br>
   copyGVAttributes(NewGV, SGV);<br>
   setVisibility(NewGV, SGV, DGV);<br>
   return NewGV;<br>
@@ -1446,7 +1458,7 @@ bool ModuleLinker::linkGlobalValueProto(<br>
     // When linking from source we setVisibility from copyGlobalValueProto.<br>
     setVisibility(NewGV, SGV, DGV);<br>
   } else {<br>
-    NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);<br>
+    NewGV = copyGlobalValueProto(TypeMap, SGV, DGV, LinkFromSrc);<br>
<br>
     if (isPerformingImport() && !doImportAsDefinition(SGV))<br>
       DoNotLinkFromSource.insert(SGV);<br>
<br>
Added: llvm/trunk/test/Linker/Inputs/comdat14.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/comdat14.ll?rev=254538&view=auto" rel="noreferrer" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/comdat14.ll?rev=254538&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Linker/Inputs/comdat14.ll (added)<br>
+++ llvm/trunk/test/Linker/Inputs/comdat14.ll Wed Dec  2 13:30:52 2015<br>
@@ -0,0 +1,12 @@<br>
+$c = comdat any<br>
+<br>
+@v2 = weak global i32 0, comdat ($c)<br>
+define i32* @f2() {<br>
+  ret i32* @v2<br>
+}<br>
+<br>
+@v3 = weak alias i32, i32* @v2<br>
+define i32* @f3() {<br>
+  ret i32* @v3<br>
+}<br>
+<br>
<br>
Added: llvm/trunk/test/Linker/comdat14.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/comdat14.ll?rev=254538&view=auto" rel="noreferrer" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/comdat14.ll?rev=254538&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Linker/comdat14.ll (added)<br>
+++ llvm/trunk/test/Linker/comdat14.ll Wed Dec  2 13:30:52 2015<br>
@@ -0,0 +1,9 @@<br>
+; RUN: llvm-link -S %s %p/Inputs/comdat14.ll -o - | FileCheck %s<br>
+<br>
+$c = comdat any<br>
+<br>
+@v = global i32 0, comdat ($c)<br>
+<br>
+; CHECK: @v = global i32 0, comdat($c)<br>
+; CHECK: @v2 = extern_weak global i32<br>
+; CHECK: @v3 = extern_weak global i32<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank" class="cremed">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank" class="cremed">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div></div>