Skip to content

Commit e46fe3b

Browse files
CaoJiaming776Cao Jiaming
andauthored
cpu-o3:Implement predwrongSource method (#683)
* cpu-o3:Implement predwrongSource method Change-Id: If763f1cc4fd54da740fde677d0ded7801ecee023 * cpu-o3: update source handling in BTBEntry and prediction logic Change-Id: I78091441671e564579c3e81f06daf51414a095fc * cpu-o3: add statistic for wrong predictions in uBTB Change-Id: I787f662435b5ab49b0f7103f7d9b7fbecbcfe270 * cpu-o3: align source handling in BTB entries and update prediction statistics * cpu-o3: align formatting in BTB entry handling Change-Id: Ie89cd4a5eeb41ce8347eb013402ef823c697626e * cpu-o3: remove unnecessary assertion for wrong branch source Change-Id: Ieab9cfbd9c70c0b5cc7885c39fc313eeaac9717a * cpu-o3: correct delay condition and update fallthrough statistics Change-Id: Ied49f642bc88a6be31bdf51d3f6de608f8661910 * cpu-o3: add handling for wrong source predictions in commit Change-Id: I9d8c591468d5af33145f58ff80762ae95c69840e * cpu-o3: removing unused s3Source in prediction structures Change-Id: I39d8808a2c9467f5362739623a09433e7a1559df * cpu-o3: align source prediction handling across multiple componen Change-Id: I9ccb31f8e110c4ba13d53aad7dd3bbdbd6ba9358 * cpu-o3: remove predwrongSource methods and associated statistics from various predictors * cpu-o3: enhance source prediction alignment Change-Id: Ic8a5757f4f87dbaf448df998fcd714e46f80d9a8 * cpu-o3: use const references Change-Id: If8e15a0d38292a384106d88d8495b06cb6ea1589 --------- Co-authored-by: Cao Jiaming <caojiaming@bosc.ac.cn>
1 parent ecce168 commit e46fe3b

12 files changed

Lines changed: 209 additions & 16 deletions

src/cpu/pred/btb/abtb.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,22 @@ AheadBTB::fillStagePredictions(const std::vector<TickedBTBEntry>& entries,
213213
ubtb_pred_entry = stagePreds[0].btbEntries[0];
214214
assert(ubtb_pred_entry.valid);
215215
mixed_entries = entries;
216+
for (auto &entry : mixed_entries) {
217+
if (entry.pc == ubtb_pred_entry.pc) {
218+
entry.valid = false; // invalidate duplicated entry from aBTB (only use for align counter)
219+
break;
220+
}
221+
}
216222
mixed_entries.push_back(TickedBTBEntry(ubtb_pred_entry, curTick()));
217223
// Deduplicate entries by pc (order can change)
218224
std::sort(mixed_entries.begin(), mixed_entries.end(),
219225
[](const TickedBTBEntry& a, const TickedBTBEntry& b) {
220226
return a.pc < b.pc;
221227
});
222-
// Remove duplicates
223-
mixed_entries.erase(std::unique(mixed_entries.begin(), mixed_entries.end(),
224-
[](const TickedBTBEntry& a, const TickedBTBEntry& b) {
225-
return a.pc == b.pc;
228+
// Drop entries invalidated during deduplication above
229+
mixed_entries.erase(std::remove_if(mixed_entries.begin(), mixed_entries.end(),
230+
[](const TickedBTBEntry& entry) {
231+
return !entry.valid;
226232
}),
227233
mixed_entries.end());
228234
// return;
@@ -596,6 +602,7 @@ AheadBTB::updateUsingS3Pred(FullBTBPrediction &s3Pred, const Addr previousPC)
596602
BranchInfo takenbranchinfo;
597603
takenbranchinfo.pc = s3Pred.getTakenEntry().pc;
598604
takenbranchinfo.target = s3Pred.getTakenEntry().target;
605+
entry.source = getComponentIdx(); // mark the entry source as AheadBTB
599606

600607
updateBTBEntry(btb_idx, btb_tag, entry, takenbranchinfo, s3Pred.isTaken());
601608
}
@@ -671,6 +678,7 @@ AheadBTB::update(const FetchStream &stream)
671678
return;
672679
}
673680
Addr btb_idx = getIndex(previousPC); // use last pc to get idx
681+
entry.source = getComponentIdx(); // mark the entry source as AheadBTB
674682
updateBTBEntry(btb_idx, btb_tag, entry, stream.exeBranchInfo, stream.exeTaken);
675683
}
676684
}
@@ -700,7 +708,9 @@ AheadBTB::getPreviousPC(const FetchStream &stream)
700708
}
701709
}
702710

