Skip to content

Commit cc7cea0

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent fd2f8d4 commit cc7cea0

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

bolt/test/merge-fdata.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Reproduces an issue where counts were not accumulated by merge-fdata
2+
3+
# RUN: split-file %s %t
4+
# RUN: merge-fdata %t/fdata | FileCheck %s
5+
# CHECK: 1 main 10 1 main 1a 1 20
6+
#--- fdata
7+
1 main 10 1 main 1a 0 10
8+
1 main 10 1 main 1a 1 10

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/FileSystem.h"
2020
#include "llvm/Support/ManagedStatic.h"
2121
#include "llvm/Support/PrettyStackTrace.h"
22+
#include "llvm/Support/Regex.h"
2223
#include "llvm/Support/Signals.h"
2324
#include "llvm/Support/ThreadPool.h"
2425
#include <algorithm>
@@ -266,7 +267,19 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
266267
errs() << "Using legacy profile format.\n";
267268
std::optional<bool> BoltedCollection;
268269
std::mutex BoltedCollectionMutex;
269-
typedef StringMap<uint64_t> ProfileTy;
270+
constexpr static const char *const FdataCountersPattern =
271+
"(.*) ([0-9]+) ([0-9]+)";
272+
Regex FdataRegex(FdataCountersPattern);
273+
struct CounterTy {
274+
uint64_t Count;
275+
uint64_t MispredCount;
276+
CounterTy &operator+(const CounterTy &O) {
277+
Count += O.Count;
278+
MispredCount += O.MispredCount;
279+
return *this;
280+
}
281+
};
282+
typedef StringMap<CounterTy> ProfileTy;
270283

271284
auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
272285
const llvm::thread::id tid = llvm::this_thread::get_id();
@@ -304,15 +317,19 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
304317
SmallVector<StringRef> Lines;
305318
SplitString(Buf, Lines, "\n");
306319
for (StringRef Line : Lines) {
307-
size_t Pos = Line.rfind(" ");
308-
if (Pos == StringRef::npos)
320+
CounterTy CurrCount;
321+
SmallVector<StringRef, 4> Fields;
322+
if (!FdataRegex.match(Line, &Fields))
309323
report_error(Filename, "Malformed / corrupted profile");
310-
StringRef Signature = Line.substr(0, Pos);
311-
uint64_t Count;
312-
if (Line.substr(Pos + 1, Line.size() - Pos).getAsInteger(10, Count))
313-
report_error(Filename, "Malformed / corrupted profile counter");
314-
Count += Profile->lookup(Signature);
315-
Profile->insert_or_assign(Signature, Count);
324+
StringRef Signature = Fields[1];
325+
if (Fields[2].getAsInteger(10, CurrCount.MispredCount))
326+
report_error(Filename, "Malformed / corrupted execution count");
327+
if (Fields[3].getAsInteger(10, CurrCount.Count))
328+
report_error(Filename, "Malformed / corrupted misprediction count");
329+
330+
CounterTy Counter = Profile->lookup(Signature);
331+
Counter = Counter + CurrCount;
332+
Profile->insert_or_assign(Signature, Counter);
316333
}
317334
};
318335

@@ -330,14 +347,14 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
330347
ProfileTy MergedProfile;
331348
for (const auto &[Thread, Profile] : ParsedProfiles)
332349
for (const auto &[Key, Value] : Profile) {
333-
uint64_t Count = MergedProfile.lookup(Key) + Value;
350+
auto Count = MergedProfile.lookup(Key) + Value;
334351
MergedProfile.insert_or_assign(Key, Count);
335352
}
336353

337354
if (BoltedCollection.value_or(false))
338355
output() << "boltedcollection\n";
339356
for (const auto &[Key, Value] : MergedProfile)
340-
output() << Key << " " << Value << "\n";
357+
output() << Key << " " << Value.MispredCount << " " << Value.Count << "\n";
341358

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

0 commit comments

Comments
 (0)