LightMySky
En anglais

Pipelining and the Cost of a Hazard

Pipelining overlaps instruction stages to increase throughput. In an ideal five-stage model with the same clock, overlap does not shorten an instruction's five-cycle latency. Data hazards may require stalls; forwarding can avoid some waits. Branch mispredictions discard younger wrong-path instructions.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Count total cycles for five instructions in the stated empty, stall-free five-stage pipeline model
  • Spot a data hazard between two neighbouring instructions and say how forwarding helps
  • Explain why a mispredicted branch throws away work

1 · Read

You have compared cache hits and misses. Pipelining overlaps instruction stages, including work that may wait for memory. Throughput counts completed instructions per cycle. Latency counts cycles from an instruction's start to its completion. Overlap can raise throughput without shortening latency in the same-clock model used here. Real designs can use different clock periods.

fetchdecodeexecutememorywrite back
Our ideal model has five stages. Each takes one cycle. An instruction visits every stage; overlap does not skip a stage.

Assume an empty pipeline, one instruction starting each cycle, and no stalls. The first completes after five cycles. Each follower completes one cycle later. For n instructions, the total is 5 + (n - 1) cycles. Three instructions take seven cycles, rather than fifteen without overlap.

I1 finishes: cycle 5I2 finishes: cycle 6I3 finishes: cycle 7

A data hazard occurs when an instruction needs a result that an earlier instruction has not yet made available. Forwarding sends an available result directly to the stage that needs it, before register write-back. For example, an add result ready after execute can feed a following instruction. Forwarding helps only if the result arrives in time.

A load may produce its result later than an add. If the next instruction needs the loaded value before it is available, it must wait even with forwarding. In the model below, the consumer cannot execute until the load result arrives. This is a data-hazard stall.

load result unavailableconsumer waitsresult arrivesconsumer executes

A branch predictor chooses a likely path before a branch is resolved. A correct prediction can avoid waiting. A misprediction discards younger wrong-path instructions and redirects fetching. Their results must not become architectural results of the program. Some effects, such as cache changes, may remain; discarding instructions does not undo every hardware effect.

predict pathbranch resolvesdiscard wrong-path workfetch correct path

For our timing questions, add the stated stall cycles to the no-stall total. A stall cycle delays useful completion in this model. For three instructions with one added stall, the total is 5 + 2 + 1 = 8 cycles. Do not infer a stall count from the word hazard alone; the timing and forwarding rules must be given.

State the pipeline model before counting cycles. Overlap raises throughput; hazards may add waits. Mispredictions discard wrong-path instructions, but may leave cache effects.

Original lesson, questions and flashcards written for this dataset.

2 · Watch

En anglais

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Pipelining and the Cost of a Hazard · Computing, ages 18 to 20 · LightMySky