C840 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 40 · Pipelining and hazards

Five instructions in flight, and what gets in their way

A single-cycle processor leaves most of its hardware idle: while the data memory serves a load, the instruction memory, the register file and the ALU have nothing to do. Pipelining puts registers between the stages so that each stage works on a different instruction, like a laundry where the washer starts the next load while the dryer runs. Five instructions are in progress at once and the clock can run several times faster. The catch is that instructions depend on each other: one needs a result another hasn't finished, or a branch changes which instructions should have been started. Those hazards decide how close a real pipeline gets to its ideal.

  • A processor's 1000 ps of logic is split into k equal stages, each followed by a pipeline register that costs r ps. Each extra stage also adds stall cycles from hazards, at the rate h per instruction per stage.
  • The curve is the time per instruction, the cycle time times the CPI. Drag k up from 1: it falls fast at first, then flattens, then rises as register overhead and stalls take over.
  • Set h to 0: deeper is always better, though with diminishing returns. Raise h and the best depth moves down.

Predict

A five-stage pipeline with no stalls runs 1000 instructions. How many clock cycles does it take?

Name it

Five stages

Fetch (read the instruction), Decode (read registers, build the immediate), Execute (ALU), Memory (load or store), Writeback (write the register file). Each instruction moves one stage per cycle:

Cycle1234567
lw t0, 0(a0)FDEMW
add t1, t2, t3FDEMW
sub t4, t5, t6FDEMW

The clock period is set by the slowest stage plus the pipeline register's overhead, Tc=max⁡(tstage)+tpcq+tsetup. N instructions in a k-stage pipeline with no stalls take k+N−1 cycles.

Hazards

  • Data hazards. add t0, ... followed by sub t1, t0, ...: the sub reads t0 in Decode before the add has written it. Forwarding sends the ALU result straight from the pipeline register back to the ALU's input, so no time is lost. A load-use hazard is worse: a loaded value isn't ready until the end of Memory, so an instruction that uses it immediately must stall one cycle even with forwarding. Compilers schedule an independent instruction into that slot when they can.
  • Control hazards. A branch is decided in Execute, by which time two more instructions have been fetched. If the processor predicted "not taken" and the branch is taken, those two are flushed: a 2-cycle penalty.
  • Structural hazards. Two stages needing one resource at once, such as fetching an instruction and loading data from a single memory. Separate instruction and data caches avoid it.

CPI with stalls

CPI=1+Fload-use⋅1+Fbranch⋅Fmispredicted⋅2

with each F the fraction of instructions (or of branches) involved.

Worked example

A design's five stages take 200, 150, 120, 200 and 80 ps, and each pipeline register adds 40 ps. Compare it with the single-cycle version on a billion instructions at a CPI of 1.15.

Show the solution
  1. Single-cycle. 750 ps a cycle at a CPI of 1: 0.75 s.
  2. The pipelined clock. The slowest stage plus a register: Tc=200+40=240ps
  3. The pipelined time. t=109×1.15×240ps=0.276s 2.7 times faster.
  4. Why less than 5. The stages aren't equal, the registers cost time, and stalls raise the CPI.

Answer 0.276 s pipelined against 0.75 s single-cycle, 2.7 times faster.

Worked example

25% of instructions are loads, and 40% of loads are followed at once by an instruction that uses the value. 15% are branches, half of them taken, predicted not taken with a 2-cycle penalty. What is the CPI?

Show the solution
  1. Load-use stalls. 0.25×0.4×1=0.10
  2. Branch penalties. 0.15×0.5×2=0.15
  3. Add them to the base CPI. CPI=1+0.10+0.15=1.25

Answer a CPI of 1.25.

Worked example

Without forwarding, an instruction that uses the result of the one just before it waits 2 cycles, and 30% of instructions do. With forwarding, only loads stall, 1 cycle, and 25% of instructions are loads of which 40% are used at once. Compare the CPIs.

Show the solution
  1. Without forwarding. CPI=1+0.3×2=1.6
  2. With forwarding. CPI=1+0.25×0.4×1=1.1
  3. Compare. A handful of multiplexers and wires make the processor 45% faster.
  4. The stall that remains. A load's data comes out of the memory stage, too late for the next instruction's execute stage; compilers hide it by scheduling an independent instruction into the gap.

Answer a CPI of 1.6 without forwarding and 1.1 with it, 45% faster.

Check

A 5-stage pipeline with no stalls runs 100 instructions.

(a)

How many cycles does it take?

(b)

How many instructions finish per cycle, on average?

Check

A pipeline's slowest three stages take 150, 180 and 200 ps (the others less), and each pipeline register adds 50 ps.

(a)

What is the clock period?

Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.

(b)

What is the clock frequency?

Check

0.2 of a program's instructions are loads, 0.3 of which are used immediately; 0.2 are branches, 0.6 of which are mispredicted at a 2-cycle penalty.

(a)

What is the CPI?

(b)

What percentage of the cycles are lost to mispredictions?

Check

A processor's 800 ps of logic is split into 10 equal stages, each with a 30 ps register, and there are no stalls.

(a)

What is the cycle time?

Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.

(b)

How many times the single-cycle throughput (with one register) is that?

Check

Which pair stalls a five-stage pipeline for a cycle even with full forwarding?

Check

Put the five pipeline stages in order.

  1. Execute in the ALU
  2. Write the result back to the register file
  3. Decode it and read registers
  4. Fetch the instruction
  5. Access data memory

Check

Without forwarding, 30% of instructions wait 2 cycles for the one before. With forwarding only load-use pairs stall 1 cycle: 20% loads, 50% of them used at once.

(a)

What's the CPI without forwarding?

(b)

And with it?

Check

A single-cycle design's T picoseconds of logic is split evenly into k stages, each with o of register overhead.

Step 1

What's the pipelined clock period?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does a load followed immediately by an instruction using its result cost a stall, even with forwarding?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.