Hazards and Stalls
A hazard is anything that stops the next instruction from starting on schedule: it needs a result that isn't ready yet, or the processor doesn't yet know which instruction comes next. Each wasted cycle adds to the average, so stall cycles per instruction, and forwarding results straight between stages removes most of them.
What the symbols mean
- Hazard frequency
The fraction of instructions that run into hazard type j.
- Stall cycles
How many cycles each such hazard costs.
Explanation
A hazard is simply a reason the pipeline can't start the next instruction on schedule, so it has to wait (stall) for a cycle or more. The two big kinds are data hazards, where an instruction needs a value the instruction just ahead of it hasn't produced yet, and control hazards, where the processor doesn't yet know which instruction comes next because a branch hasn't been decided. The formula says: an ideal pipeline finishes one instruction per cycle (the ), and each kind of hazard adds its frequency (how often it happens per instruction) times its cost (how many cycles it wastes). It's the same weighted average as average CPI, applied to wasted cycles.
Why stalls add straight onto CPI. With no hazards, instructions take about cycles (plus the small fill time). Every stall cycle is a cycle in which no instruction finishes, so the total becomes . Divide by :
And the stalls per instruction are a weighted sum over the kinds of hazards: if of instructions each cause 2 stall cycles, that's stalls per instruction.
Data hazards and forwarding. Take two instructions: I1 computes and I2 computes . I2 needs I1's answer. In the 5-stage pipeline, I1 computes the sum in EX during cycle 3 but doesn't write it to the register file until WB in cycle 5, while I2 wants to read its registers in ID during cycle 3. Without help, I2 must wait for the write-back: 2 stall cycles (the register file can write in the first half of a cycle and read in the second).
But the sum exists at the end of cycle 3, sitting in the register between EX and MEM. Forwarding (also called bypassing) adds wires that carry it straight back to the start of EX for I2 in cycle 4. No stall at all.
Forwarding can't fix everything. If I1 is a load, the value arrives from memory only at the end of MEM (cycle 4), but I2 would need it at the start of its EX, also cycle 4. You can't send a value back in time, so I2 waits 1 cycle, a load-use stall. There's an animation further down showing exactly that bubble.
Control hazards. A branch decides where to go next, but in this pipeline the decision is made in EX. By then two more instructions have already been fetched, possibly the wrong ones. The simplest fix is to stall until the branch resolves, costing 2 cycles per branch. With branches that's extra cycles per instruction, a slowdown, which is why the next lesson is about guessing instead.
A common mistake. It's tempting to add up stall cycles per hazard without weighting them by how often they happen: "load-use costs 1 and branches cost 2, so CPI is 4." Each cost has to be multiplied by its frequency per instruction. The other slip is assuming forwarding removes every data stall. It removes the ALU-to-ALU ones, but a load followed immediately by a use of its value still costs a cycle, which is why compilers try to put an unrelated instruction in between.
See it
Worked examples
In a 5-stage pipeline with forwarding, of instructions are loads, and of loads are immediately followed by an instruction that uses the loaded value (1 stall cycle each). of instructions are branches, and each one stalls the pipeline for 2 cycles. What is the CPI?
- 1
Start from the ideal: one instruction per cycle, so CPI begins at . We'll add the stall cycles per instruction for each hazard.
Without forwarding, of instructions depend on the instruction right before them and stall for 2 cycles. Adding forwarding removes those stalls except for load-use cases, which are one third of them and stall for 1 cycle. But the forwarding wires make the clock period longer. What is the speedup from adding forwarding?
- 1
CPI without forwarding: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
of instructions are branches, and the pipeline stalls cycles on every branch. Ignoring other hazards, what is the CPI?
of instructions are loads, and half of them are immediately followed by an instruction that uses the loaded value, costing 1 stall cycle. Ignoring other hazards, what is the CPI?
Forwarding lowers a program's CPI from to without changing the clock. What is the speedup? Give 3 decimal places.
In a program, of instructions are loads and of those loads are followed immediately by a use (1 stall each). A compiler reorders the code and manages to put an unrelated instruction after of those loads, removing their stalls. Ignoring other hazards, what is the speedup from the compiler change? Give 3 decimal places.
Six instructions run through a 5-stage pipeline. Two of them are uses that immediately follow loads, and each causes 1 stall cycle. How many cycles does the whole sequence take?
A pipelined processor has data-hazard stalls of cycles per instruction. In addition, of instructions access memory, of those accesses miss in the cache, and each miss stalls the pipeline for cycles. What is the CPI?
Why does a load followed immediately by an instruction that uses its value still stall for one cycle, even with forwarding?
The hardest one here. Get it, and you've got this lesson.
A pipeline without forwarding stalls cycles when an instruction uses the result of the one right before it ( of instructions) and cycle when it uses the result from two instructions back ( of instructions). Branches ( of instructions) cost stall cycle each. Adding forwarding removes all data stalls except load-use: of the back-to-back dependences involve a load and still cost cycle. Branch costs don't change. Forwarding stretches the clock from ps to ps. What is the speedup from adding forwarding? Give 3 decimal places.
Where it's used
Every pipelined processor has forwarding paths and hazard-detection logic that inserts exactly these bubbles, and compilers schedule instructions to dodge them: they pull an independent instruction into the slot after a load. Performance engineers read hardware counters for "stall cycles" by cause and compute to see which hazard is eating the most time, the same way a factory measures which machine its line waits on.