Skip to content

Commit 8901635

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. Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D135427
1 parent 48c4da2 commit 8901635

File tree

7 files changed

+179
-22
lines changed

7 files changed

+179
-22
lines changed

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: 34 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,10 @@ 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+
NonPrevailingComdats.insert(GO->getComdat());
11321134
GO->setComdat(nullptr);
1135+
}
11331136
};
11341137

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

11441177
/// 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 = available_externally dso_local unnamed_addr alias void (ptr), ptr @_ZN1AD1Ev
18+
; CHECK: @_ZN1AD1Ev = available_externally dso_local unnamed_addr alias void (ptr), ptr @_ZN1AD2Ev
19+
; CHECK: @D1_a_a = available_externally dso_local unnamed_addr alias void (ptr), ptr @D1_a
20+
; CHECK: define available_externally dso_local void @_ZN1AD2Ev(ptr noundef nonnull %0) unnamed_addr {
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: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
1-
; This test ensures that we drop the preempted copy of @f from %t2.bc from its
2-
; comdat after making it available_externally. If not we would get a
3-
; verification error.
1+
; This test ensures that we drop the preempted copy of @f/@g from %t2.bc from their
2+
; comdats after making it available_externally. If not we would get a
3+
; verification error. g_internal/g_private are changed to available_externally
4+
; as well since it is in the same comdat of g.
45
; RUN: opt -module-summary %s -o %t1.bc
56
; RUN: opt -module-summary %p/Inputs/linkonce_resolution_comdat.ll -o %t2.bc
6-
; RUN: llvm-lto -thinlto-action=run -disable-thinlto-funcattrs=0 %t1.bc %t2.bc -exported-symbol=f -exported-symbol=g -thinlto-save-temps=%t3.
7+
; RUN: llvm-lto -thinlto-action=run -disable-thinlto-funcattrs=0 %t1.bc %t2.bc -exported-symbol=f -exported-symbol=g -exported-symbol=h -thinlto-save-temps=%t3.
78

89
; RUN: llvm-dis %t3.0.3.imported.bc -o - | FileCheck %s --check-prefix=IMPORT1
910
; RUN: llvm-dis %t3.1.3.imported.bc -o - | FileCheck %s --check-prefix=IMPORT2
1011
; Copy from first module is prevailing and converted to weak_odr, copy
1112
; from second module is preempted and converted to available_externally and
1213
; removed from comdat.
13-
; IMPORT1: define weak_odr i32 @f(i8* %0) unnamed_addr [[ATTR:#[0-9]+]] comdat($c1) {
14+
; IMPORT1: @g_private = private global i32 43, comdat($g)
15+
; IMPORT1: define weak_odr i32 @f(i8* %0) unnamed_addr [[ATTR:#[0-9]+]] comdat {
16+
; IMPORT1: define weak_odr i32 @g() unnamed_addr [[ATTR]] comdat {
17+
; IMPORT1: define internal void @g_internal() unnamed_addr comdat($g) {
18+
19+
; IMPORT2: @g_private = available_externally dso_local global i32 41{{$}}
1420
; IMPORT2: define available_externally i32 @f(i8* %0) unnamed_addr [[ATTR:#[0-9]+]] {
21+
; IMPORT2: define available_externally i32 @g() unnamed_addr [[ATTR]] {
22+
; IMPORT2: define available_externally dso_local void @g_internal() unnamed_addr {
1523

1624
; CHECK-DAG: attributes [[ATTR]] = { norecurse nounwind }
1725

18-
; RUN: llvm-nm -o - < %t1.bc.thinlto.o | FileCheck %s --check-prefix=NM1
26+
; RUN: llvm-nm %t1.bc.thinlto.o | FileCheck %s --check-prefix=NM1
1927
; NM1: W f
28+
; NM1: W g
2029

21-
; RUN: llvm-nm -o - < %t2.bc.thinlto.o | FileCheck %s --check-prefix=NM2
30+
; RUN: llvm-nm %t2.bc.thinlto.o | FileCheck %s --check-prefix=NM2
2231
; f() would have been turned into available_externally since it is preempted,
23-
; and inlined into g()
32+
; and inlined into h()
2433
; NM2-NOT: f
34+
; NM2-NOT: g
2535

2636
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2737
target triple = "x86_64-unknown-linux-gnu"
2838

29-
$c1 = comdat any
39+
$f = comdat any
40+
$g = comdat any
41+
42+
@g_private = private global i32 43, comdat($g)
3043

31-
define linkonce_odr i32 @f(i8*) unnamed_addr comdat($c1) {
44+
define linkonce_odr i32 @f(i8*) unnamed_addr comdat {
3245
ret i32 43
3346
}
47+
48+
define linkonce_odr i32 @g() unnamed_addr comdat {
49+
ret i32 43
50+
}
51+
52+
define internal void @g_internal() unnamed_addr comdat($g) {
53+
ret void
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; Test an alias pointing to a GEP.
2+
; RUN: opt -module-summary %s -o %t1.bc
3+
; RUN: cp %t1.bc %t2.bc
4+
; RUN: llvm-lto2 run %t1.bc %t2.bc -r=%t1.bc,"??_7bad_array_new_length@stdext@@6B@",pl -r=%t1.bc,"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z",pl \
5+
; RUN: -r=%t1.bc,"?_Throw_bad_array_new_length@std@@YAXXZ",pl \
6+
; RUN: -r=%t2.bc,"??_7bad_array_new_length@stdext@@6B@", -r=%t2.bc,"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z", \
7+
; RUN: -r=%t2.bc,"?_Throw_bad_array_new_length@std@@YAXXZ", -o %t3 --save-temps
8+
; RUN: llvm-dis < %t3.2.1.promote.bc | FileCheck %s
9+
10+
; CHECK: @anon = private unnamed_addr constant { [2 x ptr] } { [2 x ptr] [ptr null, ptr @"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z"] }, comdat($"??_7bad_array_new_length@stdext@@6B@")
11+
; CHECK: @"??_7bad_array_new_length@stdext@@6B@" = unnamed_addr alias ptr, getelementptr inbounds ({ [4 x ptr] }, ptr @anon, i32 0, i32 0, i32 1){{$}}
12+
; CHECK: define available_externally dso_local noundef ptr @"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z"(ptr noundef nonnull %this) {
13+
; CHECK: define available_externally dso_local void @"?_Throw_bad_array_new_length@std@@YAXXZ"(ptr noundef nonnull %0) unnamed_addr {
14+
15+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
16+
target triple = "x86_64-pc-windows-msvc19.26.0"
17+
18+
$"??_7bad_array_new_length@stdext@@6B@" = comdat largest
19+
$"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z" = comdat any
20+
$"?_Throw_bad_array_new_length@std@@YAXXZ" = comdat any
21+
22+
@anon = private unnamed_addr constant { [2 x ptr] } { [2 x ptr] [ptr null, ptr @"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z"] }, comdat($"??_7bad_array_new_length@stdext@@6B@")
23+
24+
@"??_7bad_array_new_length@stdext@@6B@" = unnamed_addr alias ptr, getelementptr inbounds ({ [4 x ptr] }, ptr @anon, i32 0, i32 0, i32 1)
25+
26+
define linkonce_odr dso_local noundef ptr @"??_Gbad_array_new_length@stdext@@UEAAPEAXI@Z"(ptr noundef nonnull %this) comdat {
27+
entry:
28+
ret ptr %this
29+
}
30+
31+
define linkonce_odr dso_local void @"?_Throw_bad_array_new_length@std@@YAXXZ"(ptr noundef nonnull %0) unnamed_addr comdat {
32+
entry:
33+
store ptr @"??_7bad_array_new_length@stdext@@6B@", ptr %0, align 8
34+
ret void
35+
}

0 commit comments

Comments
 (0)