9. Pipelining and Hazards
Description
Pipelining overlaps the execution of several instructions by splitting each into stages, like an assembly line. The classic RISC-V pipeline has five stages:
IF (Fetch) → ID (Decode / register read) → EX (Execute / address calc) → MEM (Memory access) → WB (Write back)
Pipelining does not shorten the latency of one instruction; it raises throughput (ideally one instruction finishes per cycle, CPI = 1).
Hazards stop the next instruction from executing in its scheduled cycle:
| Hazard | Cause | Typical fixes |
|---|---|---|
| Structural | Two instructions need the same hardware in the same cycle (e.g. one memory port for instruction fetch and data) | Duplicate resources (split I/D caches), stall |
| Data | An instruction needs a result that is not ready | Forwarding (bypassing), stalls, instruction scheduling |
| Control | Branches change the PC before the next fetch is known | Stall, predict not taken, branch prediction, delayed branches |
Data dependences (also called hazards when they cause a stall):
- RAW (read after write): true dependence. The consumer needs the producer's value.
- WAR (write after read): anti-dependence (name dependence).
- WAW (write after write): output dependence (name dependence).
In a simple in-order 5-stage pipeline only RAW hazards occur; WAR and WAW appear with out-of-order execution (Topic 10) and are removed by register renaming.
Key Formulas
Time for n instructions on a k-stage pipeline (no stalls) = (k + n − 1) cycles
Unpipelined time = n · k cycles (if one instruction takes k cycles)
Ideal pipeline speedup → k as n → ∞
Pipelined clock period = max(stage delay) + register overhead
CPI_pipelined = 1 + (stall cycles per instruction)
Speedup from pipelining = Time_unpipelined / Time_pipelined
= (CPI_unpipe × T_unpipe) / (CPI_pipe × T_pipe)
Branch stalls per instruction = branch frequency × misprediction rate × penalty
Steps
To draw a pipeline diagram: (1) write instructions in rows, cycles in columns, (2) place IF, ID, EX, MEM, WB for each instruction, one cycle later than the previous, (3) check each register read against the producing instruction, (4) if the value is not available even with forwarding, insert a stall bubble.
To handle a RAW hazard: (1) ALU result → next ALU instruction: forward from EX/MEM to EX, no stall, (2) load result → next instruction: forward from MEM/WB, one stall (load-use), (3) reorder independent instructions to fill the slot.
Worked Example
A. Speedup with overhead. Unpipelined instruction takes 5 ns total; five equal stages of 1 ns; register overhead 0.2 ns → clock = 1.2 ns. Speedup per instruction rate = 5 / 1.2 = 4.17. For n = 100: unpipelined 500 ns, pipelined (5 + 99) × 1.2 = 124.8 ns → speedup 4.01.
B. Cycle count. n = 100, k = 5: pipelined = 104 cycles, unpipelined = 500 cycles → speedup 4.81 (approaches 5 as n grows).
C. Forwarding removes an ALU stall.
add x1, x2, x3
sub x4, x1, x5 # needs x1: forwarded from add's EX/MEM latch, no stall
D. Load-use hazard (1 stall even with forwarding).
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
ld x1, 0(x2) | IF | ID | EX | MEM | WB | |||
add x3, x1, x4 | IF | ID | stall | EX | MEM | WB | ||
sub x5, x3, x6 | IF | stall | ID | EX | MEM | WB |
The loaded value only exists at the end of MEM (cycle 4), so add must start EX in cycle 5.
E. Scheduling away the stalls.
# Before: two load-use stalls # After: zero stalls
lw x1, 0(x10) # b lw x1, 0(x10) # b
lw x2, 4(x10) # c lw x2, 4(x10) # c
add x3, x1, x2 # STALL (x2) lw x4, 8(x10) # e
lw x4, 8(x10) # e lw x5, 12(x10) # f
lw x5, 12(x10) # f add x3, x1, x2
sub x6, x4, x5 # STALL (x5) sub x6, x4, x5
F. CPI with hazards. 25% loads, 40% of loads are followed immediately by a dependent instruction (1 stall); 20% branches, predictor wrong 10% of the time, 2-cycle penalty. CPI = 1 + 0.25 × 0.4 × 1 + 0.2 × 0.1 × 2 = 1 + 0.10 + 0.04 = 1.14. With "predict not taken" and 60% of branches actually taken: 1 + 0.10 + 0.2 × 0.6 × 2 = 1.34.
G. Structural hazard. Single memory port, 30% of instructions access data: each causes a 1-cycle stall → CPI = 1 + 0.3 = 1.3. Separate instruction and data caches remove it.
Python / Assembly
def load_use_stalls(prog):
"""prog: list of tuples (op, dest, src1, src2...)"""
stalls = 0
for prev, cur in zip(prog, prog[1:]):
if prev[0] == 'ld' and prev[1] in cur[2:]:
stalls += 1
return stalls
prog = [('ld', 'x1', 'x2'), ('add', 'x3', 'x1', 'x4'), ('sub', 'x5', 'x3', 'x6')]
print(load_use_stalls(prog)) # 1
def pipeline_cycles(n, k=5, stalls=0):
return k + n - 1 + stalls
print(pipeline_cycles(100)) # 104
Notes
- A deeper pipeline allows a faster clock but increases branch penalties and hazard costs; there is an optimal depth.
- Common mistake: pipelining does not let "several commands execute in one cycle." More precisely, several instructions are in flight at once (one per stage); completing more than one per cycle needs a superscalar design (Topic 11).
- Forwarding means a result is passed from a pipeline register straight to the unit that needs it, bypassing the register file.
- Quick facts: pipeline stages are fetch, decode, execute, memory access, write-back; a stall is a delay due to hazards; branch prediction minimizes control stalls; data hazards are mitigated by forwarding, stalling or reordering; register renaming removes false dependences.
References: [HP6] Appendix C (C.1-C.3) and Ch. 3 (3.1); [PH-RV] Ch. 4 (4.5-4.8);
All rights remain with the original author (Algo-holics). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me