Skip to content

Commit 24f0251

Browse files
authored
[llvm-profgen] Filter out ambiguous cold profiles during profile generation (#81803)
For the built-in local initialization function(`__cxx_global_var_init`, `__tls_init` prefix), there could be multiple versions of the functions in the final binary, e.g. `__cxx_global_var_init`, which is a wrapper of global variable ctors, the compiler could assign suffixes like `__cxx_global_var_init.N` for different ctors. However, in the profile generation, we call `getCanonicalFnName` to canonicalize the names which strip the suffixes. Therefore, samples from different functions queries the same profile(only `__cxx_global_var_init`) and the counts are merged. As the functions are essentially different, entries of the merged profile are ambiguous. In sample loading, for each version of this function, the IR from one version would be attributed towards a merged entries, which is inaccurate, especially for fuzzy profile matching, it gets multiple callsites(from different function) but using to match one callsite, which mislead the matching and report a lot of false positives. Hence, we want to filter them out from the profile map during the profile generation time. The profiles are all cold functions, it won't have perf impact.
1 parent 5a29887 commit 24f0251

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,48 @@ 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. It's useful for fuzzy
206+
// profile matching which flattens the profile and inlinees' samples are
207+
// merged into top-level function.
208+
for (auto &Callees :
209+
const_cast<CallsiteSampleMap &>(FS.getCallsiteSamples())) {
210+
auto &CalleesMap = Callees.second;
211+
for (auto I = CalleesMap.begin(); I != CalleesMap.end();) {
212+
auto FS = I++;
213+
if (filterAmbiguousProfile(FS->second))
214+
CalleesMap.erase(FS);
215+
}
216+
}
217+
return false;
218+
}
219+
220+
// For built-in local initialization function such as __cxx_global_var_init,
221+
// __tls_init prefix function, there could be multiple versions of the functions
222+
// in the final binary. However, in the profile generation, we call
223+
// getCanonicalFnName to canonicalize the names which strips the suffixes.
224+
// Therefore, samples from different functions queries the same profile and the
225+
// samples are merged. As the functions are essentially different, entries of
226+
// the merged profile are ambiguous. In sample loader, the IR from one version
227+
// would be attributed towards a merged entries, which is inaccurate. Especially
228+
// for fuzzy profile matching, it gets multiple callsites(from different
229+
// function) but used to match one callsite, which misleads the matching and
230+
// causes a lot of false positives report. Hence, we want to filter them out
231+
// from the profile map during the profile generation time. The profiles are all
232+
// cold functions, it won't have perf impact.
233+
void ProfileGeneratorBase::filterAmbiguousProfile(SampleProfileMap &Profiles) {
234+
for (auto I = ProfileMap.begin(); I != ProfileMap.end();) {
235+
auto FS = I++;
236+
if (filterAmbiguousProfile(FS->second))
237+
ProfileMap.erase(FS);
238+
}
239+
}
240+
199241
double ProfileGeneratorBase::calculateDensity(const SampleProfileMap &Profiles,
200242
uint64_t HotCntThreshold) {
201243
double Density = DBL_MAX;
@@ -491,6 +533,7 @@ void ProfileGenerator::generateProfile() {
491533
void ProfileGenerator::postProcessProfiles() {
492534
computeSummaryAndThreshold(ProfileMap);
493535
trimColdProfiles(ProfileMap, ColdCountThreshold);
536+
filterAmbiguousProfile(ProfileMap);
494537
calculateAndShowDensity(ProfileMap);
495538
}
496539

@@ -1024,6 +1067,7 @@ void CSProfileGenerator::postProcessProfiles() {
10241067
CSConverter.convertCSProfiles();
10251068
FunctionSamples::ProfileIsCS = false;
10261069
}
1070+
filterAmbiguousProfile(ProfileMap);
10271071
}
10281072

10291073
void ProfileGeneratorBase::computeSummaryAndThreshold(

llvm/tools/llvm-profgen/ProfileGenerator.h

Lines changed: 8 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,10 @@ class ProfileGeneratorBase {
128132
virtual bool collectFunctionsFromLLVMProfile(
129133
std::unordered_set<const BinaryFunction *> &ProfiledFunctions) = 0;
130134

135+
// List of function prefix to filter out.
136+
static constexpr const char *FuncPrefixsToFilter[] = {"__cxx_global_var_init",
137+
"__tls_init"};
138+
131139
// Thresholds from profile summary to answer isHotCount/isColdCount queries.
132140
uint64_t HotCountThreshold;
133141

0 commit comments

Comments
 (0)