Control Hazards & Branch Handling
Day 3 of 5 — branches, flushes, and the cost of guessing wrong
Control hazards trip up more candidates than data hazards, because the fix (flushing) sounds simple but is easy to get subtly wrong. Here's what interviewers actually probe for.
In This Post
- Q14. What is a control hazard?
- Q15. Where is branch outcome typically resolved in a simple 5-stage design, and what does that cost?
- Q16. How does a pipeline flush actually work at the RTL level?
- Q17. How would you reduce the branch penalty from 2 cycles to 1?
- Q18. What is branch prediction, and is it required in a basic pipeline?
- Q19. How does a JAL (unconditional jump) differ from a branch in terms of hazard handling?
Q14What is a control hazard?
The pipeline doesn't know which instruction to fetch next until a branch or jump's outcome and target are resolved — but by then, it has already speculatively fetched the next instructions assuming "not taken" (or whatever default it picked).
Q15Where is branch outcome typically resolved in a simple 5-stage design, and what does that cost?
In the EX stage — the ALU performs the subtraction needed for a BEQ/BNE comparison, and the zero flag decides the outcome. Because resolution happens in EX, the two instructions already fetched into IF/ID and ID/EX are wrong on a taken branch and must be discarded. That's a 2-cycle branch penalty every time a branch or jump is taken.
Q16How does a pipeline flush actually work at the RTL level?
A branch_taken_flush signal, asserted when EX determines a taken branch (or an unconditional jump), forces IF/ID to reset to zero on the next clock edge (clearing the speculatively fetched instruction) and forces ID/EX's control signals into a "do nothing" bubble state, while simultaneously redirecting the PC to the real branch/jump target.
Q17How would you reduce the branch penalty from 2 cycles to 1?
Move branch resolution earlier, into the ID stage, by adding a dedicated comparator there instead of waiting for the shared ALU in EX. The catch: you then need forwarding paths into ID, not just into EX, since the operands for that earlier comparison might themselves still be in-flight results.
Q18What is branch prediction, and is it required in a basic pipeline?
No, it's not required — a basic design can implicitly assume "not taken" by always fetching PC+4 sequentially until EX says otherwise. That's the simplest possible static prediction. Real CPUs use static schemes (e.g. "backward branches taken, forward not taken") or dynamic 2-bit saturating counters and branch history tables to guess correctly more often and shrink the average penalty.
Q19How does a JAL (unconditional jump) differ from a branch in terms of hazard handling?
JAL is always taken, so it always causes a flush — there's no "taken vs. not taken" uncertainty, only the target address needs computing. JAL also writes a value back (PC+4, the return address) into its destination register, which BEQ/BNE never do.
Quick Recap
- Control hazard = not knowing the next PC until a branch resolves
- Resolving in EX costs 2 flushed cycles per taken branch/jump
- A flush overwrites control signals into a bubble — it doesn't remove the instruction
- Moving resolution to ID cuts the penalty to 1 cycle, at the cost of earlier forwarding
RISC-V Interview Q&A — 5-Day Series
- Day 1
Fundamentals - Day 2
Data Hazards - Day 3
Control Hazards - Day 4
Decode & Verify - Day 5
Trade-offs
Note: links for days not yet published will go live as each post is released —
swap the href="#" placeholders for the real post URLs once published.

Comments
Post a Comment