Sub topic 9
Stalls and bubbles: resolving hazards by waiting
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), the formula of k + n − 1 cycles for n instructions, and the three hazard classes (structural, data, control). It covers the simplest remedy for any hazard: wait until the conflict is gone. Later chapters show how forwarding and branch handling reduce the waiting.
What a stall is
A stall is a cycle in which an instruction cannot move to its next stage, so it stays where it is. Instructions behind it also stay put. Instructions ahead of it keep moving.
A bubble is the empty slot the stall leaves in the later stages. The stalled instruction does not advance, so the stage after it receives a do-nothing instruction (a NOP) in that cycle. The bubble travels down the pipeline and does no work in any stage. The words "stall cycle" and "bubble" describe the same lost cycle, seen from the stalled instruction and from the stages downstream.
How hardware creates a bubble
A hazard detection unit compares register numbers of instructions in the pipeline. When it finds a conflict, it does three things in the same cycle:
- It holds the program counter, so the same instruction is fetched again.
- It holds the IF/ID pipeline register, so the instruction in ID stays in ID.
- It zeroes the control signals going into the ID/EX register. The next stage then receives a NOP: no register write, no memory write, no branch.
This hardware check is called an interlock. The alternative is software scheduling. The compiler inserts explicit NOPs, or reorders independent instructions into the gap. Then the hardware needs no detection logic, but the code depends on the exact pipeline depth.
Worked example: RAW stall without forwarding
Take the sequence from the topic's code example:
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in ID: RAW hazard on r1Assume the register file writes in the first half of a cycle and reads in the second half. Then an instruction can read a register in the same cycle in which the producer writes it. add writes r1 in cycle 5, so sub must be in ID in cycle 5 or later.
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| add | IF | ID | EX | MEM | WB | |||
| sub | IF | ID (stall) | ID (stall) | ID | EX | MEM | WB | |
| Bubble in EX | NOP | NOP |
sub would normally be in ID in cycle 3. It stays in ID for cycles 3 and 4 without moving on, then reads r1 correctly in cycle 5. That costs 2 stall cycles. Two instructions would finish in 6 cycles (5 + 2 − 1) with no stall, and they finish in 8 cycles here.
If the register file does not allow same-cycle write and read, sub must read in cycle 6, and the cost is 3 stalls. GATE questions state this assumption, or you must infer it from the wording. If the question is silent, the split-cycle assumption (2 stalls) is the common convention. State your assumption in your working.
Stalls versus distance
Stalls depend on how many instructions sit between the producer and the consumer. Let d be the distance, where d = 1 means the consumer immediately follows the producer. For an ALU producer and no forwarding:
| Distance d | Stalls (split-cycle register file) | Stalls (no same-cycle read) |
|---|---|---|
| 1 | 2 | 3 |
| 2 | 1 | 2 |
| 3 | 0 | 1 |
| 4 or more | 0 | 0 |
The formulas are max(0, 3 − d) and max(0, 4 − d). Independent instructions placed between producer and consumer fill the gap and remove stalls, which is what compiler scheduling does.
Counting stalls with a program
The function below models an in-order five-stage pipeline with no forwarding. It places each instruction's ID stage as early as its operands allow.
def stalls(instrs, split=True):
"""instrs: list of (dest, sources). Returns stall cycles, no forwarding."""
ready = {} # register -> cycle in which its producer does WB
id_cycle = 1
for dest, srcs in instrs:
id_cycle += 1 # earliest ID: one cycle after the previous ID
for s in srcs:
if s in ready:
need = ready[s] if split else ready[s] + 1
id_cycle = max(id_cycle, need)
if dest:
ready[dest] = id_cycle + 3 # EX, MEM, WB follow ID
total = id_cycle + 3 # cycle of the last WB
return total - (5 + len(instrs) - 1)
prog = [("r1", ["r2", "r3"]), # add r1, r2, r3
("r4", ["r1", "r5"]), # sub r4, r1, r5
("r6", ["r1", "r7"])] # and r6, r1, r7
print(stalls(prog, split=True)) # 2
print(stalls(prog, split=False)) # 3The and instruction adds no stall of its own. By the time sub finally leaves ID, r1 is already written, so and reads it without waiting. A stall for one instruction often covers later readers of the same register.
Stalls from the other hazard classes
Stalls are not only for data hazards.
- Structural hazard. If a single memory serves both instruction fetch and data access, a load or store in MEM blocks the IF of a later instruction. IF waits one cycle, giving a bubble each time a memory instruction passes through MEM.
- Control hazard. Without prediction, the pipeline stalls after fetching a branch until the outcome is known. Every branch then costs the stall cycles between fetch and resolution. Branch handling is the subject of a later chapter.
- Data hazard. As above, the consumer waits in ID.
Stalls change CPI, not the clock
A stall does not lengthen the clock period. It adds cycles to the instruction count in time. In terms of CPI:
CPI = 1 + (average stall cycles per instruction)
Total time for n instructions in a k-stage pipeline is (k + n − 1 + S) × clock, where S is the total number of stall cycles. This matches the pipelined_time function from the performance chapter, where stall_cycles is S.
Example: 100 instructions, 5 stages, and 40 stall cycles in total. Cycles = 5 + 99 + 40 = 144. CPI ≈ 144 / 100 = 1.44. Strictly, the average stall per instruction is 0.4 and the fill time of 4 cycles adds 0.04, so the usual GATE shortcut CPI = 1 + 0.4 = 1.4 holds for long programs.
Common errors
- Counting a stall for each instruction in a dependent chain without checking distance. Only readers close enough to the producer stall.
- Forgetting the assumption about same-cycle register write and read, and giving 3 stalls where the question expects 2, or the reverse.
- Treating WAR and WAW as stall sources in this pipeline. In an in-order five-stage pipeline with writes always in WB, they cannot cause wrong results, so no stall is needed. They matter in out-of-order designs.
- Adding stalls to the clock period instead of to the cycle count.
Exercise
The instructions lw r1, 0(r2) is not used here. Instead consider add r1, r2, r3, then add r4, r5, r6, then sub r7, r1, r4, in a five-stage pipeline with no forwarding and a register file that writes in the first half and reads in the second half of a cycle. How many stall cycles occur, and how many cycles does the sequence take in total?
Show answer
The sub needs r4 from the second add, which is at distance 1, so it needs 2 stalls (max(0, 3 − 1)). Its need for r1 is at distance 2, which requires 1 stall, which the 2 stalls already cover. Total stalls = 2. Total cycles = 5 + 3 − 1 + 2 = 9.
Exercise
A program has 25% of its instructions each followed by a dependent instruction at distance 1, with no forwarding and a split-cycle register file. Ignore all other stalls. What is the effective CPI, and what is the speedup of this pipeline over an unpipelined machine with a CPI of 5 at the same clock?
Show answer
Each such event costs 2 stalls, so average stalls per instruction = 0.25 × 2 = 0.5. CPI = 1 + 0.5 = 1.5. Speedup = 5 / 1.5 ≈ 3.33.