[PATCH] D29367: LTO: Link non-prevailing weak_odr, linkonce_odr or available_externally globals into the combined module with available_externally linkage.
Peter Collingbourne via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 31 20:50:38 PST 2017
pcc updated this revision to Diff 86568.
pcc added a comment.
- Simplify
https://reviews.llvm.org/D29367
Files:
llvm/lib/LTO/LTO.cpp
llvm/test/LTO/Resolution/X86/Inputs/link-availextern.ll
llvm/test/LTO/Resolution/X86/link-availextern.ll
Index: llvm/test/LTO/Resolution/X86/link-availextern.ll
===================================================================
--- /dev/null
+++ llvm/test/LTO/Resolution/X86/link-availextern.ll
@@ -0,0 +1,22 @@
+; RUN: llvm-as %s -o %t1
+; RUN: llvm-as %S/Inputs/link-availextern.ll -o %t2
+
+; RUN: llvm-lto2 -o %t3 %t1 -r %t1,f, -save-temps
+; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=SINGLE %s
+
+; RUN: llvm-lto2 -o %t3 %t1 %t2 -r %t1,f, -r %t2,f,p -save-temps
+; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=BOTH %s
+
+; RUN: llvm-lto2 -o %t3 %t2 %t1 -r %t1,f, -r %t2,f,p -save-temps
+; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=BOTH %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; SINGLE: define available_externally i32 @f()
+; SINGLE-NEXT: ret i32 1
+; BOTH: define weak_odr i32 @f()
+; BOTH-NEXT: ret i32 2
+define linkonce_odr i32 @f() {
+ ret i32 1
+}
Index: llvm/test/LTO/Resolution/X86/Inputs/link-availextern.ll
===================================================================
--- /dev/null
+++ llvm/test/LTO/Resolution/X86/Inputs/link-availextern.ll
@@ -0,0 +1,6 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define linkonce_odr i32 @f() {
+ ret i32 2
+}
Index: llvm/lib/LTO/LTO.cpp
===================================================================
--- llvm/lib/LTO/LTO.cpp
+++ llvm/lib/LTO/LTO.cpp
@@ -455,20 +455,36 @@
SymbolResolution Res = *ResI++;
addSymbolToGlobalRes(Used, Sym, Res, 0);
- if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
- continue;
- if (Res.Prevailing && Sym.isGV()) {
+ if (Sym.isGV()) {
GlobalValue *GV = Sym.getGV();
- Keep.push_back(GV);
- switch (GV->getLinkage()) {
- default:
- break;
- case GlobalValue::LinkOnceAnyLinkage:
- GV->setLinkage(GlobalValue::WeakAnyLinkage);
- break;
- case GlobalValue::LinkOnceODRLinkage:
- GV->setLinkage(GlobalValue::WeakODRLinkage);
- break;
+ if (Res.Prevailing) {
+ if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
+ continue;
+ Keep.push_back(GV);
+ switch (GV->getLinkage()) {
+ default:
+ break;
+ case GlobalValue::LinkOnceAnyLinkage:
+ GV->setLinkage(GlobalValue::WeakAnyLinkage);
+ break;
+ case GlobalValue::LinkOnceODRLinkage:
+ GV->setLinkage(GlobalValue::WeakODRLinkage);
+ break;
+ }
+ } else if (isa<GlobalObject>(GV) &&
+ (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
+ GV->hasAvailableExternallyLinkage())) {
+ // Either of the above three types of linkage indicates that the
+ // chosen prevailing symbol will have the same semantics as this copy of
+ // the symbol, so we can link it with available_externally linkage. We
+ // only need to do this if the symbol is undefined.
+ GlobalValue *CombinedGV =
+ RegularLTO.CombinedModule->getNamedValue(GV->getName());
+ if (!CombinedGV || CombinedGV->isDeclaration()) {
+ Keep.push_back(GV);
+ GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
+ cast<GlobalObject>(GV)->setComdat(nullptr);
+ }
}
}
// Common resolution: collect the maximum size/alignment over all commons.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29367.86568.patch
Type: text/x-patch
Size: 3508 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170201/94a97add/attachment.bin>
More information about the llvm-commits
mailing list