Yeh page tumhara self-test workbench hai data hazards aur forwarding ke liye. Har problem L1 (Recognition) se L5 (Mastery) tak graded hai. Problem padho, khud try karo, phir collapsible solution kholo. Har level ke baad ek [!mistake] callout hai jo us level ka sabse common trap steel-man karke batata hai.
Shuru karne se pehle, vocabulary fix kar lete hain taaki koi bhi symbol bina earn kiye na aaye.
Prerequisites agar yeh sab shaky lag raha hai: 5.2.01-Five-stage-pipeline-basics aur 3.1.05-Register-file-design.
ADDR1 apne WB stage mein likhta hai. SUBR1 apne ID stage mein padh ta hai.
Cycles align karo: ADD cycles 1–5 mein IF ID EX MEM WB occupy karta hai. SUB ka ID naturally cycle 3 mein hai.
Write-then-read register file ke saath, SUBusi cycle mein padh sakta hai jab WB hota hai (cycle 5) kyunki write pehle complete hoti hai. Toh SUB ka ID cycle 5 tak push karna hoga.
Iska natural ID cycle 3 tha → cycle 5 tak push karo → 2 bubbles. ✅
stalls=(WB cycle of i)−(natural ID cycle of j)=5−3=2
Recall Solution 2.1... err, 2.2
ADD ka ALU result EX ke end (cycle 3) par exist karta hai, cycle 4 mein EX/MEM register mein baitha hai. SUB ko yeh apne EX ke start (cycle 4) par chahiye. Forwarding wire ise exactly time par deliver kar deti hai.
0 stalls. ✅ Yahi forwarding ka poora fayda hai ALU→ALU dependencies ke liye.
Recall Solution 2.3
Cycle-align karo: I1 EX cycle 3 mein, toh I1 MEM mein (cycle 4) aur WB mein (cycle 5) hai. I3 EX cycle 5 mein hai.
Cycle 5 mein, I1 ka result MEM/WB register mein aa chuka hai (yeh EX/MEM do cycles pehle chhod chuka tha).
Toh SUB MEM/WB se forward karta hai. 0 stalls — independent OR ne conveniently gap fill kar diya.
LW ka data MEM ke end (cycle 4) par appear karta hai. ADD ko yeh apne EX ke start (cycle 4) par chahiye. Yeh same cycle mein overlap karte hain — tum ek value ko same cycle mein exist hone se pehle backward forward nahi kar sakte.
Toh 1 bubble insert karo: ADD ka EX cycle 5 tak push karo. Ab LW ka data MEM/WB register mein hai aur cleanly forward hota hai.
1 stall, MEM/WB se forward. ✅ Yeh ek hazard hai jise forwarding akele poori tarah hide nahi kar sakta — dekho 6.2.03-Compiler-optimization-techniques jisme compilers is slot ko fill karne ke liye code reorder karte hain.
Recall Solution 3.2
Cycle-align karo: I2 EX cycle 4 mein → iska result EX/MEM mein cycle 5 mein hai. I1 ka purana result MEM/WB mein cycle 5 mein hai. I3 ka EX cycle 5 mein hai.
SUB ko sabse recentR1 padhna chahiye, jo I2 ka hai. Forwarding unit priority rule use karta hai: EX/MEM forwarding, MEM/WB ko override karta hai.
Toh EX/MEM se forward karo (I2 ki value). MEM/WB se lena stale I1 value pakad leta — ek subtle bug. ✅
Recall Solution 3.3
Input A (Rs side) ke liye EX/MEM condition check karo:
RegWrite=1∧Rd=5=0∧Rd=5=Rs=5 → sab true.
Toh ALU input AEX/MEM se forward hota hai. MEM/WB bhi match karta hai, lekin priority EX/MEM ko deti hai.
Input B: Rt = 9, na Rd = 5 se match karta hai na kisi se → input B normally register file se padha jaata hai.
I2→I3 (R3): I2 ek load hai, I3 immediately next instruction hai jo ise use kare → classic load-use → 1 stall, phir MEM/WB se forward.
I1→I3 (R2): I1 ka data I3 se kaafi pehle available hai (I3 2 instructions baad hai, plus stall) → koi extra stall nahi, forward ho jaata hai.
I3→I4 (R4): ALU result → store. Store ko R4 apne MEM stage ke liye chahiye; forwarding usse time par deliver kar deta hai → 0 stalls.
Total = 1 stall cycle. ✅ Sirf tight load-use pair (I2→I3) kuch cost karta hai.
Recall Solution 4.2
Load-use gap mein ek independent instruction move karo. Yahan hum swap kar sakte hain taaki dono loads dependent ADD se pehle finish ho jaayein... lekin woh already adjacent hain. Trick yeh hai ki doosre load ko uske use se alag karo:
Sirf inhi chaar instructions ke saath koi independent instruction hoist karne ke liye nahi hai, toh honest answer yeh hai: is exact block ke liye 1 stall irreducible hai. 0 reach karne ke liye, compiler ko zyada bade window se independent kaam dhundna hoga — jaise koi baad wala address computation upar le aana:
Exercise 2.3 se, I3 ko R1MEM/WB se milta hai (I1 WB mein hai jab I3 EX mein hai). Woh path hata do aur I3 ko feed nahi ho sakta → use 1 cycle stall karna padega jab tak register file khud R1 write-then-read se provide kare.
Toh MEM/WB forwarding hatana area ke badle exactly 2 instructions apart wali har dependency par ek stall laata hai. ✅
Recall Quick self-quiz
ALU→ALU dependency, no forwarding, kitne stalls ::: 2
Load-use dependency, with full forwarding, kitne stalls ::: 1
Jab do writes same register ko target karein, kaunsa path jeetta hai ::: EX/MEM (most recent)
Kaunsa stage source registers padhta hai ::: ID
Kaunsa pipeline register same-cycle EX→next-EX forward ko feed karta hai ::: EX/MEM