Skip to content

cpu-pred-btb: remove fetch stream use fetch target#744

Merged
tastynoob merged 1 commit intoxs-devfrom
kmh-smt
Jan 29, 2026
Merged

cpu-pred-btb: remove fetch stream use fetch target#744
tastynoob merged 1 commit intoxs-devfrom
kmh-smt

Conversation

@tastynoob
Copy link
Copy Markdown
Collaborator

@tastynoob tastynoob commented Jan 28, 2026

Summary by CodeRabbit

  • Refactor

    • Overhauled fetch/branch-prediction internals to unify stream/target concepts and simplify queue/ID handling across fetch and commit pipelines.
  • Bug Fixes

    • Removed redundant stream ID propagation and related logging to reduce noise and improve correctness of squash/commit traces.
  • Documentation

    • Updated docs and tests to reflect the new fetch/target terminology.

✏️ Tip: You can customize this high-level summary in your review settings.

@github-actions
Copy link
Copy Markdown

🚀 Performance test triggered: spec06-0.8c

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 28, 2026

📝 Walkthrough

Walkthrough

Renames and removes FSQ/FetchStream identifiers in favor of FTQ/FetchTarget across O3 pipeline, fetch/BTB predictors, and related tests; removes squashedStreamId and consolidates stream bookkeeping to FTQ-based fields without changing higher-level behavior.

Changes

