Skip to content

[BOLT] Fix merge-fdata for memory events #128108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: users/aaupov/spr/main.bolt-fix-merge-fdata-for-memory-events
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bolt/test/merge-fdata-bat-no-lbr.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

# CHECK: boltedcollection
# CHECK: no_lbr
# CHECK: main 2
# CHECK: 1 main 2

#--- a.fdata
boltedcollection
no_lbr
main 1
1 main 1
#--- b.fdata
boltedcollection
no_lbr
main 1
1 main 1
17 changes: 17 additions & 0 deletions bolt/test/merge-fdata-mem-prof.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Check that merge-fdata tool correctly handles memory profile, emitting it
## after branch profile.

# REQUIRES: system-linux

# RUN: split-file %s %t
# RUN: merge-fdata %t/a.fdata %t/b.fdata -o %t/merged.fdata
# RUN: FileCheck %s --input-file %t/merged.fdata

# CHECK: 1 main 5 1 main 10 0 1
# CHECK-NEXT: 4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 3

#--- a.fdata
4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 1
4 Curl_cf_def_query c 4 Curl_cft_h1_proxy 68 2
#--- b.fdata
1 main 5 1 main 10 0 1
4 changes: 2 additions & 2 deletions bolt/test/merge-fdata-mixed-bat-no-lbr.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#--- a.fdata
boltedcollection
no_lbr
main 1
1 main 1
#--- b.fdata
no_lbr
main 1
1 main 1
4 changes: 2 additions & 2 deletions bolt/test/merge-fdata-mixed-mode.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

#--- a.fdata
no_lbr
main 1
1 main 1
#--- b.fdata
main 1
1 main 1
6 changes: 3 additions & 3 deletions bolt/test/merge-fdata-no-lbr-mode.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
# RUN: FileCheck %s --input-file %t/merged.fdata

# CHECK: no_lbr
# CHECK: main 2
# CHECK: 1 main 2

#--- a.fdata
no_lbr
main 1
1 main 1
#--- b.fdata
no_lbr
main 1
1 main 1
30 changes: 20 additions & 10 deletions bolt/tools/merge-fdata/merge-fdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
}
CounterTy operator+(const CounterTy &O) { return *this += O; }
};
typedef StringMap<CounterTy> ProfileTy;
struct ProfileTy {
StringMap<CounterTy> Branch;
StringMap<CounterTy> Memory;
};

auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
const llvm::thread::id tid = llvm::this_thread::get_id();
Expand Down Expand Up @@ -316,19 +319,23 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
do {
StringRef Line(FdataLine);
CounterTy Count;
unsigned Type = 0;
if (Line.split(' ').first.getAsInteger(10, Type))
report_error(Filename, "Malformed / corrupted entry type");
bool IsBranchEntry = Type < 3;
auto [Signature, ExecCount] = Line.rsplit(' ');
if (ExecCount.getAsInteger(10, Count.Exec))
report_error(Filename, "Malformed / corrupted execution count");
// Only LBR profile has misprediction field
if (!NoLBRCollection.value_or(false)) {
if (!NoLBRCollection.value_or(false) && IsBranchEntry) {
auto [SignatureLBR, MispredCount] = Signature.rsplit(' ');
Signature = SignatureLBR;
if (MispredCount.getAsInteger(10, Count.Mispred))
report_error(Filename, "Malformed / corrupted misprediction count");
}

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

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

ProfileTy MergedProfile;
for (const auto &[Thread, Profile] : ParsedProfiles)
for (const auto &[Key, Value] : Profile) {
CounterTy Count = MergedProfile.lookup(Key) + Value;
MergedProfile.insert_or_assign(Key, Count);
}
for (const auto &[Thread, Profile] : ParsedProfiles) {
for (const auto &[Key, Value] : Profile.Branch)
MergedProfile.Branch[Key] += Value;
for (const auto &[Key, Value] : Profile.Memory)
MergedProfile.Memory[Key] += Value;
}

if (BoltedCollection.value_or(false))
output() << "boltedcollection\n";
if (NoLBRCollection.value_or(false))
output() << "no_lbr\n";
for (const auto &[Key, Value] : MergedProfile) {
for (const auto &[Key, Value] : MergedProfile.Branch) {
output() << Key << " ";
if (!NoLBRCollection.value_or(false))
output() << Value.Mispred << " ";
output() << Value.Exec << "\n";
}
for (const auto &[Key, Value] : MergedProfile.Memory)
output() << Key << ' ' << Value.Exec << '\n';

errs() << "Profile from " << Filenames.size() << " files merged.\n";
}
Expand Down
Loading