Sub topic 3
The five-stage pipeline: IF, ID, EX, MEM, WB
This chapter assumes you know the basic instruction cycle (fetch, decode, execute), what registers and memory are, and simple load/store-style assembly. It introduces the five-stage pipeline used in most GATE CS questions. Timing diagrams, clock period and speedup get their own chapters.
Why five stages
A single instruction needs several kinds of work: get it from memory, understand it, compute something, touch data memory, and store a result. A pipeline gives each kind of work its own hardware stage. While one instruction uses the ALU, the next one can be decoded and a third one fetched.
The classic RISC pipeline (the MIPS-style design used in textbooks and GATE) has five stages:
| Stage | Name | Main job |
|---|---|---|
| IF | Instruction Fetch | Read the instruction at the address in the PC. Compute PC + 4. |
| ID | Instruction Decode | Decode the opcode. Read source registers. Sign-extend the immediate. |
| EX | Execute | The ALU computes a result, a memory address, or a branch condition and target. |
| MEM | Memory access | Loads read data memory. Stores write it. Other instructions do nothing here. |
| WB | Write Back | Write the result into the destination register. |
What each stage does in detail
IF: instruction fetch
The PC supplies an address to instruction memory. The instruction comes out and is stored in the IF/ID pipeline register. In parallel, an adder computes PC + 4 so that the next sequential instruction can be fetched in the next cycle. For a taken branch, the PC is later overwritten with the branch target, which is the source of control hazards.
ID: instruction decode and register read
The control logic reads the opcode and works out what the instruction needs. At the same time, the register file reads the source registers named in the instruction. These two jobs happen in parallel because the register numbers sit in fixed bit positions in a RISC format. The 16-bit immediate field is sign-extended to 32 bits. Many designs also resolve branches here, which shortens the branch penalty (covered in the control hazards chapter).
EX: execute
The ALU does the real work. What it computes depends on the instruction type:
- Arithmetic and logic: the ALU applies the operation to two register values, or to a register and an immediate.
- Load and store: the ALU adds the base register and the offset to form the effective address.
- Branch: the ALU compares the registers, and a separate adder computes the target address.
MEM: memory access
Only loads and stores do work here. A load sends the address from EX to data memory and receives the data. A store sends the address and the value to write. Every other instruction passes through MEM without doing anything. It still occupies the stage, which keeps all instructions the same length and keeps the pipeline simple.
WB: write back
The result (the ALU output, or the loaded data) is written into the destination register. Stores and branches write no register, but they still pass through this stage.
Pipeline registers
Between every pair of stages sits a pipeline register (also called a latch): IF/ID, ID/EX, EX/MEM and MEM/WB. At each clock edge, each register captures the outputs of the stage before it. It holds everything the later stages need, which includes:
- the instruction's control signals,
- the register values read,
- the ALU result,
- the destination register number.
The destination register number travels with the instruction all the way to WB. Without that, the result written in WB could not be matched to the instruction that produced it.
Which instructions use which stages
Not every instruction does useful work in all five stages. This matters when you reason about hazards.
| Instruction type | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
| R-type (add, sub) | fetch | read regs | ALU op | idle | write result |
| Load (lw) | fetch | read base | compute address | read memory | write loaded value |
| Store (sw) | fetch | read base and value | compute address | write memory | idle |
| Branch (beq) | fetch | read regs | compare, compute target | idle | idle |
Two consequences follow:
- A load's data is only available at the end of MEM. An R-type result is available at the end of EX. This difference is the root of the load-use hazard.
- A uniform five-stage length for all instructions avoids two instructions arriving at WB in the same cycle.
A worked example
Consider these three instructions, with no hazards between them:
lw r1, 0(r2)
add r4, r5, r6
sw r4, 8(r7)Each takes five cycles, and each starts one cycle after the previous one:
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| lw | IF | ID | EX | MEM | WB | ||
| add | IF | ID | EX | MEM | WB | ||
| sw | IF | ID | EX | MEM | WB |
In cycle 3 three instructions are in the pipeline at once: lw is in EX, add is in ID and sw is in IF. Three instructions finish in 7 cycles instead of 15. The general formula for n instructions in a k-stage pipeline is k + n - 1 cycles. Here that is 5 + 3 - 1 = 7.
def pipeline_cycles(k, n, stalls=0):
# The first instruction takes k cycles; each later one adds one cycle.
return k + n - 1 + stalls
print(pipeline_cycles(5, 3)) # 7
print(pipeline_cycles(5, 100)) # 104What the pipeline does not change
Pipelining improves throughput, the number of instructions finished per unit time. It does not make a single instruction faster. The latency of one instruction stays at least five cycles. With a latch delay added to every stage, a single instruction actually takes longer than it would on a non-pipelined machine.
The pipeline only reaches one instruction per cycle when every stage is busy every cycle. Each hazard breaks that, and the later chapters measure the cost.
Common errors
- Counting cycles as 5 × n. Overlap means the answer is k + n - 1.
- Assuming a store or branch skips stages. In the standard model every instruction passes through all five.
- Assuming a load's value is ready after EX. It is ready after MEM.
- Forgetting that the clock period is set by the slowest stage, so a balanced pipeline matters.
- Mixing up where the register file is read (ID) and written (WB). The two can happen in the same cycle on different instructions.
Exercises
Exercise
A 5-stage pipeline (IF, ID, EX, MEM, WB) executes 20 instructions with no stalls. How many clock cycles does it take, and in which cycle does the 20th instruction reach WB?
Show answer
Total cycles = k + n - 1 = 5 + 20 - 1 = 24. The 20th instruction starts IF in cycle 20 and finishes WB in cycle 24, so it reaches WB in cycle 24.
Exercise
In the five-stage pipeline, at the end of which stage is the result of "add r1, r2, r3" available, and at the end of which stage is the result of "lw r1, 0(r2)" available? Why does the difference matter?
Show answer
The add result exists at the end of EX. The lw value exists only at the end of MEM, one stage later. The difference matters because a following instruction that needs the value must wait one extra cycle after a load. This is the load-use hazard.
Exercise
Why does a store instruction still pass through the WB stage even though it writes no register?
Show answer
The standard pipeline gives every instruction the same five-stage length. This keeps the control simple and avoids two instructions needing a shared resource, such as the register file write port, in the same cycle. The store simply does nothing in WB.