4.1.20 · D1Computer Architecture (Deep)

Foundations — Hazard mitigation — stalling, forwarding - bypassing, branch prediction

2,031 words9 min readBack to topic

This page assumes you have seen nothing. We build every word, every clock, every arrow the parent note leans on, one at a time, each on top of the one before it.


0. The clock — the heartbeat behind everything

Before instructions, before pipelines, there is the clock.

The picture: imagine a metronome. On each click, every worker in a factory takes one step and freezes. Nothing happens between clicks — the world is snapshotted tick by tick.

Why the topic needs it: every diagram in the parent (cyc 1 | 2 | 3 …) is measured in cycles. A "stall" is a wasted tick; "throughput" is answers per tick. Without the clock, none of those words mean anything.

Figure — Hazard mitigation — stalling, forwarding - bypassing, branch prediction

1. What is an "instruction"? What is a register?

The picture: think of registers as 32 numbered cups on the CPU's desk. An instruction like add $t1, $t0, $s1 means: take the number in cup $t0, add the number in cup $s1, drop the result into cup $t1.

Why the topic needs it: the whole forwarding machinery is just comparing "the rd I am about to write" with "the rs/rt someone else wants to read." If you don't know which slot is which, the forwarding formulas are gibberish.


2. lw, add, sub, beq — the four instructions on the parent page

The picture: add/sub are desk work (fast). lw is "walk to the filing cabinet and fetch a folder" (slow, one extra stage). beq is a fork in the road.

Why the topic needs it: the parent's two headline problems are the load-use hazard (born from lw being slow) and the branch penalty (born from beq being a fork). Each traces back to one of these instructions behaving differently from a plain add.


3. The 5 pipeline stages: IF · ID · EX · MEM · WB

The picture — the assembly line: five workers in a row. At any tick, 5 different instructions occupy the 5 stages. See the figure: the diagonal shows each instruction sliding one stage rightward per cycle.

Figure — Hazard mitigation — stalling, forwarding - bypassing, branch prediction

Why the topic needs it: hazards happen because instruction A is still in (say) MEM while instruction B has reached EX and wants A's answer. You cannot see that collision without the staircase picture of overlapping stages.

Recall Why does overlap create throughput?

Without overlap, one instruction finishes before the next starts (5 cycles each). With overlap, once the line is full, one instruction completes every cycle ::: because all 5 workers are busy on 5 different instructions at once.


4. The ALU

The picture: a box with two input wires (A, B) and one output wire. Whatever numbers arrive on A and B get combined this tick, and the answer leaves on the output wire at the end of EX.

Why the topic needs it: forwarding is literally "which wire feeds ALU input A?" — the register file, or a shortcut from a later stage. The mux (§5) that chooses is the whole trick.


5. Pipeline latches, RegWrite, and the mux

Between each pair of stages sits a latch — a wall of tiny boxes that snapshots everything at the end of a cycle so the next stage sees it next cycle.

The picture: the forwarding mux sits right in front of ALU input A. Three wires arrive — (1) the normal value from the register file, (2) a shortcut from EX/MEM, (3) a shortcut from MEM/WB. A selector picks the freshest correct one.

Figure — Hazard mitigation — stalling, forwarding - bypassing, branch prediction

Why the topic needs it: the parent's forwarding formulas are just the selector logic for that mux. Decoded:

  • means ANDall three must be true.
  • "the ahead-instruction actually writes a register" (RegWrite), and
  • "it isn't the junk register $0" (rd ≠ 0, because writing $0 means nothing), and
  • "the register it writes is exactly the one I want to read" (rd = rs).

When all three hold, flip the mux to take the shortcut.


6. Branches, PC, and "resolve"

The picture: a road with a fork. The PC is the arrow saying "go here next." At a branch we reach a fork but the signpost (condition result) isn't lit until the branch reaches EX. Meanwhile IF has already grabbed the next 2–3 instructions down one road — possibly the wrong one.

Why the topic needs it: the branch penalty = the instructions we fetched down the wrong road and must flush (throw away). Predicting the fork direction early is what §4 of the parent is about.


7. Probability & the CPI formula's symbols

The parent's CPI formula uses , , . These are just fractions and counts.

Why the topic needs it: this is the scoreboard. Every mitigation (predict-not-taken, 2-bit predictor) is judged by how much it shrinks , hence CPI.


Prerequisite map

Clock cycle

5 stages IF ID EX MEM WB

Instruction and registers

ALU inputs A and B

Pipeline latches RegWrite rd

Forwarding mux selector

Forwarding and stalling

lw is slow

Branch PC resolve

Branch prediction

b m p probabilities

CPI scoreboard

Hazard mitigation topic

Ready to go deeper? See Pipelining basics — 5-stage MIPS, Datapath and control signals, and the parent topic note. For the scoreboard math, Amdahl's Law and CPI analysis; for filling stall slots, Compiler instruction scheduling; for the advanced fix, Out-of-order execution and Tomasulo and Caches and memory hierarchy (why lw is slow).


Equipment checklist

Cover the right side and test yourself:

One tick where all hardware does one step, then advances together
A clock cycle
The tiny named box holding one number inside the CPU
A register (e.g. $t0)
Register that is always 0 and never written
$0 / $zero
In add $t1,$t0,$s1, which is the destination?
$t1 (rd)
The five pipeline stages in order
IF, ID, EX, MEM, WB
The stage where the ALU runs
EX
The stage where lw actually gets its data
MEM
The stage where a result is written into a register
WB
LATCH.rd notation means
read the rd field stored inside that pipeline latch
The 1-bit flag "does this instruction write a register?"
RegWrite
A hardware switch that picks one of several input wires
A multiplexer (mux)
What means in the forwarding condition
logical AND — all parts must be true
Why we check rd ≠ 0 before forwarding
$0 is always zero; forwarding it would be meaningless
Register holding the address of the next instruction to fetch
The PC (Program Counter)
When a branch "resolves"
when its taken/not-taken outcome and target are finally known
In CPI = 1 + b·m·p, what is ?
the misprediction rate (fraction of branches guessed wrong)
Why b, m, p multiply
cost happens only when it IS a branch AND is mispredicted — AND = multiply