Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b01c3d6

Browse files
authored
[BOLT] Fix merge-fdata for memory events (#128108)
Don't attempt to parse mispredictions for memory entries in LBR profile. Test Plan: added merge-fdata-mem-prof.test
1 parent f07988f commit b01c3d6

6 files changed

Lines changed: 47 additions & 20 deletions

File tree

bolt/test/merge-fdata-bat-no-lbr.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
# CHECK: boltedcollection
1010
# CHECK: no_lbr
11-
# CHECK: main 2
11+
# CHECK: 1 main 2
1212

1313
#--- a.fdata
1414
boltedcollection
1515
no_lbr
16-
main 1
16+
1 main 1
1717
#--- b.fdata
1818
boltedcollection
1919
no_lbr
20-
main 1
20+
1 main 1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Check that merge-fdata tool correctly handles memory profile, emitting it
2+
## after branch profile.
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: split-file %s %t
7+
# RUN: merge-fdata %t/a.fdata %t/b.fdata -o %t/merged.fdata
8+
# RUN: FileCheck %s --input-file %t/merged.fdata
9+
10+
# CHECK: 1 main 5 1 main 10 0 1
11+
# CHECK-NEXT: 4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 3
12+
13+
#--- a.fdata
14+
4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 1
15+
4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 2
16+
#--- b.fdata
17+
1 main 5 1 main 10 0 1

bolt/test/merge-fdata-mixed-bat-no-lbr.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#--- a.fdata
1111
boltedcollection
1212
no_lbr
13-
main 1
13+
1 main 1
1414
#--- b.fdata
1515
no_lbr
16-
main 1
16+
1 main 1

bolt/test/merge-fdata-mixed-mode.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#--- a.fdata
1212
no_lbr
13-
main 1
13+
1 main 1
1414
#--- b.fdata
15-
main 1
15+
1 main 1

bolt/test/merge-fdata-no-lbr-mode.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
# RUN: FileCheck %s --input-file %t/merged.fdata
99

1010
# CHECK: no_lbr
11-
# CHECK: main 2
11+
# CHECK: 1 main 2
1212

1313
#--- a.fdata
1414
no_lbr
15-
main 1
15+
1 main 1
1616
#--- b.fdata
1717
no_lbr
18-
main 1
18+
1 main 1

bolt/tools/merge-fdata/merge-fdata.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
278278
}
279279
CounterTy operator+(const CounterTy &O) { return *this += O; }
280280
};
281-
typedef StringMap<CounterTy> ProfileTy;
281+
struct ProfileTy {
282+
StringMap<CounterTy> Branch;
283+
StringMap<CounterTy> Memory;
284+
};
282285

283286
auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
284287
const llvm::thread::id tid = llvm::this_thread::get_id();
@@ -316,19 +319,23 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
316319
do {
317320
StringRef Line(FdataLine);
318321
CounterTy Count;
322+
unsigned Type = 0;
323+
if (Line.split(' ').first.getAsInteger(10, Type))
324+
report_error(Filename, "Malformed / corrupted entry type");
325+
bool IsBranchEntry = Type < 3;
319326
auto [Signature, ExecCount] = Line.rsplit(' ');
320327
if (ExecCount.getAsInteger(10, Count.Exec))
321328
report_error(Filename, "Malformed / corrupted execution count");
322329
// Only LBR profile has misprediction field
323-
if (!NoLBRCollection.value_or(false)) {
330+
if (!NoLBRCollection.value_or(false) && IsBranchEntry) {
324331
auto [SignatureLBR, MispredCount] = Signature.rsplit(' ');
325332
Signature = SignatureLBR;
326333
if (MispredCount.getAsInteger(10, Count.Mispred))
327334
report_error(Filename, "Malformed / corrupted misprediction count");
328335
}
329336

330-
Count += Profile->lookup(Signature);
331-
Profile->insert_or_assign(Signature, Count);
337+
auto &ProfileMap = IsBranchEntry ? Profile->Branch : Profile->Memory;
338+
ProfileMap[Signature] += Count;
332339
} while (std::getline(FdataFile, FdataLine));
333340
};
334341

@@ -344,22 +351,25 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
344351
Pool.wait();
345352

346353
ProfileTy MergedProfile;
347-
for (const auto &[Thread, Profile] : ParsedProfiles)
348-
for (const auto &[Key, Value] : Profile) {
349-
CounterTy Count = MergedProfile.lookup(Key) + Value;
350-
MergedProfile.insert_or_assign(Key, Count);
351-
}
354+
for (const auto &[Thread, Profile] : ParsedProfiles) {
355+
for (const auto &[Key, Value] : Profile.Branch)
356+
MergedProfile.Branch[Key] += Value;
357+
for (const auto &[Key, Value] : Profile.Memory)
358+
MergedProfile.Memory[Key] += Value;
359+
}
352360

353361
if (BoltedCollection.value_or(false))
354362
output() << "boltedcollection\n";
355363
if (NoLBRCollection.value_or(false))
356364
output() << "no_lbr\n";
357-
for (const auto &[Key, Value] : MergedProfile) {
365+
for (const auto &[Key, Value] : MergedProfile.Branch) {
358366
output() << Key << " ";
359367
if (!NoLBRCollection.value_or(false))
360368
output() << Value.Mispred << " ";
361369
output() << Value.Exec << "\n";
362370
}
371+
for (const auto &[Key, Value] : MergedProfile.Memory)
372+
output() << Key << ' ' << Value.Exec << '\n';
363373

364374
errs() << "Profile from " << Filenames.size() << " files merged.\n";
365375
}

0 commit comments

Comments
 (0)