<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:25 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 Wed, May 4, 2016 at 10:14 PM, Mehdi Amini via llvm-commits<span class="Apple-converted-space"> </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="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;">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="Apple-converted-space"> </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><br class=""></div><div>To be sure I understand: you mean we would use llvm.compiler.used on Darwin, and the weak_odr conversion otherwise?</div><div><br class=""></div><div>I'm not fan of special-casing, do you see a drawback with the llvm.compiler.used approach?</div><div><br class=""></div><div>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.</div><div><br class=""></div><div><br class=""></div><div>-- </div><div>Mehdi</div><div><br class=""></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;"><br class="">From: Mehdi Amini <<a href="mailto:mehdi.amini@apple.com" class="">mehdi.amini@apple.com</a>><br class=""><br class="">Modified:<br class="">   <span class="Apple-converted-space"> </span>llvm/trunk/lib/LTO/LTOCodeGenerator.cpp<br class="">   <span class="Apple-converted-space"> </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="Apple-converted-space"> </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="Apple-converted-space"> </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="Apple-converted-space"> </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" 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><br class=""></body></html>