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

S3 · Lesson 42 · Pipelining the core

The five-stage RISC-V pipeline, data and control hazards, forwarding and stalls

Cut the single-cycle core into five stages (fetch, decode, execute, memory, write-back) with registers between them, and five instructions are in flight at once. The clock period drops to the slowest stage, and ideally one instruction finishes every cycle. Instructions depend on each other, though: an add that needs the result of the instruction just ahead would read a stale register, and a branch isn't decided until fetch has already moved on. Forwarding paths, a hazard unit that stalls when it must, and flushing on mispredicted branches keep the pipeline correct, and the cycles they cost decide how close the CPI gets to 1.

  • CPI of a five-stage pipeline against the branch misprediction rate. Solid: with forwarding, the only data stalls are loads followed at once by a use. Dashed: without forwarding, an instruction that uses the result of the one just before stalls 2 cycles, and of the one two before, 1 cycle.
  • L is the fraction of instructions that are loads, u the fraction of loads used by the next instruction, B the fraction that are branches and P the misprediction penalty. d1 and d2 are the fractions of instructions that depend on the instruction one or two ahead.
  • Forwarding removes most of the data stalls. Branches remain: a deeper pipeline raises P, which is why prediction matters more as pipelines get longer.

Predict

In a five-stage pipeline with full forwarding, lw t1, 0(t0) is followed immediately by add t2, t1, t3. How many cycles does the add stall?

Name it

The five stages

StageWorkPipeline register after it
IFfetch from instruction memory, PC + 4IF/ID
IDdecode, read registers, generate immediateID/EX
EXALU, branch compare and targetEX/MEM
MEMdata memoryMEM/WB
WBwrite the register file

With no stalls, N instructions take k+N−1 cycles in a k-stage pipeline. The register file writes in the first half of a cycle and reads in the second, so WB and ID in the same cycle don't conflict.

Data hazards

A read-after-write (RAW) hazard: an instruction reads a register an earlier instruction hasn't written yet.

  • Forwarding (bypassing): multiplexers at the ALU inputs take the value from EX/MEM (the instruction one ahead) or MEM/WB (two ahead) when its destination matches the source register (and isn't x0).
  • Load-use stall: a load's value exists only after MEM, so an immediately following use stalls one cycle. The hazard unit freezes PC and IF/ID and inserts a bubble into ID/EX.
  • Without forwarding, a dependence at distance 1 costs 2 stalls and at distance 2 costs 1.

Control hazards

A branch resolved in EX has fetched two more instructions by then. Predicting not taken and flushing them when wrong costs 2 cycles per misprediction; resolving in ID costs 1. In general the penalty is the number of stages fetched past before the branch resolves.

CPI=1+floadu+fbranchmP

Speedup over the single-cycle core is TsingleCPI⋅Tpipe.

Worked example

A program has 25% loads (40% of them used at once by the next instruction) and 15% branches (30% mispredicted, 2-cycle penalty). What's the CPI, and how much faster is the pipeline at 320 ps than the 945 ps single-cycle core?

Show the solution
  1. CPI. CPI=1+0.25×0.4+0.15×0.3×2=1.19
  2. Time per instruction. 1.19×320=381ps against 945 ps, 2.48 times faster.
  3. Without forwarding. If 30% of instructions depend on the one before and 15% on the one two before: CPI=1+0.6+0.15+0.09=1.84 and the speedup is only 1.6.

Answer a CPI of 1.19, which makes the pipeline 2.48 times faster than the single-cycle core.

Worked example

How many cycles does this sequence take on the five-stage pipeline with forwarding, branch predicted not taken and resolved in EX: lw t1, 0(t0), add t2, t1, t1, beq t2, zero, L (taken), then the target instruction, which is the last?

Show the solution
  1. No hazards. Four instructions would take 5+4−1=8cycles
  2. Load-use stall. The add stalls 1 cycle after the load.
  3. The branch's operand. The add's result is forwarded to the branch with no stall.
  4. The taken branch. It flushes the 2 instructions fetched after it.
  5. Total. 8+1+2=11cycles

Answer 11 cycles.

Worked example

A six-instruction loop runs 1000 times on the five-stage pipeline. Each iteration has one load-use stall and ends in a taken branch, predicted not taken and resolved in EX. How many cycles does it take, and what's the CPI?

Show the solution
  1. One iteration. 6 cycles plus 1 stall plus a 2-cycle flush: 6+1+2=9cycles
  2. The whole loop. Over 1000 iterations that's 9000, plus 4 to fill the pipeline at the start: 9000+4=9004cycles
  3. CPI. 90046000=1.50
  4. Improving it. Moving an independent instruction between the load and its use, and predicting the backward branch taken, would bring it near 1.0.

Answer 9004 cycles, a CPI of 1.50.

Check

How many cycles do 10 instructions take on a 7-stage pipeline with no stalls?

Check

A pipeline with forwarding runs code with 0.2 loads (0.3 of them used at once), 0.2 branches, a 0.2 misprediction rate and a 2-cycle penalty. What's the CPI?

Check

Without forwarding, 0.2 of instructions depend on the one just before and 0.1 on the one two before. Ignoring branches, what's the CPI?

Check

A single-cycle core runs at 800 ps per instruction; its pipelined version has a 250 ps clock and a CPI of 1.4. What's the speedup?

Check

A five-stage pipeline runs 20 instructions with 2 load-use stalls and 3 mispredicted branches at a 3-cycle penalty. How many cycles in all?

Check

A pipeline predicts branches not taken and resolves them in stage 3 (fetch is stage 1). How many cycles does a misprediction cost?

Check

A program has 0.2 loads (0.3 of them used by the next instruction) and 0.2 branches (0.3 of them mispredicted, 1 cycles each). The pipeline runs at 250 ps; the single-cycle core it replaces ran at 900 ps.

(a)

What is the pipeline's CPI?

(b)

How much faster is it than the single-cycle core?

Check

A k-stage pipeline with no stalls runs N instructions.

Step 1

How many cycles does it take?

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 can forwarding remove the stall between an add and an instruction that uses its result, but not between a load and an instruction that uses its value?

Practice

Keep practicing

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