19
19
#include " llvm/Support/FileSystem.h"
20
20
#include " llvm/Support/ManagedStatic.h"
21
21
#include " llvm/Support/PrettyStackTrace.h"
22
+ #include " llvm/Support/Regex.h"
22
23
#include " llvm/Support/Signals.h"
23
24
#include " llvm/Support/ThreadPool.h"
24
25
#include < algorithm>
@@ -266,7 +267,19 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
266
267
errs () << " Using legacy profile format.\n " ;
267
268
std::optional<bool > BoltedCollection;
268
269
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;
270
283
271
284
auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
272
285
const llvm::thread::id tid = llvm::this_thread::get_id ();
@@ -304,15 +317,19 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
304
317
SmallVector<StringRef> Lines;
305
318
SplitString (Buf, Lines, " \n " );
306
319
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))
309
323
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);
316
333
}
317
334
};
318
335
@@ -330,14 +347,14 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
330
347
ProfileTy MergedProfile;
331
348
for (const auto &[Thread, Profile] : ParsedProfiles)
332
349
for (const auto &[Key, Value] : Profile) {
333
- uint64_t Count = MergedProfile.lookup (Key) + Value;
350
+ auto Count = MergedProfile.lookup (Key) + Value;
334
351
MergedProfile.insert_or_assign (Key, Count);
335
352
}
336
353
337
354
if (BoltedCollection.value_or (false ))
338
355
output () << " boltedcollection\n " ;
339
356
for (const auto &[Key, Value] : MergedProfile)
340
- output () << Key << " " << Value << " \n " ;
357
+ output () << Key << " " << Value. MispredCount << " " << Value. Count << " \n " ;
341
358
342
359
errs () << " Profile from " << Filenames.size () << " files merged.\n " ;
343
360
}
0 commit comments