Skip to content
Tactiverge

School/✦ Research Engineer/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?

About 50 min · 3 goals · Checked by automated tests

1.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.

2.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?

Guess first. Being wrong here is how the surprise works.

3.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

lw r5, 4(r2) / add r6, r5, r1. Which hazard and, with forwarding, how many stall cycles?

5.Watch

→ 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.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

6.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

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.

Worked example 1

lw r1, 0(r2) then add r3, r1, r4, with forwarding. Stalls?

  1. 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.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A pipeline has base CPI 1. Branches are 15% of instructions, 20% of them are mispredicted, costing 2 cycles each. Loads and stores are 20% of instructions, 10% of them miss in the cache, costing 100 cycles each. What is the effective CPI?

Practice 3

Design B doubles the pipeline depth of A, raising the clock 1.6× and the branch penalty from 2 to 5 cycles. Branch fraction 20%, mispredict 10%, base CPI 1 for both, memory ignored. Which is faster?

Practice 4 · a fresh version every time

A pipeline has base CPI 1. Branches are 20% of instructions, 10% of them are mispredicted, costing 15 cycles each. Loads and stores are 35% of instructions, 4% of them miss in the cache, costing 50 cycles each. What is the effective CPI?

Practice 5

On the classic 5-stage pipeline with no forwarding, a chain of 4 ALU instructions each uses the previous one's result. How many total cycles? (Ideal is 8.)

cycles

Practice 6

Why do most processors have separate L1 instruction and data caches?

8.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.

9.Reflect

Concepts: Pipelining, hazards and the Iron Law