Skip to content

[BOLT] Fix counts aggregation in merge-fdata #119652

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

Merged
Merged
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-lbr-mode.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# RUN: FileCheck %s --input-file %t/merged.fdata

# CHECK-NOT: no_lbr
# CHECK: main 2
# CHECK: 1 main 0 1 main 2 1 3

#--- a.fdata
main 1
1 main 0 1 main 2 0 1
#--- b.fdata
main 1
1 main 0 1 main 2 1 2
41 changes: 30 additions & 11 deletions bolt/tools/merge-fdata/merge-fdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,17 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
std::optional<bool> BoltedCollection;
std::optional<bool> NoLBRCollection;
std::mutex BoltedCollectionMutex;
typedef StringMap<uint64_t> ProfileTy;
struct CounterTy {
uint64_t Exec{0};
uint64_t Mispred{0};
CounterTy &operator+=(const CounterTy &O) {
Exec += O.Exec;
Mispred += O.Mispred;
return *this;
}
CounterTy operator+(const CounterTy &O) { return *this += O; }
};
typedef StringMap<CounterTy> ProfileTy;

auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
const llvm::thread::id tid = llvm::this_thread::get_id();
Expand Down Expand Up @@ -305,13 +315,18 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {

do {
StringRef Line(FdataLine);
size_t Pos = Line.rfind(" ");
if (Pos == StringRef::npos)
report_error(Filename, "Malformed / corrupted profile");
StringRef Signature = Line.substr(0, Pos);
uint64_t Count;
if (Line.substr(Pos + 1, Line.size() - Pos).getAsInteger(10, Count))
report_error(Filename, "Malformed / corrupted profile counter");
CounterTy Count;
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)) {
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);
} while (std::getline(FdataFile, FdataLine));
Expand All @@ -331,16 +346,20 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
ProfileTy MergedProfile;
for (const auto &[Thread, Profile] : ParsedProfiles)
for (const auto &[Key, Value] : Profile) {
uint64_t Count = MergedProfile.lookup(Key) + Value;
CounterTy Count = MergedProfile.lookup(Key) + Value;
MergedProfile.insert_or_assign(Key, Count);
}

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

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