Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions bolt/test/merge-fdata.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Reproduces an issue where counts were not accumulated by merge-fdata

# RUN: split-file %s %t
# RUN: merge-fdata %t/fdata | FileCheck %s
# CHECK: 1 main 10 1 main 1a 1 20
#--- fdata
1 main 10 1 main 1a 0 10
1 main 10 1 main 1a 1 10
35 changes: 26 additions & 9 deletions bolt/tools/merge-fdata/merge-fdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ThreadPool.h"
#include <algorithm>
Expand Down Expand Up @@ -266,7 +267,20 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
errs() << "Using legacy profile format.\n";
std::optional<bool> BoltedCollection;
std::mutex BoltedCollectionMutex;
typedef StringMap<uint64_t> ProfileTy;
constexpr static const char *const FdataCountersPattern =
"(.*) ([0-9]+) ([0-9]+)";
Regex FdataRegex(FdataCountersPattern);
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 @@ -304,13 +318,16 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
SmallVector<StringRef> Lines;
SplitString(Buf, Lines, "\n");
for (StringRef Line : Lines) {
size_t Pos = Line.rfind(" ");
if (Pos == StringRef::npos)
SmallVector<StringRef, 4> Fields;
if (!FdataRegex.match(Line, &Fields))
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");
StringRef Signature = Fields[1];
CounterTy Count;
if (Fields[2].getAsInteger(10, Count.Mispred))
report_error(Filename, "Malformed / corrupted execution count");
if (Fields[3].getAsInteger(10, Count.Exec))
report_error(Filename, "Malformed / corrupted misprediction count");

Count += Profile->lookup(Signature);
Profile->insert_or_assign(Signature, Count);
}
Expand All @@ -330,14 +347,14 @@ 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;
auto Count = MergedProfile.lookup(Key) + Value;
MergedProfile.insert_or_assign(Key, Count);
}

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

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