Skip to content

Commit 12050a3

Browse files
committed
[LTO] Make local linkage GlobalValue in non-prevailing COMDAT available_externally
For a local linkage GlobalObject in a non-prevailing COMDAT, it remains defined while its leader has been made available_externally. This violates the COMDAT rule that its members must be retained or discarded as a unit. To fix this, update the regular LTO change D34803 to track local linkage GlobalValues, and port the code to ThinLTO (GlobalAliases are not handled.) This fixes two problems. (a) `__cxx_global_var_init` in a non-prevailing COMDAT group used to linger around (unreferenced, hence benign), and is now correctly discarded. ``` int foo(); inline int v = foo(); ``` (b) Fix #58215: as a size optimization, we place private `__profd_` in a COMDAT with a `__profc_` key. When FuncImport.cpp makes `__profc_` available_externally due to a non-prevailing COMDAT, `__profd_` incorrectly remains private. This change makes the `__profd_` available_externally. ``` cat > c.h <<'eof' extern void bar(); inline __attribute__((noinline)) void foo() {} eof cat > m1.cc <<'eof' #include "c.h" int main() { bar(); foo(); } eof cat > m2.cc <<'eof' #include "c.h" __attribute__((noinline)) void bar() { foo(); } eof clang -O2 -fprofile-generate=./t m1.cc m2.cc -flto -fuse-ld=lld -o t_gen rm -fr t && ./t_gen && llvm-profdata show -function=foo t/default_*.profraw clang -O2 -fprofile-generate=./t m1.cc m2.cc -flto=thin -fuse-ld=lld -o t_gen rm -fr t && ./t_gen && llvm-profdata show -function=foo t/default_*.profraw ``` If a GlobalAlias references a GlobalValue which is just changed to available_externally, change the GlobalAlias as well (e.g. C5/D5 comdats due to cc1 -mconstructor-aliases). The GlobalAlias may be referenced by other available_externally functions, so it cannot easily be removed. Depends on D137441: we use available_externally to mark a GlobalAlias in a non-prevailing COMDAT, similar to how we handle GlobalVariable/Function. GlobalAlias may refer to a ConstantExpr, not changing GlobalAlias to GlobalVariable gives flexibility for future extensions (the use case is niche. For simplicity we don't handle it yet). In addition, available_externally GlobalAlias is the most straightforward implementation and retains the aliasee information to help optimizers. See windows-vftable.ll: Windows vftable uses an alias pointing to a private constant where the alias is the COMDAT leader. The COMDAT use case is skeptical and ThinLTO does not discard the alias in the non-prevailing COMDAT. This patch retains the behavior. See new tests ctor-dtor-alias2.ll: depending on whether the complete object destructor emitted, when ctor/dtor aliases are used, we may see D0/D2 COMDATs in one TU and D0/D1/D2 in a D5 COMDAT in another TU. Allow such a mix-and-match with `if (GO->getComdat()->getName() == GO->getName()) NonPrevailingComdats.insert(GO->getComdat());` GlobalAlias handling in ThinLTO is still weird, but this patch should hopefully improve the situation for at least all cases I can think of. Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D135427
1 parent 2c239da commit 12050a3

File tree

9 files changed

+345
-22
lines changed

9 files changed

+345
-22
lines changed

