[compiler-rt] [llvm] [InstrPGO][TypeProf]Annotate vtable types when they are present in the profile (PR #99402)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 18 14:56:49 PDT 2024
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/99402
>From 73d6985170704df2cee4abb1a25400ff7a0c7283 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Wed, 17 Jul 2024 15:17:10 -0700
Subject: [PATCH 1/5] [InstrPGO][TypeProf]Annotate vtable types if they are
present in the profiles and 'enable-vtable-value-profiling' is not explicitly
off
---
.../Linux/instrprof-vtable-value-prof.cpp | 24 +++++++++++++++----
.../Instrumentation/PGOInstrumentation.cpp | 16 +++++++++++--
2 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
index 24a96a227393a..5555899c4f6b7 100644
--- a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
+++ b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
@@ -1,4 +1,4 @@
-// REQUIRES: lld, lld-available
+// REQUIRES: lld-available
// Building the instrumented binary will fail because lld doesn't support
// big-endian ELF for PPC (aka ABI 1).
@@ -109,6 +109,20 @@
// ICTEXT: {{.*}}instrprof-vtable-value-prof.cpp;_ZTVN12_GLOBAL__N_18Derived2E:750
// ICTEXT: _ZTV8Derived1:250
+// When vtable value profiles exist, pgo-instr-use pass should annotate them
+// even if `-enable-vtable-value-profiling` is not explicitly on.
+// 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
+
+// ANNOTATE-NOT: Inconsistent number of value sites
+// ANNOTATE: !{!"VP", i32 2
+
+// When vtable value profiles exist, pgo-instr-use pass will not annotate them
+// if `-enable-vtable-profile-use` is explicitly off.
+// 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
+
+// OMIT: Inconsistent number of value sites
+// OMIT-NOT: !{!"VP", i32 2
+
// Test indirect call promotion transformation using vtable profiles.
// - Build with `-g` to enable debug information.
// - In real world settings, ICP pass is disabled in prelink pipeline. In
@@ -128,12 +142,12 @@
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
// For the indirect call site `ptr->func`
-// 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}
-// 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}
+// 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}
+// 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}
//
// For the indirect call site `delete ptr`
-// 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}
-// 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}
+// 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}
+// 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}
// The IR matchers for indirect callsite `ptr->func`.
// IR-LABEL: @main
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 35b1bbf21be97..be6f4c7464dc2 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1096,7 +1096,7 @@ class PGOUseFunc {
: F(Func), M(Modu), BFI(BFIin), PSI(PSI),
FuncInfo(Func, TLI, ComdatMembers, false, BPI, BFIin, IsCS,
InstrumentFuncEntry, HasSingleByteCoverage),
- FreqAttr(FFA_Normal), IsCS(IsCS) {}
+ FreqAttr(FFA_Normal), IsCS(IsCS), VPC(Func, TLI) {}
void handleInstrProfError(Error Err, uint64_t MismatchedFuncSum);
@@ -1178,6 +1178,8 @@ class PGOUseFunc {
// Is to use the context sensitive profile.
bool IsCS;
+ ValueProfileCollector VPC;
+
// Find the Instrumented BB and set the value. Return false on error.
bool setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
@@ -1755,8 +1757,18 @@ void PGOUseFunc::annotateValueSites() {
void PGOUseFunc::annotateValueSites(uint32_t Kind) {
assert(Kind <= IPVK_Last);
unsigned ValueSiteIndex = 0;
- auto &ValueSites = FuncInfo.ValueSites[Kind];
unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
+ // FuncPGOInstrumentation ctor finds value sites for each kind. It runs on the
+ // common path of pgo-instr-gen and pgo-instr-use, and vtable kind path
+ // is gated by `-enable-vtable-value-profiling`. Give pgo-instr-use pass a
+ // second chance to find out vtable value sites when vtable profiles are
+ // present and `-enable-vtable-profile-use` is not explicitly off.
+ if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
+ NumValueSites != FuncInfo.ValueSites[IPVK_VTableTarget].size() &&
+ !(EnableVTableProfileUse.getNumOccurrences() &&
+ EnableVTableProfileUse == false))
+ FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
+ auto &ValueSites = FuncInfo.ValueSites[Kind];
if (NumValueSites != ValueSites.size()) {
auto &Ctx = M->getContext();
Ctx.diagnose(DiagnosticInfoPGOProfile(
>From 85dccaf6d77da5e3dda04ee2781a29c3aaac55c5 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Wed, 17 Jul 2024 16:13:49 -0700
Subject: [PATCH 2/5] fix line number
---
.../test/profile/Linux/instrprof-vtable-value-prof.cpp | 10 +++++-----
.../Transforms/Instrumentation/PGOInstrumentation.cpp | 8 ++++----
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
index 5555899c4f6b7..5e9a39932a94e 100644
--- a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
+++ b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
@@ -1,4 +1,4 @@
-// REQUIRES: lld-available
+// REQUIRES: lld, lld-available
// Building the instrumented binary will fail because lld doesn't support
// big-endian ELF for PPC (aka ABI 1).
@@ -142,12 +142,12 @@
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
// For the indirect call site `ptr->func`
-// 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}
-// 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}
+// REMARK: instrprof-vtable-value-prof.cpp:221: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}
+// REMARK: instrprof-vtable-value-prof.cpp:221:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
//
// For the indirect call site `delete ptr`
-// 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}
-// 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}
+// REMARK: instrprof-vtable-value-prof.cpp:223: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}
+// REMARK: instrprof-vtable-value-prof.cpp:223:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
// The IR matchers for indirect callsite `ptr->func`.
// IR-LABEL: @main
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index be6f4c7464dc2..2baca05988808 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1760,11 +1760,11 @@ void PGOUseFunc::annotateValueSites(uint32_t Kind) {
unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
// FuncPGOInstrumentation ctor finds value sites for each kind. It runs on the
// common path of pgo-instr-gen and pgo-instr-use, and vtable kind path
- // is gated by `-enable-vtable-value-profiling`. Give pgo-instr-use pass a
- // second chance to find out vtable value sites when vtable profiles are
- // present and `-enable-vtable-profile-use` is not explicitly off.
+ // is gated by `-enable-vtable-value-profiling`. If vtable profiles are
+ // present, not explicitly discarded and vtable sites remain empty, try to
+ // find the sites again.
if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
- NumValueSites != FuncInfo.ValueSites[IPVK_VTableTarget].size() &&
+ FuncInfo.ValueSites[Kind].empty() &&
!(EnableVTableProfileUse.getNumOccurrences() &&
EnableVTableProfileUse == false))
FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
>From a2ec8c44a1ddc892eda1d731b52ef9b439ede374 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Wed, 17 Jul 2024 21:35:53 -0700
Subject: [PATCH 3/5] wrap RUN lines and update remark line number accordingly
---
.../Linux/instrprof-vtable-value-prof.cpp | 20 ++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
index 5e9a39932a94e..83ceb9b0cf556 100644
--- a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
+++ b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
@@ -111,14 +111,20 @@
// When vtable value profiles exist, pgo-instr-use pass should annotate them
// even if `-enable-vtable-value-profiling` is not explicitly on.
-// 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
+// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \
+// RUN: -mllvm -print-after=pgo-instr-use -mllvm -filter-print-funcs=main \
+// RUN: -mllvm -print-module-scope %s 2>&1 | FileCheck %s --check-prefix=ANNOTATE
// ANNOTATE-NOT: Inconsistent number of value sites
// ANNOTATE: !{!"VP", i32 2
// When vtable value profiles exist, pgo-instr-use pass will not annotate them
-// if `-enable-vtable-profile-use` is explicitly off.
-// 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
+// if `-enable-vtable-profile-use` is explicitly set to false to discard vtable
+// value profiles.
+// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \
+// RUN: -mllvm -enable-vtable-profile-use=false -mllvm -print-after=pgo-instr-use \
+// RUN: -mllvm -filter-print-funcs=main -mllvm -print-module-scope %s 2>&1 | \
+// RUN: FileCheck %s --check-prefix=OMIT
// OMIT: Inconsistent number of value sites
// OMIT-NOT: !{!"VP", i32 2
@@ -142,12 +148,12 @@
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
// For the indirect call site `ptr->func`
-// REMARK: instrprof-vtable-value-prof.cpp:221: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}
-// REMARK: instrprof-vtable-value-prof.cpp:221:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
+// REMARK: instrprof-vtable-value-prof.cpp:227: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}
+// REMARK: instrprof-vtable-value-prof.cpp:227:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
//
// For the indirect call site `delete ptr`
-// REMARK: instrprof-vtable-value-prof.cpp:223: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}
-// REMARK: instrprof-vtable-value-prof.cpp:223:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
+// REMARK: instrprof-vtable-value-prof.cpp:229: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}
+// REMARK: instrprof-vtable-value-prof.cpp:229:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
// The IR matchers for indirect callsite `ptr->func`.
// IR-LABEL: @main
>From 36b85b825fa67ec7b4cb3c9a71382fa9e99341fe Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 18 Jul 2024 11:03:50 -0700
Subject: [PATCH 4/5] resolve review feedback
---
.../Linux/instrprof-vtable-value-prof.cpp | 13 ++++++------
.../Instrumentation/PGOInstrumentation.cpp | 21 ++++++-------------
2 files changed, 12 insertions(+), 22 deletions(-)
diff --git a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
index 83ceb9b0cf556..dcc00be867cd6 100644
--- a/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
+++ b/compiler-rt/test/profile/Linux/instrprof-vtable-value-prof.cpp
@@ -119,10 +119,9 @@
// ANNOTATE: !{!"VP", i32 2
// When vtable value profiles exist, pgo-instr-use pass will not annotate them
-// if `-enable-vtable-profile-use` is explicitly set to false to discard vtable
-// value profiles.
+// if `-max-num-vtable-annotaitons` is set to zero.
// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \
-// RUN: -mllvm -enable-vtable-profile-use=false -mllvm -print-after=pgo-instr-use \
+// RUN: -mllvm -icp-max-num-vtables=0 -mllvm -print-after=pgo-instr-use \
// RUN: -mllvm -filter-print-funcs=main -mllvm -print-module-scope %s 2>&1 | \
// RUN: FileCheck %s --check-prefix=OMIT
@@ -148,12 +147,12 @@
// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"
// For the indirect call site `ptr->func`
-// REMARK: instrprof-vtable-value-prof.cpp:227: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}
-// REMARK: instrprof-vtable-value-prof.cpp:227:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
+// 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}
+// 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}
//
// For the indirect call site `delete ptr`
-// REMARK: instrprof-vtable-value-prof.cpp:229: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}
-// REMARK: instrprof-vtable-value-prof.cpp:229:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}
+// 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}
+// 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}
// The IR matchers for indirect callsite `ptr->func`.
// IR-LABEL: @main
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 2baca05988808..af75d6a981043 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -603,7 +603,10 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
NumOfPGOMemIntrinsics += ValueSites[IPVK_MemOPSize].size();
NumOfPGOBB += MST.bbInfoSize();
ValueSites[IPVK_IndirectCallTarget] = VPC.get(IPVK_IndirectCallTarget);
- if (EnableVTableValueProfiling)
+ // Collect value sites for 'pgo-instr-use' pass if `icp-max-num-vtables`
+ // is not zero.
+ if (EnableVTableValueProfiling ||
+ (!CreateGlobalVar && MaxNumVTableAnnotations != 0))
ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
} else {
NumOfCSPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
@@ -1096,7 +1099,7 @@ class PGOUseFunc {
: F(Func), M(Modu), BFI(BFIin), PSI(PSI),
FuncInfo(Func, TLI, ComdatMembers, false, BPI, BFIin, IsCS,
InstrumentFuncEntry, HasSingleByteCoverage),
- FreqAttr(FFA_Normal), IsCS(IsCS), VPC(Func, TLI) {}
+ FreqAttr(FFA_Normal), IsCS(IsCS) {}
void handleInstrProfError(Error Err, uint64_t MismatchedFuncSum);
@@ -1178,8 +1181,6 @@ class PGOUseFunc {
// Is to use the context sensitive profile.
bool IsCS;
- ValueProfileCollector VPC;
-
// Find the Instrumented BB and set the value. Return false on error.
bool setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
@@ -1757,18 +1758,8 @@ void PGOUseFunc::annotateValueSites() {
void PGOUseFunc::annotateValueSites(uint32_t Kind) {
assert(Kind <= IPVK_Last);
unsigned ValueSiteIndex = 0;
- unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
- // FuncPGOInstrumentation ctor finds value sites for each kind. It runs on the
- // common path of pgo-instr-gen and pgo-instr-use, and vtable kind path
- // is gated by `-enable-vtable-value-profiling`. If vtable profiles are
- // present, not explicitly discarded and vtable sites remain empty, try to
- // find the sites again.
- if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
- FuncInfo.ValueSites[Kind].empty() &&
- !(EnableVTableProfileUse.getNumOccurrences() &&
- EnableVTableProfileUse == false))
- FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
auto &ValueSites = FuncInfo.ValueSites[Kind];
+ unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
if (NumValueSites != ValueSites.size()) {
auto &Ctx = M->getContext();
Ctx.diagnose(DiagnosticInfoPGOProfile(
>From 523ee2235cbd3678b420d528c487777d1e39e4b5 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 18 Jul 2024 14:48:22 -0700
Subject: [PATCH 5/5] compute the instrumented vtable sites from function IR if
function profile record has vtable profiles
---
.../Instrumentation/PGOInstrumentation.cpp | 25 ++++++++++++++-----
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index af75d6a981043..a100e89dc6abd 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -603,10 +603,7 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
NumOfPGOMemIntrinsics += ValueSites[IPVK_MemOPSize].size();
NumOfPGOBB += MST.bbInfoSize();
ValueSites[IPVK_IndirectCallTarget] = VPC.get(IPVK_IndirectCallTarget);
- // Collect value sites for 'pgo-instr-use' pass if `icp-max-num-vtables`
- // is not zero.
- if (EnableVTableValueProfiling ||
- (!CreateGlobalVar && MaxNumVTableAnnotations != 0))
+ if (EnableVTableValueProfiling)
ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
} else {
NumOfCSPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
@@ -1099,7 +1096,7 @@ class PGOUseFunc {
: F(Func), M(Modu), BFI(BFIin), PSI(PSI),
FuncInfo(Func, TLI, ComdatMembers, false, BPI, BFIin, IsCS,
InstrumentFuncEntry, HasSingleByteCoverage),
- FreqAttr(FFA_Normal), IsCS(IsCS) {}
+ FreqAttr(FFA_Normal), IsCS(IsCS), VPC(Func, TLI) {}
void handleInstrProfError(Error Err, uint64_t MismatchedFuncSum);
@@ -1181,6 +1178,8 @@ class PGOUseFunc {
// Is to use the context sensitive profile.
bool IsCS;
+ ValueProfileCollector VPC;
+
// Find the Instrumented BB and set the value. Return false on error.
bool setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
@@ -1758,8 +1757,22 @@ void PGOUseFunc::annotateValueSites() {
void PGOUseFunc::annotateValueSites(uint32_t Kind) {
assert(Kind <= IPVK_Last);
unsigned ValueSiteIndex = 0;
- auto &ValueSites = FuncInfo.ValueSites[Kind];
+
unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
+
+ // Since there isn't a reliable or fast way for profile reader to tell if a
+ // profile is generated with `-enable-vtable-value-profiling` on, we run the
+ // value profile collector over the function IR to find the instrumented sites
+ // iff function profile records shows the number of instrumented vtable sites
+ // is not zero. Function cfg already takes the number of instrumented
+ // indirect call sites into account so it doesn't hash the number of
+ // instrumented vtables; as a side effect it makes it easier to enable
+ // profiling and profile use in two steps if needed.
+ if (NumValueSites > 0 && Kind == IPVK_VTableTarget &&
+ NumValueSites != FuncInfo.ValueSites[IPVK_VTableTarget].size() &&
+ MaxNumVTableAnnotations != 0)
+ FuncInfo.ValueSites[IPVK_VTableTarget] = VPC.get(IPVK_VTableTarget);
+ auto &ValueSites = FuncInfo.ValueSites[Kind];
if (NumValueSites != ValueSites.size()) {
auto &Ctx = M->getContext();
Ctx.diagnose(DiagnosticInfoPGOProfile(
More information about the llvm-commits
mailing list