
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 https://github.com/llvm/llvm-project/issues/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
56 lines
2.7 KiB
LLVM
56 lines
2.7 KiB
LLVM
; Test of comdat handling with mixed thinlto and regular lto compilation.
|
|
|
|
; This module is compiled with ThinLTO
|
|
; RUN: opt -module-summary -o %t1.o %s
|
|
; Input module compiled for regular LTO
|
|
; RUN: opt -o %t2.o %p/Inputs/comdat-mixed-lto.ll
|
|
|
|
; The copy of C from this module is prevailing. The copy of C from the
|
|
; regular LTO module is not prevailing, and will be dropped to
|
|
; available_externally.
|
|
; 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
|
|
|
|
; The Input module (regular LTO) is %t3.0. Check to make sure that we removed
|
|
; __cxx_global_var_init and testglobfunc from comdat. Also check to ensure
|
|
; that testglobfunc was dropped to available_externally. Otherwise we would
|
|
; have linker multiply defined errors as it is no longer in a comdat and
|
|
; would clash with the copy from this module.
|
|
; RUN: llvm-dis %t3.0.0.preopt.bc -o - | FileCheck %s
|
|
|
|
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @C }]
|
|
; CHECK: @C = available_externally dso_local global %"class.Test::ptr" zeroinitializer, align 4
|
|
; CHECK-NOT: declare
|
|
; CHECK: declare dso_local void @__cxx_global_var_init() section ".text.startup"
|
|
; CHECK-NOT: declare
|
|
|
|
; Check the behavior with the prevailing testglobfunc in %t2.o.
|
|
; 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
|
|
; RUN: llvm-dis %t4.0.0.preopt.bc -o - | FileCheck %s --check-prefix=CHECK2
|
|
|
|
; CHECK2: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @C }]
|
|
; CHECK2: @C = available_externally dso_local global %"class.Test::ptr" zeroinitializer, align 4
|
|
; CHECK2: declare dso_local void @__cxx_global_var_init() section ".text.startup"
|
|
; CHECK2: define available_externally dso_local void @testglobfunc() section ".text.startup" {
|
|
|
|
; ModuleID = 'comdat-mixed-lto.o'
|
|
source_filename = "comdat-mixed-lto.cpp"
|
|
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
|
|
target triple = "x86_64-unknown-linux-gnu"
|
|
|
|
%"class.Test::ptr" = type { i32 }
|
|
|
|
$C = comdat any
|
|
|
|
@C = linkonce_odr global %"class.Test::ptr" zeroinitializer, comdat, align 4
|
|
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @__cxx_global_var_init, i8* bitcast (%"class.Test::ptr"* @C to i8*) }]
|
|
define void @testglobfunc() #1 section ".text.startup" comdat($C) {
|
|
entry:
|
|
ret void
|
|
}
|
|
|
|
; Function Attrs: noinline uwtable
|
|
define internal void @__cxx_global_var_init() #1 section ".text.startup" comdat($C) {
|
|
entry:
|
|
ret void
|
|
}
|