Sub topic 2
Why pipeline? Overlapping instructions
This chapter explains the idea behind pipelining and what it does and does not improve. It assumes you know what a CPU, a register, memory and a clock are. It also assumes you can read simple assembly-style instructions such as load, store, add and branch, and that you are comfortable with ratio arithmetic. The chapter follows the recap of the instruction cycle. The next chapter splits the work into the five classic stages.
The problem: idle hardware
A simple processor finishes one instruction before it starts the next. Each instruction passes through several kinds of work: fetching it from memory, decoding it, doing the arithmetic, accessing data memory, and writing a result back. The processor has separate hardware for each kind of work.
When the instruction is being fetched, the arithmetic unit does nothing. When it is in the arithmetic unit, the instruction memory does nothing. At any moment, most of the hardware is idle. The processor has paid for all of it, but only one part works at a time.
Pipelining fixes this by starting the next instruction as soon as the first one has left the first piece of hardware.
The assembly-line idea
A car factory does not build one car completely before touching the next. A car moves from the welding station to the paint station, and a new car enters the welding station at once. Every station stays busy, and a finished car leaves at the same rate that new cars enter.
A pipelined processor works the same way:
- The work for one instruction is divided into stages. A stage is one step of the instruction cycle, with its own hardware.
- Each stage handles a different instruction in the same clock cycle.
- At every clock edge, each instruction moves one stage forward and a new instruction enters the first stage.
A timing view
The table below shows four instructions in a five-stage pipeline. Stages are labelled S1 to S5, and each column is one clock cycle. A dash means the instruction is not in the pipeline in that cycle.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| I1 | S1 | S2 | S3 | S4 | S5 | - | - | - |
| I2 | - | S1 | S2 | S3 | S4 | S5 | - | - |
| I3 | - | - | S1 | S2 | S3 | S4 | S5 | - |
| I4 | - | - | - | S1 | S2 | S3 | S4 | S5 |
Read the table in two directions.
- Along a row, you see one instruction moving through all five stages. This takes 5 cycles, the same as in a non-pipelined machine that spends one cycle per stage.
- Down a column, you see what the hardware does in one cycle. In cycle 5, I1 is in S5, I2 in S4, I3 in S3 and I4 in S2. All the stage hardware is working at once.
I1 completes in cycle 5. After that, one instruction completes in every cycle: I2 in cycle 6, I3 in cycle 7, I4 in cycle 8.
Counting cycles
Assume every stage takes one clock cycle, there are k stages, and the n instructions never interfere with each other.
- Without pipelining, each instruction needs k cycles, so n instructions need n × k cycles.
- With pipelining, the first instruction needs k cycles to fill the pipeline. Each of the remaining n − 1 instructions finishes one cycle after the one before it. The total is k + n − 1 cycles.
For the table above, k = 5 and n = 4, so the pipelined machine needs 5 + 4 − 1 = 8 cycles. The non-pipelined machine needs 20.
The following program computes both counts and the ratio between them.
def cycles_unpipelined(k, n):
return k * n
def cycles_pipelined(k, n):
# k cycles to fill the pipeline, then one completion per cycle
return k + n - 1
k = 5
for n in (1, 4, 100, 1_000_000):
u = cycles_unpipelined(k, n)
p = cycles_pipelined(k, n)
print(n, u, p, round(u / p, 2))
# 1 5 5 1.0
# 4 20 8 2.5
# 100 500 104 4.81
# 1000000 5000000 1000004 5.0The ratio rises toward k as n grows. For a single instruction (n = 1) there is no gain at all. For a long run of instructions, the ratio approaches 5, the number of stages. This is the best case. Later chapters show what pulls the real figure below it.
Latency versus throughput
Two quantities are easy to confuse.
- Latency is the time one instruction takes from start to finish.
- Throughput is the number of instructions completed per unit time.
Pipelining improves throughput. It does not improve latency. In the ideal case, one instruction completes per cycle, where before one completed every k cycles. But each instruction still passes through all k stages. In a real design its latency gets slightly worse, because the extra registers between stages add delay (the timing chapter covers this).
What makes the overlap work
The assembly-line picture hides some conditions that the hardware must meet. Each of them matters later when we look at hazards.
- Independent hardware per stage. Two instructions that are in different stages at the same time must not need the same unit in that cycle. If they do, one must wait.
- Roughly equal stage times. All stages share one clock, so the clock period must be long enough for the slowest stage. A stage that takes 90 ns forces every other stage to wait for it, even a stage that needs only 50 ns.
- Independent instructions. If one instruction needs a result that an earlier instruction has not yet produced, the later one cannot proceed on schedule.
- A known next instruction. If a branch has not yet decided where the program goes next, the front of the pipeline does not know which instruction to fetch.
Conditions 1, 3 and 4 correspond to the three kinds of hazard: structural, data and control. This chapter only notes them. Condition 2 is treated in the timing chapter.
Why a regular instruction set helps
Pipelining is much easier when every instruction follows a similar pattern. Designs in the RISC style (fixed instruction length, arithmetic only on registers, memory touched only by load and store) fit the five-stage layout well. Every instruction goes through the same stages in the same order, so the hardware can treat them uniformly. An instruction set with variable lengths and many memory-to-memory forms makes stage timing uneven and is harder to pipeline. GATE questions almost always assume a RISC-style pipeline.
Common errors
- Using k × n for the pipelined time. The correct count is k + n − 1.
- Saying the speedup is always k. That holds only in the limit of many instructions and with no stalls. For small n the ratio is lower, and the n = 4 example above gives 2.5.
- Forgetting the fill time. The first result appears after k cycles, not after 1.
- Believing a longer pipeline is always better. More stages mean a shorter clock period in principle, but they also add register overhead and make hazards costlier. Later chapters quantify both effects.
Exercise
A pipeline has 4 stages, each taking one clock cycle, and no stalls occur. How many cycles does it need to complete 8 instructions, how many would a non-pipelined machine need, and what is the speedup to two decimal places?
Show answer
Pipelined: k + n − 1 = 4 + 8 − 1 = 11 cycles. Non-pipelined: n × k = 8 × 4 = 32 cycles. Speedup = 32 / 11 ≈ 2.91.
Exercise
A student claims that adding a pipeline makes a single instruction execute in fewer clock cycles. Explain whether this is correct.
Show answer
It is not correct. A single instruction still passes through every stage, so it needs at least k cycles, the same as before. Pipelining raises throughput, because after the pipeline fills one instruction completes per cycle. It does not reduce the latency of one instruction.