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