Cohort / File(s) Summary
O3 Comm & Time structs
src/cpu/o3/comm.hh
Removed squashedStreamId from IEWStruct; renamed resolvedFSQIdresolvedFTQId; renamed fsqId/doneFsqIdftqId/doneFtqId.
Commit stage
src/cpu/o3/commit.cc, src/cpu/o3/commit.hh
Removed committedStreamId and squashedStreamId propagation; switched FSQ→FTQ ids in commit logic; changed default committedTargetId.
Decode & DynInst
src/cpu/o3/decode.cc, src/cpu/o3/dyn_inst.hh
Removed fsqId field and accessors from DynInst; stopped propagating fsqId during fusion; comment updates to use ftqId.
Fetch pipeline
src/cpu/o3/fetch.cc, src/cpu/o3/fetch.hh, src/cpu/o3/trace/TraceFetch.cc
Replaced FSQ APIs/names with FTQ equivalents (fsqHasHead→ftqHasHead, fsqHead→ftqHead, fsqEmpty→ftqEmpty); adjusted consume/lookup and tracing to use FTQ ids; updated resolvedFSQId→resolvedFTQId.
IEW / Squash handling
src/cpu/o3/iew.cc
Removed recording/logging of FsQ stream id in squash paths; changed ResolvedCFIEntry field fsqIdftqId.
BTB common types
src/cpu/pred/btb/common.hh
Renamed FetchStreamFetchTarget; removed FetchStreamId alias; added ThreadID tid to FetchTarget; minor enum/format changes.
Decoupled BPU / BTB core
src/cpu/pred/btb/decoupled_bpred.*, src/cpu/pred/btb/decoupled_bpred_stats.cc, src/cpu/pred/ftb/decoupled_bpred.cc
Massive refactor: fetchStreamQueuefetchTargetQueue, createFetchStreamEntrycreateFetchTargetEntry, removed stream_id parameters in favor of target_id, updated consume/fill/squash/resolve/update flows and statistics to use FetchTarget/FTQ ids; added overrideStats() in FTB variant.
BTB predictors (signatures)
src/cpu/pred/btb/*.cc, src/cpu/pred/btb/*.hh (abtb, btb_tage, btb_mgsc, mbtb, btb_ittage, btb_ubtb, ras, uras, timed_base_pred, folded_hist, history_manager, btb helpers)
Replaced public/internal method parameters and includes to accept FetchTarget instead of FetchStream; switched includes from stream_struct.hhcommon.hh.
Tests & docs
src/cpu/pred/btb/test/*, src/cpu/pred/btb/docs/*
Updated tests and documentation to use FetchTarget and FTQ-related naming; adjusted test helper signatures and variables to match refactor.

Sequence Diagram(s)

sequenceDiagram
  participant Fetch as FetchUnit
  participant BTB as DecoupledBPU/BTB
  participant Decode as Decode
  participant IEW as IEW
  participant Commit as Commit

  Fetch->>BTB: create FetchTarget(ftqId) / enqueue prediction
  BTB-->>Fetch: prediction (FetchTarget carries ftqId)
  Fetch->>Decode: build instruction with ftqId
  Decode->>IEW: forward dynInst(ftqId)
  IEW->>Commit: commit signal doneFtqId / doneFtqId used for updates
  Commit->>BTB: update/notify using ftqId (doneFtqId)
  BTB->>BTB: prepareResolveUpdateEntries(mark CFI resolved using ftqId)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Suggested labels

align-kmhv3

Suggested reviewers

  • jensen-yan
  • Yakkhini
  • CJ362ff

Poem

🐇 I hopped through queues and changed a name,

Streams to Targets — now FTQ's the game.
IDs rebounded, squashed ones set free,
Fetch, predict, commit — in tidy harmony.
A little rabbit clap — refactor jubilee!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.93% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: replacing FetchStream with FetchTarget across the BTB prediction system.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (6)
src/cpu/pred/btb/test/btb.test.cc (1)

48-67: Refresh docstring to match FetchTarget.

The helper’s docs still refer to “FetchStream” even though the type is now FetchTarget. Updating these comments will avoid confusion.

📝 Suggested doc fix
- * `@brief` Setup a FetchStream with common parameters for BTB update
+ * `@brief` Setup a FetchTarget with common parameters for BTB update
@@
- * `@return` FetchStream Initialized fetch stream
+ * `@return` FetchTarget Initialized fetch target
src/cpu/pred/btb/common.hh (1)

276-354: Missing initialization of tid member in constructor.

The new tid member added at line 278 is not initialized in the FetchTarget constructor. Since ThreadID is typically an integer type, this will leave tid with an indeterminate value, which could cause subtle bugs when the field is later read.

Proposed fix
 FetchTarget()
-    : startPC(0),
+    : tid(0),
+      startPC(0),
       predTaken(false),
src/cpu/pred/btb/btb_tage.hh (1)

146-150: Type mismatch in UNIT_TEST compatibility wrapper.

The recoverHist wrapper under UNIT_TEST still uses const FetchStream &entry parameter, but it forwards to recoverPHist which now expects const FetchTarget &entry. If FetchStream and FetchTarget are distinct types, this will cause a compilation error in unit test builds.

Suggested fix
-    void recoverHist(const boost::dynamic_bitset<> &history, const FetchStream &entry, int shamt,
+    void recoverHist(const boost::dynamic_bitset<> &history, const FetchTarget &entry, int shamt,
                      bool cond_taken) override
     {
         recoverPHist(history, entry, shamt, cond_taken);
     }
src/cpu/pred/btb/btb_mgsc.cc (1)

540-557: Filter predicate doesn’t match the comment intent.

Line 546 only removes entries that are both non-conditional and not always-taken, so always-taken entries still pass. If the intent is “keep only conditional and not always-taken,” this should be !e.isCond || e.alwaysTaken (or update the comment if the current behavior is intended).

🔧 Suggested fix
-    auto remove_it = std::remove_if(all_entries.begin(), all_entries.end(),
-                                    [](const BTBEntry &e) { return !e.isCond && !e.alwaysTaken; });
+    auto remove_it = std::remove_if(all_entries.begin(), all_entries.end(),
+                                    [](const BTBEntry &e) { return !e.isCond || e.alwaysTaken; });
src/cpu/o3/fetch.cc (1)

1512-1555: Update resolve APIs to accept full 64-bit FetchTargetId instead of narrowing to 32-bit.

Line 1544 narrows entry.resolvedFTQId (uint64_t) to unsigned int, truncating the FTQ ID and causing incorrect queue lookups and predictor updates. The root cause is the resolve API signatures—resolveUpdate, prepareResolveUpdateEntries, and markCFIResolved—which accept unsigned &target_id instead of FetchTargetId. Change both the API signatures in src/cpu/pred/btb/decoupled_bpred.hh and their implementations, and use FetchTargetId stream_id at line 1544.

Required changes

decoupled_bpred.hh (update signatures):

-    bool resolveUpdate(unsigned &target_id);
-    void prepareResolveUpdateEntries(unsigned &target_id);
-    void markCFIResolved(unsigned &target, uint64_t resolvedInstPC);
+    bool resolveUpdate(FetchTargetId target_id);
+    void prepareResolveUpdateEntries(FetchTargetId target_id);
+    void markCFIResolved(FetchTargetId target, uint64_t resolvedInstPC);

fetch.cc (line 1544):

-        unsigned int stream_id = entry.resolvedFTQId;
+        FetchTargetId stream_id = entry.resolvedFTQId;
src/cpu/pred/btb/decoupled_bpred.cc (1)

890-898: Variable naming inconsistency.

The variable fsqid on line 894 should be renamed to ftqid (or ftq_id) to align with the FTQ terminology used throughout the rest of the file.

🔧 Suggested fix
 Addr
 DecoupledBPUWithBTB::getPreservedReturnAddr(const DynInstPtr &dynInst)
 {
     DPRINTF(DecoupleBP, "acquiring reutrn address for inst pc %#lx from decode\n", dynInst->pcState().instAddr());
-    auto fsqid = dynInst->getFtqId();
-    auto retAddr = ras->getTopAddrFromMetas(getTarget(fsqid));
+    auto ftqid = dynInst->getFtqId();
+    auto retAddr = ras->getTopAddrFromMetas(getTarget(ftqid));
     DPRINTF(DecoupleBP, "get ret addr %#lx\n", retAddr);
     return retAddr;
 }
🤖 Fix all issues with AI agents
In `@src/cpu/o3/commit.cc`:
- Around line 1321-1323: The current branch only updates
toIEW->commitInfo[tid].doneFtqId when head_inst->getFtqId() > 1, leaving a stale
value when ftqId is 1 or 0; add an else branch that explicitly resets
toIEW->commitInfo[tid].doneFtqId (e.g., set to 0 or another sentinel
representing "none") when head_inst->getFtqId() <= 1 so downstream FTQ
bookkeeping cannot read a stale value; locate this around the
head_inst->getFtqId() check in commit.cc and update the else-path to clear the
doneFtqId for the given tid.

In `@src/cpu/o3/dyn_inst.hh`:
- Around line 398-402: Update the stale comments for the fields ftqId and
loopIteration to use FTQ terminology instead of FSQ/fetch stream queue: change
the comment above ftqId to say it is the FTQ (fetch target queue) ID used for
squashing and committing, and update the loopIteration comment to reference the
loop iteration within an FTQ entry (or FTQ entry metadata) rather than an "fsq
entry"; ensure the comments around the unsigned ftqId and unsigned loopIteration
match naming of FTQ used elsewhere in the codebase.

In `@src/cpu/pred/btb/abtb.cc`:
- Line 33: Replace the inconsistent relative include in abtb.cc—currently
including "common.hh"—with the same full include path used elsewhere
("cpu/pred/btb/common.hh") so it matches the header abtb.hh and other files;
update the include directive in abtb.cc to reference cpu/pred/btb/common.hh to
prevent build failures due to differing include search configurations.

In `@src/cpu/pred/btb/btb_ubtb.cc`:
- Line 34: Replace the relative include in btb_ubtb.cc: change the `#include`
"common.hh" to the full module path "#include \"cpu/pred/btb/common.hh\"" so it
matches btb_ubtb.hh and the rest of the module; this keeps include style
consistent across the BTB files and avoids mismatched headers.

In `@src/cpu/pred/btb/decoupled_bpred.cc`:
- Around line 402-405: The debug message in the DPRINTF call for DecoupleBP uses
outdated variable names in the format string ("fsqId" and "fetchHeadFsqId")
while the actual variables are ftqId and fetchHeadFtqId; update the format
string passed to DPRINTF(DecoupleBP, ...) to reference ftqId and fetchHeadFtqId
(keep the s0PC reference and existing format specifiers %#lx/%lu as appropriate)
so the printed labels match the variables ftqId, fetchHeadFtqId, and s0PC.

In `@src/cpu/pred/btb/ras.hh`:
- Around line 101-112: The UNIT_TEST conditional for the commitBranch
declaration uses the old type FetchStream which was renamed to FetchTarget;
update the `#ifdef` UNIT_TEST branch so commitBranch takes const FetchTarget
&stream (matching the non-UNIT_TEST signature) and keep the DynInstPtr parameter
unchanged; ensure the symbol commitBranch now has the same parameter types
(const FetchTarget &stream, const DynInstPtr &inst) in both branches to avoid
compilation errors.
🧹 Nitpick comments (5)
src/cpu/pred/btb/common.hh (1)

1-2: Header guard naming inconsistent with FetchTarget refactor.

The header guards still reference STREAM_STRUCT while the file now defines FetchTarget instead of FetchStream. Consider updating for consistency:

Proposed fix
-#ifndef __CPU_PRED_BTB_STREAM_STRUCT_HH__
-#define __CPU_PRED_BTB_STREAM_STRUCT_HH__
+#ifndef __CPU_PRED_BTB_COMMON_HH__
+#define __CPU_PRED_BTB_COMMON_HH__

And at line 753:

-#endif  // __CPU_PRED_BTB_STREAM_STRUCT_HH__
+#endif  // __CPU_PRED_BTB_COMMON_HH__
src/cpu/pred/btb/decoupled_bpred.hh (1)

166-190: Consider updating log wording to FTQ.

Line 169 and Line 171 still print “FSQ … target” after the FTQ migration. Small clarity win to rename these log strings.

src/cpu/pred/btb/decoupled_bpred.cc (3)

109-112: Optional: Rename variables to align with FTQ terminology.

These variable names still use the old Fsq prefix while the rest of the file has been refactored to use Ftq/Target terminology:

  • commitFsqEntryHasInstsVector
  • lastPhaseFsqEntryNumCommittedInstDist
  • commitFsqEntryFetchedInstsVector
  • lastPhaseFsqEntryNumFetchedInstDist

Consider renaming for consistency, or deferring to a follow-up PR.


167-172: Optional: Update comments to reflect FTQ terminology.

The comments on lines 167 and 171 still reference "FSQ" while the code has been refactored to use FTQ terminology.

📝 Suggested comment updates
-    if (validateFSQEnqueue()) {
-        // Create new FSQ entry with the current prediction
+    if (validateFSQEnqueue()) {
+        // Create new FTQ entry with the current prediction
         processNewPrediction();

-        DPRINTF(Override, "FSQ entry enqueued, prediction state reset\n");
+        DPRINTF(Override, "FTQ entry enqueued, prediction state reset\n");

654-687: Optional: Rename function and update debug messages to align with FTQ terminology.

The function validateFSQEnqueue() and its debug messages still reference "FSQ" while the code uses FTQ/target queue operations internally.

📝 Suggested renames
 bool
-DecoupledBPUWithBTB::validateFSQEnqueue()
+DecoupledBPUWithBTB::validateFTQEnqueue()
 {
     // Monitor FSQ size for statistics
     dbpBtbStats.fsqEntryDist.sample(fetchTargetQueue.size(), 1);
     if (targetQueueFull()) {
         dbpBtbStats.fsqFullCannotEnq++;
-        DPRINTF(Override, "FSQ is full (%lu entries)\n", fetchTargetQueue.size());
+        DPRINTF(Override, "FTQ is full (%lu entries)\n", fetchTargetQueue.size());
         return false;
     }

     // 1. Check if a prediction is available to enqueue
     if (bpuState != BpuState::PREDICTION_OUTSTANDING) {
-        DPRINTF(Override, "No prediction available to enqueue into FSQ\n");
+        DPRINTF(Override, "No prediction available to enqueue into FTQ\n");
         return false;
     }

@github-actions
Copy link
Copy Markdown

🚀 Performance test triggered: spec06-0.8c

@github-actions
Copy link
Copy Markdown

🚀 Coremark Smoke Test Results

Branch IPC Change
Base (xs-dev) 2.1727 -
This PR 2.1727 ➡️ 0.0000 (0.00%)

✅ Difftest smoke test passed!

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/cpu/pred/btb/common.hh (1)

276-354: New tid field is not initialized in the constructor.

The newly added ThreadID tid field at line 278 is not included in the constructor's initializer list (lines 325-349). This leaves tid with an indeterminate value, which could cause undefined behavior if accessed before being explicitly set.

🐛 Proposed fix
    FetchTarget()
-       : startPC(0),
+       : tid(0),
+         startPC(0),
          predTaken(false),
🤖 Fix all issues with AI agents
In `@src/cpu/o3/fetch.cc`:
- Around line 1542-1546: The code narrows entry.resolvedFTQId (a uint64_t) into
an unsigned int, risking truncation; update the declaration of stream_id in the
resolveQueue handling to preserve full width (e.g., use auto or uint64_t for
stream_id) where resolveQueue, entry, resolvedFTQId, and
dbpbtb->prepareResolveUpdateEntries are used so prepareResolveUpdateEntries
receives the full-width ID without truncation.

In `@src/cpu/pred/btb/docs/decoupled_bpred.md`:
- Line 98: Rename the documented field declaration that currently reads
"FetchTargetId fsqID;" to use the FTQ naming convention: change the identifier
to "ftqId" so it becomes "FetchTargetId ftqId;"; update any nearby references in
the same doc block to match ftqId to keep terminology consistent with the FTQ
migration and the FetchTargetId type.
- Around line 109-110: Update the outdated comment that refers to "fetch streams
(FSQ)" to use FTQ/Fetch Target terminology: change the second bullet describing
std::map<FetchTargetId, FetchTarget> FetchTargetQueue to say it manages fetch
targets (FTQ) or Fetch Target Queue, ensuring consistency with FetchTargetQueue,
FetchTargetId, and FetchTarget identifiers and removing the FSQ term.
- Around line 214-219: The FSQ enqueue stage text uses outdated FSQ terminology
(fsqId, FSQ); update the wording in the tryEnqFetchTarget section to match the
FetchTarget migration by replacing FSQ/`fsqId` mentions with the current names
(e.g., FetchTargetQueue identifiers or whatever the new stream id is), keep
references to `tryEnqFetchTarget`, `FetchTarget`, `FetchTargetQueue`, and
`receivedPred` intact, and adjust the bullet points to say "assigns stream id"
or the new identifier name and "increments the stream id for the next stream"
instead of "Increments `fsqId`."
🧹 Nitpick comments (1)
src/cpu/pred/btb/btb_tage.hh (1)

136-137: Minor formatting: missing space after comma.

There's a missing space between entry, and int in the parameter list.

Suggested fix
     void recoverPHist(const boost::dynamic_bitset<> &history,
-                        const FetchTarget &entry,int shamt, bool cond_taken) override;
+                        const FetchTarget &entry, int shamt, bool cond_taken) override;

@XiangShanRobot
Copy link
Copy Markdown

[Generated by GEM5 Performance Robot]
commit: 92878cb
workflow: On-Demand SPEC Test (Tier 1.5)

Ideal BTB Performance

Overall Score

PR Master Diff(%)
Score 20.27 20.27 +0.01 🟢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants