Skip to content

Commit 61036c7

Browse files
committed
cpu-o3: revert 4a767ea
use startPC instead of alignedPC, to calculate index, tag, bank easier Change-Id: Id76bf1658e4286aad2cd4580943ff724bfb7e1e7
1 parent fc34e6b commit 61036c7

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

src/cpu/pred/btb/btb_tage.cc

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ BTBTAGE::tickStart() {}
195195
* @brief Generate prediction for a single BTB entry by searching TAGE tables
196196
*
197197
* @param btb_entry The BTB entry to generate prediction for
198-
* @param alignedPC The aligned PC address for calculating indices and tags
198+
* @param startPC The starting PC address for calculating indices and tags
199199
* @param predMeta Optional prediction metadata; if provided, use snapshot for index/tag
200200
* calculation (update path); if nullptr, use current folded history (prediction path)
201201
* @return TagePrediction containing main and alternative predictions
202202
*/
203203
BTBTAGE::TagePrediction
204204
BTBTAGE::generateSinglePrediction(const BTBEntry &btb_entry,
205-
const Addr &alignedPC,
205+
const Addr &startPC,
206206
std::shared_ptr<TageMeta> predMeta) {
207207
DPRINTF(TAGE, "generateSinglePrediction for btbEntry: %#lx\n", btb_entry.pc);
208208

@@ -213,16 +213,16 @@ BTBTAGE::generateSinglePrediction(const BTBEntry &btb_entry,
213213

214214
// Search from highest to lowest table for matches
215215
// Calculate branch position within the block (like RTL's cfiPosition)
216-
unsigned position = getBranchIndexInBlock(btb_entry.pc, alignedPC);
216+
unsigned position = getBranchIndexInBlock(btb_entry.pc, startPC);
217217

218218
for (int i = numPredictors - 1; i >= 0; --i) {
219219
// Calculate index and tag: use snapshot if provided, otherwise use current folded history
220220
// Tag includes position XOR (like RTL: tag = tempTag ^ cfiPosition)
221-
Addr index = predMeta ? getTageIndex(alignedPC, i, predMeta->indexFoldedHist[i].get())
222-
: getTageIndex(alignedPC, i);
223-
Addr tag = predMeta ? getTageTag(alignedPC, i,
221+
Addr index = predMeta ? getTageIndex(startPC, i, predMeta->indexFoldedHist[i].get())
222+
: getTageIndex(startPC, i);
223+
Addr tag = predMeta ? getTageTag(startPC, i,
224224
predMeta->tagFoldedHist[i].get(), predMeta->altTagFoldedHist[i].get(), position)
225-
: getTageTag(alignedPC, i, position);
225+
: getTageTag(startPC, i, position);
226226

227227
bool match = false; // for each table, only one way can be matched
228228
TageEntry matching_entry;
@@ -266,8 +266,8 @@ BTBTAGE::generateSinglePrediction(const BTBEntry &btb_entry,
266266
bool main_taken = main_info.taken();
267267
bool alt_taken = alt_info.taken();
268268
// Use base table instead of btb_entry.ctr
269-
Addr base_idx = getBaseTableIndex(alignedPC);
270-
unsigned branch_idx = getBranchIndexInBlock(btb_entry.pc, alignedPC);
269+
Addr base_idx = getBaseTableIndex(startPC);
270+
unsigned branch_idx = getBranchIndexInBlock(btb_entry.pc, startPC);
271271
bool base_taken = getDelay() != 0 ? baseTable[base_idx][branch_idx] >= 0 : btb_entry.ctr >= 0;
272272
bool alt_pred = alt_provided ? alt_taken : base_taken; // if alt provided, use alt prediction, otherwise use base
273273

@@ -296,21 +296,21 @@ BTBTAGE::generateSinglePrediction(const BTBEntry &btb_entry,
296296
/**
297297
* @brief Look up predictions in TAGE tables for a stream of instructions
298298
*
299-
* @param alignedPC The aligned PC address for the instruction stream
299+
* @param startPC The starting PC address for the instruction stream
300300
* @param btbEntries Vector of BTB entries to make predictions for
301301
* @return Map of branch PC addresses to their predicted outcomes
302302
*/
303303
void
304-
BTBTAGE::lookupHelper(const Addr &alignedPC, const std::vector<BTBEntry> &btbEntries,
304+
BTBTAGE::lookupHelper(const Addr &startPC, const std::vector<BTBEntry> &btbEntries,
305305
std::unordered_map<Addr, TageInfoForMGSC> &tageInfoForMgscs, CondTakens& results)
306306
{
307-
DPRINTF(TAGE, "lookupHelper alignedPC: %#lx\n", alignedPC);
307+
DPRINTF(TAGE, "lookupHelper startAddr: %#lx\n", startPC);
308308

309309
// Process each BTB entry to make predictions
310310
for (auto &btb_entry : btbEntries) {
311311
// Only predict for valid conditional branches
312312
if (btb_entry.isCond && btb_entry.valid) {
313-
auto pred = generateSinglePrediction(btb_entry, alignedPC);
313+
auto pred = generateSinglePrediction(btb_entry, startPC);
314314
meta->preds[btb_entry.pc] = pred;
315315
tageStats.updateStatsWithTagePrediction(pred, true);
316316
results.push_back({btb_entry.pc, pred.taken || btb_entry.alwaysTaken});
@@ -337,14 +337,14 @@ BTBTAGE::lookupHelper(const Addr &alignedPC, const std::vector<BTBEntry> &btbEnt
337337
* 2. Stores predictions in the stage prediction structure
338338
* 3. Handles multiple prediction stages with different delays
339339
*
340-
* @param stream_start Starting PC of the instruction stream
340+
* @param startPC Starting PC of the instruction stream
341341
* @param history Current branch history
342342
* @param stagePreds Vector of predictions for different pipeline stages
343343
*/
344344
void
345-
BTBTAGE::putPCHistory(Addr stream_start, const bitset &history, std::vector<FullBTBPrediction> &stagePreds) {
345+
BTBTAGE::putPCHistory(Addr startPC, const bitset &history, std::vector<FullBTBPrediction> &stagePreds) {
346346
// use 32byte(blockSize) aligned PC for prediction(get index and tag)
347-
Addr alignedPC = stream_start & ~(blockSize - 1);
347+
Addr alignedPC = startPC & ~(blockSize - 1);
348348

349349
// Record prediction bank for next tick's conflict detection
350350
lastPredBankId = getBankId(alignedPC);
@@ -356,7 +356,7 @@ BTBTAGE::putPCHistory(Addr stream_start, const bitset &history, std::vector<Full
356356
#endif
357357

358358
DPRINTF(TAGE, "putPCHistory startAddr: %#lx, alignedPC: %#lx, bank: %u\n",
359-
stream_start, alignedPC, lastPredBankId);
359+
startPC, alignedPC, lastPredBankId);
360360

361361
// IMPORTANT: when this function is called,
362362
// btb entries should already be in stagePreds
@@ -373,7 +373,7 @@ BTBTAGE::putPCHistory(Addr stream_start, const bitset &history, std::vector<Full
373373
// TODO: only lookup once for one btb entry in different stages
374374
auto &stage_pred = stagePreds[s];
375375
stage_pred.condTakens.clear();
376-
lookupHelper(alignedPC, stage_pred.btbEntries, stage_pred.tageInfoForMgscs, stage_pred.condTakens);
376+
lookupHelper(startPC, stage_pred.btbEntries, stage_pred.tageInfoForMgscs, stage_pred.condTakens);
377377
}
378378

379379
}
@@ -437,9 +437,9 @@ BTBTAGE::updatePredictorStateAndCheckAllocation(const BTBEntry &entry,
437437
auto &alt_info = pred.altInfo;
438438
bool used_alt = pred.useAlt;
439439
// Use base table instead of entry.ctr for fallback prediction
440-
Addr alignedPC = stream.getRealStartPC() & ~(blockSize - 1);
441-
Addr base_idx = getBaseTableIndex(alignedPC);
442-
unsigned branch_idx = getBranchIndexInBlock(entry.pc, alignedPC);
440+
Addr startPC = stream.getRealStartPC();
441+
Addr base_idx = getBaseTableIndex(startPC);
442+
unsigned branch_idx = getBranchIndexInBlock(entry.pc, startPC);
443443
bool base_taken = baseTable[base_idx][branch_idx] >= 0;
444444
bool alt_taken = alt_info.found ? alt_info.taken() : base_taken;
445445

@@ -557,15 +557,15 @@ BTBTAGE::updatePredictorStateAndCheckAllocation(const BTBEntry &entry,
557557
/**
558558
* @brief Handle allocation of new entries
559559
*
560-
* @param alignedPC The aligned PC address
560+
* @param startPC The starting PC address
561561
* @param entry The BTB entry being updated
562562
* @param actual_taken The actual outcome of the branch
563563
* @param start_table The starting table for allocation
564564
* @param meta The metadata of the predictor
565565
* @return true if allocation is successful
566566
*/
567567
bool
568-
BTBTAGE::handleNewEntryAllocation(const Addr &alignedPC,
568+
BTBTAGE::handleNewEntryAllocation(const Addr &startPC,
569569
const BTBEntry &entry,
570570
bool actual_taken,
571571
unsigned start_table,
@@ -579,11 +579,11 @@ BTBTAGE::handleNewEntryAllocation(const Addr &alignedPC,
579579
// - If none, apply a one-step age penalty to a strong, not-useful way (no allocation).
580580

581581
// Calculate branch position within the block (like RTL's cfiPosition)
582-
unsigned position = getBranchIndexInBlock(entry.pc, alignedPC);
582+
unsigned position = getBranchIndexInBlock(entry.pc, startPC);
583583

584584
for (unsigned ti = start_table; ti < numPredictors; ++ti) {
585-
Addr newIndex = getTageIndex(alignedPC, ti, meta->indexFoldedHist[ti].get());
586-
Addr newTag = getTageTag(alignedPC, ti,
585+
Addr newIndex = getTageIndex(startPC, ti, meta->indexFoldedHist[ti].get());
586+
Addr newTag = getTageTag(startPC, ti,
587587
meta->tagFoldedHist[ti].get(), meta->altTagFoldedHist[ti].get(), position);
588588

589589
auto &set = tageTable[ti][newIndex];
@@ -711,7 +711,7 @@ BTBTAGE::update(const FetchStream &stream) {
711711
TagePrediction recomputed;
712712
if (updateOnRead) { // if update on read is enabled, re-read providers using snapshot
713713
// Re-read providers using snapshot (do not rely on prediction-time main/alt)
714-
recomputed = generateSinglePrediction(btb_entry, alignedPC, predMeta);
714+
recomputed = generateSinglePrediction(btb_entry, startAddr, predMeta);
715715
} else { // otherwise, use the prediction from the prediction-time main/alt
716716
recomputed = predMeta->preds[btb_entry.pc];
717717
}
@@ -732,7 +732,7 @@ BTBTAGE::update(const FetchStream &stream) {
732732
if (main_info.found) {
733733
start_table = main_info.table + 1; // start from the table after the main prediction table
734734
}
735-
alloc_success = handleNewEntryAllocation(alignedPC, btb_entry, actual_taken,
735+
alloc_success = handleNewEntryAllocation(startAddr, btb_entry, actual_taken,
736736
start_table, predMeta, allocated_table, allocated_index, allocated_way);
737737
}
738738

@@ -808,7 +808,7 @@ BTBTAGE::getTageTag(Addr pc, int t, uint64_t foldedHist, uint64_t altFoldedHist,
808808
Addr mask = (1ULL << tableTagBits[t]) - 1;
809809

810810
// Extract lower bits of PC directly
811-
Addr pcBits = (pc >> floorLog2(blockSize)) & mask; // pc is already aligned
811+
Addr pcBits = (pc >> floorLog2(blockSize)) & mask;
812812

813813
// Extract and prepare folded history bits
814814
Addr foldedBits = foldedHist & mask;
@@ -889,9 +889,10 @@ BTBTAGE::getBaseTableIndex(Addr pc) {
889889
}
890890

891891
unsigned
892-
BTBTAGE::getBranchIndexInBlock(Addr pc, Addr alignedPC) {
892+
BTBTAGE::getBranchIndexInBlock(Addr pc, Addr startPC) {
893893
// Calculate branch position within the 64-byte block (0-31)
894894
// alignedPC is guaranteed to be 32-byte aligned
895+
Addr alignedPC = startPC & ~(blockSize - 1);
895896
Addr offset = (pc - alignedPC) >> 1; // Position index 0-31
896897
assert(offset < maxBranchPositions);
897898
return offset;

src/cpu/pred/btb/btb_tage.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class BTBTAGE : public TimedBaseBTBPredictor
165165
#endif
166166

167167
// Look up predictions in TAGE tables for a stream of instructions
168-
void lookupHelper(const Addr &alignedPC, const std::vector<BTBEntry> &btbEntries,
169-
std::unordered_map<Addr, TageInfoForMGSC> &tageInfoForMgscs, CondTakens& results);
168+
void lookupHelper(const Addr &startPC, const std::vector<BTBEntry> &btbEntries,
169+
std::unordered_map<Addr, TageInfoForMGSC> &tageInfoForMgscs, CondTakens& results);
170170

171171
// Calculate TAGE index for a given PC and table
172172
Addr getTageIndex(Addr pc, int table);
@@ -191,7 +191,7 @@ class BTBTAGE : public TimedBaseBTBPredictor
191191
Addr getBaseTableIndex(Addr pc);
192192

193193
// Get branch index within a prediction block
194-
unsigned getBranchIndexInBlock(Addr pc, Addr alignedPC);
194+
unsigned getBranchIndexInBlock(Addr pc, Addr startPC);
195195

196196
// Get bank ID from aligned PC
197197
// Extract pc[bankIdShift+bankIdWidth-1 : bankIdShift]
@@ -401,7 +401,7 @@ private:
401401
// If predMeta is provided, use snapshot folded history for index/tag calculation (update path)
402402
// If predMeta is nullptr, use current folded history (prediction path)
403403
TagePrediction generateSinglePrediction(const BTBEntry &btb_entry,
404-
const Addr &alignedPC,
404+
const Addr &startPC,
405405
const std::shared_ptr<TageMeta> predMeta = nullptr);
406406

407407
// Helper method to prepare BTB entries for update
@@ -414,7 +414,7 @@ private:
414414
const FetchStream &stream);
415415

416416
// Helper method to handle new entry allocation
417-
bool handleNewEntryAllocation(const Addr &alignedPC,
417+
bool handleNewEntryAllocation(const Addr &startPC,
418418
const BTBEntry &entry,
419419
bool actual_taken,
420420
unsigned main_table,

0 commit comments

Comments
 (0)