711+
703712
#ifndef UNIT_TEST
713+
704714
void
705715
AheadBTB::commitBranch(const FetchStream &stream, const DynInstPtr &inst)
706716
{

src/cpu/pred/btb/btb_ittage.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ BTBITTAGE::lookupHelper(Addr startAddr, const std::vector<BTBEntry> &btbEntries,
149149
ittageStats.predTableHits.sample(main_info.table, 1);
150150
}
151151
// Note: predTargetHit will be updated in the update phase when we know the actual target
152-
153152
TagePrediction pred(btb_entry.pc, main_info, alt_info, use_alt, main_target);
154153
meta->preds[btb_entry.pc] = pred;
155154
}
@@ -501,6 +500,21 @@ BTBITTAGE::doUpdateHist(const boost::dynamic_bitset<> &history, bool taken, Addr
501500
}
502501
}
503502

503+
bool
504+
BTBITTAGE::tageHit()
505+
{
506+
auto meta = getPredictionMeta();
507+
auto preds = std::static_pointer_cast<TageMeta>(meta)->preds;
508+
bool hit = false;
509+
for (auto & [pc, pred] : preds) {
510+
if (pred.mainInfo.found) {
511+
hit = true;
512+
break;
513+
}
514+
}
515+
return hit;
516+
}
517+
504518
/**
505519
* @brief Updates branch history for speculative execution
506520
*

src/cpu/pred/btb/btb_ittage.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class BTBITTAGE : public TimedBaseBTBPredictor
141141
// Update branch history
142142
void doUpdateHist(const bitset &history, bool taken, Addr pc, Addr target);
143143

144-
145144
const unsigned numPredictors;
146145

147146
std::vector<unsigned> tableSizes;
@@ -271,6 +270,7 @@ public:
271270
bool debugFlag = false;
272271

273272
void recoverFoldedHist(const bitset& history);
273+
bool tageHit();
274274

275275
// void checkFoldedHist(const bitset& history);
276276
};

src/cpu/pred/btb/btb_tage.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ BTBTAGE::getLRUVictim(int table, Addr index)
11301130
}
11311131

11321132
#ifndef UNIT_TEST
1133+
11331134
void
11341135
BTBTAGE::commitBranch(const FetchStream &stream, const DynInstPtr &inst)
11351136
{

src/cpu/pred/btb/btb_tage.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ class BTBTAGE : public TimedBaseBTBPredictor
101101
bool taken; // Final prediction (taken/not taken) = use_alt ? alt_provided ? alt_taken : base_taken : main_taken
102102
bool altPred; // Alternative prediction = alt_provided ? alt_taken : base_taken;
103103

104+
104105
TagePrediction() : btb_pc(0), useAlt(false), taken(false), altPred(false) {}
105106

106107
TagePrediction(Addr btb_pc, TageTableInfo mainInfo, TageTableInfo altInfo,
107108
bool useAlt, bool taken, bool altPred) :
108109
btb_pc(btb_pc), mainInfo(mainInfo), altInfo(altInfo),
109-
useAlt(useAlt), taken(taken), altPred(altPred) {}
110+
useAlt(useAlt), taken(taken), altPred(altPred){}
110111
};
111112

112113

@@ -374,6 +375,8 @@ class BTBTAGE : public TimedBaseBTBPredictor
374375
Scalar predHit;
375376
Scalar predMiss;
376377

378+
Scalar s3PredwrongTage;
379+
377380
int bankIdx;
378381
int numPredictors;
379382
int numBanks;

src/cpu/pred/btb/btb_ubtb.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ UBTB::updateUsingS3Pred(FullBTBPrediction &s3Pred)
212212
}
213213
auto startAddr = s3Pred.bbStart;
214214
UBTBIter oldEntryIter = lastPred.hit_entry;
215+
takenEntry.source = getComponentIdx();
215216
updateNewEntry(oldEntryIter, takenEntry, startAddr);
216217

217218
}

src/cpu/pred/btb/decoupled_bpred.cc

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ DecoupledBPUWithBTB::tick()
182182
bpuState = BpuState::IDLE;
183183
}
184184

185-
186185
// Decrement override bubbles counter
187186
if (numOverrideBubbles > 0) {
188187
numOverrideBubbles--;
@@ -246,6 +245,53 @@ DecoupledBPUWithBTB::generateFinalPredAndCreateBubbles()
246245
// Store the chosen prediction as our final prediction
247246
finalPred = *chosenPrediction;
248247

248+
finalPred.s1Source = -1;//meaning fallthrough
249+
finalPred.s3Source = -1;
250+
251+
if (predsOfEachStage[0].btbEntries.size() != 0) {
252+
for (auto entry : predsOfEachStage[0].btbEntries){
253+
if (entry.isIndirect || entry.isDirect || entry.ctr >= 0 ||entry.alwaysTaken){
254+
finalPred.s1Source = entry.source;
255+
break;
256+
}
257+
}
258+
}
259+
260+
bool found_s3_taken = false;
261+
bool na_s3_taken_but_have_cond = false;
262+
263+
for (BTBEntry entry : predsOfEachStage[2].btbEntries) {
264+
if (entry.isDirect || entry.isIndirect || entry.ctr >= 0 || entry.alwaysTaken) {
265+
found_s3_taken = true;
266+
}else if (entry.isCond){
267+
//only use when there's no taken prediction in s3
268+
na_s3_taken_but_have_cond = true;
269+
}
270+
}
271+
272+
if (found_s3_taken) {
273+
auto pred_taken_entry = finalPred.getTakenEntry();
274+
if (pred_taken_entry.valid) {
275+
if (pred_taken_entry.isReturn) {
276+
finalPred.s3Source = ras->getComponentIdx();
277+
} else if (pred_taken_entry.isIndirect && ittage->tageHit()) {
278+
finalPred.s3Source = ittage->getComponentIdx();
279+
}else if (pred_taken_entry.isCond) {
280+
finalPred.s3Source = tage->getComponentIdx();
281+
} else {
282+
finalPred.s3Source = mbtb->getComponentIdx();
283+
}
284+
}else {
285+
if (na_s3_taken_but_have_cond) {
286+
finalPred.s3Source = tage->getComponentIdx();
287+
}else {
288+
finalPred.s3Source = -1;
289+
}
290+
}
291+
}
292+
293+
294+
249295
// 3. Calculate override bubbles needed for pipeline consistency
250296
// Override bubbles are needed when earlier stages predict differently from later stages
251297
unsigned first_hit_stage = 0;
@@ -1011,6 +1057,9 @@ DecoupledBPUWithBTB::createFetchStreamEntry()
10111057
entry.predSource = finalPred.predSource;
10121058
entry.overrideReason = finalPred.overrideReason;
10131059

1060+
entry.s1Source = finalPred.s1Source;
1061+
entry.s3Source = finalPred.s3Source;
1062+
10141063
// Save predictors' metadata
10151064
for (int i = 0; i < numComponents; i++) {
10161065
entry.predMetas[i] = components[i]->getPredictionMeta();

src/cpu/pred/btb/decoupled_bpred.hh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ class DecoupledBPUWithBTB : public BPredUnit
316316
// Fine-grained branch classification statistics
317317
statistics::Vector branchClassCounts; ///< Classified branch occurrences
318318
statistics::Vector branchClassMisses; ///< Mispredictions per class
319+
statistics::Scalar branchClassCountsTotal; ///< Total classified branches
319320
statistics::Vector controlSquashByClass; ///< Commit/Resolve-path squashes per class
320321

321322
// Branch coverage statistics
@@ -368,6 +369,14 @@ class DecoupledBPUWithBTB : public BPredUnit
368369
// Window blocking statistics
369370
statistics::Scalar predictionBlockedForUpdate; // Times prediction was blocked for update priority
370371

372+
statistics::Scalar s1PredWrongFallthrough;
373+
statistics::Scalar s1PredWrongUbtb;
374+
statistics::Scalar s1PredWrongAbtb;
375+
statistics::Scalar s3PredWrongMbtb;
376+
statistics::Scalar s3PredWrongTage;
377+
statistics::Scalar s3PredWrongIttage;
378+
statistics::Scalar s3PredWrongRas;
379+
371380
DBPBTBStats(statistics::Group* parent, unsigned numStages, unsigned fsqSize, unsigned maxInstsNum);
372381
} dbpBtbStats;
373382

@@ -908,6 +917,9 @@ class DecoupledBPUWithBTB : public BPredUnit
908917
*/
909918
void commitBranch(const DynInstPtr &inst, bool miss);
910919

