Skip to content

Commit 5ff2685

Browse files
committed
[llvm-profgen] Filter out ambiguous profiles during profile generation
1 parent 4cd7616 commit 5ff2685

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
foo:12345:1000
2+
1: 1000
3+
4: bar:1000
4+
1: 1000
5+
2: __tls_init.llvm.123:1
6+
1: 1
7+
3: goo:300
8+
1: 300
9+
8: __cxx_global_var_init.4:4
10+
1: 1
11+
2: goo:3
12+
1: 3
13+
__cxx_global_var_init.1:1:1
14+
1: 1
15+
__tls_init.llvm.345:1:1
16+
1: 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: llvm-profgen --format=text --llvm-sample-profile=%S/Inputs/filter-ambiguous-profile.prof --binary=%S/Inputs/inline-cs-noprobe.perfbin --csspgo-preinliner=0 --output=%t1 || FileCheck %s --input-file %t1
2+
3+
;CHECK: foo:12345:1000
4+
;CHECK-NEXT 1: 1000
5+
;CHECK-NEXT 4: bar:1000
6+
;CHECK-NEXT 1: 1000
7+
;CHECK-NEXT 3: goo:300
8+
;CHECK-NEXT 1: 300

llvm/tools/llvm-profgen/ProfileGenerator.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,33 @@ void ProfileGeneratorBase::showDensitySuggestion(double Density) {
196196
<< "% total samples: " << format("%.1f", Density) << "\n";
197197
}
198198

199+
bool ProfileGeneratorBase::filterAmbiguousProfile(FunctionSamples &FS) {
200+
for (const auto &Prefix : FuncPrefixsToFilter) {
201+
if (FS.getFuncName().starts_with(Prefix))
202+
return true;
203+
}
204+
205+
// Filter the function profiles for the inlinees.
206+
for (auto &Callees :
207+
const_cast<CallsiteSampleMap &>(FS.getCallsiteSamples())) {
208+
auto &CalleesMap = Callees.second;
209+
for (auto I = CalleesMap.begin(); I != CalleesMap.end();) {
210+
auto Tmp = I++;
211+
if (filterAmbiguousProfile(Tmp->second))
212+
CalleesMap.erase(Tmp);
213+
}
214+
}
215+
return false;
216+
}
217+
218+
void ProfileGeneratorBase::filterAmbiguousProfile(SampleProfileMap &Profiles) {
219+
for (auto I = ProfileMap.begin(); I != ProfileMap.end();) {
220+
auto Tmp = I++;
221+
if (filterAmbiguousProfile(Tmp->second))
222+
ProfileMap.erase(Tmp);
223+
}
224+
}
225+
199226
double ProfileGeneratorBase::calculateDensity(const SampleProfileMap &Profiles,
200227
uint64_t HotCntThreshold) {
201228
double Density = DBL_MAX;
@@ -491,6 +518,7 @@ void ProfileGenerator::generateProfile() {
491518
void ProfileGenerator::postProcessProfiles() {
492519
computeSummaryAndThreshold(ProfileMap);
493520
trimColdProfiles(ProfileMap, ColdCountThreshold);
521+
filterAmbiguousProfile(ProfileMap);
494522
calculateAndShowDensity(ProfileMap);
495523
}
496524

@@ -1024,6 +1052,7 @@ void CSProfileGenerator::postProcessProfiles() {
10241052
CSConverter.convertCSProfiles();
10251053
FunctionSamples::ProfileIsCS = false;
10261054
}
1055+
filterAmbiguousProfile(ProfileMap);
10271056
}
10281057

10291058
void ProfileGeneratorBase::computeSummaryAndThreshold(

llvm/tools/llvm-profgen/ProfileGenerator.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class ProfileGeneratorBase {
108108

109109
void updateCallsiteSamples();
110110

111+
void filterAmbiguousProfile(SampleProfileMap &Profiles);
112+
113+
bool filterAmbiguousProfile(FunctionSamples &FS);
114+
111115
StringRef getCalleeNameForAddress(uint64_t TargetAddress);
112116

113117
void computeSummaryAndThreshold(SampleProfileMap &ProfileMap);
@@ -128,6 +132,13 @@ class ProfileGeneratorBase {
128132
virtual bool collectFunctionsFromLLVMProfile(
129133
std::unordered_set<const BinaryFunction *> &ProfiledFunctions) = 0;
130134

135+
// Those functions are usually cold but could have multiple versions, the
136+
// profile are either from one version or the merged version, which is
137+
// ambiguous. We can't attribute the profile for each version accurately, so
138+
// filter them out from the profile map.
139+
static constexpr const char *FuncPrefixsToFilter[] = {"__cxx_global_var_init",
140+
"__tls_init"};
141+
131142
// Thresholds from profile summary to answer isHotCount/isColdCount queries.
132143
uint64_t HotCountThreshold;
133144

0 commit comments

Comments
 (0)