Data produced in EX stage (for ALU ops) → available in cycle n+2 (EX at n, MEM at n+1, WB at n+2)
Data needed in ID stage by dependent instruction
If dependent instruction is in cycle m, it needs data by cycle m+1 (ID stage)
Stalls required: (n+2)−(m+1)=n−m+1
For consecutive instructions (m=n+1): Stalls = n−(n+1)+1=0 stalls? Wrong! This ignores that data isn't written to register file until WB completes.
Correct calculation: If instruction i is in EX at cycle n, result written at cycle n+2 (end of WB). If instruction i+1 is in ID at cycle n+1, it reads at cycle n+1. Gap = (n+2)−(n+1)=1 cycle... but register file read happens before clock edge where write occurs. Actual gap = 2 cycles for ALU-to-ALU dependency.
if (EX/MEM.RegWrite
and EX/MEM.RegisterRd ≠ 0
and EX/MEM.RegisterRd = ID/EX.RegisterRs)
then ForwardA = 10
else if (MEM/WB.RegWrite
and MEM/WB.RegisterRd ≠ 0
and MEM/WB.RegisterRd = ID/EX.RegisterRs
and not(EX/MEM hazard condition)) // Priority check
then ForwardA = 01
else ForwardA = 00
Why the priority check? If both EX/MEM and MEM/WB match, we want the more recent value (EX/MEM), so MEM/WB forwarding only happens when EX/MEM doesn't match.
ForwardB logic is identical, but compares with ID/EX.RegisterRt.
3.1.05-Register-file-design - Hardware that forwarding bypasses
6.2.03-Compiler-optimization-techniques - Instruction scheduling to minimize hazards
Recall Explain to a 12-Year-Old
Imagine you're building Lego spaceships on an assembly line. You have 5 stations: grab pieces (IF), check instructions (ID), snap pieces together (EX), add special parts from a box (MEM), and put the finished section on the shelf (WB).
Now, Station 3 is building a red wing, and Station 2 (right behind it) needs that same red wing to attach to the body. But the wing isn't on the shelf yet—it's still being built at Station 3!
Bad solution: Stop the whole line and wait until the wing reaches the shelf. Slow!
Smart solution (forwarding): Station 3 just hands the wing directly to Station 2. Why put it on the shelf first if the next person needs it right now?
But sometimes you need a piece from the special parts box (memory). That box is slow—you have to walk over, unlock it, and find the piece. Even with forwarding, you still have to wait one turn because the piece literally doesn't exist until you open the box. That's a load-use hazard.
Forwarding is like teamwork: pass the result directly to whoever needs it next instead of following every rule about putting stuff away first.
#flashcards/hardware
What is a data hazard in a pipeline? :: When an instruction needs a register value that a previous instruction hasn't written yet, because the previous instruction is still in the pipeline. The dependent instruction would read stale data.
What are the three types of data hazards?
RAW (Read After Write - true dependency), WAR (Write After Read - anti-dependency), WAW (Write After Write - output dependency). RAW is the most common in simple pipelines.
What is forwarding/bypassing?
A technique that routes computed results directly from pipeline registers (EX/MEM or MEM/WB) to the ALU inputs of dependent instructions, avoiding the need to wait for write-back to the register file.
What is a load-use hazard?
When an instruction immediately following a load needs the loaded value. Even with forwarding, one stall cycle is required because the data doesn't exist until the load's MEM stage completes.
What are the two main forwarding paths in a 5-stage pipeline?
EX/MEM to EX stage (for results one cycle old) and MEM/WB to EX stage (for results two cycles old). EX/MEM has priority when both match.
Why can't forwarding eliminate load-use hazards?
The loaded data doesn't exist until the end of the MEM stage, but the dependent instruction needs it at the start of the EX stage (one stage earlier). The data can't be forwarded before it exists.
How many stall cycles are needed for a load-use hazard with forwarding?
One stall cycle. Without forwarding, it would be two or three cycles depending on the architecture.
What is the forwarding unit?
Combinational logic that monitors pipeline registers, compares destination and source registers, and generates ForwardA/ForwardB control signals for ALU input multiplexers.
Why does EX/MEM forwarding have priority over MEM/WB forwarding?
Because EX/MEM contains the more recent result. If two instructions write the same register, the dependent instruction should use the newer value.
What does ForwardA = 10 mean?
Forward the ALU result from the EX/MEM pipeline register to ALU input A (the first operand). 10 is the encoding for EX/MEM forwarding.
What is instruction scheduling in the context of hazards?
A compiler optimization that reorders independent instructions to fill load delay slots and minimize pipeline stalls while preserving program semantics.
What condition creates an EX-to-EX forwarding opportunity?
When EX/MEM.RegWrite = 1, EX/MEM.RegisterRd ≠ 0, and EX/MEM.RegisterRd equals either ID/EX.RegisterRs or ID/EX.RegisterRt.
Why do we check RegisterRd ≠ 0 in forwarding logic?
Register 0 is hardwired to zero in architectures like MIPS and RISC-V, so writes to it don't create real dependencies. No forwarding is needed for R0.
What is a pipeline bubble?
A NOP (no operation) inserted into the pipeline to stall execution, effectively creating an empty pipeline stage where no useful work is done.
How does forwarding improve CPI?
By eliminating most ALU-to-ALU data hazard stalls. Typical improvement: from CPI ≈ 1.975 (stall-only) to CPI ≈ 1.125 (with forwarding) a 1.76× speedup.
Dekho yaar, pipeline ek assembly line jaisa hai jahan multiple instructions ek saath different stages mein chal rahe hote hain. Problem tab aati hai jab ek instruction ko previous instruction ka result chahiye, lekin wo result abhi tak register mein likha hi nahi gaya. Jaise agar ADD instruction R1 mein answer daal raha hai, aur uske turant baad SUB ko wahi R1 padhna hai, toh SUB apne ID stage mein pahunch jaata hai lekin ADD ne abhi tak WB (write back) kiya hi nahi hota. Isko hum data hazard kehte hain, aur sabse common type hai RAW (Read After Write) jahan ek instruction wahi cheez padhna chahta hai jo dusra abhi likh raha hai. Bina kisi fix ke, SUB purana ya stale data utha lega aur galat computation ho jaayega.
Ab iska ek seedha-saada solution hai stalling — matlab pipeline mein "bubbles" ya NOPs daal do taaki dependent instruction thoda wait kare jab tak data ready na ho jaaye. 5-stage pipeline mein ALU-to-ALU dependency ke liye tumhe roughly 2 cycles rukna padta hai, kyunki data tab tak register file mein properly write nahi hota jab tak WB stage complete na ho jaaye. Lekin dhyaan rakho — ye maan lena ki "sirf ek cycle stall karna kaafi hai" ek common galti hai, kyunki register file ka read write se pehle ho jaata hai clock edge par, isliye actual gap zyada hota hai. Ye samajhna zaroori hai warna tum stall count galat lagaoge.
Ye topic isliye matter karta hai kyunki stalling se performance kaafi gir jaati hai — 3 wasted cycles ka matlab hai teen guna slowdown sirf ek chhoti si instruction sequence ke liye. Real processors mein isiliye forwarding (ya bypassing) use hoti hai, jahan result ko directly EX stage se le liya jaata hai bina register file ka wait kiye. Agar tum data hazards ki root cause samajh loge, toh aage forwarding, pipeline optimization, aur modern CPU design ke concepts bahut aasaan lagenge. Ye foundation hai jispe pura efficient processor performance khada hota hai.