Module 3 · 12 min read · Free chapter
Instruction pipelining and hazards
A k-stage pipeline runs n instructions in (k + n − 1) cycles, so the speed-up nk/(k + n − 1) approaches k. Hazards cause stalls: forwarding removes most data stalls but a load-use pair still needs one, and branches add frequency times penalty to the CPI.
Start from zero: the tiffin line
Picture a school kitchen packing 500 tiffins. If one cook fills each box completely, adding rice, dal, sabzi, roti and the lid before touching the next box, and each item takes one minute, then every box takes five minutes. All 500 boxes then need 2,500 minutes.
Now line up five helpers, each adding one item and passing the box on. The first box still takes five minutes to come out, but after that a finished box leaves every minute. The 500 boxes now take about 504 minutes, almost five times faster.
A processor does the same with instructions, and the trick is called pipelining. Each instruction is split into stages, and while one instruction is being executed, the next is being decoded and the one after that is being fetched.
Why it matters. Pipelining is the main reason a modern processor finishes close to one instruction per clock cycle. Its catch, which this chapter studies, is that some instructions must wait for others, and every wait is lost speed.
The five-stage pipeline
The classic pipeline has five stages, each taking one clock cycle. IF fetches the instruction from memory, ID decodes it and reads its registers, and EX does the ALU work. MEM reads or writes data memory for loads and stores, and WB writes the result into the register file.
Between stages sit pipeline latches (registers such as IF/ID and ID/EX) that hold each instruction's partial results. They let every stage work on a different instruction in the same cycle without mixing them up.
Cycle 1 2 3 4 5 6 7 8
I1 IF ID EX MEM WB
I2 IF ID EX MEM WB
I3 IF ID EX MEM WB
I4 IF ID EX MEM WB
This space-time diagram shows the main facts at a glance. The first instruction still needs 5 cycles, which is the latency, but from cycle 5 onwards one instruction finishes every cycle, which is the throughput. Four instructions finish in 8 cycles instead of 20.
Why it matters. Almost every pipeline question in GATE is solved by drawing this diagram. Practise it until you can place stalls and branches in it without thinking.
Speed-up and throughput numericals
Let a pipeline have stages and cycle time , and let it run instructions with no stalls. The first instruction takes cycles and each of the other instructions adds one more cycle.
As becomes large, approaches , so a -stage pipeline can be at most times faster. Throughput approaches instructions per second, and efficiency is , the fraction of stage slots doing useful work.
Worked example 1. A 5-stage pipeline with = 2 ns runs 100 instructions. Then ns and ns, so .
Real stages are not equal. The clock must suit the slowest stage, and each latch adds its own delay, so the cycle time is the largest stage delay plus the latch delay. The non-pipelined machine has no latches, so it takes the plain sum of the stage delays.
Worked example 2. Five stages take 150, 120, 160, 140 and 130 ps, and each latch adds 10 ps. So ps, while one non-pipelined instruction takes ps. For 1,000 instructions the times and speed-up are as follows.
Why it matters. The gap between 4.10 and the ideal 5 comes from unequal stages and latch delays, and designers balance stages to close it. GATE numericals ask for exactly these four numbers: cycle time, total time, speed-up and throughput.
Structural, data and control hazards
The ideal pipeline assumes that each instruction can enter its next stage every cycle. A hazard is anything that breaks this, forcing the pipeline to stall, which inserts an empty slot called a bubble. There are three kinds.
A structural hazard happens when two instructions need the same hardware in the same cycle. For example, with a single memory port, a load in its MEM stage and a later instruction in its IF stage both want memory. Separate instruction and data caches remove this common case.
A data hazard happens when an instruction needs a value that an earlier instruction has not yet produced. Dependences come in three types, named by the order of the two accesses.
- Read after write (RAW): the second instruction reads a register that the first writes, as in ADD R1, R2, R3 followed by SUB R4, R1, R5, and this is a true dependence.
- Write after read (WAR): the second instruction writes a register that the first reads, which is a name clash rather than a real flow of data.
- Write after write (WAW): both instructions write the same register, so their order must be kept.
In a simple in-order five-stage pipeline, registers are read in ID and written in WB, always in program order. So WAR and WAW cannot go wrong there, and only RAW causes stalls; the other two matter in out-of-order machines.
A control hazard comes from branches. The pipeline fetches the next instructions before it knows whether the branch is taken, so if the guess is wrong those instructions must be thrown away.
Why it matters. Each hazard type has its own cure: more hardware for structural hazards, forwarding for data hazards and prediction for control hazards. GATE often gives a code fragment and asks you to list every RAW dependence.
Stalls and forwarding
Take ADD R1, R2, R3 followed at once by SUB R4, R1, R5. ADD writes R1 in its WB stage, in cycle 5, but SUB wants to read R1 in its ID stage, in cycle 3. Suppose the register file is written in the first half of a cycle and read in the second half; then SUB can read R1 in cycle 5 at the earliest, so it needs two stall cycles.
Cycle 1 2 3 4 5 6 7 8
ADD IF ID EX MEM WB
SUB IF -- -- ID EX MEM WB (no forwarding)
SUB IF ID EX MEM WB (with forwarding)
Forwarding, also called bypassing, removes most of these stalls. The result of ADD already exists at the end of its EX stage, in the EX/MEM latch, so extra wires carry it straight to the ALU input for SUB in cycle 4. With forwarding from the EX/MEM and MEM/WB latches, back-to-back ALU instructions need no stall at all.
Loads are the exception. In LOAD R1, 0(R2) followed by ADD R4, R1, R5, the loaded value appears only at the end of MEM, in cycle 4, but ADD wants it at the start of its EX, also in cycle 4. So one stall is unavoidable even with forwarding, and this is called the load-use hazard.
Worked example. Five instructions run on a five-stage pipeline with forwarding, and one load-use pair causes the only stall. Total cycles are , against 9 for an ideal run.
Compilers reduce load-use stalls by scheduling, which means moving an independent instruction into the slot between the load and its use. If no such instruction exists, the hardware inserts the bubble by itself.
Why it matters. Forwarding is cheap and removes most data stalls, which is why every real pipeline has it. GATE questions state whether forwarding exists and how the register file is read and written, and those two lines decide the answer.
CPI with stalls, branch penalty and prediction
An ideal pipeline has a CPI (cycles per instruction) of 1. Stalls add to it, so the effective CPI is one plus the average stall cycles per instruction. For balanced stages, the speed-up over a non-pipelined machine that takes cycles per instruction follows directly.
A branch is known to be taken only when it is resolved, often in EX. The instructions fetched after it in the meantime are wasted if the branch is taken, and the number of lost cycles is the branch penalty . If a fraction of instructions are branches and the pipeline always stalls, then ; with 20 percent branches and , CPI becomes 1.6.
Predict-not-taken keeps fetching the next instructions and pays the penalty only when the branch is taken. Static schemes use a fixed rule, such as predicting backward branches as taken, because loops branch backwards. Dynamic schemes remember history, and a 2-bit saturating counter changes its guess only after two wrong guesses in a row, so a loop exit does not spoil the next run of the loop.
A delayed branch takes a different route. The instruction right after the branch, in the delay slot, always executes, and the compiler fills it with useful work so that nothing is wasted.
Worked example. In a five-stage pipeline with forwarding, 30 percent of instructions are loads and 40 percent of loads are followed at once by a user of the value. Also, 20 percent are branches with under predict-not-taken, and 60 percent of branches are taken.
The speed-up over a non-pipelined machine taking 5 cycles per instruction at the same clock is .
Why it matters. Branches are about one instruction in five in ordinary code, so their penalty is the biggest single loss in many pipelines. Deeper pipelines make larger, which is why good prediction becomes more important as clocks get faster.
Descriptive lens: how written papers ask this
Written papers for engineering services, IT officer posts and state Assistant Programmer exams ask about this topic in four ways:
- Explain pipelining with a space-time diagram and derive the speed-up formula.
- Classify hazards with an example of each and state how each is removed.
- Analyse a code fragment, mark its dependences and count the stalls with and without forwarding.
- Discuss branch handling methods such as prediction and delayed branches.
Practice questions (our own, not previous-year questions).
- Derive the speed-up of a -stage pipeline for instructions and show that it can never exceed . (10 marks)
- Explain structural, data and control hazards with one example each, and describe a hardware or software remedy for each. (10 marks)
- For the sequence LOAD R1, 0(R2); ADD R3, R1, R4; SUB R5, R3, R6; STORE R5, 0(R7), draw the five-stage timing with and without forwarding and count the cycles. (15 marks)
Answer plan for question 1.
- Define a -stage pipeline with cycle time and state the assumption of no stalls and equal stages.
- Show with a space-time diagram that the first instruction finishes after cycles and each later one adds one cycle.
- Write the pipelined time as and the non-pipelined time as .
- Divide the two to get .
- Take the limit as grows to show that approaches but never exceeds it, because the denominator is always at least .
- Remark that unequal stages, latch delays and hazards keep the real speed-up below .
Quick revision
- A -stage pipeline keeps instructions in flight, raising throughput but not cutting latency.
- The five stages are IF, ID, EX, MEM and WB, separated by latches.
- Pipelined time is , and speed-up tends to .
- With unequal stages, the cycle time is the slowest stage plus the latch delay.
- Hazards are structural, data and control, and in a simple in-order pipeline only RAW data hazards stall.
- Forwarding removes ALU-to-ALU stalls, but a load-use pair still needs one stall.
- Effective CPI is one plus stall cycles per instruction, and a branch adds to it.
- Predict-not-taken pays only on taken branches, and a 2-bit counter changes after two wrong guesses.