-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdecoupled_bpred_stats.cc
More file actions
1131 lines (983 loc) · 43.1 KB
/
decoupled_bpred_stats.cc
File metadata and controls
1131 lines (983 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <sstream>
#include <tuple>
#include "base/output.hh"
#include "cpu/o3/dyn_inst.hh"
#include "cpu/pred/btb/decoupled_bpred.hh"
#include "debug/BTB.hh"
#include "debug/Profiling.hh"
#include "sim/core.hh"
namespace gem5
{
namespace branch_prediction
{
namespace btb_pred
{
void
DecoupledBPUWithBTB::initDB()
{
bpdb.init_db();
enableBranchTrace = checkGivenSwitch(bpDBSwitches, std::string("basic"));
if (enableBranchTrace) {
std::vector<std::pair<std::string, DataType>> fields_vec = {
std::make_pair("fsqId", UINT64),
std::make_pair("startPC", UINT64),
std::make_pair("controlPC", UINT64),
std::make_pair("controlType", UINT64),
std::make_pair("taken", UINT64),
std::make_pair("mispred", UINT64),
std::make_pair("fallThruPC", UINT64),
std::make_pair("source", UINT64),
std::make_pair("target", UINT64)
};
bptrace = bpdb.addAndGetTrace("BPTRACE", fields_vec);
bptrace->init_table();
removeGivenSwitch(bpDBSwitches, std::string("basic"));
someDBenabled = true;
}
enablePredFSQTrace = checkGivenSwitch(bpDBSwitches, std::string("predfsq"));
if (enablePredFSQTrace) {
// Initialize prediction trace manager for recording predictions
std::vector<std::pair<std::string, DataType>> pred_fields_vec = {
std::make_pair("fsqId", UINT64),
std::make_pair("startPC", UINT64),
std::make_pair("predTaken", UINT64),
std::make_pair("predEndPC", UINT64),
std::make_pair("controlPC", UINT64),
std::make_pair("target", UINT64),
std::make_pair("predSource", UINT64),
std::make_pair("btbHit", UINT64)
};
predTraceManager = bpdb.addAndGetTrace("PREDTRACE", pred_fields_vec);
predTraceManager->init_table();
removeGivenSwitch(bpDBSwitches, std::string("predfsq"));
someDBenabled = true;
}
enablePredFTQTrace = checkGivenSwitch(bpDBSwitches, std::string("predftq"));
if (enablePredFTQTrace) {
std::vector<std::pair<std::string, DataType>> ftq_fields_vec = {
std::make_pair("ftqId", UINT64),
std::make_pair("fsqId", UINT64),
std::make_pair("startPC", UINT64),
std::make_pair("endPC", UINT64),
std::make_pair("takenPC", UINT64),
std::make_pair("taken", UINT64),
std::make_pair("target", UINT64)
};
ftqTraceManager = bpdb.addAndGetTrace("FTQTRACE", ftq_fields_vec);
ftqTraceManager->init_table();
removeGivenSwitch(bpDBSwitches, std::string("predftq"));
someDBenabled = true;
}
// check whether "loop" is in bpDBSwitches
enableLoopDB = checkGivenSwitch(bpDBSwitches, std::string("loop"));
if (enableLoopDB) {
std::vector<std::pair<std::string, DataType>> loop_fields_vec = {
std::make_pair("pc", UINT64),
std::make_pair("target", UINT64),
std::make_pair("mispred", UINT64),
std::make_pair("training", UINT64),
std::make_pair("trainSpecCnt", UINT64),
std::make_pair("trainTripCnt", UINT64),
std::make_pair("trainConf", UINT64),
std::make_pair("inMain", UINT64),
std::make_pair("mainTripCnt", UINT64),
std::make_pair("mainConf", UINT64),
std::make_pair("predSpecCnt", UINT64),
std::make_pair("predTripCnt", UINT64),
std::make_pair("predConf", UINT64)
};
lptrace = bpdb.addAndGetTrace("LOOPTRACE", loop_fields_vec);
lptrace->init_table();
removeGivenSwitch(bpDBSwitches, std::string("loop"));
someDBenabled = true;
}
}
void
DecoupledBPUWithBTB::dumpStats()
{
// Helper function: create output file and write header
auto createOutputFile = [](const std::string& filename, const std::string& header) {
auto handle = simout.create(filename, false, true);
*handle->stream() << header << std::endl;
return handle;
};
// Generic sort function by key
auto sortByKey = [](auto& data, auto keyFn) {
std::sort(data.begin(), data.end(),
[&keyFn](const auto& a, const auto& b) {
return keyFn(a) > keyFn(b);
});
};
// 1. Output top mispredictions
auto outFile = createOutputFile("topMisPredicts.csv", "startPC,control_pc,count");
// topMisPredPC: Vector of mispredict records (startPC, controlPC) -> count
// startPC: Starting address of fetch block
// controlPC: Address of branch instruction
// count: Number of mispredictions
std::vector<std::pair<std::pair<Addr, Addr>, int>> topMisPredPC(
topMispredicts.begin(), topMispredicts.end());
sortByKey(topMisPredPC, [](const auto& entry) { return entry.second; });
for (const auto& entry : topMisPredPC) {
*outFile->stream() << std::hex << entry.first.first << ","
<< entry.first.second << ","
<< std::dec << entry.second << std::endl;
}
simout.close(outFile);
// 2. Output mispredictions by branch
outFile = createOutputFile("topMispredictsByBranch.csv",
"pc,type,mispredicts,total,misPermil,dirMiss,tgtMiss,noPredMiss");
// topMisPredPCByBranch: Detailed misprediction records per branch
std::vector<std::tuple<Addr, int, int, int, double, int, int, int>> topMisPredPCByBranch;
for (const auto &it : topMispredictsByBranch) {
const auto &stats = it.second;
topMisPredPCByBranch.push_back(std::make_tuple(
stats.pc, stats.branchType,
stats.mispredCount, stats.totalCount,
stats.getMispredRate(),
stats.dirWrongCount, stats.targetWrongCount, stats.noPredCount));
}
sortByKey(topMisPredPCByBranch,
[](const auto& entry) {
return std::get<2>(entry); // Sort by mispredCount
});
for (const auto& entry : topMisPredPCByBranch) {
*outFile->stream()
<< std::hex << std::get<0>(entry) << ","
<< std::dec << std::get<1>(entry) << ","
<< std::get<2>(entry) << ","
<< std::get<3>(entry) << ","
<< std::get<4>(entry) << ","
<< std::get<5>(entry) << ","
<< std::get<6>(entry) << ","
<< std::get<7>(entry) << std::endl;
}
simout.close(outFile);
// 3. Sort branches by misrate (per-mille)
outFile = createOutputFile("topMisrateByBranch.csv",
"pc,type,mispredicts,total,misPermil,dirMiss,tgtMiss,noPredMiss");
// Reuse previous data, but sort by misrate
int mispCntThres = 100;
sortByKey(topMisPredPCByBranch, [](const auto& entry) { return std::get<4>(entry); });
for (const auto& entry : topMisPredPCByBranch) {
if (std::get<3>(entry) < mispCntThres) continue;
*outFile->stream() << std::hex << std::get<0>(entry) << std::dec
<< "," << std::get<1>(entry)
<< "," << std::get<2>(entry)
<< "," << std::get<3>(entry)
<< "," << (int)std::get<4>(entry)
<< "," << (int)std::get<5>(entry)
<< "," << (int)std::get<6>(entry)
<< "," << (int)std::get<7>(entry) << std::endl;
}
simout.close(outFile);
// Create CSV header for topN tables
auto createTopNHeader = [](std::ostream& out, int outputTopN, const std::string& prefix) {
out << prefix;
for (int i = 0; i < outputTopN; i++) {
out << ",topMispPC_" << i
<< ",type_" << i
<< ",misCnt_" << i;
}
out << std::endl;
};
// Output phase-classified mispredictions
int outputTopN = 5;
// 4. Phase-based mispredictions
auto processPhaseData = [&](const std::string& filename,
const auto& dataByPhase,
const auto& takenBranches) {
outFile = simout.create(filename, false, true);
auto& out = *outFile->stream();
// Write header
createTopNHeader(out, outputTopN,
filename.find("Sub") != std::string::npos ?
"subPhaseID,numBranches,numEverTakenBranches,totalMispredicts" :
"phaseID,numBranches,numEverTakenBranches,totalMispredicts");
int phaseID = 0;
for (const auto& phaseData : dataByPhase) {
int numStaticBranches = phaseData.size();
int numEverTakenStaticBranches = takenBranches[phaseID].size();
// Copy data and calculate total mispredictions
std::vector<std::pair<BranchKey, BranchStats>> phaseRecords;
for (const auto& record : phaseData) {
phaseRecords.push_back(record);
}
// Calculate total mispredicts
int totalMispredicts = 0;
for (const auto& rec : phaseRecords) {
totalMispredicts += rec.second.mispredCount;
}
// Output phase basic info
out << phaseID << "," << numStaticBranches << ","
<< numEverTakenStaticBranches << "," << totalMispredicts;
// Sort by misprediction count
std::sort(phaseRecords.begin(), phaseRecords.end(),
[](const auto& a, const auto& b) {
return a.second.mispredCount > b.second.mispredCount;
});
// Output top-N
for (int i = 0; i < outputTopN && i < phaseRecords.size(); i++) {
const auto& stats = phaseRecords[i].second;
out << "," << std::hex << stats.pc // pc
<< "," << std::dec << stats.branchType // type
<< "," << stats.mispredCount; // count
}
out << std::dec << std::endl;
phaseID++;
}
simout.close(outFile);
};
processPhaseData("topMispredictByPhase.csv",
topMispredictsByBranchByPhase,
takenBranchesByPhase);
processPhaseData("topMispredictBySubPhase.csv",
topMispredictsByBranchBySubPhase,
takenBranchesBySubPhase);
// 5. Output history misprediction data
outFile = createOutputFile("topMisPredictHist.csv", "Hist,count");
// Vector of (history pattern, count) pairs
std::vector<std::pair<uint64_t, uint64_t>>
topMisPredHistVec(topMispredHist.begin(), topMispredHist.end());
sortByKey(topMisPredHistVec, [](const auto& entry) { return entry.second; });
for (const auto& entry : topMisPredHistVec) {
*outFile->stream() << std::hex << entry.first << ","
<< std::dec << entry.second << std::endl;
}
simout.close(outFile);
// 6. Output indirect mispredictions
outFile = createOutputFile("misPredIndirectStream.csv", "count,address");
// Vector of (address, count) pairs for indirect branches
std::vector<std::pair<Addr, unsigned>>
indirectVec(topMispredIndirect.begin(), topMispredIndirect.end());
sortByKey(indirectVec, [](const auto& entry) { return entry.second; });
for (const auto& entry : indirectVec) {
*outFile->stream() << std::oct << entry.second << ","
<< std::hex << entry.first << std::endl;
}
simout.close(outFile);
// Process FSQ distribution data
auto processFsqDistribution = [&](const std::string& filename,
const auto& distByPhase) {
outFile = simout.create(filename, false, true);
auto& out = *outFile->stream();
// Write header
out << "phaseID";
for (int i = 0; i <= maxInstsNum; i++) {
out << "," << i;
}
out << ",average" << std::endl;
// Write data for each phase
int phaseID = 0;
for (const auto& dist : distByPhase) {
out << phaseID;
int numFsqEntries = 0;
for (int i = 0; i <= maxInstsNum; i++) {
numFsqEntries += dist[i];
}
for (int i = 0; i <= maxInstsNum; i++) {
out << "," << dist[i];
}
out << "," << (double)phaseSizeByInst / (double)numFsqEntries << std::endl;
phaseID++;
}
simout.close(outFile);
};
// 7-8. Output FSQ distribution data
processFsqDistribution("fsqEntryCommittedInstNumDistsByPhase.csv",
fsqEntryNumCommittedInstDistByPhase);
processFsqDistribution("fsqEntryFetchedInstNumDistsByPhase.csv",
fsqEntryNumFetchedInstDistByPhase);
// 9. Output BTB entries
int outputTopNEntries = 1;
std::stringstream headerSS;
headerSS << "phaseID,numBTBEntries";
for (int i = 0; i <= outputTopNEntries; i++) {
headerSS << ",entry_" << i << "_pc,entry_" << i << "_type";
}
outFile = createOutputFile("btbEntriesByPhase.csv", headerSS.str());
int phaseID = 0;
for (auto& phase : BTBEntriesByPhase) {
auto& out = *outFile->stream();
out << std::dec << phaseID << "," << phase.size();
// Vector of (PC, BTBEntry, count) tuples
std::vector<std::tuple<Addr, BTBEntry, int>> btbEntries;
for (auto& entry : phase) {
btbEntries.push_back(std::make_tuple(
entry.first, entry.second.first, entry.second.second));
}
std::sort(btbEntries.begin(), btbEntries.end(),
[](const auto &a, const auto &b) {
return std::get<2>(a) > std::get<2>(b);
});
for (int i = 0; i <= outputTopNEntries && i < btbEntries.size(); i++) {
const auto &entry = btbEntries[i];
out << "," << std::hex << std::get<0>(entry);
// BTBEntry.getType() is not a const method, need to create a copy
BTBEntry btbEntry = std::get<1>(entry);
out << "," << std::dec << btbEntry.getType();
}
out << std::endl;
phaseID++;
}
simout.close(outFile);
// Save the database
if (someDBenabled) {
bpdb.save_db(simout.resolve("bp.db").c_str());
}
}
DecoupledBPUWithBTB::BpTrace::BpTrace(uint64_t fsqId, FetchStream &stream, const DynInstPtr &inst, bool mispred)
{
_tick = curTick();
Addr pc = inst->pcState().instAddr();
const auto &rv_pc = inst->pcState().as<RiscvISA::PCState>();
Addr target = rv_pc.npc();
Addr fallThru = rv_pc.getFallThruPC();
BranchInfo info(pc, target, inst->staticInst, fallThru-pc);
set(fsqId, stream.startPC, pc, info.getType(), inst->branching(), mispred, fallThru, stream.predSource, target);
// for (auto it = _uint64_data.begin(); it != _uint64_data.end(); it++) {
// printf("%s: %ld\n", it->first.c_str(), it->second);
// }
}
namespace {
constexpr std::array<const char*, DecoupledBPUWithBTB::NumBranchClasses>
BranchClassLabels = {
"cond_branch",
"direct_call",
"indirect_call",
"return",
"direct_jump",
"indirect_jump",
"unknown"
};
template <typename InstPtr>
DecoupledBPUWithBTB::BranchClass
classifyBranchImpl(const InstPtr &inst)
{
using BranchClass = DecoupledBPUWithBTB::BranchClass;
if (!inst || !inst->isControl()) {
return BranchClass::Unknown;
}
if (inst->isReturn()) {
return BranchClass::Return;
}
if (inst->isCall()) {
return inst->isIndirectCtrl() ? BranchClass::IndirectCall
: BranchClass::DirectCall;
}
if (inst->isCondCtrl()) {
return BranchClass::CondBranch;
}
if (inst->isIndirectCtrl()) {
return BranchClass::IndirectJump;
}
if (inst->isDirectCtrl() || inst->isUncondCtrl()) {
return BranchClass::DirectJump;
}
return BranchClass::Unknown;
}
} // anonymous namespace
DecoupledBPUWithBTB::DBPBTBStats::DBPBTBStats(
statistics::Group* parent, unsigned numStages, unsigned fsqSize, unsigned maxInstsNum):
statistics::Group(parent),
ADD_STAT(condNum, statistics::units::Count::get(), "the number of cond branches"),
ADD_STAT(uncondNum, statistics::units::Count::get(), "the number of uncond branches"),
ADD_STAT(returnNum, statistics::units::Count::get(), "the number of return branches"),
ADD_STAT(otherNum, statistics::units::Count::get(), "the number of other branches"),
ADD_STAT(condMiss, statistics::units::Count::get(), "the number of cond branch misses"),
ADD_STAT(uncondMiss, statistics::units::Count::get(), "the number of uncond branch misses"),
ADD_STAT(returnMiss, statistics::units::Count::get(), "the number of return branch misses"),
ADD_STAT(otherMiss, statistics::units::Count::get(), "the number of other branch misses"),
ADD_STAT(branchClassCounts, statistics::units::Count::get(), "branch counts by fine-grained class"),
ADD_STAT(branchClassMisses, statistics::units::Count::get(), "branch mispredictions by fine-grained class"),
ADD_STAT(branchClassCountsTotal, statistics::units::Count::get(), "total number of classified branches"),
ADD_STAT(controlSquashByClass, statistics::units::Count::get(), "commit/resolve-path squashes by branch class"),
ADD_STAT(staticBranchNum, statistics::units::Count::get(), "the number of all (different) static branches"),
ADD_STAT(staticBranchNumEverTaken, statistics::units::Count::get(), "the number of all (different) static branches that are once taken"),
ADD_STAT(predsOfEachStage, statistics::units::Count::get(), "the number of preds of each stage that account for final pred"),
ADD_STAT(overrideBubbleNum, statistics::units::Count::get(), "the number of override bubbles"),
ADD_STAT(overrideCount, statistics::units::Count::get(), "the number of overrides"),
ADD_STAT(commitPredsFromEachStage, statistics::units::Count::get(),
"the number of preds of each stage that account for a committed stream"),
ADD_STAT(commitOverrideBubbleNum, statistics::units::Count::get(),
"the number of override bubbles, on the commit path"),
ADD_STAT(commitOverrideCount, statistics::units::Count::get(), "the number of overrides, on the commit path"),
ADD_STAT(overrideFallThruMismatch, statistics::units::Count::get(),
"Number of overrides due to validity mismatches, on commit path"),
ADD_STAT(overrideControlAddrMismatch, statistics::units::Count::get(),
"Number of overrides due to control address mismatches, on commit path"),
ADD_STAT(overrideTargetMismatch, statistics::units::Count::get(),
"Number of overrides due to target mismatches, on commit path"),
ADD_STAT(overrideEndMismatch, statistics::units::Count::get(),
"Number of overrides due to end address mismatches, on commit path"),
ADD_STAT(overrideHistInfoMismatch, statistics::units::Count::get(),
"Number of overrides due to history info mismatches, on commit path"),
ADD_STAT(fsqEntryDist, statistics::units::Count::get(), "the distribution of number of entries in fsq"),
ADD_STAT(fsqEntryEnqueued, statistics::units::Count::get(), "the number of fsq entries enqueued"),
ADD_STAT(fsqEntryCommitted, statistics::units::Count::get(), "the number of fsq entries committed at last"),
ADD_STAT(controlSquashFromDecode, statistics::units::Count::get(), "the number of control squashes in bpu from decode"),
ADD_STAT(controlSquashFromCommit, statistics::units::Count::get(), "the number of control squashes in bpu from commit"),
ADD_STAT(nonControlSquash, statistics::units::Count::get(), "the number of non-control squashes in bpu"),
ADD_STAT(trapSquash, statistics::units::Count::get(), "the number of trap squashes in bpu"),
ADD_STAT(ftqNotValid, statistics::units::Count::get(), "fetch needs ftq req but ftq not valid"),
ADD_STAT(fsqNotValid, statistics::units::Count::get(), "ftq needs fsq req but fsq not valid"),
ADD_STAT(fsqFullCannotEnq, statistics::units::Count::get(), "bpu has req but fsq full cannot enqueue"),
ADD_STAT(ftqFullCannotEnq, statistics::units::Count::get(), "fsq has entry but ftq full cannot enqueue"),
ADD_STAT(fsqFullFetchHungry, statistics::units::Count::get(), "fetch hungry when fsq full and bpu cannot enqueue"),
ADD_STAT(fsqEmpty, statistics::units::Count::get(), "fsq is empty"),
ADD_STAT(commitFsqEntryHasInsts, statistics::units::Count::get(), "number of insts that commit fsq entries have"),
ADD_STAT(commitFsqEntryFetchedInsts, statistics::units::Count::get(), "number of insts that commit fsq entries fetched"),
ADD_STAT(commitFsqEntryOnlyHasOneJump, statistics::units::Count::get(), "number of fsq entries with only one instruction (jump)"),
ADD_STAT(btbHit, statistics::units::Count::get(), "btb hits (in predict block)"),
ADD_STAT(btbMiss, statistics::units::Count::get(), "btb misses (in predict block)"),
ADD_STAT(btbEntriesWithDifferentStart, statistics::units::Count::get(), "number of btb entries with different start PC"),
ADD_STAT(btbEntriesWithOnlyOneJump, statistics::units::Count::get(), "number of btb entries with different start PC starting with a jump"),
ADD_STAT(predFalseHit, statistics::units::Count::get(), "false hit detected at pred"),
ADD_STAT(commitFalseHit, statistics::units::Count::get(), "false hit detected at commit"),
ADD_STAT(predictionBlockedForUpdate, statistics::units::Count::get(), "prediction blocked for update priority"),
ADD_STAT(s1PredWrongFallthrough, statistics::units::Count::get(), "S1pred wrong full throughs"),
ADD_STAT(s1PredWrongUbtb, statistics::units::Count::get(),"S1pred wrong using ubtb "),
ADD_STAT(s1PredWrongAbtb, statistics::units::Count::get(), "S1pred wrong using abtb "),
ADD_STAT(s3PredWrongMbtb, statistics::units::Count::get(), "S3pred wrong blame mbtb "),
ADD_STAT(s3PredWrongTage, statistics::units::Count::get(), "S3pred wrong blame tage "),
ADD_STAT(s3PredWrongIttage, statistics::units::Count::get(), "S3pred wrong blame ittage "),
ADD_STAT(s3PredWrongRas, statistics::units::Count::get(), "S3pred wrong blame ras "),
ADD_STAT(fetch2Attempts, statistics::units::Count::get(), "Number of 2fetch attempts"),
ADD_STAT(fetch2Successes, statistics::units::Count::get(), "Number of successful 2fetch cycles"),
ADD_STAT(fetch2SpanTooLarge, statistics::units::Count::get(), "Rejected due to span > maxFetchBytes"),
ADD_STAT(fetch2NoNextFTQ, statistics::units::Count::get(), "Rejected due to no next FTQ entry"),
ADD_STAT(fetch2FirstNotTaken, statistics::units::Count::get(), "Rejected due to current FTQ is not taken"),
ADD_STAT(fetch2FirstNotAtStart, statistics::units::Count::get(), "Rejected due to PC is != next FTQ start")
{
predsOfEachStage.init(numStages);
commitPredsFromEachStage.init(numStages+1);
commitOverrideBubbleNum = commitPredsFromEachStage[1] + 2 * commitPredsFromEachStage[2] ;
commitOverrideCount = commitPredsFromEachStage[1] + commitPredsFromEachStage[2];
fsqEntryDist.init(0, fsqSize, 20).flags(statistics::total);
commitFsqEntryHasInsts.init(0, maxInstsNum >> 1, 1);
commitFsqEntryFetchedInsts.init(0, maxInstsNum >> 1, 1);
branchClassCounts.init(NumBranchClasses);
branchClassMisses.init(NumBranchClasses);
controlSquashByClass.init(NumBranchClasses);
for (size_t i = 0; i < NumBranchClasses; ++i) {
branchClassCounts.subname(i, BranchClassLabels[i]);
branchClassMisses.subname(i, BranchClassLabels[i]);
controlSquashByClass.subname(i, BranchClassLabels[i]);
}
}
void DecoupledBPUWithBTB::overrideStats(OverrideReason overrideReason)
{
// Track specific override reasons for statistics
switch (overrideReason) {
case OverrideReason::FALL_THRU:
dbpBtbStats.overrideFallThruMismatch++;
break;
case OverrideReason::CONTROL_ADDR:
dbpBtbStats.overrideControlAddrMismatch++;
break;
case OverrideReason::TARGET:
dbpBtbStats.overrideTargetMismatch++;
break;
case OverrideReason::END:
dbpBtbStats.overrideEndMismatch++;
break;
case OverrideReason::HIST_INFO:
dbpBtbStats.overrideHistInfoMismatch++;
break;
default:
break;
}
}
void
DecoupledBPUWithBTB::processFetchDistributions(std::vector<int> ¤tPhaseCommittedDist,
std::vector<int> ¤tPhaseFetchedDist)
{
// Initialize distributions with zeros
currentPhaseCommittedDist.resize(maxInstsNum+1, 0);
currentPhaseFetchedDist.resize(maxInstsNum+1, 0);
// Calculate the difference between current and last phase values
for (int i = 0; i <= maxInstsNum; i++) {
currentPhaseCommittedDist[i] = commitFsqEntryHasInstsVector[i] -
lastPhaseFsqEntryNumCommittedInstDist[i];
lastPhaseFsqEntryNumCommittedInstDist[i] = commitFsqEntryHasInstsVector[i];
currentPhaseFetchedDist[i] = commitFsqEntryFetchedInstsVector[i] -
lastPhaseFsqEntryNumFetchedInstDist[i];
lastPhaseFsqEntryNumFetchedInstDist[i] = commitFsqEntryFetchedInstsVector[i];
}
}
std::unordered_map<Addr, std::pair<BTBEntry, int>>
DecoupledBPUWithBTB::processBTBEntries()
{
std::unordered_map<Addr, std::pair<BTBEntry, int>> currentPhaseBTBEntries;
// Process each BTB entry
for (auto &it : totalBTBEntries) {
auto &entry = it.second.first;
auto visit_cnt = it.second.second;
// Check if this entry was already present in last phase
auto last_it = lastPhaseBTBEntries.find(it.first);
if (last_it != lastPhaseBTBEntries.end()) {
visit_cnt -= last_it->second.second;
}
// Only add entries with new visits in this phase
if (visit_cnt > 0) {
currentPhaseBTBEntries[it.first] = std::make_pair(entry, visit_cnt);
}
}
// Update last phase BTB entries for next time
lastPhaseBTBEntries = totalBTBEntries;
return currentPhaseBTBEntries;
}
bool
DecoupledBPUWithBTB::processPhase(bool isSubPhase, int phaseID, int &phaseToDump,
BranchStatsMap &lastPhaseStats,
std::vector<BranchStatsMap> &phaseStatsList,
std::unordered_map<Addr, int> ¤tPhaseBranches,
std::vector<std::unordered_map<Addr, int>> &phaseBranchesList)
{
// Check if this phase should be processed
if (phaseToDump > phaseID) {
return false;
}
// Debug output
DPRINTF(Profiling, "dump %s phase %d\n",
isSubPhase ? "sub" : "main", phaseToDump);
// Create map for current phase statistics
BranchStatsMap currentPhaseStats;
// Process each branch in the global statistics
for (auto &it : topMispredictsByBranch) {
const auto &key = it.first;
const auto &stats = it.second;
// Find stats from last phase
auto lastIt = lastPhaseStats.find(key);
// If branch exists in last phase, calculate difference
if (lastIt != lastPhaseStats.end()) {
const auto &lastStats = lastIt->second;
// Skip branches with no new executions
if (stats.totalCount <= lastStats.totalCount) {
continue;
}
// Create stats for current phase (delta from last phase)
BranchStats phaseStats(stats.pc, stats.branchType);
phaseStats.totalCount = stats.totalCount - lastStats.totalCount;
phaseStats.mispredCount = stats.mispredCount - lastStats.mispredCount;
phaseStats.dirWrongCount = stats.dirWrongCount - lastStats.dirWrongCount;
phaseStats.targetWrongCount = stats.targetWrongCount - lastStats.targetWrongCount;
phaseStats.noPredCount = stats.noPredCount - lastStats.noPredCount;
currentPhaseStats[key] = phaseStats;
} else {
// This is a new branch in this phase
currentPhaseStats[key] = stats;
}
}
// Store the processed data
lastPhaseStats = topMispredictsByBranch;
phaseStatsList.push_back(currentPhaseStats);
// Handle taken branches map
phaseBranchesList.push_back(currentPhaseBranches);
currentPhaseBranches.clear();
// Increment phase counter for next time
phaseToDump++;
return true;
}
void
DecoupledBPUWithBTB::dumpFsq(const char *when)
{
DPRINTF(DecoupleBPProbe, "dumping fsq entries %s...\n", when);
for (auto it = fetchStreamQueue.begin(); it != fetchStreamQueue.end();
it++) {
DPRINTFR(DecoupleBPProbe, "StreamID %lu, ", it->first);
printStream(it->second);
}
}
DecoupledBPUWithBTB::BranchClass
DecoupledBPUWithBTB::classifyBranch(const DynInstPtr &inst) const
{
return classifyBranchImpl(inst);
}
DecoupledBPUWithBTB::BranchClass
DecoupledBPUWithBTB::classifyBranch(const StaticInstPtr &inst) const
{
return classifyBranchImpl(inst);
}
const char *
DecoupledBPUWithBTB::branchClassName(BranchClass cls)
{
auto idx = static_cast<size_t>(cls);
if (idx < BranchClassLabels.size()) {
return BranchClassLabels[idx];
}
return "invalid";
}
void
DecoupledBPUWithBTB::addBranchClassStat(BranchClass cls, bool mispred)
{
auto idx = static_cast<size_t>(cls);
if (idx >= NumBranchClasses) {
DPRINTF(DBPBTBStats, "Skip invalid branch class stats update %d\n",
static_cast<int>(cls));
return;
}
dbpBtbStats.branchClassCounts[idx]++;
if (mispred) {
dbpBtbStats.branchClassMisses[idx]++;
dbpBtbStats.branchClassCountsTotal++;
}
DPRINTF(DBPBTBStats, "Branch classified as %s, mispred=%d\n",
branchClassName(cls), mispred);
}
void
DecoupledBPUWithBTB::addControlSquashCommitStat(BranchClass cls)
{
auto idx = static_cast<size_t>(cls);
if (idx >= NumBranchClasses) {
DPRINTF(DBPBTBStats,
"Skip invalid commit squash class stats update %d\n",
static_cast<int>(cls));
return;
}
dbpBtbStats.controlSquashByClass[idx]++;
DPRINTF(DBPBTBStats, "Commit squash classified as %s\n",
branchClassName(cls));
}
void
DecoupledBPUWithBTB::updateStatistics(const FetchStream &stream)
{
// Check if this stream was mispredicted
bool miss_predicted = stream.squashType == SQUASH_CTRL;
// Track indirect mispredictions
if (miss_predicted && stream.exeBranchInfo.isIndirect) {
topMispredIndirect[stream.startPC]++;
}
// --- BTB Statistics ---
if (stream.isHit) {
// Count BTB hits
dbpBtbStats.btbHit++;
} else {
// Count BTB misses for taken branches
if (stream.exeTaken) {
dbpBtbStats.btbMiss++;
DPRINTF(BTB, "BTB miss detected when update, stream start %#lx, predTick %lu, printing branch info:\n",
stream.startPC, stream.predTick);
auto &slot = stream.exeBranchInfo;
DPRINTF(BTB, " pc:%#lx, size:%d, target:%#lx, cond:%d, indirect:%d, call:%d, return:%d\n",
slot.pc, slot.size, slot.target, slot.isCond, slot.isIndirect, slot.isCall, slot.isReturn);
}
// Count false hits
if (stream.falseHit) {
dbpBtbStats.commitFalseHit++;
}
}
if (stream.isHit || stream.exeTaken) {
// Update BTB entry statistics
auto it = totalBTBEntries.find(stream.startPC);
if (it == totalBTBEntries.end()) {
auto &btb_entry = stream.updateNewBTBEntry;
totalBTBEntries[stream.startPC] = std::make_pair(btb_entry, 1);
dbpBtbStats.btbEntriesWithDifferentStart++;
} else {
it->second.second++;
it->second.first = stream.updateNewBTBEntry;
}
}
// Track which predictor stage was used
dbpBtbStats.commitPredsFromEachStage[stream.predSource]++;
overrideStats(stream.overrideReason);
// --- Instruction Statistics ---
// Track committed instruction counts
dbpBtbStats.commitFsqEntryHasInsts.sample(stream.commitInstNum, 1);
if (stream.commitInstNum >= 0 && stream.commitInstNum <= maxInstsNum) {
commitFsqEntryHasInstsVector[stream.commitInstNum]++;
if (stream.commitInstNum == 1 && stream.exeBranchInfo.isUncond()) {
dbpBtbStats.commitFsqEntryOnlyHasOneJump++;
}
}
// Track fetched instruction counts
dbpBtbStats.commitFsqEntryFetchedInsts.sample(stream.fetchInstNum, 1);
if (stream.fetchInstNum >= 0 && stream.fetchInstNum <= maxInstsNum) {
commitFsqEntryFetchedInstsVector[stream.fetchInstNum]++;
}
// --- Misprediction Statistics ---
// Track control squashes (mispredictions)
if (stream.squashType == SQUASH_CTRL) {
// Record mispredict pair (start PC, branch PC)
auto find_it = topMispredicts.find(std::make_pair(stream.startPC, stream.exeBranchInfo.pc));
if (find_it == topMispredicts.end()) {
topMispredicts[std::make_pair(stream.startPC, stream.exeBranchInfo.pc)] = 1;
} else {
find_it->second++;
}
// Track history pattern for mispredictions
auto hist(stream.history);
hist.resize(18);
uint64_t pattern = hist.to_ulong();
auto find_it_hist = topMispredHist.find(pattern);
if (find_it_hist == topMispredHist.end()) {
topMispredHist[pattern] = 1;
} else {
find_it_hist->second++;
}
}
}
void
DecoupledBPUWithBTB::commitBranch(const DynInstPtr &inst, bool mispred)
{
// ---------- Update overall branch statistics ----------
if (inst->isUncondCtrl()) {
addCfi(UNCOND, mispred);
}
if (inst->isCondCtrl()) {
addCfi(COND, mispred);
}
if (inst->isReturn()) {
addCfi(RETURN, mispred);
} else if (inst->isIndirectCtrl()) {
addCfi(OTHER, mispred);
}
auto branchClass = classifyBranch(inst);
addBranchClassStat(branchClass, mispred);
// ---------- Find corresponding fetch stream entry ----------
auto streamIt = fetchStreamQueue.find(inst->fsqId);
assert(streamIt != fetchStreamQueue.end());
auto entry = streamIt->second;
// Record branch trace if enabled
if (enableBranchTrace) {
bptrace->write_record(BpTrace(streamIt->first, entry, inst, mispred));
}
// ---------- Extract branch information ----------
Addr branchAddr = inst->pcState().instAddr();
const auto &rv_pc = inst->pcState().as<RiscvISA::PCState>();
Addr targetAddr = rv_pc.npc();
Addr fallThruPC = rv_pc.getFallThruPC();
BranchInfo info(branchAddr, targetAddr, inst->staticInst, fallThruPC-branchAddr);
bool taken = rv_pc.branching() || inst->isUncondCtrl();
// ---------- Process misprediction and update statistics ----------
processMisprediction(entry, branchAddr, info, taken, mispred);
// ---------- Track taken branches for statistics ----------
if (taken) {
trackTakenBranch(branchAddr);
}
// ---------- Update predictor components ----------
for (auto component : components) {
component->commitBranch(entry, inst);
}
//here add final counter
if (mispred) {
commitPredWrongSource(entry);
}
}
void
DecoupledBPUWithBTB::commitPredWrongSource(const FetchStream &entry)
{
int ubtbid = ubtb->getComponentIdx();
int abtbid = abtb->getComponentIdx();
int mbtbid = mbtb->getComponentIdx();
int tageid = tage->getComponentIdx();
int ittageid = ittage->getComponentIdx();
int rasid = ras->getComponentIdx();
int s1PredSource = entry.s1Source;
int s3PredSource = entry.s3Source;
auto exeBranchInfo = entry.exeBranchInfo;
bool onlyDirectionWrong = entry.exeTaken != entry.predTaken;
assert(s1PredSource < mbtbid);
if (s1PredSource == ubtbid) {
dbpBtbStats.s1PredWrongUbtb++;
} else if (s1PredSource == abtbid) {
dbpBtbStats.s1PredWrongAbtb++;
}else {
dbpBtbStats.s1PredWrongFallthrough++;
}
if (s3PredSource == rasid) {
if (exeBranchInfo.isCond) {
dbpBtbStats.s3PredWrongTage++;
} else if (exeBranchInfo.isReturn) {
dbpBtbStats.s3PredWrongRas++;
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
} else if (s3PredSource == ittageid) {
if (exeBranchInfo.isIndirect) {
dbpBtbStats.s3PredWrongIttage++;
} else if (exeBranchInfo.isCond) {
dbpBtbStats.s3PredWrongTage++;
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
} else if (s3PredSource == tageid) {
if (exeBranchInfo.isCond) {
if (onlyDirectionWrong) {
dbpBtbStats.s3PredWrongTage++;
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
}else if (s3PredSource == mbtbid) {
if (exeBranchInfo.isCond) {
if (onlyDirectionWrong) {
dbpBtbStats.s3PredWrongTage++;
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
} else if (exeBranchInfo.isIndirect) {
dbpBtbStats.s3PredWrongIttage++;
} else {
dbpBtbStats.s3PredWrongMbtb++;
}
}else if (s3PredSource == -1) {
dbpBtbStats.s3PredWrongMbtb++;
}
}
/**
* @brief Handle instruction commits and phase-based statistics
*
* This function is called whenever an instruction is committed. It updates
* instruction counts and maintains phase-based statistics. When a phase
* boundary is reached, it collects detailed statistics for the phase.
*/
void
DecoupledBPUWithBTB::notifyInstCommit(const DynInstPtr &inst)
{
// Update committed instruction count for stream
auto it = fetchStreamQueue.find(inst->fsqId);
assert(it != fetchStreamQueue.end());
it->second.commitInstNum++;
// Update global committed instruction count
numInstCommitted++;
DPRINTF(Profiling, "notifyInstCommit, inst=%s, commitInstNum=%d\n",
inst->staticInst->disassemble(inst->pcState().instAddr()),
it->second.commitInstNum);
// ----------------------- Main Phase Processing -------------------------
if (numInstCommitted % phaseSizeByInst == 0) {
int currentPhaseID = numInstCommitted / phaseSizeByInst;
// Process main phase statistics if needed
if (processPhase(false, currentPhaseID, phaseIdToDump,
lastPhaseTopMispredictsByBranch,
topMispredictsByBranchByPhase,
currentPhaseTakenBranches,
takenBranchesByPhase)) {
// Process fetch instruction distributions
std::vector<int> committedInstDist, fetchedInstDist;
processFetchDistributions(committedInstDist, fetchedInstDist);
// Store the distributions
fsqEntryNumCommittedInstDistByPhase.push_back(committedInstDist);
fsqEntryNumFetchedInstDistByPhase.push_back(fetchedInstDist);
// Process BTB entries
BTBEntriesByPhase.push_back(processBTBEntries());
}
}
// ---------------------- Sub-Phase Processing --------------------------
if (numInstCommitted % subPhaseSizeByInst() == 0) {