Sub topic 16
Solving GATE-style pipelining problems
This chapter assumes you know the five stages (IF, ID, EX, MEM, WB), the three hazard classes, and the basic formulas for pipeline time and CPI. Here we combine them into one method you can apply under exam time pressure.
A systematic method
Most GATE pipelining questions fall into four families: timing and speedup, stall counting, effective CPI, and conceptual classification. The same checklist works for all of them.
- Write down every assumption the question gives. These include the number of stages, the latch delay, whether forwarding exists, and when the register file is written and read.
- Identify the question family.
- Apply the matching formula or rule.
- Check units (ns or cycles) and sanity: speedup can never exceed the number of stages.
Pattern 1: timing and speedup
The clock period is set by the slowest stage plus the pipeline-register (latch) delay. A pipeline of k stages finishes n instructions in k + n - 1 cycles. Each stall adds one cycle.
def pipelined_time(stage_delays, latch_delay, n, stall_cycles=0):
# The clock period is set by the slowest stage plus the pipeline-register delay.
clock = max(stage_delays) + latch_delay
k = len(stage_delays)
return (k + n - 1 + stall_cycles) * clock
def non_pipelined_time(stage_delays, n):
return n * sum(stage_delays)
delays = [60, 80, 70, 90, 50] # ns per stage
n = 100
tp = pipelined_time(delays, 10, n) # (5 + 99) * 100 = 10400 ns
tn = non_pipelined_time(delays, n) # 100 * 350 = 35000 ns
print(tp, tn, round(tn / tp, 2)) # 10400 35000 3.37The speedup is 3.37, well below 5. The reasons are unbalanced stages (the 90 ns stage sets the clock) and the 10 ns latch overhead. The non-pipelined machine has no latches, so it uses the plain sum of stage delays. If a question says otherwise, follow the question.
Efficiency is speedup divided by k. Throughput is n divided by total time. As n grows, the speedup approaches the ratio of the non-pipelined instruction time to the pipelined clock period, here 350 / 100 = 3.5.
Pattern 2: counting stalls
Draw the timeline for the producer and the consumer. The rules below assume the standard five-stage pipeline and a register file that is written in the first half of a cycle and read in the second half.
| Producer, consumer adjacent | Without forwarding | With forwarding |
|---|---|---|
| ALU instruction, then dependent instruction | 2 stalls | 0 stalls |
| Load, then dependent instruction | 2 stalls | 1 stall |
| Dependent instruction one slot later | 1 stall | 0 stalls |
If the question says the register file cannot be written and read in the same cycle, add one stall to the no-forwarding cases.
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in ID: RAW hazard on r1Here sub must stall 2 cycles without forwarding. With forwarding from EX/MEM it needs none.
lw r1, 0(r2) # r1 is available only after MEM
add r3, r1, r4 # needs r1 at the start of EX: load-use hazardEven with forwarding, add must stall 1 cycle. Instructions in between reduce the stall, and stalls that already delayed an earlier instruction also count as distance. The function below handles both effects by tracking the cycle in which each instruction reaches ID.
def count_stalls(program, forwarding):
# program: list of (op, dest, sources). Five stages; the register file is
# written in the first half of a cycle and read in the second half.
id_cycle = []
last_writer = {}
for i, (op, dest, srcs) in enumerate(program):
earliest = 2 if i == 0 else id_cycle[-1] + 1
for r in srcs:
if r in last_writer:
j = last_writer[r]
if forwarding:
gap = 2 if program[j][0] == "lw" else 1
else:
gap = 3
earliest = max(earliest, id_cycle[j] + gap)
id_cycle.append(earliest)
if dest:
last_writer[dest] = i
return id_cycle[-1] - (len(program) + 1)
prog = [
("lw", "r1", ["r2"]),
("add", "r3", ["r1", "r4"]),
("sub", "r5", ["r3", "r1"]),
]
print(count_stalls(prog, False), count_stalls(prog, True)) # 4 1Without forwarding, add waits 2 cycles for lw and sub waits 2 more for add, so the total is 4. With forwarding, only the load-use stall between lw and add remains. The sub then finds r3 and r1 available through bypass paths.
Pattern 3: effective CPI
Effective CPI is the ideal CPI of 1 plus the average stall cycles per instruction. Sum frequency times penalty over every stall source.
def effective_cpi(base_cpi, stall_events):
# stall_events: list of (frequency per instruction, stall cycles per event)
return base_cpi + sum(freq * penalty for freq, penalty in stall_events)
# 20% branches with a 2-cycle penalty, 10% load-use stalls of 1 cycle
cpi = effective_cpi(1.0, [(0.20, 2), (0.10, 1)])
print(cpi) # 1.5
print(5 / cpi) # speedup over an unpipelined 5-cycle CPI machine at the same clock: 3.33...For branch prediction, the stall frequency is the branch frequency times the misprediction rate. With 20% branches, 90% accuracy and a 3-cycle misprediction penalty, the CPI is 1 + 0.20 x 0.10 x 3 = 1.06. If a predicted-taken branch also pays a fixed cost, such as waiting for the target address, add it as a separate term.
Pattern 4: conceptual questions
- A hazard caused by two instructions needing the same hardware unit in the same cycle is structural. A single memory port shared by instruction fetch and data access is the usual example.
- A hazard caused by a register dependency is a data hazard. RAW is a true dependency. In a simple in-order five-stage pipeline, WAR and WAW cannot occur, because all writes happen in WB in program order and reads happen early in ID. They matter in out-of-order designs.
- A hazard caused by a branch or jump changing the PC is a control hazard.
- Forwarding removes most RAW stalls but not the load-use stall.
- Delayed branches and prediction reduce control penalties but do not remove them.
Common mistakes
| Mistake | Correction |
|---|---|
| Using the sum of stage delays as the pipelined clock | Use the maximum stage delay plus the latch delay |
| Using n x k cycles for the pipeline | Use k + n - 1, plus stalls |
| Forgetting the same-cycle write and read assumption | Read the question. If absent, add one stall |
| Applying the branch penalty to all instructions | Multiply by the branch frequency |
| Reporting speedup above k | Re-check the CPI and clock terms |
Exercises
Exercise
A five-stage pipeline has stage delays 40, 50, 60, 45 and 55 ns and a latch delay of 5 ns. Find the pipelined execution time for 200 instructions with no stalls, and the speedup over a non-pipelined machine.
Show answer
The clock is 60 + 5 = 65 ns. Cycles are 5 + 200 - 1 = 204, so the time is 204 x 65 = 13260 ns. The non-pipelined time is 200 x 250 = 50000 ns. Speedup = 50000 / 13260, about 3.77.
Exercise
A program has 25% branches, and the predictor is correct for 80% of them. A misprediction costs 4 cycles. In addition, 10% of all instructions suffer a 1-cycle load-use stall. Find the effective CPI and the speedup over an unpipelined machine with CPI 5 and the same clock.
Show answer
Branch stalls = 0.25 x 0.20 x 4 = 0.20. Load-use stalls = 0.10 x 1 = 0.10. CPI = 1 + 0.20 + 0.10 = 1.3. Speedup = 5 / 1.3, about 3.85.
Exercise
How many stall cycles does the sequence "lw r1, 0(r2); add r3, r1, r4; sub r5, r3, r1" need with forwarding, and why?
Show answer
One stall. The add must wait one cycle for the loaded r1 (load-use). After that, the sub gets r3 from the add by forwarding, and r1 is already available, so it needs no further stall.