lld/test/ELF/lto/ctor-dtor-alias2.ll

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
; REQUIRES: x86
2+
;; Test mixed D0/D2 and D5 COMDATs. The file matches llvm/test/ThinLTO/X86/ctor-dtor-alias2.ll
3+
4+
; RUN: rm -rf %t && split-file %s %t && cd %t
5+
6+
;; a.bc defines D0 in comdat D0 and D2 in comdat D2. b.bc defines D0/D1/D2 in comdat D5.
7+
; RUN: opt -module-summary a.ll -o a.bc
8+
; RUN: opt -module-summary b.ll -o b.bc
9+
; RUN: ld.lld -shared a.bc b.bc -o out.so
10+
; RUN: llvm-nm -D out.so
11+
12+
;; Although D0/D2 in b.bc is non-prevailing, keep D1/D2 as definitions, otherwise
13+
;; the output may have an undefined and unsatisfied D1.
14+
; CHECK: W _ZN1AIiED0Ev
15+
; CHECK-NEXT: W _ZN1AIiED1Ev
16+
; CHECK-NEXT: W _ZN1AIiED2Ev
17+
; CHECK-NEXT: U _ZdlPv
18+
; CHECK-NEXT: T aa
19+
; CHECK-NEXT: T bb
20+
21+
;--- a.ll
22+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
23+
target triple = "x86_64-unknown-linux-gnu"
24+
25+
$_ZN1AIiED2Ev = comdat any
26+
27+
$_ZN1AIiED0Ev = comdat any
28+
29+
define void @aa() {
30+
entry:
31+
%a = alloca ptr, align 8
32+
call void @_ZN1AIiED2Ev(ptr noundef nonnull %a)
33+
ret void
34+
}
35+
36+
define linkonce_odr void @_ZN1AIiED2Ev(ptr noundef nonnull %this) unnamed_addr comdat {
37+
ret void
38+
}
39+
40+
define linkonce_odr void @_ZN1AIiED0Ev(ptr noundef nonnull %this) unnamed_addr comdat {
41+
entry:
42+
call void @_ZN1AIiED2Ev(ptr noundef nonnull %this)
43+
call void @_ZdlPv(ptr noundef %this)
44+
ret void
45+
}
46+
47+
declare void @_ZdlPv(ptr noundef)
48+
49+
;--- b.ll
50+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
51+
target triple = "x86_64-unknown-linux-gnu"
52+
53+
$_ZN1AIiED5Ev = comdat any
54+
55+
$_ZTV1AIiE = comdat any
56+
57+
@_ZN1AIiED1Ev = weak_odr unnamed_addr alias void (ptr), ptr @_ZN1AIiED2Ev
58+
59+
define weak_odr void @_ZN1AIiED2Ev(ptr noundef nonnull %this) unnamed_addr comdat($_ZN1AIiED5Ev) {
60+
ret void
61+
}
62+
63+
define weak_odr void @_ZN1AIiED0Ev(ptr noundef nonnull %this) unnamed_addr comdat($_ZN1AIiED5Ev) {
64+
entry:
65+
call void @_ZN1AIiED1Ev(ptr noundef nonnull %this)
66+
call void @_ZdlPv(ptr noundef %this)
67+
ret void
68+
}
69+
70+
declare void @_ZdlPv(ptr noundef)
71+
72+
define void @bb(ptr noundef %a) {
73+
entry:
74+
call void @_ZN1AIiED1Ev(ptr noundef nonnull %a)
75+
call void @_ZdlPv(ptr noundef %a)
76+
ret void
77+
}

