@@ -444,19 +444,6 @@ int DataAggregator::prepareToParse(StringRef Name, PerfProcessInfo &Process,
444
444
Error DataAggregator::preprocessProfile (BinaryContext &BC) {
445
445
this ->BC = &BC;
446
446
447
- if (opts::ReadPreAggregated) {
448
- parsePreAggregated ();
449
- return Error::success ();
450
- }
451
-
452
- if (std::optional<StringRef> FileBuildID = BC.getFileBuildID ()) {
453
- outs () << " BOLT-INFO: binary build-id is: " << *FileBuildID << " \n " ;
454
- processFileBuildID (*FileBuildID);
455
- } else {
456
- errs () << " BOLT-WARNING: build-id will not be checked because we could "
457
- " not read one from input binary\n " ;
458
- }
459
-
460
447
auto ErrorCallback = [](int ReturnCode, StringRef ErrBuf) {
461
448
errs () << " PERF-ERROR: return code " << ReturnCode << " \n " << ErrBuf;
462
449
exit (1 );
@@ -469,6 +456,19 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
469
456
ErrorCallback (ReturnCode, ErrBuf);
470
457
};
471
458
459
+ if (opts::ReadPreAggregated) {
460
+ parsePreAggregated ();
461
+ goto heatmap;
462
+ }
463
+
464
+ if (std::optional<StringRef> FileBuildID = BC.getFileBuildID ()) {
465
+ outs () << " BOLT-INFO: binary build-id is: " << *FileBuildID << " \n " ;
466
+ processFileBuildID (*FileBuildID);
467
+ } else {
468
+ errs () << " BOLT-WARNING: build-id will not be checked because we could "
469
+ " not read one from input binary\n " ;
470
+ }
471
+
472
472
if (BC.IsLinuxKernel ) {
473
473
// Current MMap parsing logic does not work with linux kernel.
474
474
// MMap entries for linux kernel uses PERF_RECORD_MMAP
@@ -501,14 +501,6 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
501
501
(opts::BasicAggregation && parseBasicEvents ()))
502
502
errs () << " PERF2BOLT: failed to parse samples\n " ;
503
503
504
- if (opts::HeatmapMode) {
505
- if (std::error_code EC = printLBRHeatMap ()) {
506
- errs () << " ERROR: failed to print heat map: " << EC.message () << ' \n ' ;
507
- exit (1 );
508
- }
509
- exit (0 );
510
- }
511
-
512
504
// Special handling for memory events
513
505
if (prepareToParse (" mem events" , MemEventsPPI, MemEventsErrorCallback))
514
506
return Error::success ();
@@ -519,6 +511,14 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
519
511
520
512
deleteTempFiles ();
521
513
514
+ heatmap:
515
+ if (opts::HeatmapMode) {
516
+ if (std::error_code EC = printLBRHeatMap ()) {
517
+ errs () << " ERROR: failed to print heat map: " << EC.message () << ' \n ' ;
518
+ exit (1 );
519
+ }
520
+ exit (0 );
521
+ }
522
522
return Error::success ();
523
523
}
524
524
@@ -555,9 +555,7 @@ bool DataAggregator::mayHaveProfileData(const BinaryFunction &Function) {
555
555
}
556
556
557
557
void DataAggregator::processProfile (BinaryContext &BC) {
558
- if (opts::ReadPreAggregated)
559
- processPreAggregated ();
560
- else if (opts::BasicAggregation)
558
+ if (opts::BasicAggregation)
561
559
processBasicEvents ();
562
560
else
563
561
processBranchEvents ();
@@ -586,7 +584,6 @@ void DataAggregator::processProfile(BinaryContext &BC) {
586
584
// Release intermediate storage.
587
585
clear (BranchLBRs);
588
586
clear (FallthroughLBRs);
589
- clear (AggregatedLBRs);
590
587
clear (BasicSamples);
591
588
clear (MemSamples);
592
589
}
@@ -1215,15 +1212,14 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
1215
1212
ErrorOr<StringRef> TypeOrErr = parseString (FieldSeparator);
1216
1213
if (std::error_code EC = TypeOrErr.getError ())
1217
1214
return EC;
1218
- auto Type = AggregatedLBREntry::TRACE;
1219
- if (LLVM_LIKELY (TypeOrErr.get () == " T" )) {
1220
- } else if (TypeOrErr.get () == " B" ) {
1221
- Type = AggregatedLBREntry::BRANCH;
1222
- } else if (TypeOrErr.get () == " F" ) {
1223
- Type = AggregatedLBREntry::FT;
1224
- } else if (TypeOrErr.get () == " f" ) {
1225
- Type = AggregatedLBREntry::FT_EXTERNAL_ORIGIN;
1226
- } else {
1215
+ enum AggregatedLBREntry { TRACE, BRANCH, FT, FT_EXTERNAL_ORIGIN, INVALID };
1216
+ auto Type = StringSwitch<AggregatedLBREntry>(TypeOrErr.get ())
1217
+ .Case (" T" , TRACE)
1218
+ .Case (" B" , BRANCH)
1219
+ .Case (" F" , FT)
1220
+ .Case (" f" , FT_EXTERNAL_ORIGIN)
1221
+ .Default (INVALID);
1222
+ if (Type == INVALID) {
1227
1223
reportError (" expected T, B, F or f" );
1228
1224
return make_error_code (llvm::errc::io_error);
1229
1225
}
@@ -1279,13 +1275,28 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
1279
1275
BF->setHasProfileAvailable ();
1280
1276
1281
1277
uint64_t Count = static_cast <uint64_t >(Frequency.get ());
1282
- AggregatedLBREntry Entry{From.get (), To.get (), Count, Mispreds, Type};
1283
- AggregatedLBRs.emplace_back (Entry);
1284
- if (Type == AggregatedLBREntry::TRACE) {
1285
- auto FtType = (FromFunc == ToFunc) ? AggregatedLBREntry::FT
1286
- : AggregatedLBREntry::FT_EXTERNAL_ORIGIN;
1287
- AggregatedLBREntry TraceFt{To.get (), TraceFtEnd.get (), Count, 0 , FtType};
1288
- AggregatedLBRs.emplace_back (TraceFt);
1278
+
1279
+ Trace Trace (From->Offset , To->Offset );
1280
+ // Taken trace
1281
+ if (Type == TRACE || Type == BRANCH) {
1282
+ TakenBranchInfo &Info = BranchLBRs[Trace];
1283
+ Info.TakenCount += Count;
1284
+ Info.MispredCount += Mispreds;
1285
+
1286
+ NumTotalSamples += Count;
1287
+ }
1288
+ // Construct fallthrough part of the trace
1289
+ if (Type == TRACE) {
1290
+ Trace.From = To->Offset ;
1291
+ Trace.To = TraceFtEnd->Offset ;
1292
+ Type = FromFunc == ToFunc ? FT : FT_EXTERNAL_ORIGIN;
1293
+ }
1294
+ // Add fallthrough trace
1295
+ if (Type != BRANCH) {
1296
+ FTInfo &Info = FallthroughLBRs[Trace];
1297
+ (Type == FT ? Info.InternCount : Info.ExternCount ) += Count;
1298
+
1299
+ NumTraces += Count;
1289
1300
}
1290
1301
1291
1302
return std::error_code ();
@@ -1567,7 +1578,6 @@ std::error_code DataAggregator::parseBranchEvents() {
1567
1578
printBranchStacksDiagnostics (NumTotalSamples - NumSamples);
1568
1579
}
1569
1580
}
1570
- printBranchSamplesDiagnostics ();
1571
1581
1572
1582
return std::error_code ();
1573
1583
}
@@ -1595,6 +1605,7 @@ void DataAggregator::processBranchEvents() {
1595
1605
const TakenBranchInfo &Info = AggrLBR.second ;
1596
1606
doBranch (Loc.From , Loc.To , Info.TakenCount , Info.MispredCount );
1597
1607
}
1608
+ printBranchSamplesDiagnostics ();
1598
1609
}
1599
1610
1600
1611
std::error_code DataAggregator::parseBasicEvents () {
@@ -1704,43 +1715,16 @@ std::error_code DataAggregator::parsePreAggregatedLBRSamples() {
1704
1715
outs () << " PERF2BOLT: parsing pre-aggregated profile...\n " ;
1705
1716
NamedRegionTimer T (" parseAggregated" , " Parsing aggregated branch events" ,
1706
1717
TimerGroupName, TimerGroupDesc, opts::TimeAggregator);
1707
- while (hasData ())
1718
+ size_t AggregatedLBRs = 0 ;
1719
+ while (hasData ()) {
1708
1720
if (std::error_code EC = parseAggregatedLBREntry ())
1709
1721
return EC;
1710
-
1711
- return std::error_code ();
1712
- }
1713
-
1714
- void DataAggregator::processPreAggregated () {
1715
- outs () << " PERF2BOLT: processing pre-aggregated profile...\n " ;
1716
- NamedRegionTimer T (" processAggregated" , " Processing aggregated branch events" ,
1717
- TimerGroupName, TimerGroupDesc, opts::TimeAggregator);
1718
-
1719
- for (const AggregatedLBREntry &AggrEntry : AggregatedLBRs) {
1720
- switch (AggrEntry.EntryType ) {
1721
- case AggregatedLBREntry::BRANCH:
1722
- case AggregatedLBREntry::TRACE:
1723
- doBranch (AggrEntry.From .Offset , AggrEntry.To .Offset , AggrEntry.Count ,
1724
- AggrEntry.Mispreds );
1725
- NumTotalSamples += AggrEntry.Count ;
1726
- break ;
1727
- case AggregatedLBREntry::FT:
1728
- case AggregatedLBREntry::FT_EXTERNAL_ORIGIN: {
1729
- LBREntry First{AggrEntry.EntryType == AggregatedLBREntry::FT
1730
- ? AggrEntry.From .Offset
1731
- : 0 ,
1732
- AggrEntry.From .Offset , false };
1733
- LBREntry Second{AggrEntry.To .Offset , AggrEntry.To .Offset , false };
1734
- doTrace (First, Second, AggrEntry.Count );
1735
- NumTraces += AggrEntry.Count ;
1736
- break ;
1737
- }
1738
- }
1722
+ ++AggregatedLBRs;
1739
1723
}
1740
1724
1741
- outs () << " PERF2BOLT: read " << AggregatedLBRs. size ()
1742
- << " aggregated LBR entries \n " ;
1743
- printBranchSamplesDiagnostics ();
1725
+ outs () << " PERF2BOLT: read " << AggregatedLBRs << " aggregated LBR entries \n " ;
1726
+
1727
+ return std::error_code ();
1744
1728
}
1745
1729
1746
1730
std::optional<int32_t > DataAggregator::parseCommExecEvent () {
0 commit comments