Skip to content

Commit 32837a0

Browse files
committed
[PGO] Use linkonce_odr linkage for __profd_ variables in comdat groups
This fixes relocations against __profd_ symbols in discarded sections, which is PR41380. In general, instrumentation happens very early, and optimization and inlining happens afterwards. The counters for a function are calculated early, and after inlining, counters for an inlined function may be widely referenced by other functions. For C++ inline functions of all kinds (linkonce_odr & available_externally mainly), instr profiling wants to deduplicate these __profc_ and __profd_ globals. Otherwise the binary would be quite large. I made __profd_ and __profc_ comdat in r355044, but I chose to make __profd_ internal. At the time, I was only dealing with coverage, and in that case, none of the instrumentation needs to reference __profd_. However, if you use PGO, then instrumentation passes add calls to __llvm_profile_instrument_range which reference __profd_ globals. The solution is to make these globals externally visible by using linkonce_odr linkage for data as was done for counters. This is safe because PGO adds a CFG hash to the names of the data and counter globals, so if different TUs have different globals, they will get different data and counter arrays. Reviewers: xur, hans Differential Revision: https://reviews.llvm.org/D67579 llvm-svn: 372020
1 parent 69911b8 commit 32837a0

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,8 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
740740
PD = It->second;
741741
}
742742

743-
// Match the linkage and visibility of the name global, except on COFF, where
744-
// the linkage must be local and consequentially the visibility must be
745-
// default.
743+
// Match the linkage and visibility of the name global. COFF supports using
744+
// comdats with internal symbols, so do that if we can.
746745
Function *Fn = Inc->getParent()->getParent();
747746
GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
748747
GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
@@ -759,15 +758,16 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
759758
// of the parent function, that will result in relocations against discarded
760759
// sections.
761760
Comdat *Cmdt = nullptr;
762-
GlobalValue::LinkageTypes CounterLinkage = Linkage;
763761
if (needsComdatForCounter(*Fn, *M)) {
764762
StringRef CmdtPrefix = getInstrProfComdatPrefix();
765763
if (TT.isOSBinFormatCOFF()) {
766764
// For COFF, the comdat group name must be the name of a symbol in the
767765
// group. Use the counter variable name, and upgrade its linkage to
768-
// something externally visible, like linkonce_odr.
766+
// something externally visible, like linkonce_odr. Use hidden visibility
767+
// to imply that this is dso local.
769768
CmdtPrefix = getInstrProfCountersVarPrefix();
770-
CounterLinkage = GlobalValue::LinkOnceODRLinkage;
769+
Linkage = GlobalValue::LinkOnceODRLinkage;
770+
Visibility = GlobalValue::HiddenVisibility;
771771
}
772772
Cmdt = M->getOrInsertComdat(getVarName(Inc, CmdtPrefix));
773773
}
@@ -786,7 +786,7 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
786786
getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
787787
CounterPtr->setAlignment(8);
788788
CounterPtr->setComdat(Cmdt);
789-
CounterPtr->setLinkage(CounterLinkage);
789+
CounterPtr->setLinkage(Linkage);
790790

791791
auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
792792
// Allocate statically the array of pointers to value profile nodes for
@@ -841,6 +841,7 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
841841
Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
842842
Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
843843
Data->setComdat(Cmdt);
844+
Data->setLinkage(Linkage);
844845

845846
PD.RegionCounters = CounterPtr;
846847
PD.DataVar = Data;

llvm/test/Instrumentation/InstrProfiling/PR23499.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ $_Z3barIvEvv = comdat any
2020

2121

2222
; COFF-NOT: __profn__Z3barIvEvv
23-
; COFF: @__profc__Z3barIvEvv = linkonce_odr dso_local global [1 x i64] zeroinitializer, section "{{.*}}prfc$M", comdat, align 8
24-
; COFF: @__profd__Z3barIvEvv = internal global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat($__profc__Z3barIvEvv), align 8
23+
; COFF: @__profc__Z3barIvEvv = linkonce_odr hidden global [1 x i64] zeroinitializer, section "{{.*}}prfc$M", comdat, align 8
24+
; COFF: @__profd__Z3barIvEvv = linkonce_odr hidden global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat($__profc__Z3barIvEvv), align 8
2525