920+
921+
void commitPredWrongSource(const FetchStream &entry);
922+
911923
/**
912924
* @brief Process branch misprediction, determine type and update statistics
913925
*

src/cpu/pred/btb/decoupled_bpred_stats.cc

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ DecoupledBPUWithBTB::DBPBTBStats::DBPBTBStats(
454454
ADD_STAT(otherMiss, statistics::units::Count::get(), "the number of other branch misses"),
455455
ADD_STAT(branchClassCounts, statistics::units::Count::get(), "branch counts by fine-grained class"),
456456
ADD_STAT(branchClassMisses, statistics::units::Count::get(), "branch mispredictions by fine-grained class"),
457+
ADD_STAT(branchClassCountsTotal, statistics::units::Count::get(), "total number of classified branches"),
457458
ADD_STAT(controlSquashByClass, statistics::units::Count::get(), "commit/resolve-path squashes by branch class"),
458459
ADD_STAT(staticBranchNum, statistics::units::Count::get(), "the number of all (different) static branches"),
459460
ADD_STAT(staticBranchNumEverTaken, statistics::units::Count::get(), "the number of all (different) static branches that are once taken"),
@@ -497,7 +498,15 @@ DecoupledBPUWithBTB::DBPBTBStats::DBPBTBStats(
497498
ADD_STAT(btbEntriesWithOnlyOneJump, statistics::units::Count::get(), "number of btb entries with different start PC starting with a jump"),
498499
ADD_STAT(predFalseHit, statistics::units::Count::get(), "false hit detected at pred"),
499500
ADD_STAT(commitFalseHit, statistics::units::Count::get(), "false hit detected at commit"),
500-
ADD_STAT(predictionBlockedForUpdate, statistics::units::Count::get(), "prediction blocked for update priority")
501+
ADD_STAT(predictionBlockedForUpdate, statistics::units::Count::get(), "prediction blocked for update priority"),
502+
ADD_STAT(s1PredWrongFallthrough, statistics::units::Count::get(), "S1pred wrong full throughs"),
503+
ADD_STAT(s1PredWrongUbtb, statistics::units::Count::get(),"S1pred wrong using ubtb "),
504+
ADD_STAT(s1PredWrongAbtb, statistics::units::Count::get(), "S1pred wrong using abtb "),
505+
ADD_STAT(s3PredWrongMbtb, statistics::units::Count::get(), "S3pred wrong blame mbtb "),
506+
ADD_STAT(s3PredWrongTage, statistics::units::Count::get(), "S3pred wrong blame tage "),
507+
ADD_STAT(s3PredWrongIttage, statistics::units::Count::get(), "S3pred wrong blame ittage "),
508+
ADD_STAT(s3PredWrongRas, statistics::units::Count::get(), "S3pred wrong blame ras ")
509+
501510
{
502511
predsOfEachStage.init(numStages);
503512
commitPredsFromEachStage.init(numStages+1);
@@ -700,6 +709,7 @@ DecoupledBPUWithBTB::addBranchClassStat(BranchClass cls, bool mispred)
700709
dbpBtbStats.branchClassCounts[idx]++;
701710
if (mispred) {
702711
dbpBtbStats.branchClassMisses[idx]++;
712+
dbpBtbStats.branchClassCountsTotal++;
703713
}
704714

705715
DPRINTF(DBPBTBStats, "Branch classified as %s, mispred=%d\n",
@@ -859,9 +869,82 @@ DecoupledBPUWithBTB::commitBranch(const DynInstPtr &inst, bool mispred)
859869
for (auto component : components) {
860870
component->commitBranch(entry, inst);
861871
}
872+
//here add final counter
873+
874+
if (mispred) {
875+
commitPredWrongSource(entry);
876+
}
877+
862878
}
863879

880+
void
881+
DecoupledBPUWithBTB::commitPredWrongSource(const FetchStream &entry)
882+
{
883+
int ubtbid = ubtb->getComponentIdx();
884+
int abtbid = abtb->getComponentIdx();
885+
int mbtbid = mbtb->getComponentIdx();
886+
int tageid = tage->getComponentIdx();
887+
int ittageid = ittage->getComponentIdx();
888+
int rasid = ras->getComponentIdx();
889+
890+
int s1PredSource = entry.s1Source;
891+
int s3PredSource = entry.s3Source;
892+
893+
auto exeBranchInfo = entry.exeBranchInfo;
894+
895+
bool onlyDirectionWrong = entry.exeTaken != entry.predTaken;
896+
897+
assert(s1PredSource < mbtbid);
898+
if (s1PredSource == ubtbid) {
899+
dbpBtbStats.s1PredWrongUbtb++;
900+
} else if (s1PredSource == abtbid) {
901+
dbpBtbStats.s1PredWrongAbtb++;
902+
}else {
903+
dbpBtbStats.s1PredWrongFallthrough++;
904+
}
864905

906+
if (s3PredSource == rasid) {
907+
if (exeBranchInfo.isCond) {
908+
dbpBtbStats.s3PredWrongTage++;
909+
} else if (exeBranchInfo.isReturn) {
910+
dbpBtbStats.s3PredWrongRas++;
911+
} else {
912+
dbpBtbStats.s3PredWrongMbtb++;
913+
}
914+
} else if (s3PredSource == ittageid) {
915+
if (exeBranchInfo.isIndirect) {
916+
dbpBtbStats.s3PredWrongIttage++;
917+
} else if (exeBranchInfo.isCond) {
918+
dbpBtbStats.s3PredWrongTage++;
919+
} else {
920+
dbpBtbStats.s3PredWrongMbtb++;
921+
}
922+
} else if (s3PredSource == tageid) {
923+
if (exeBranchInfo.isCond) {
924+
if (onlyDirectionWrong) {
925+
dbpBtbStats.s3PredWrongTage++;
926+
} else {
927+
dbpBtbStats.s3PredWrongMbtb++;
928+
}
929+
} else {
930+
dbpBtbStats.s3PredWrongMbtb++;
931+
}
932+
}else if (s3PredSource == mbtbid) {
933+
if (exeBranchInfo.isCond) {
934+
if (onlyDirectionWrong) {
935+
dbpBtbStats.s3PredWrongTage++;
936+
} else {
937+
dbpBtbStats.s3PredWrongMbtb++;
938+
}
939+
} else if (exeBranchInfo.isIndirect) {
940+
dbpBtbStats.s3PredWrongIttage++;
941+
} else {
942+
dbpBtbStats.s3PredWrongMbtb++;
943+
}
944+
}else if (s3PredSource == -1) {
945+
dbpBtbStats.s3PredWrongMbtb++;
946+
}
947+
}
865948
/**
866949
* @brief Handle instruction commits and phase-based statistics
867950
*

src/cpu/pred/btb/mbtb.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ MBTB::buildUpdatedEntry(const BTBEntry& req_entry,
584584
if (entry_to_write.isIndirect && stream.exeTaken && stream.getControlPC() == entry_to_write.pc) {
585585
entry_to_write.target = stream.exeBranchInfo.target;
586586
}
587-
588587
return entry_to_write;
589588
}
590589

@@ -811,6 +810,7 @@ MBTB::insertVictimCache(const TickedBTBEntry& evicted_entry)
811810
}
812811

813812
#ifndef UNIT_TEST
813+
814814
void
815815
MBTB::commitBranch(const FetchStream &stream, const DynInstPtr &inst)
816816
{
@@ -957,8 +957,7 @@ MBTB::BTBStats::BTBStats(statistics::Group* parent, int numWays) :
957957
ADD_STAT(returnHits, statistics::units::Count::get(), "returns committed that was predicted hit"),
958958
ADD_STAT(returnMisses, statistics::units::Count::get(), "returns committed that was predicted miss"),
959959

960-
ADD_STAT(victimCacheHit, statistics::units::Count::get(), "victim cache hits"),
961-
ADD_STAT(predHitCount, statistics::units::Count::get(), "number of hit entries encountered on mbtb hit")
960+
ADD_STAT(victimCacheHit, statistics::units::Count::get(), "victim cache hits")
962961

963962
{
964963
predHitCount.init(0, numWays * 2, 1); // max 4ways * 2(halfAligned) + VC

0 commit comments

Comments
 (0)