[LTO] Add SelectionKind to IRSymtab and use it in ld.lld/LLVMgold
authorFangrui Song <i@maskray.me>
Tue, 20 Jul 2021 20:22:00 +0000 (13:22 -0700)
committerFangrui Song <i@maskray.me>
Tue, 20 Jul 2021 20:22:00 +0000 (13:22 -0700)
commitdb5e0786900e53843f87c4169c404f8c7831eb8c
tree35da5fa3a9c42965f2504fdae276b09481ead698
parentce8024e8ff76e7be8b9ffa1a39d1dc9310bf74c7
[LTO] Add SelectionKind to IRSymtab and use it in ld.lld/LLVMgold

In PGO, a C++ external linkage function `foo` has a private counter
`__profc_foo` and a private `__profd_foo` in a `comdat nodeduplicate`.

A `__attribute__((weak))` function `foo` has a weak hidden counter `__profc_foo`
and a private `__profd_foo` in a `comdat nodeduplicate`.

In `ld.lld a.o b.o`, say a.o defines an external linkage `foo` and b.o
defines a weak `foo`. Currently we treat `comdat nodeduplicate` as `comdat any`,
ld.lld will incorrectly consider `b.o:__profc_foo` non-prevailing.  In the worst
case when `b.o:__profd_foo` is retained and `b.o:__profc_foo` isn't, there will
be dangling reference causing an `undefined hidden symbol` error.

Add SelectionKind to `Comdat` in IRSymtab and let linkers ignore nodeduplicate comdat.

Differential Revision: https://reviews.llvm.org/D106228
12 files changed:
lld/COFF/InputFiles.cpp
lld/ELF/InputFiles.cpp
lld/test/ELF/lto/comdat-nodeduplicate.ll [new file with mode: 0644]
lld/wasm/InputFiles.cpp
llvm/include/llvm/LTO/LTO.h
llvm/include/llvm/Object/IRSymtab.h
llvm/lib/Object/IRSymtab.cpp
llvm/test/LTO/Resolution/X86/symtab.ll
llvm/test/Object/X86/irsymtab.ll
llvm/test/tools/gold/X86/comdat-nodeduplicate.ll [new file with mode: 0644]
llvm/tools/gold/gold-plugin.cpp
llvm/tools/llvm-lto2/llvm-lto2.cpp