@@ -278,7 +278,10 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
278278 }
279279 CounterTy operator +(const CounterTy &O) { return *this += O; }
280280 };
281- typedef StringMap<CounterTy> ProfileTy;
281+ struct ProfileTy {
282+ StringMap<CounterTy> Branch;
283+ StringMap<CounterTy> Memory;
284+ };
282285
283286 auto ParseProfile = [&](const std::string &Filename, auto &Profiles) {
284287 const llvm::thread::id tid = llvm::this_thread::get_id ();
@@ -316,19 +319,23 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
316319 do {
317320 StringRef Line (FdataLine);
318321 CounterTy Count;
322+ unsigned Type = 0 ;
323+ if (Line.split (' ' ).first .getAsInteger (10 , Type))
324+ report_error (Filename, " Malformed / corrupted entry type" );
325+ bool IsBranchEntry = Type < 3 ;
319326 auto [Signature, ExecCount] = Line.rsplit (' ' );
320327 if (ExecCount.getAsInteger (10 , Count.Exec ))
321328 report_error (Filename, " Malformed / corrupted execution count" );
322329 // Only LBR profile has misprediction field
323- if (!NoLBRCollection.value_or (false )) {
330+ if (!NoLBRCollection.value_or (false ) && IsBranchEntry ) {
324331 auto [SignatureLBR, MispredCount] = Signature.rsplit (' ' );
325332 Signature = SignatureLBR;
326333 if (MispredCount.getAsInteger (10 , Count.Mispred ))
327334 report_error (Filename, " Malformed / corrupted misprediction count" );
328335 }
329336
330- Count += Profile->lookup (Signature) ;
331- Profile-> insert_or_assign ( Signature, Count) ;
337+ auto &ProfileMap = IsBranchEntry ? Profile->Branch : Profile-> Memory ;
338+ ProfileMap[ Signature] += Count;
332339 } while (std::getline (FdataFile, FdataLine));
333340 };
334341
@@ -344,22 +351,25 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
344351 Pool.wait ();
345352
346353 ProfileTy MergedProfile;
347- for (const auto &[Thread, Profile] : ParsedProfiles)
348- for (const auto &[Key, Value] : Profile) {
349- CounterTy Count = MergedProfile.lookup (Key) + Value;
350- MergedProfile.insert_or_assign (Key, Count);
351- }
354+ for (const auto &[Thread, Profile] : ParsedProfiles) {
355+ for (const auto &[Key, Value] : Profile.Branch )
356+ MergedProfile.Branch [Key] += Value;
357+ for (const auto &[Key, Value] : Profile.Memory )
358+ MergedProfile.Memory [Key] += Value;
359+ }
352360
353361 if (BoltedCollection.value_or (false ))
354362 output () << " boltedcollection\n " ;
355363 if (NoLBRCollection.value_or (false ))
356364 output () << " no_lbr\n " ;
357- for (const auto &[Key, Value] : MergedProfile) {
365+ for (const auto &[Key, Value] : MergedProfile. Branch ) {
358366 output () << Key << " " ;
359367 if (!NoLBRCollection.value_or (false ))
360368 output () << Value.Mispred << " " ;
361369 output () << Value.Exec << " \n " ;
362370 }
371+ for (const auto &[Key, Value] : MergedProfile.Memory )
372+ output () << Key << ' ' << Value.Exec << ' \n ' ;
363373
364374 errs () << " Profile from " << Filenames.size () << " files merged.\n " ;
365375}
0 commit comments