RISC-V 5-Stage Pipeline: IF, ID, EX, MEM and WB
A pipelined processor works on several instructions at once, each in a different stage, the way a production line does. The classic RISC-V pipeline has five stages: fetch (IF), decode (ID), execute (EX), memory (MEM) and write back (WB). Each instruction still takes five cycles, but a new one starts every cycle, so once the pipeline is full one instruction finishes every cycle. This page walks through each stage, draws the cycle diagram, and works out how much faster pipelining really is.
Learning objectives
- Say what each of the five stages does for an add, a load, a store and a branch
- Explain what the pipeline registers between stages hold, and why they are needed
- Draw and read a per-cycle pipeline diagram
- Compute the clock period, the run time and the real speedup of a pipelined design
Prerequisites
- RISC-V instruction formats: what rd, rs1, rs2 and the immediate are
- Flip-flops and the idea of a clock period set by the slowest path
What each stage does
| Stage | add x3, x1, x2 | lw x3, 8(x1) | sw x3, 8(x1) | beq x1, x2, L |
|---|---|---|---|---|
| IF: fetch | read the instruction at PC; PC + 4 | same | same | same |
| ID: decode | read x1 and x2 | read x1; make the immediate 8 | read x1 and x3; immediate 8 | read x1 and x2; branch offset |
| EX: execute | x1 + x2 | address x1 + 8 | address x1 + 8 | compare x1 and x2; target PC |
| MEM: memory | nothing | read data memory | write x3 to memory | nothing |
| WB: write back | write x3 | write the loaded word to x3 | nothing | nothing |
Pipeline registers
Between each pair of stages sits a pipeline register: IF/ID, ID/EX, EX/MEM and MEM/WB. On every clock edge each one captures everything the next stage will need for that instruction: the instruction bits, the register values read, the ALU result, the destination register number and the control signals. Without them, the next instruction entering a stage would overwrite the values the previous one still needs. The five stages and the formats they decode come from the RISC-V base integer ISA; the pipeline itself is an implementation choice, and this classic version is the one most courses use.
A cycle diagram
The diagram below has one row per instruction and one column per clock cycle. Instruction 1 is fetched in cycle 1, decoded in cycle 2, and so on. Instruction 2 starts one cycle later. After 4 cycles of filling, one instruction completes in every cycle: n instructions take n + 4 cycles instead of 5n.
The diagrams in this series use the same timing model as the site's pipeline visualizer. Each instruction's execute cycle is the one after the previous instruction's, unless a hazard holds it back; Part 2 covers that part. Five independent instructions take 9 cycles with no stalls.
Worked example: the real speedup
Pipelining does not make an instruction faster; it makes the clock faster. A single-cycle design must fit fetch, decode, execute, memory and write back into one clock period, so the period is the sum of the stage delays. A pipelined design's period is the slowest stage plus the pipeline register overhead (clock-to-q and setup). Take example stage delays of 200, 150, 200, 250 and 100 ps and a 30 ps register overhead: the single-cycle period is 900 ps, the pipelined one 250 + 30 = 280 ps.
| Instructions | Single-cycle (900 ps period) | Pipelined (280 ps period) | Speedup |
|---|---|---|---|
| 1 | 0.90 ns | 1.40 ns | 0.64 |
| 5 | 4.50 ns | 2.52 ns | 1.79 |
| 1000 | 900.00 ns | 281.12 ns | 3.20 |
| Many | 3.21 at most |
The speedup approaches 3.21, not 5. The MEM stage at 250 ps sets the clock and the other stages wait for it, and every stage pays the 30 ps register overhead. A single instruction is slower pipelined (1.40 ns against 0.90 ns), because it now takes five full periods of the slowest stage. Pipelining wins on throughput, for long runs of instructions.
Common mistakes
- Expecting a 5-stage pipeline to be 5 times faster. Unequal stages and register overhead cap it, and hazards (Part 2) take more.
- Saying pipelining reduces the latency of an instruction. It increases it slightly; it raises throughput.
- Forgetting the fill cycles: n instructions take n + 4 cycles, not n.
- Leaving a value out of a pipeline register. A stage must not read anything a later instruction can overwrite.
- Assuming every instruction uses every stage. A store skips WB and an add does nothing in MEM, but each still passes through so the timing stays regular.
Quick check
1. How many cycles do 20 independent instructions take in the 5-stage pipeline?
20 + 4 = 24: four cycles to fill, then one completes every cycle.
2. Which stage does a store not use, and which does an add not use?
A store writes no register, so its WB does nothing; an add does not touch data memory, so its MEM does nothing.
3. Why is the pipelined clock period not the single-cycle period divided by 5?
The period is set by the slowest stage, not the average one, and every stage also pays the pipeline register overhead.
Exercises
E1 (easy). With the stage delays above, how long does one instruction take on each design?
0.90 ns single-cycle, 1.40 ns pipelined: five periods of 280 ps. Pipelining makes a lone instruction slower.
E2 (easy). Five instructions: how many cycles and how many nanoseconds pipelined, and what speedup?
9 cycles of 280 ps, 2.52 ns, against 4.50 ns single-cycle: a speedup of 1.79. The 4 fill cycles still weigh heavily on a short run.
E3 (medium). Split MEM into two stages of 125 ps each, making a 6-stage pipeline. What is the new period and speedup for 1000 instructions?
The slowest stage is now 200 ps, so the period is 230 ps, and 1000 instructions take 1005 cycles, 231.15 ns: a speedup of 3.89 over single-cycle, up from 3.20. A deeper pipeline also makes hazards cost more cycles, which this count leaves out.
E4 (hard). From how many instructions does the pipelined design finish first?
From 2. One instruction takes 0.90 ns single-cycle against 1.40 ns pipelined, but two take 1.80 ns against (2 + 4) times 280 ps, 1.68 ns.
Interview questions
- Q1. What limits the clock frequency of a pipelined processor? Answer: the slowest stage plus the pipeline register overhead; balancing the stages or splitting the slowest raises it.
- Q2. Why not pipeline into 50 stages? Answer: register overhead grows as a share of each stage, and every hazard costs more cycles to resolve, so CPI rises; past a point the faster clock no longer pays for it.
- Q3. What does the ID/EX pipeline register hold for a load? Answer: the base register value, the sign-extended immediate, the destination register number and the control bits for EX, MEM and WB.
Summary
- IF fetches, ID decodes and reads registers, EX computes, MEM reads or writes data memory, WB writes the result.
- Pipeline registers between stages carry each instruction's values and controls.
- n instructions take n + 4 cycles; the clock is the slowest stage plus register overhead.
- The real speedup is below the number of stages, and a single instruction gets slower.
Related
Sources
Last verified
Every number on this page was computed and checked on 27 November 2026; the cycle diagrams use the same model as the pipeline visualizer.