Skip to content

Commit 73d6985

Browse files
[InstrPGO][TypeProf]Annotate vtable types if they are present in the profiles and 'enable-vtable-value-profiling' is not explicitly off
1 parent 495d3ea commit 73d6985

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// REQUIRES: lld, lld-available
1+
// REQUIRES: lld-available
22

33
// Building the instrumented binary will fail because lld doesn't support
44
// big-endian ELF for PPC (aka ABI 1).
@@ -109,6 +109,20 @@
109109
// ICTEXT: {{.*}}instrprof-vtable-value-prof.cpp;_ZTVN12_GLOBAL__N_18Derived2E:750
110110
// ICTEXT: _ZTV8Derived1:250
111111

112+
// When vtable value profiles exist, pgo-instr-use pass should annotate them
113+
// even if `-enable-vtable-value-profiling` is not explicitly on.
114+
// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 -mllvm -print-after=pgo-instr-use -mllvm -filter-print-funcs=main -mllvm -print-module-scope %s 2>&1 | FileCheck %s --check-prefix=ANNOTATE
115+
116+
// ANNOTATE-NOT: Inconsistent number of value sites
117+
// ANNOTATE: !{!"VP", i32 2
118+
119+
// When vtable value profiles exist, pgo-instr-use pass will not annotate them
120+
// if `-enable-vtable-profile-use` is explicitly off.
121+
// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 -mllvm -enable-vtable-profile-use=false -mllvm -print-after=pgo-instr-use -mllvm -filter-print-funcs=main -mllvm -print-module-scope %s 2>&1 | FileCheck %s --check-prefix=OMIT
122+
123+
// OMIT: Inconsistent number of value sites
124+
// OMIT-NOT: !{!"VP", i32 2
125+
112126
// Test indirect call promotion transformation using vtable profiles.
113127
// - Build with `-g` to enable debug information.
114128
// - In real world settings, ICP pass is disabled in prelink pipeline. In
@@ -128,12 +142,12 @@
128142
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
129143

130144
// For the indirect call site `ptr->func`
131-
// REMARK: instrprof-vtable-value-prof.cpp:205:19: Promote indirect call to _ZN12_GLOBAL__N_18Derived24funcEii with count 150 out of 200, sink 1 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E}
132-
// REMARK: instrprof-vtable-value-prof.cpp:205:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
145+
// REMARK: instrprof-vtable-value-prof.cpp:214:19: Promote indirect call to _ZN12_GLOBAL__N_18Derived24funcEii with count 150 out of 200, sink 1 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E}
146+
// REMARK: instrprof-vtable-value-prof.cpp:214:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
133147
//
134148
// For the indirect call site `delete ptr`
135-
// REMARK: instrprof-vtable-value-prof.cpp:207:5: Promote indirect call to _ZN12_GLOBAL__N_18Derived2D0Ev with count 750 out of 1000, sink 2 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E}
136-
// REMARK: instrprof-vtable-value-prof.cpp:207:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
149+
// REMARK: instrprof-vtable-value-prof.cpp:216:5: Promote indirect call to _ZN12_GLOBAL__N_18Derived2D0Ev with count 750 out of 1000, sink 2 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E}
150+
// REMARK: instrprof-vtable-value-prof.cpp:216:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
137151

138152
// The IR matchers for indirect callsite `ptr->func`.
139153
// IR-LABEL: @main

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ class PGOUseFunc {
10961096
: F(Func), M(Modu), BFI(BFIin), PSI(PSI),
10971097
FuncInfo(Func, TLI, ComdatMembers, false, BPI, BFIin, IsCS,
10981098
InstrumentFuncEntry, HasSingleByteCoverage),
1099-
FreqAttr(FFA_Normal), IsCS(IsCS) {}
1099+
FreqAttr(FFA_Normal), IsCS(IsCS), VPC(Func, TLI) {}
11001100

11011101
void handleInstrProfError(Error Err, uint64_t MismatchedFuncSum);
11021102

@@ -1178,6 +1178,8 @@ class PGOUseFunc {
11781178
// Is to use the context sensitive profile.
11791179
bool IsCS;
11801180

1181+
ValueProfileCollector VPC;
1182+
11811183
// Find the Instrumented BB and set the value. Return false on error.
11821184
bool setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
11831185

@@ -1755,8 +1757,18 @@ void PGOUseFunc::annotateValueSites() {
17551757
void PGOUseFunc::annotateValueSites(uint32_t Kind) {
17561758
assert(Kind <= IPVK_Last);
17571759
unsigned ValueSiteIndex = 0;
1758-
auto &ValueSites = FuncInfo.ValueSites[Kind];
17591760
unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
1761+
// FuncPGOInstrumentation ctor finds value sites for each kind. It runs on the
1762+
// common path of pgo-instr-gen and pgo-instr-use, and vtable kind path
1763+
// is gated by `-enable-vtable-value-profiling`. Give pgo-instr-use pass a
1764+
// second chance to find out vtable value sites when vtable profiles are
1765+
// present and `-enable-vtable-profile-use` is not explicitly off.
1766+
if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
1767+
NumValueSites != FuncInfo.ValueSites[IPVK_VTableTarget].size() &&
1768+
!(EnableVTableProfileUse.getNumOccurrences() &&
1769+
EnableVTableProfileUse == false))
1770+
FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
1771+
auto &ValueSites = FuncInfo.ValueSites[Kind];
17601772
if (NumValueSites != ValueSites.size()) {
17611773
auto &Ctx = M->getContext();
17621774
Ctx.diagnose(DiagnosticInfoPGOProfile(

0 commit comments

Comments
 (0)