Data Hazards & Forwarding
Day 2 of 5 — where most interviews spend the most time
Data hazards are the heart of pipeline design, and forwarding is the single mechanism interviewers most want you to be able to draw on a whiteboard. This part covers RAW hazards, the forwarding unit, and the one case forwarding alone can never fix.
In This Post
- Q7. What is a RAW hazard? Give a concrete example.
- Q8. How does forwarding (bypassing) solve RAW hazards?
- Q9. Where exactly does forwarding happen, and from where?
- Q10. Why doesn't forwarding alone solve every data hazard?
- Q11. What is a load-use hazard? Why is it special?
- Q12. How is a load-use hazard detected and resolved?
- Q13. Why might a register file need its own internal write-forwarding logic, on top of the pipeline-level forwarding unit?
Q7What is a RAW hazard? Give a concrete example.
Read-After-Write: an instruction needs to read a register that an earlier, still-in-flight instruction hasn't written back yet. Example: addi x1,x0,5 followed immediately by add x3,x1,x2 — the add needs x1 while the addi producing it is still several stages from writeback.
Q8How does forwarding (bypassing) solve RAW hazards?
Instead of waiting for the value to be written to the register file and read back out, the value is routed directly from the pipeline register where it currently lives (EX/MEM or MEM/WB) straight into the ALU input that needs it — one or two cycles earlier than the register file would naturally supply it.
Q9Where exactly does forwarding happen, and from where?
In a dedicated forwarding unit, feeding two 3-way muxes on the ALU's operand inputs in the EX stage. It compares EX's source registers against the destination registers latched in EX/MEM and MEM/WB. EX/MEM takes priority over MEM/WB when both match, since it holds the more recently produced value.
Q10Why doesn't forwarding alone solve every data hazard?
Because forwarding can only supply a value once it has actually been computed. If the very next instruction needs a value before it's even produced, there's nothing yet to forward — you have to buy time with a stall instead.
Q11What is a load-use hazard? Why is it special?
It's when the instruction right after a lw uses the loaded register. The load doesn't have its data until the end of its MEM stage, but the following instruction needs it as an ALU input during its own EX stage — one cycle before the load even reaches MEM. No forwarding can move data backward in time, so this is the one hazard that requires a stall.
Q12How is a load-use hazard detected and resolved?
A hazard-detection unit checks: is the instruction sitting in ID/EX a load, and does its destination register match either source register of the instruction currently in ID? If so, it stalls — freezing the PC and IF/ID for one cycle while injecting a bubble into ID/EX, effectively re-running the ID-stage decode one cycle later once the load data is ready.
Q13Why might a register file need its own internal write-forwarding logic, on top of the pipeline-level forwarding unit?
It handles a same-cycle WB-then-ID read/write race: an instruction writing back in the exact cycle a later instruction is reading that same register in ID. The EX-stage forwarding unit only reaches instructions relative to the EX stage — it doesn't cover an ID-stage read happening concurrently with a WB-stage write. It's a distinct, narrower hazard from the EX-stage RAW cases above.
Quick Recap
- RAW hazard = reading a register before an earlier instruction has written it
- Forwarding reroutes a value straight from EX/MEM or MEM/WB into the ALU input
- Load-use hazards can't be forwarded away — they need a 1-cycle stall
- Register-file write-forwarding is a separate, narrower fix from the EX forwarding unit
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