llvm/lib/LTO/LTO.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ handleNonPrevailingComdat(GlobalValue &GV,
712712
if (!NonPrevailingComdats.count(C))
713713
return;
714714

715-
// Additionally need to drop externally visible global values from the comdat
716-
// to available_externally, so that there aren't multiply defined linker
717-
// errors.
718-
if (!GV.hasLocalLinkage())
719-
GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
715+
// Additionally need to drop all global values from the comdat to
716+
// available_externally, to satisfy the COMDAT requirement that all members
717+
// are discarded as a unit. The non-local linkage global values avoid
718+
// duplicate definition linker errors.
719+
GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
720720

721721
if (auto GO = dyn_cast<GlobalObject>(&GV))
722722
GO->setComdat(nullptr);

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ bool llvm::convertToDeclaration(GlobalValue &GV) {
10511051
void llvm::thinLTOFinalizeInModule(Module &TheModule,
10521052
const GVSummaryMapTy &DefinedGlobals,
10531053
bool PropagateAttrs) {
1054+
DenseSet<Comdat *> NonPrevailingComdats;
10541055
auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
10551056
// See if the global summary analysis computed a new resolved linkage.
10561057
const auto &GS = DefinedGlobals.find(GV.getGUID());
@@ -1128,8 +1129,11 @@ void llvm::thinLTOFinalizeInModule(Module &TheModule,
11281129
// as this is a declaration for the linker, and will be dropped eventually.
11291130
// It is illegal for comdats to contain declarations.
11301131
auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1131-
if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
1132+
if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1133+
if (GO->getComdat()->getName() == GO->getName())
1134+
NonPrevailingComdats.insert(GO->getComdat());
11321135
GO->setComdat(nullptr);
1136+
}
11331137
};
11341138

11351139
// Process functions and global now
@@ -1139,6 +1143,36 @@ void llvm::thinLTOFinalizeInModule(Module &TheModule,
11391143
FinalizeInModule(GV);
11401144
for (auto &GV : TheModule.aliases())
11411145
FinalizeInModule(GV);
1146+
1147+
// For a non-prevailing comdat, all its members must be available_externally.
1148+
// FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1149+
// local linkage GlobalValues.
1150+
if (NonPrevailingComdats.empty())
1151+
return;
1152+
for (auto &GO : TheModule.global_objects()) {
1153+
if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1154+
GO.setComdat(nullptr);
1155+
GO.setLinkage(GlobalValue::AvailableExternallyLinkage);
1156+
}
1157+
}
1158+
bool Changed;
1159+
do {
1160+
Changed = false;
1161+
// If an alias references a GlobalValue in a non-prevailing comdat, change
1162+
// it to available_externally. For simplicity we only handle GlobalValue and
1163+
// ConstantExpr with a base object. ConstantExpr without a base object is
1164+
// unlikely used in a COMDAT.
1165+
for (auto &GA : TheModule.aliases()) {
1166+
if (GA.hasAvailableExternallyLinkage())
1167+
continue;
1168+
GlobalObject *Obj = GA.getAliaseeObject();
1169+
assert(Obj && "aliasee without an base object is unimplemented");
1170+
if (Obj->hasAvailableExternallyLinkage()) {
1171+
GA.setLinkage(GlobalValue::AvailableExternallyLinkage);
1172+
Changed = true;
1173+
}
1174+
}
1175+
} while (Changed);
11421176
}
11431177

11441178
/// Run internalization on \p TheModule based on symmary analysis.

llvm/test/LTO/Resolution/X86/comdat-mixed-lto.ll

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@
88
; The copy of C from this module is prevailing. The copy of C from the
99
; regular LTO module is not prevailing, and will be dropped to
1010
; available_externally.
11-
; RUN: llvm-lto2 run -r=%t1.o,C,pl -r=%t2.o,C,l -r=%t2.o,testglobfunc,lxp -r=%t1.o,testglobfunc,lx -o %t3 %t1.o %t2.o -save-temps
11+
; RUN: llvm-lto2 run -r=%t1.o,C,pl -r=%t2.o,C,l -r=%t1.o,testglobfunc,lxp -r=%t2.o,testglobfunc,lx -o %t3 %t1.o %t2.o -save-temps
1212

1313
; The Input module (regular LTO) is %t3.0. Check to make sure that we removed
1414
; __cxx_global_var_init and testglobfunc from comdat. Also check to ensure
1515
; that testglobfunc was dropped to available_externally. Otherwise we would
1616
; have linker multiply defined errors as it is no longer in a comdat and
1717
; would clash with the copy from this module.
1818
; RUN: llvm-dis %t3.0.0.preopt.bc -o - | FileCheck %s
19-
; CHECK: define internal void @__cxx_global_var_init() section ".text.startup" {
20-
; CHECK: define available_externally dso_local void @testglobfunc() section ".text.startup" {
19+
20+
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @C }]
21+
; CHECK: @C = available_externally dso_local global %"class.Test::ptr" zeroinitializer, align 4
22+
; CHECK-NOT: declare
23+
; CHECK: declare dso_local void @__cxx_global_var_init() section ".text.startup"
24+
; CHECK-NOT: declare
25+
26+
; Check the behavior with the prevailing testglobfunc in %t2.o.
27+
; RUN: llvm-lto2 run -r=%t1.o,C,pl -r=%t2.o,C,l -r=%t1.o,testglobfunc,lx -r=%t2.o,testglobfunc,plx -o %t4 %t1.o %t2.o -save-temps
28+
; RUN: llvm-dis %t4.0.0.preopt.bc -o - | FileCheck %s --check-prefix=CHECK2
29+
30+
; CHECK2: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @C }]
31+
; CHECK2: @C = available_externally dso_local global %"class.Test::ptr" zeroinitializer, align 4
32+
; CHECK2: declare dso_local void @__cxx_global_var_init() section ".text.startup"
33+
; CHECK2: define available_externally dso_local void @testglobfunc() section ".text.startup" {
2134

2235
; ModuleID = 'comdat-mixed-lto.o'
2336
source_filename = "comdat-mixed-lto.cpp"
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
22
target triple = "x86_64-unknown-linux-gnu"
33

4-
$c2 = comdat any
4+
$f = comdat any
5+
$g = comdat any
56

6-
define linkonce_odr i32 @f(i8*) unnamed_addr comdat($c2) {
7+
@g_private = private global i32 41, comdat($g)
8+
9+
define linkonce_odr i32 @f(i8*) unnamed_addr comdat($f) {
10+
ret i32 41
11+
}
12+
13+
define linkonce_odr i32 @g() unnamed_addr comdat($g) {
714
ret i32 41
815
}
916

10-
define i32 @g() {
17+
define internal void @g_internal() unnamed_addr comdat($g) {
18+
ret void
19+
}
20+
21+
define i32 @h() {
1122
%i = call i32 @f(i8* null)
1223
ret i32 %i
1324
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
;; The constructor alias example is reduced from
2+
;;
3+
;; template <typename T>
4+
;; struct A { A() {} virtual ~A() {} };
5+
;; template struct A<void>;
6+
;; void *foo() { return new A<void>; }
7+
;;
8+
;; clang -c -fpic -O1 -flto=thin a.cc && cp a.o b.o && ld.lld -shared a.o b.so
9+
10+
; RUN: opt -module-summary %s -o %t1.bc
11+
; RUN: cp %t1.bc %t2.bc
12+
; RUN: llvm-lto2 run %t1.bc %t2.bc -r=%t1.bc,_ZTV1A,pl -r=%t1.bc,_ZN1AD0Ev,pl -r=%t1.bc,_ZN1AD1Ev,pl -r=%t1.bc,_ZN1AD2Ev,pl -r=%t1.bc,D1_a,pl -r=%t1.bc,D1_a_a,pl \
13+
; RUN: -r=%t2.bc,_ZTV1A,l -r=%t2.bc,_ZN1AD0Ev,l -r=%t2.bc,_ZN1AD1Ev,l -r=%t2.bc,_ZN1AD2Ev,l -r=%t2.bc,D1_a,l -r=%t2.bc,D1_a_a,l -o %t3 --save-temps
14+
; RUN: llvm-dis < %t3.2.1.promote.bc | FileCheck %s
15+
16+
; CHECK: @_ZTV1A = available_externally dso_local unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr null, ptr @_ZN1AD1Ev, ptr @_ZN1AD0Ev] }
17+
; CHECK: @D1_a = weak_odr dso_local unnamed_addr alias void (ptr), ptr @_ZN1AD1Ev
18+
; CHECK: @_ZN1AD1Ev = weak_odr dso_local unnamed_addr alias void (ptr), ptr @_ZN1AD2Ev
19+
; CHECK: @D1_a_a = weak_odr dso_local unnamed_addr alias void (ptr), ptr @D1_a
20+
; CHECK: define weak_odr dso_local void @_ZN1AD2Ev(ptr noundef nonnull %0) unnamed_addr comdat($_ZN1AD5Ev) {
21+
; CHECK: define available_externally dso_local void @_ZN1AD0Ev(ptr noundef nonnull %0) unnamed_addr {
22+
23+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
24+
target triple = "x86_64-unknown-linux-gnu"
25+
26+
$_ZN1AD5Ev = comdat any
27+
$_ZTV1A = comdat any
28+
29+
@_ZTV1A = weak_odr unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr null, ptr @_ZN1AD1Ev, ptr @_ZN1AD0Ev] }, comdat
30+
31+
@D1_a = weak_odr unnamed_addr alias void (ptr), ptr @_ZN1AD1Ev
32+
@_ZN1AD1Ev = weak_odr unnamed_addr alias void (ptr), ptr @_ZN1AD2Ev
33+
@D1_a_a = weak_odr unnamed_addr alias void (ptr), ptr @D1_a
34+
35+
define weak_odr void @_ZN1AD2Ev(ptr noundef nonnull %0) unnamed_addr comdat($_ZN1AD5Ev) {
36+
ret void
37+
}
38+
39+
define weak_odr void @_ZN1AD0Ev(ptr noundef nonnull %0) unnamed_addr comdat($_ZN1AD5Ev) {
40+
call void @D1_a(ptr noundef nonnull %0)
41+
call void @D1_a_a(ptr noundef nonnull %0)
42+
call void @_ZN1AD1Ev(ptr noundef nonnull %0)
43+
ret void
44+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
;; Test mixed D0/D2 and D5 COMDATs. Reduced from:
2+
;;
3+
;; // a.cc
4+
;; template <typename T>
5+
;; struct A final { virtual ~A() {} };
6+
;; extern "C" void aa() { A<int> a; }
7+
;; // b.cc
8+
;; template <typename T>
9+
;; struct A final { virtual ~A() {} };
10+
;; template struct A<int>;
11+
;; extern "C" void bb(A<int> *a) { delete a; }
12+
;;
13+
;; clang -c -fpic -O0 -flto=thin a.cc && ld.lld -shared a.o b.o
14+
;;
15+
;; The file matches lld/test/ELF/lto/ctor-dtor-alias2.ll
16+
17+
; RUN: rm -rf %t && split-file %s %t && cd %t
18+
19+
;; a.bc defines D0 in comdat D0 and D2 in comdat D2. b.bc defines D0/D1/D2 in comdat D5.
20+
; RUN: opt -module-summary a.ll -o a.bc
21+
; RUN: opt -module-summary b.ll -o b.bc
22+
; RUN: llvm-lto2 run a.bc b.bc -r=a.bc,aa,px -r=a.bc,_ZN1AIiED0Ev,px -r=a.bc,_ZN1AIiED2Ev,px -r=a.bc,_ZdlPv, \
23+
; RUN: -r=b.bc,bb,px -r=b.bc,_ZN1AIiED0Ev, -r=b.bc,_ZN1AIiED1Ev,px -r=b.bc,_ZN1AIiED2Ev, -r=b.bc,_ZdlPv, -o out --save-temps
24+
; RUN: llvm-dis < out.2.1.promote.bc | FileCheck %s
25+
26+
;; Although D0/D2 in b.bc is non-prevailing, keep D1/D2 as definitions, otherwise
27+
;; the output may have an undefined and unsatisfied D1.
28+
; CHECK: @_ZN1AIiED1Ev = weak_odr unnamed_addr alias void (ptr), ptr @_ZN1AIiED2Ev
29+
; CHECK: define weak_odr void @_ZN1AIiED2Ev(ptr noundef nonnull %this) unnamed_addr comdat($_ZN1AIiED5Ev) {
30+
; CHECK: define available_externally void @_ZN1AIiED0Ev(ptr noundef nonnull %this) unnamed_addr {
31+
32+
;--- a.ll
33+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
34+
target triple = "x86_64-unknown-linux-gnu"
35+
36+
$_ZN1AIiED2Ev = comdat any
37+
38+
$_ZN1AIiED0Ev = comdat any
39+
40+
define void @aa() {
41+
entry:
42+
%a = alloca ptr, align 8
43+
call void @_ZN1AIiED2Ev(ptr noundef nonnull %a)
44+
ret void
45+
}
46+
47+
define linkonce_odr void @_ZN1AIiED2Ev(ptr noundef nonnull %this) unnamed_addr comdat {
48+
ret void
49+
}
50+
51+
define linkonce_odr void @_ZN1AIiED0Ev(ptr noundef nonnull %this) unnamed_addr comdat {
52+
entry:
53+
call void @_ZN1AIiED2Ev(ptr noundef nonnull %this)
54+
call void @_ZdlPv(ptr noundef %this)
55+
ret void
56+
}
57+
58+
declare void @_ZdlPv(ptr noundef)
59+
60+
;--- b.ll
61+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
62+
target triple = "x86_64-unknown-linux-gnu"
63+
64+
$_ZN1AIiED5Ev = comdat any
65+
66+
$_ZTV1AIiE = comdat any
67+
68+
@_ZN1AIiED1Ev = weak_odr unnamed_addr alias void (ptr), ptr @_ZN1AIiED2Ev
69+
70+
define weak_odr void @_ZN1AIiED2Ev(ptr noundef nonnull %this) unnamed_addr comdat($_ZN1AIiED5Ev) {
71+
ret void
72+
}
73+
74+
define weak_odr void @_ZN1AIiED0Ev(ptr noundef nonnull %this) unnamed_addr comdat($_ZN1AIiED5Ev) {
75+
entry:
76+
call void @_ZN1AIiED1Ev(ptr noundef nonnull %this)
77+
call void @_ZdlPv(ptr noundef %this)
78+
ret void
79+
}
80+
81+
declare void @_ZdlPv(ptr noundef)
82+
83+
define void @bb(ptr noundef %a) {
84+
entry:
85+
call void @_ZN1AIiED1Ev(ptr noundef nonnull %a)
86+
call void @_ZdlPv(ptr noundef %a)
87+
ret void
88+
}

0 commit comments

Comments
 (0)