Skip to content

Commit a634171

Browse files
[InstrPGO][TypeProf]Annotate vtable types when they are present in the profile (#99402)
Before this change, when `file.profdata` have vtable profiles but `--enable-vtable-value-profiling` is not on for optimized build, warnings from this line [1] will show up. They are benign for performance but confusing. It's better to automatically annotate vtable profiles if `file.profdata` has them. This PR implements it in profile use pass. * If `-icp-max-num-vtables` is zero (default value is 6), vtable profiles won't be annotated. [1] https://github.com/llvm/llvm-project/blob/464d321ee8dde1eaf14b5537eaf030e6df513849/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp#L1762-L1768
1 parent 3b24e5d commit a634171

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@
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 \
115+
// RUN: -mllvm -print-after=pgo-instr-use -mllvm -filter-print-funcs=main \
116+
// RUN: -mllvm -print-module-scope %s 2>&1 | FileCheck %s --check-prefix=ANNOTATE
117+
118+
// ANNOTATE-NOT: Inconsistent number of value sites
119+
// ANNOTATE: !{!"VP", i32 2
120+
121+
// When vtable value profiles exist, pgo-instr-use pass will not annotate them
122+
// if `-icp-max-num-vtables` is set to zero.
123+
// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \
124+
// RUN: -mllvm -icp-max-num-vtables=0 -mllvm -print-after=pgo-instr-use \
125+
// RUN: -mllvm -filter-print-funcs=main -mllvm -print-module-scope %s 2>&1 | \
126+
// RUN: FileCheck %s --check-prefix=OMIT
127+
128+
// OMIT: Inconsistent number of value sites
129+
// OMIT-NOT: !{!"VP", i32 2
130+
112131
// Test indirect call promotion transformation using vtable profiles.
113132
// - Build with `-g` to enable debug information.
114133
// - In real world settings, ICP pass is disabled in prelink pipeline. In
@@ -128,12 +147,12 @@
128147
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
129148

130149
// 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}
150+
// REMARK: instrprof-vtable-value-prof.cpp:226: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}
151+
// REMARK: instrprof-vtable-value-prof.cpp:226:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
133152
//
134153
// 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}
154+
// REMARK: instrprof-vtable-value-prof.cpp:228: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}
155+
// REMARK: instrprof-vtable-value-prof.cpp:228:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
137156

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

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 19 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,23 @@ 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];
1760+
17591761
unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
1762+
1763+
// Since there isn't a reliable or fast way for profile reader to tell if a
1764+
// profile is generated with `-enable-vtable-value-profiling` on, we run the
1765+
// value profile collector over the function IR to find the instrumented sites
1766+
// iff function profile records shows the number of instrumented vtable sites
1767+
// is not zero. Function cfg already takes the number of instrumented
1768+
// indirect call sites into account so it doesn't hash the number of
1769+
// instrumented vtables; as a side effect it makes it easier to enable
1770+
// profiling and profile use in two steps if needed.
1771+
// TODO: Remove this if/when -enable-vtable-value-profiling is on by default.
1772+
if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
1773+
NumValueSites != FuncInfo.ValueSites[IPVK_VTableTarget].size() &&
1774+
MaxNumVTableAnnotations != 0)
1775+
FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
1776+
auto &ValueSites = FuncInfo.ValueSites[Kind];
17601777
if (NumValueSites != ValueSites.size()) {
17611778
auto &Ctx = M->getContext();
17621779
Ctx.diagnose(DiagnosticInfoPGOProfile(

0 commit comments

Comments
 (0)