<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 5, 2016, at 11:37 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><br class="Apple-interchange-newline"><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">On Thu, May 5, 2016 at 11:30 AM, Mehdi Amini<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><br class=""><div class=""><span class=""><blockquote type="cite" class=""><div class="">On May 5, 2016, at 11:25 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank" class="">dblaikie@gmail.com</a>> wrote:</div><br class=""><div class=""><br class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">On Wed, May 4, 2016 at 10:14 PM, Mehdi Amini via llvm-commits<span class=""> </span><span dir="ltr" class=""><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank" class="">llvm-commits@lists.llvm.org</a>></span><span class=""> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">Author: mehdi_amini<br class="">Date: Thu May  5 00:14:24 2016<br class="">New Revision: 268607<br class=""><br class="">URL:<span class=""> </span><a href="http://llvm.org/viewvc/llvm-project?rev=268607&view=rev" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project?rev=268607&view=rev</a><br class="">Log:<br class="">LTOCodeGenerator: add linkonce(_odr) to "llvm.compiler.used" when present in "MustPreserve" set<br class=""><br class="">If the linker requested to preserve a linkonce function, we should<br class="">honor this even if we drop all uses.<br class="">We explicitely avoid turning them into weak_odr (unlike the first<br class="">version of this patch in r267644), because the codegen can be<br class="">different on Darwin: because of `llvm::canBeOmittedFromSymbolTable()`<br class="">we may emit the symbol as weak_def_can_be_hidden instead of<br class="">weak_definition.<br class=""></blockquote><div class=""><br class=""></div><div class="">Do we only do this on Darwin then, and otherwise turn it into weak_odr everywhere else?</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">To be sure I understand: you mean we would use llvm.compiler.used on Darwin, and the weak_odr conversion otherwise?</div></div></div></blockquote><div class=""><br class=""></div><div class="">Yes - this, of course, isn't my wheelhouse, so there may be lots of good reasons not to do this, I'm just curious.</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><div class="">I'm not fan of special-casing, do you see a drawback with the llvm.compiler.used approach?<br class=""></div></div></div></blockquote><div class=""><br class=""></div><div class="">Don't know enough about it - sounds like it might lead to a different linkage in the generated object, which could have an impact on linking? or maybe it all comes out the same in the end?<br class=""></div></div></div></blockquote><div><br class=""></div><div>My intention is to not change the linkage at all. My understanding was that only difference between linkonce and weak is that the former can be dropped if unused in the current module.</div><div>A mentioned in the commit message, I discovered that Darwin differs a little bit under some circumstances.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="">But it seems like Darwin is the special case here - so it seems strange to do the same odd things to non-Darwin platforms in the name of consistency. But, again, I don't have lots of context here.</div></div></div></blockquote><div><br class=""></div><div>True. </div><div>The context is: during LTO the linker ask "MustPreserve(foo)". LibLTO will not turn "foo" into internal linkage. However if "foo" is a linkonce and LTO is able to discard the uses in the LTO module, it will drop it. As a result the linker gets back an object without "foo" even though it asked for it to be "preserved".</div><div>To solve this, I'm trying to force the compiler to not drop it. Since the request to preserve it comes from the linker, llvm.compiler.used seems appropriate to express that the compiler should treat the symbol "as if there is a reference to the symbol that it cannot see" (citation from LangRef).</div><div><br class=""></div><div>(that was my first patch, and then Duncan suggested to use the weak transformation instead for consistency with gold).</div><div><br class=""></div><div>-- </div><div>Mehdi</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><div class=""></div><div class="">Ideally, I'd prefer to get ride of the "weak_def_can_be_hidden" thing we do at the MC level. It is not clear to me why it is not expressed at the IR level, but I haven't had time to give it more thoughts.<br class=""></div></div></div></blockquote><div class=""><br class=""></div><div class="">*nod*<br class=""><br class="">- Dave</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><div class=""></div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-- </div><div class="">Mehdi</div></font></span><div class=""><div class="h5"><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><br class="">From: Mehdi Amini <<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>><br class=""><br class="">Modified:<br class="">   <span class=""> </span>llvm/trunk/lib/LTO/LTOCodeGenerator.cpp<br class="">   <span class=""> </span>llvm/trunk/test/tools/lto/hide-linkonce-odr.ll<br class=""><br class="">Modified: llvm/trunk/lib/LTO/LTOCodeGenerator.cpp<br class="">URL:<span class=""> </span><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=268607&r1=268606&r2=268607&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=268607&r1=268606&r2=268607&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/LTO/LTOCodeGenerator.cpp (original)<br class="">+++ llvm/trunk/lib/LTO/LTOCodeGenerator.cpp Thu May  5 00:14:24 2016<br class="">@@ -348,8 +348,73 @@ std::unique_ptr<TargetMachine> LTOCodeGe<br class="">                                 <span class=""> </span>RelocModel, CodeModel::Default, CGOptLevel));<br class=""> }<br class=""><br class="">+// If a linkonce global is present in the MustPreserveSymbols, we need to make<br class="">+// sure we honor this. To force the compiler to not drop it, we add it to the<br class="">+// "llvm.compiler.used" global.<br class="">+static void preserveDiscardableGVs(<br class="">+    Module &TheModule,<br class="">+    llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {<br class="">+  SetVector<Constant *> UsedValuesSet;<br class="">+  if (GlobalVariable *LLVMUsed =<br class="">+          TheModule.getGlobalVariable("llvm.compiler.used")) {<br class="">+    ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());<br class="">+    for (auto &V : Inits->operands())<br class="">+      UsedValuesSet.insert(cast<Constant>(&V));<br class="">+    LLVMUsed->eraseFromParent();<br class="">+  }<br class="">+  llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());<br class="">+  auto mayPreserveGlobal = [&](GlobalValue &GV) {<br class="">+    if (!GV.isDiscardableIfUnused() || GV.isDeclaration())<br class="">+      return;<br class="">+    if (!mustPreserveGV(GV))<br class="">+      return;<br class="">+    assert(!GV.hasAvailableExternallyLinkage() && !GV.hasInternalLinkage());<br class="">+    UsedValuesSet.insert(ConstantExpr::getBitCast(&GV, i8PTy));<br class="">+  };<br class="">+  for (auto &GV : TheModule)<br class="">+    mayPreserveGlobal(GV);<br class="">+  for (auto &GV : TheModule.globals())<br class="">+    mayPreserveGlobal(GV);<br class="">+  for (auto &GV : TheModule.aliases())<br class="">+    mayPreserveGlobal(GV);<br class="">+<br class="">+  if (UsedValuesSet.empty())<br class="">+    return;<br class="">+<br class="">+  llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesSet.size());<br class="">+  auto *LLVMUsed = new llvm::GlobalVariable(<br class="">+      TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,<br class="">+      llvm::ConstantArray::get(ATy, UsedValuesSet.getArrayRef()),<br class="">+      "llvm.compiler.used");<br class="">+  LLVMUsed->setSection("llvm.metadata");<br class="">+}<br class="">+<br class=""> void LTOCodeGenerator::applyScopeRestrictions() {<br class="">-  if (ScopeRestrictionsDone || !ShouldInternalize)<br class="">+  if (ScopeRestrictionsDone)<br class="">+    return;<br class="">+<br class="">+  // Declare a callback for the internalize pass that will ask for every<br class="">+  // candidate GlobalValue if it can be internalized or not.<br class="">+  SmallString<64> MangledName;<br class="">+  auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {<br class="">+    // Unnamed globals can't be mangled, but they can't be preserved either.<br class="">+    if (!GV.hasName())<br class="">+      return false;<br class="">+<br class="">+    // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled<br class="">+    // with the linker supplied name, which on Darwin includes a leading<br class="">+    // underscore.<br class="">+    MangledName.clear();<br class="">+    MangledName.reserve(GV.getName().size() + 1);<br class="">+    Mangler::getNameWithPrefix(MangledName, GV.getName(),<br class="">+                               MergedModule->getDataLayout());<br class="">+    return MustPreserveSymbols.count(MangledName);<br class="">+  };<br class="">+<br class="">+  // Preserve linkonce value on linker request<br class="">+  preserveDiscardableGVs(*MergedModule, mustPreserveGV);<br class="">+<br class="">+  if (!ShouldInternalize)<br class="">     return;<br class=""><br class="">   if (ShouldRestoreGlobalsLinkage) {<br class="">@@ -373,22 +438,7 @@ void LTOCodeGenerator::applyScopeRestric<br class="">   // symbols referenced from asm<br class="">   UpdateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);<br class=""><br class="">-  // Declare a callback for the internalize pass that will ask for every<br class="">-  // candidate GlobalValue if it can be internalized or not.<br class="">-  Mangler Mangler;<br class="">-  SmallString<64> MangledName;<br class="">-  auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {<br class="">-    // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled<br class="">-    // with the linker supplied name, which on Darwin includes a leading<br class="">-    // underscore.<br class="">-    MangledName.clear();<br class="">-    MangledName.reserve(GV.getName().size() + 1);<br class="">-    Mangler::getNameWithPrefix(MangledName, GV.getName(),<br class="">-                               MergedModule->getDataLayout());<br class="">-    return MustPreserveSymbols.count(MangledName);<br class="">-  };<br class="">-<br class="">-  internalizeModule(*MergedModule, MustPreserveGV);<br class="">+  internalizeModule(*MergedModule, mustPreserveGV);<br class=""><br class="">   ScopeRestrictionsDone = true;<br class=""> }<br class=""><br class="">Modified: llvm/trunk/test/tools/lto/hide-linkonce-odr.ll<br class="">URL:<span class=""> </span><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/lto/hide-linkonce-odr.ll?rev=268607&r1=268606&r2=268607&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/lto/hide-linkonce-odr.ll?rev=268607&r1=268606&r2=268607&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/test/tools/lto/hide-linkonce-odr.ll (original)<br class="">+++ llvm/trunk/test/tools/lto/hide-linkonce-odr.ll Thu May  5 00:14:24 2016<br class="">@@ -1,21 +1,28 @@<br class=""> ; RUN: llvm-as %s -o %t.o<br class="">-; RUN: %ld64 -lto_library %llvmshlibdir/libLTO.dylib -dylib -arch x86_64 -macosx_version_min 10.10.0 -lSystem -o %t.dylib %t.o -save-temps  -undefined dynamic_lookup<br class="">+; RUN: %ld64 -lto_library %llvmshlibdir/libLTO.dylib -dylib -arch x86_64 -macosx_version_min 10.10.0 -lSystem -o %t.dylib %t.o -save-temps  -undefined dynamic_lookup -exported_symbol _c -exported_symbol _b  -exported_symbol _GlobLinkonce<br class=""><br class=""> ; RUN: llvm-dis %t.dylib.lto.opt.bc -o - | FileCheck --check-prefix=IR %s<br class="">-; check that @a is still a linkonce_odr definition<br class="">-; IR: define linkonce_odr void @a()<br class="">+; check that @a is no longer a linkonce_odr definition<br class="">+; IR-NOT: define linkonce_odr void @a()<br class="">+; check that @b is appended in llvm.used<br class="">+; IR: @llvm.compiler.used = appending global [2 x i8*] [i8* bitcast ([1 x i8*]* @GlobLinkonce to i8*), i8* bitcast (void ()* @b to i8*)], section "llvm.metadata"<br class=""><br class=""> ; RUN: llvm-nm %t.dylib | FileCheck --check-prefix=NM %s<br class="">-; check that the linker can hide @a but not @b<br class="">+; check that the linker can hide @a but not @b, nor @GlobLinkonce<br class="">+; NM: 0000000000000f48 S _GlobLinkonce<br class=""> ; NM: 0000000000000f10 t _a<br class=""> ; NM: 0000000000000f20 T _b<br class=""> ; NM: 0000000000000f00 T _c<br class=""><br class="">+<br class=""> target triple = "x86_64-apple-macosx10.10.0"<br class=""><br class=""> declare void @external()<br class=""><br class="">+@GlobLinkonce = linkonce_odr unnamed_addr constant [1 x i8*] [i8* null], align 8<br class="">+<br class=""> define linkonce_odr void @a() noinline {<br class="">+  %use_of_GlobLinkonce = load [1 x i8*], [1 x i8*] *@GlobLinkonce<br class="">   call void @external()<br class="">   ret void<br class=""> }<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" target="_blank" class="">llvm-commits@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a></blockquote></div></div></blockquote></div></div></div></div></blockquote></div></div></blockquote></div><br class=""></body></html>