Skip to content

Commit 0a221f6

Browse files
authored
Use PHR in ITTAGE (#628)
* cpu-o3: use phr in ittage Change-Id: Id57fceb89d8e89791cfaa5e31f0321fc07f4b1a9 * cpu-o3: fix PHR type miss in ittage Change-Id: I82a46832bae8466ca27136d3633fa67224ddc5c2 * cpu-o3: update `doUpdateHist` function doc in ittage Change-Id: I5c990086b68d69cac3e6fb49e7596b52fa6633b8 * cpu-o3: check fold history for ittage Change-Id: I269cf140d83fa2593f92c4a6e580037faefba688
1 parent 12d1d24 commit 0a221f6

File tree

5 files changed

+84
-31
lines changed

5 files changed

+84
-31
lines changed

src/cpu/pred/BranchPredictor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,7 @@ class BTBITTAGE(TimedBaseBTBPredictor):
10761076
cxx_class = 'gem5::branch_prediction::btb_pred::BTBITTAGE'
10771077
cxx_header = "cpu/pred/btb/btb_ittage.hh"
10781078

1079+
needMoreHistories = Param.Bool(True, "BTBITTAGE needs more histories")
10791080
numPredictors = Param.Unsigned(5, "Number of TAGE predictors")
10801081
tableSizes = VectorParam.Unsigned([256]*2 + [512]*3, "the ITTAGE T0~Tn length")
10811082
TTagBitSizes = VectorParam.Unsigned([9]*5, "the T0~Tn entry's tag bit size")

src/cpu/pred/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ DebugFlag('TAGE')
127127
DebugFlag('TAGEUseful')
128128
DebugFlag('TAGEHistory')
129129
DebugFlag('ITTAGE')
130+
DebugFlag('ITTAGEHistory')
130131
DebugFlag('FoldedHist')
131132
DebugFlag('RAS')
132133
DebugFlag('MGSC')

src/cpu/pred/btb/btb_ittage.cc

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "debug/DecoupleBPVerbose.hh"
1313
#include "debug/DecoupleBPUseful.hh"
1414
#include "debug/ITTAGE.hh"
15+
#include "debug/ITTAGEHistory.hh"
1516

1617
namespace gem5 {
1718

@@ -30,6 +31,7 @@ maxHistLen(p.maxHistLen),
3031
numTablesToAlloc(p.numTablesToAlloc),
3132
ittageStats(this, p.numPredictors)
3233
{
34+
this->needMoreHistories = p.needMoreHistories;
3335
DPRINTF(ITTAGE, "BTBITTAGE constructor numBr=%d\n", numBr);
3436
tageTable.resize(numPredictors);
3537
tableIndexBits.resize(numPredictors);
@@ -51,9 +53,9 @@ ittageStats(this, p.numPredictors)
5153

5254
assert(tablePcShifts.size() >= numPredictors);
5355

54-
tagFoldedHist.push_back(GlobalFoldedHist((int)histLengths[i], (int)tableTagBits[i], (int)16));
55-
altTagFoldedHist.push_back(GlobalFoldedHist((int)histLengths[i], (int)tableTagBits[i]-1, (int)16));
56-
indexFoldedHist.push_back(GlobalFoldedHist((int)histLengths[i], (int)tableIndexBits[i], (int)16));
56+
tagFoldedHist.push_back(PathFoldedHist((int)histLengths[i], (int)tableTagBits[i], (int)16));
57+
altTagFoldedHist.push_back(PathFoldedHist((int)histLengths[i], (int)tableTagBits[i]-1, (int)16));
58+
indexFoldedHist.push_back(PathFoldedHist((int)histLengths[i], (int)tableIndexBits[i], (int)16));
5759
}
5860
// useAlt.resize(128);
5961
// for (unsigned i = 0; i < useAlt.size(); ++i) {
@@ -451,50 +453,84 @@ BTBITTAGE::satDecrement(int min, short &counter)
451453
return counter == min;
452454
}
453455

456+
/**
457+
* @brief Updates branch history for speculative execution
458+
*
459+
* This function updates three types of folded histories:
460+
* - Tag folded history: Used for tag computation
461+
* - Alternative tag folded history: Used for alternative tag computation
462+
* - Index folded history: Used for table index computation
463+
*
464+
* @param history The current branch history
465+
* @param taken Whether the branch was taken
466+
* @param pc The program counter of the branch
467+
* @param target The target address of the branch
468+
*/
454469
void
455-
BTBITTAGE::doUpdateHist(const boost::dynamic_bitset<> &history, int shamt, bool taken)
470+
BTBITTAGE::doUpdateHist(const boost::dynamic_bitset<> &history, bool taken, Addr pc, Addr target)
456471
{
457-
if (debugFlag) {
472+
if (debug::ITTAGEHistory) { // if debug flag is off, do not use to_string since it's too slow
458473
std::string buf;
459474
boost::to_string(history, buf);
460-
DPRINTF(ITTAGE, "in doUpdateHist, shamt %d, taken %d, history %s\n", shamt, taken, buf);
475+
DPRINTF(ITTAGEHistory, "in doUpdateHist, taken %d, pc %#lx, history %s\n", taken, pc, buf.c_str());
461476
}
462-
if (shamt == 0) {
463-
DPRINTF(ITTAGE, "shamt is 0, returning\n");
477+
if (!taken) {
478+
DPRINTF(ITTAGEHistory, "not updating folded history, since FB not taken\n");
464479
return;
465480
}
466481

467482
for (int t = 0; t < numPredictors; t++) {
468483
for (int type = 0; type < 3; type++) {
469-
DPRINTF(ITTAGE, "t: %d, type: %d\n", t, type);
470-
471484
auto &foldedHist = type == 0 ? indexFoldedHist[t] : type == 1 ? tagFoldedHist[t] : altTagFoldedHist[t];
472-
foldedHist.update(history, shamt, taken);
485+
// since we have folded path history, we can put arbitrary shamt here, and it wouldn't make a difference
486+
foldedHist.update(history, 2, taken, pc, target);
487+
DPRINTF(ITTAGEHistory, "t: %d, type: %d, foldedHist _folded 0x%lx\n", t, type, foldedHist.get());
473488
}
474489
}
475490
}
476491

492+
/**
493+
* @brief Updates branch history for speculative execution
494+
*
495+
* This function updates the branch history for speculative execution
496+
* based on the provided history and prediction information.
497+
*
498+
* It first retrieves the history information from the prediction metadata
499+
* and then calls the doUpdateHist function to update the folded histories.
500+
*
501+
* @param history The current branch history
502+
* @param pred The prediction metadata containing history information
503+
*/
477504
void
478-
BTBITTAGE::specUpdateHist(const boost::dynamic_bitset<> &history, FullBTBPrediction &pred)
505+
BTBITTAGE::specUpdatePHist(const boost::dynamic_bitset<> &history, FullBTBPrediction &pred)
479506
{
480-
int shamt;
481-
bool cond_taken;
482-
std::tie(shamt, cond_taken) = pred.getHistInfo();
483-
doUpdateHist(history, shamt, cond_taken);
507+
auto [pc, target, taken] = pred.getPHistInfo();
508+
doUpdateHist(history, taken, pc, target);
484509
}
485510

511+
/**
512+
* @brief Recovers branch history state after a misprediction
513+
*
514+
* This function:
515+
* 1. Restores the folded histories from the saved metadata
516+
* 2. Updates the histories with the correct branch outcome
517+
* 3. Ensures predictor state is consistent after recovery
518+
*
519+
* @param history The branch history to recover to
520+
* @param entry The fetch stream entry containing recovery information
521+
* @param shamt Number of bits to shift in history update
522+
* @param cond_taken The actual branch outcome
523+
*/
486524
void
487-
BTBITTAGE::recoverHist(const boost::dynamic_bitset<> &history,
488-
const FetchStream &entry, int shamt, bool cond_taken)
525+
BTBITTAGE::recoverPHist(const boost::dynamic_bitset<> &history, const FetchStream &entry, int shamt, bool cond_taken)
489526
{
490-
// TODO: need to get idx
491527
std::shared_ptr<TageMeta> predMeta = std::static_pointer_cast<TageMeta>(entry.predMetas[getComponentIdx()]);
492528
for (int i = 0; i < numPredictors; i++) {
493529
tagFoldedHist[i].recover(predMeta->tagFoldedHist[i]);
494530
altTagFoldedHist[i].recover(predMeta->altTagFoldedHist[i]);
495531
indexFoldedHist[i].recover(predMeta->indexFoldedHist[i]);
496532
}
497-
doUpdateHist(history, shamt, cond_taken);
533+
doUpdateHist(history, cond_taken, entry.getControlPC(), entry.getTakenTarget());
498534
}
499535

500536
void

src/cpu/pred/btb/btb_ittage.hh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ class BTBITTAGE : public TimedBaseBTBPredictor
100100

101101
std::shared_ptr<void> getPredictionMeta() override;
102102

103-
void specUpdateHist(const boost::dynamic_bitset<> &history, FullBTBPrediction &pred) override;
103+
// speculative update 3 folded history, according history and pred.taken
104+
// the other specUpdateHist methods are left blank
105+
void specUpdatePHist(const boost::dynamic_bitset<> &history, FullBTBPrediction &pred) override;
104106

105-
void recoverHist(const boost::dynamic_bitset<> &history, const FetchStream &entry, int shamt, bool cond_taken) override;
107+
// Recover 3 folded history after a misprediction, then update 3 folded history according to history and pred.taken
108+
// the other recoverHist methods are left blank
109+
void recoverPHist(const boost::dynamic_bitset<> &history,
110+
const FetchStream &entry,int shamt, bool cond_taken) override;
106111

107112
void update(const FetchStream &entry) override;
108113

@@ -132,7 +137,9 @@ class BTBITTAGE : public TimedBaseBTBPredictor
132137
return (pc & (blockSize - 1)) >> 1;
133138
}
134139

135-
void doUpdateHist(const bitset &history, int shamt, bool taken);
140+
// Update branch history
141+
void doUpdateHist(const bitset &history, bool taken, Addr pc, Addr target);
142+
136143

137144
const unsigned numPredictors;
138145

@@ -144,9 +151,9 @@ class BTBITTAGE : public TimedBaseBTBPredictor
144151
std::vector<bitset> tableTagMasks;
145152
std::vector<unsigned> tablePcShifts;
146153
std::vector<unsigned> histLengths;
147-
std::vector<GlobalFoldedHist> tagFoldedHist;
148-
std::vector<GlobalFoldedHist> altTagFoldedHist;
149-
std::vector<GlobalFoldedHist> indexFoldedHist;
154+
std::vector<PathFoldedHist> tagFoldedHist;
155+
std::vector<PathFoldedHist> altTagFoldedHist;
156+
std::vector<PathFoldedHist> indexFoldedHist;
150157

151158
LFSR64 allocLFSR;
152159

@@ -218,12 +225,12 @@ class BTBITTAGE : public TimedBaseBTBPredictor
218225
{
219226
std::unordered_map<Addr, TagePrediction> preds;
220227
bitset usefulMask;
221-
std::vector<GlobalFoldedHist> tagFoldedHist;
222-
std::vector<GlobalFoldedHist> altTagFoldedHist;
223-
std::vector<GlobalFoldedHist> indexFoldedHist;
228+
std::vector<PathFoldedHist> tagFoldedHist;
229+
std::vector<PathFoldedHist> altTagFoldedHist;
230+
std::vector<PathFoldedHist> indexFoldedHist;
224231
TageMeta(std::unordered_map<Addr, TagePrediction> preds, bitset usefulMask,
225-
std::vector<GlobalFoldedHist> tagFoldedHist, std::vector<GlobalFoldedHist> altTagFoldedHist,
226-
std::vector<GlobalFoldedHist> indexFoldedHist)
232+
std::vector<PathFoldedHist> tagFoldedHist, std::vector<PathFoldedHist> altTagFoldedHist,
233+
std::vector<PathFoldedHist> indexFoldedHist)
227234
: preds(preds),
228235
usefulMask(usefulMask),
229236
tagFoldedHist(tagFoldedHist),

src/cpu/pred/btb/decoupled_bpred.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,9 @@ DecoupledBPUWithBTB::updateHistoryForPrediction(FetchStream &entry)
11501150
if (tage->isEnabled()) {
11511151
tage->checkFoldedHist(s0PHistory, "speculative update");
11521152
}
1153+
if (ittage->isEnabled()) {
1154+
ittage->checkFoldedHist(s0PHistory, "speculative update");
1155+
}
11531156
if (microtage->isEnabled()) {
11541157
microtage->checkFoldedHist(s0PHistory, "speculative update");
11551158
}
@@ -1241,6 +1244,11 @@ DecoupledBPUWithBTB::recoverHistoryForSquash(
12411244
squash_type == SQUASH_CTRL ? "control squash" :
12421245
squash_type == SQUASH_OTHER ? "non control squash" : "trap squash");
12431246
}
1247+
if (ittage->isEnabled()) {
1248+
ittage->checkFoldedHist(s0PHistory,
1249+
squash_type == SQUASH_CTRL ? "control squash" :
1250+
squash_type == SQUASH_OTHER ? "non control squash" : "trap squash");
1251+
}
12441252
if (microtage->isEnabled()) {
12451253
microtage->checkFoldedHist(s0PHistory,
12461254
squash_type == SQUASH_CTRL ? "control squash" :

0 commit comments

Comments
 (0)