Lite / Independent Engineering Research / Week 20
Hazards, forwarding and branch prediction
An instruction needs a number the previous one hasn't finished computing. Does the whole assembly line stop?
Why should I care?
Hazards are where pipelines lose their promise. Forwarding paths, branch predictors and separate instruction and data caches exist purely to fight them, and they occupy a large fraction of a modern core's design effort. Compilers reorder code to dodge them too.
- Identify data (read-after-write), control and structural hazards in a short program.
- Count stall cycles with and without forwarding, including the load-use case.
- Explain branch prediction and compute the CPI cost of mispredictions.
Predict
add r1, r2, r3 is followed immediately by sub r4, r1, r5. With no forwarding (results read from the register file), how many stall cycles?
- 0
- 1
- 2
- 4
Guess first, then reveal
sub must read r1 in its ID stage no earlier than add's WB (with the register file written in the first half of the cycle and read in the second). add's WB is 3 cycles after its ID, so sub waits 2 cycles. Forwarding the ALU result straight into the next EX removes both.
Discover
Every stall cycle adds to CPI, and this calculator shows how much. Set branches to 20%, mispredict to 10% and penalty to 2: read the CPI. Raise the penalty to 15 (a deep pipeline) and watch the branch slice grow. Pin a design, then trade a faster clock for a bigger penalty and read the speed-up.
What the interactive shows (text version)
The Iron Law calculator shows CPI as base + branch stalls + memory stalls. With 20% branches, 10% mispredicted at 2 cycles, and 30% memory instructions missing 5% at 40 cycles, CPI = 1 + 0.04 + 0.6 = 1.64, and T = IC × CPI ÷ f.
Watch (optional)
Watch for: Watch the section on pipelining and branch prediction: note what happens to instructions fetched down the wrong path.
Covers caches, pipelining, branch prediction and multiple cores: the tricks that make processors fast, in one sitting.
“Advanced CPU Designs: Crash Course Computer Science #9” by CrashCourse (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Data hazards
A read-after-write hazard occurs when an instruction needs a register that an earlier, still-in-flight instruction will write. Without help, the reader stalls in ID until the writer reaches WB: 2 bubbles for adjacent instructions in the classic 5-stage pipe. Forwarding (bypassing) wires the ALU output straight back to the ALU input, removing the stall for ALU results.
The load-use stall
A load's data appears only at the end of MEM. Even with forwarding, an instruction that uses it immediately must wait one cycle. Compilers hide this by scheduling an independent instruction between the load and its use, which is why the 'Load then use' program in the pipeline diagram has an unrelated sub in the middle.
Control and structural hazards
A branch is not resolved until EX, so the pipeline has already fetched 2 instructions that may be wrong. Stalling on every branch wastes those cycles always; predict not taken wastes them only when the branch is taken; dynamic predictors learn each branch's habit and are right well over 90% of the time on typical code. A structural hazard arises when two stages need one resource, like a single memory port for fetch and data: hence separate instruction and data caches.
Hazards in the Iron Law
Every stall adds to CPI: CPI = 1 + (data stalls per instruction) + (branch fraction × mispredict rate × penalty) + (memory stalls). Deeper pipelines raise the branch penalty, so a faster clock can lose to a shorter pipe with a better predictor.
Explain it another way
Intuitive
A kitchen line where the next cook needs the sauce the previous cook is still stirring. Either they wait, or the first cook hands the sauce across the counter as soon as it's ready instead of putting it in the fridge first. That hand-over is forwarding.
Technical
Hazard detection logic in ID compares source registers with destination registers of instructions in EX and MEM, and either inserts a bubble (stalls PC and IF/ID, zeroes ID/EX control) or selects a forwarding mux input. Branch predictors (2-bit saturating counters, branch target buffers) let fetch continue speculatively; mispredictions flush the wrong-path instructions.
Mathematical
With branch fraction b, misprediction rate m and penalty P: CPI_branch = b·m·P. For b = 0.2, m = 0.05, P = 15: 0.15 extra CPI. Halving m to 0.025 is worth the same as cutting P from 15 to 7.5.
Applied
Long pipelines in early-2000s desktop processors reached very high clocks but suffered heavy misprediction penalties; later designs returned to shorter pipelines with much better predictors. The trade-off is exactly the one in the Iron Law calculator.
Worked example 1
lw r1, 0(r2) then add r3, r1, r4, with forwarding. Stalls?
- The loaded value exists only after MEM; add's EX must come the cycle after lw's MEM, one cycle later than it would without the dependency.
Worked example 2
Branches are 15% of instructions; a predictor is wrong 8% of the time; the penalty is 3 cycles. Extra CPI?
0.15 × 0.08 × 3 = 0.036. With base CPI 1, effective CPI 1.036.
Common mix-ups
Forwarding removes all data hazards.
✓ It cannot help a load followed immediately by its use: the data does not exist yet.Branch prediction is a guess, so it cannot be relied on.
✓ Wrong guesses are detected and flushed; prediction only affects speed, never correctness.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Take the 'Loop with a taken branch' program in the pipeline diagram. Reorder instructions (on paper) to remove the load-use stall without changing the result, then count the cycles you save over 1,000 iterations with predict-not-taken versus a perfect predictor.
Reflect
Which hazard surprised you most, and which fix (hardware or compiler) do you find more elegant?
Write your answer on paper or in your own notes. Lite never stores what you write.