2626

2727
declare void @llvm.instrprof.increment(i8*, i64, i32, i32) #1

llvm/test/Instrumentation/InstrProfiling/comdat.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ $foo_inline = comdat any
1717

1818
; ELF: @__profc_foo_inline = linkonce_odr hidden global{{.*}}, section "__llvm_prf_cnts", comdat($__profv_foo_inline), align 8
1919
; ELF: @__profd_foo_inline = linkonce_odr hidden global{{.*}}, section "__llvm_prf_data", comdat($__profv_foo_inline), align 8
20-
; COFF: @__profc_foo_inline = linkonce_odr dso_local global{{.*}}, section ".lprfc$M", comdat, align 8
21-
; COFF: @__profd_foo_inline = internal global{{.*}}, section ".lprfd$M", comdat($__profc_foo_inline), align 8
20+
; COFF: @__profc_foo_inline = linkonce_odr hidden global{{.*}}, section ".lprfc$M", comdat, align 8
21+
; COFF: @__profd_foo_inline = linkonce_odr hidden global{{.*}}, section ".lprfd$M", comdat($__profc_foo_inline), align 8
2222
define weak_odr void @foo_inline() comdat {
2323
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_inline, i32 0, i32 0), i64 0, i32 1, i32 0)
2424
ret void
@@ -30,8 +30,8 @@ $foo_extern = comdat any
3030

3131
; ELF: @__profc_foo_extern = linkonce_odr hidden global{{.*}}, section "__llvm_prf_cnts", comdat($__profv_foo_extern)
3232
; ELF: @__profd_foo_extern = linkonce_odr hidden global{{.*}}, section "__llvm_prf_data", comdat($__profv_foo_extern)
33-
; COFF: @__profc_foo_extern = linkonce_odr dso_local global{{.*}}, section ".lprfc$M", comdat, align 8
34-
; COFF: @__profd_foo_extern = internal global{{.*}}, section ".lprfd$M", comdat($__profc_foo_extern), align 8
33+
; COFF: @__profc_foo_extern = linkonce_odr hidden global{{.*}}, section ".lprfc$M", comdat, align 8
34+
; COFF: @__profd_foo_extern = linkonce_odr hidden global{{.*}}, section ".lprfd$M", comdat($__profc_foo_extern), align 8
3535
define available_externally void @foo_extern() {
3636
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_extern, i32 0, i32 0), i64 0, i32 1, i32 0)
3737
ret void

llvm/test/Instrumentation/InstrProfiling/linkage.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ define linkonce_odr void @foo_inline() {
5757
; LINUX: @__profd_foo_extern = linkonce_odr hidden global {{.*}}section "__llvm_prf_data", comdat($__profv_foo_extern), align 8
5858
; MACHO: @__profc_foo_extern = linkonce_odr hidden global
5959
; MACHO: @__profd_foo_extern = linkonce_odr hidden global
60-
; COFF: @__profc_foo_extern = linkonce_odr dso_local global {{.*}}section ".lprfc$M", comdat, align 8
61-
; COFF: @__profd_foo_extern = internal global {{.*}}section ".lprfd$M", comdat($__profc_foo_extern), align 8
60+
; COFF: @__profc_foo_extern = linkonce_odr hidden global {{.*}}section ".lprfc$M", comdat, align 8
61+
; COFF: @__profd_foo_extern = linkonce_odr hidden global {{.*}}section ".lprfd$M", comdat($__profc_foo_extern), align 8
6262
define available_externally void @foo_extern() {
6363
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_extern, i32 0, i32 0), i64 0, i32 1, i32 0)
6464
ret void

0 commit comments

Comments
 (0)