Skip to content

Commit 293f3ec

Browse files
committed
!fixup Document and sort by weak/linkince ODR.
1 parent bc627d5 commit 293f3ec

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

llvm/lib/Transforms/IPO/MergeFunctions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
891891

892892
// Merge two equivalent functions. Upon completion, Function G is deleted.
893893
void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
894+
895+
// Create a new thunk that both F and G can call, if F cannot call G directly.
896+
// That is the case if F is either interposable or if G is either weak_odr or
897+
// linkonce_odr.
894898
if (F->isInterposable() || G->hasWeakODRLinkage() ||
895899
G->hasLinkOnceODRLinkage()) {
896900
assert(G->isInterposable() || G->hasWeakODRLinkage() ||
@@ -993,11 +997,20 @@ static bool isFuncOrderCorrect(const Function *F, const Function *G) {
993997
// one, but not the other way around.
994998
return !F->isInterposable();
995999
}
1000+
1001+
if (F->hasWeakODRLinkage() != G->hasWeakODRLinkage() ||
1002+
F->hasLinkOnceODRLinkage() != G->hasLinkOnceODRLinkage()) {
1003+
// ODR functions before non-ODR functions. A ODR function can call a non-ODR
1004+
// function if it is not interposable, but not the other way around.
1005+
return F->hasWeakODRLinkage() || F->hasLinkOnceODRLinkage();
1006+
}
1007+
9961008
if (F->hasLocalLinkage() != G->hasLocalLinkage()) {
9971009
// External before local, because we definitely have to keep the external
9981010
// function, but may be able to drop the local one.
9991011
return !F->hasLocalLinkage();
10001012
}
1013+
10011014
// Impose a total order (by name) on the replacement of functions. This is
10021015
// important when operating on more than one module independently to prevent
10031016
// cycles of thunks calling each other when the modules are linked together.

llvm/test/Transforms/MergeFunc/merge-linkonce-odr.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ declare void @zar(ptr)
6363
; CHECK-NEXT: ret void
6464
;
6565
;
66-
; CHECK-LABEL: define private void @1(
66+
; CHECK-LABEL: define linkonce_odr hidden void @non_linkonce_caller_of_bar_2(
6767
; CHECK-SAME: ptr [[P:%.*]]) {
6868
; CHECK-NEXT: [[ENTRY:.*:]]
6969
; CHECK-NEXT: tail call void @bar(ptr [[P]])
@@ -93,15 +93,9 @@ declare void @zar(ptr)
9393
; CHECK-NEXT: ret void
9494
;
9595
;
96-
; CHECK-LABEL: define linkonce_odr hidden void @non_linkonce_caller_of_bar_2(
97-
; CHECK-SAME: ptr [[TMP0:%.*]]) {
98-
; CHECK-NEXT: tail call void @[[GLOB1:[0-9]+]](ptr [[TMP0]])
99-
; CHECK-NEXT: ret void
100-
;
101-
;
10296
; CHECK-LABEL: define hidden void @linkonce_odr_caller_of_bar_1(
10397
; CHECK-SAME: ptr [[TMP0:%.*]]) {
104-
; CHECK-NEXT: tail call void @[[GLOB1]](ptr [[TMP0]])
98+
; CHECK-NEXT: tail call void @non_linkonce_caller_of_bar_2(ptr [[TMP0]])
10599
; CHECK-NEXT: ret void
106100
;
107101
;

llvm/test/Transforms/MergeFunc/merge-weak-odr.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ declare void @zar(ptr)
7272
; CHECK-NEXT: ret void
7373
;
7474
;
75-
; CHECK-LABEL: define private void @1(
75+
; CHECK-LABEL: define weak_odr hidden void @weak_odr_caller_of_zar_2(
7676
; CHECK-SAME: ptr [[P:%.*]]) {
7777
; CHECK-NEXT: [[ENTRY:.*:]]
7878
; CHECK-NEXT: tail call void @zar(ptr [[P]])
@@ -99,14 +99,8 @@ declare void @zar(ptr)
9999
; CHECK-NEXT: ret void
100100
;
101101
;
102-
; CHECK-LABEL: define weak_odr hidden void @weak_odr_caller_of_zar_2(
103-
; CHECK-SAME: ptr [[TMP0:%.*]]) {
104-
; CHECK-NEXT: tail call void @[[GLOB1:[0-9]+]](ptr [[TMP0]])
105-
; CHECK-NEXT: ret void
106-
;
107-
;
108102
; CHECK-LABEL: define hidden void @non_weak_caller_of_zar_1(
109103
; CHECK-SAME: ptr [[TMP0:%.*]]) {
110-
; CHECK-NEXT: tail call void @[[GLOB1]](ptr [[TMP0]])
104+
; CHECK-NEXT: tail call void @weak_odr_caller_of_zar_2(ptr [[TMP0]])
111105
; CHECK-NEXT: ret void
112106
;

0 commit comments

Comments
 (0)