[InstrProfiling] Keep profd non-private for non-renamable comdat functions
authorFangrui Song <i@maskray.me>
Tue, 24 Aug 2021 22:59:35 +0000 (15:59 -0700)
committerFangrui Song <i@maskray.me>
Tue, 24 Aug 2021 22:59:35 +0000 (15:59 -0700)
commitf653beea88d2684cdc8117e662b321ba04666771
treed7950840d751ddceda00f3fddd68dbd54a751ac5
parent98aa694d0d731307bf83d289803f57886d2d3aaa
[InstrProfiling] Keep profd non-private for non-renamable comdat functions

The NS==0 condition used by D103717 missed a corner case: if the current copy
does not have a hash suffix (e.g. weak_odr), a copy with value profiling (with a
different CFG) may exist. This is super rare, but is possible with pre-inlining
PGO instrumentation (which can make a weak_odr function inlines its callees
differently, sometimes with value profiling while sometimes without).

If the current copy with private profd is prevailing, the non-prevailing copy
may get an undefined symbol if a caller inlining the non-prevailing function
references its profd. If the other copy with non-private profd is prevailing,
the current copy may cause a "relocation to discarded section" linker error.

The fix is straightforward: just keep non-private profd in this case.

With this change, a stage 2 (`-DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_BUILD_INSTRUMENTED=IR`)
clang is 0.08% larger (172431496/172286720-1).
`stat -c %s **/*.o | awk '{s+=$1}END{print s}' is 0.026% larger.
The majority of D103717's benefits remains.

Reviewed By: xur

Differential Revision: https://reviews.llvm.org/D108432
llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
llvm/test/Instrumentation/InstrProfiling/linkage.ll
llvm/test/Transforms/PGOProfile/comdat.ll [new file with mode: 0644]