4.1.20 · D3Computer Architecture (Deep)

Worked examples — Hazard mitigation — stalling, forwarding - bypassing, branch prediction

4,120 words19 min readBack to topic

Before we start, we need two things: the timeline convention and the field notation used in every forwarding condition below.


The scenario matrix

Every case this topic can throw, and which example covers it.

# Case class Concrete trigger Covered by
A Producer 1 ahead (EX-hazard) add then dependent add Ex 1
B Producer 2 ahead (MEM-hazard) one gap instruction between Ex 2
C Double writer, newest-wins two producers write same reg Ex 3
D Load-use, unavoidable stall lw then immediate use Ex 4
E Degenerate: write to $zero rd = $0 Ex 5
F No hazard at all dependency ≥ 3 apart Ex 6
G Two operands, two latches at once rs from EX/MEM, rt from MEM/WB Ex 7
H Control hazard: branch flush mispredict, flush the grid Ex 8
I Word problem + limits: CPI / real / Ex 9
J 2-bit predictor on a loop taken×N then not-taken Ex 10
K Exam twist: load-use hidden by scheduling compiler reorders Ex 11

The figures ahead draw the cycle grid so you can see where a value is born and where it is needed.


Case A — Producer exactly 1 instruction ahead (EX-hazard forward)

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

Case B — Producer 2 instructions ahead (MEM-hazard forward)


Case C — Two writers, same register (newest-wins)

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

Case D — Load-use: the stall forwarding cannot remove

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

Case E — Degenerate: producer writes $zero


Case F — No hazard at all (dependency ≥ 3 apart)


Case G — Two operands, two latches simultaneously

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

Case H — Control hazard: a branch misprediction flush


Case I — Word problem + limiting cases (CPI)


Case J — 2-bit saturating predictor on a loop


Case K — Exam twist: hide the load-use stall by scheduling


Recall Self-test (reveal after guessing)

Producer 1 ahead needs which latch? ::: EX/MEM (EX-hazard forward). Producer 2 ahead needs which latch? ::: MEM/WB (MEM-hazard forward). What does EX/MEM.rd = ID/EX.rs mean in English? ::: "The register the instruction one-ahead-of-me will write equals the one I'm reading as operand 1" — forward it. Two writers to same reg — who wins? ::: The closer one (EX/MEM), i.e. newest write. State the stall rule. ::: , =birth cycle, =need cycle (start of EX). Why does lw→use cost a stall even with forwarding? ::: ; the value is born after the deadline and forwarding can't go backward. Can one instruction forward two operands from two different latches? ::: Yes — rs from MEM/WB and rt from EX/MEM in the same cycle (Case G). Branch penalty for a not-taken predictor when the branch is taken and resolves in EX? ::: 2 flushed instructions ⇒ . CPI formula with branches? ::: . Mispredictions for a 2-bit counter on a loop of N taken + 1 exit? ::: Just 1 (hysteresis absorbs the single anomaly).


See also: Pipelining basics — 5-stage MIPS · Datapath and control signals · Out-of-order execution and Tomasulo · Caches and memory hierarchy · Amdahl's Law and CPI analysis · Compiler instruction scheduling