Load-use hazard and stalls
What is a Load-Use Hazard?
A load-use data hazard occurs when an instruction immediately following a load instruction depends on the loaded data. Unlike ALU-ALU hazards (which forwarding solves), load hazards require a stall because:
- Load completes in MEM stage (end of cycle 4)
- Dependent instruction needs data at the START of its EX stage (start of cycle 4 for the very next instruction)
- Forwarding can't bridge this gap — the data doesn't exist yet when needed (it only appears at the end of cycle 4)
Why Forwarding Alone Fails for Loads
The Timeline Problem:
Cycle: 1 2 3 4 5
lw $2, 0($1) IF ID EX MEM WB ← data available END of cycle 4 (MEM)
add $4, $2, $3 IF ID EX MEM ← needs $2 START of cycle 4 (EX)
Even with forwarding from MEM/WB → EX, the add instruction's EX stage begins at the start of cycle 4, but the load's data is only ready at the end of cycle 4. The data doesn't exist yet when the ALU needs it.
After inserting 1-cycle stall:
Cycle: 1 2 3 4 5 6
lw $2, 0($1) IF ID EX MEM WB
add $4, $2, $3 IF ID (stall) EX MEM ← add held in ID one extra cycle
Now add executes EX in cycle 5. The load's data (available end of cycle 4, held in MEM/WB) is forwarded from MEM/WB → EX at cycle 5. Perfect.
Implementing the Stall: Hardware Mechanism
The hazard detection unit sits between IF and ID stages. When it detects a load-use hazard:
Step 1: Prevent new instruction progress
- Keep PC unchanged (don't increment)
- Keep IF/ID register unchanged (re-read same instruction next cycle)
Step 2: Nullify the dependent instruction
- Force all ID/EX control signals to 0
- This makes the EX stage perform a NOP (no write, no memory access, no operation)
Step 3: Let earlier stages complete
- MEM and WB stages proceed normally
- After 1 cycle, the load's data is available for forwarding
Common Mistakes in Understanding Load-Use Hazards
Performance Impact: CPI Degradation
For an ideal 5-stage pipeline with no hazards, CPI = 1.0. Each load-use hazard adds 1 stall cycle:
If 20% of instructions are loads and 50% of loads have immediate dependencies:
This means 10% performance loss compared to an ideal pipeline.
Mitigation strategies:
- Compiler scheduling: Reorder independent instructions to fill load delay slots
- Out-of-order execution: Hardware dynamically reorders instructions (advanced processors)
- Better caches: Faster memory reduces effective load latency (doesn't eliminate hazard, but improves overall CPI)
Recall Explain to a 12-year-old
Imagine you're making a sandwich assembly line with your friends:
- Person A gets bread from the pantry (like
lwloading from memory) - Person B is supposed to spread peanut butter on that bread immediately after The problem: Person A only brings the bread back at the very end of their turn, but Person B needs the bread at the very start of their turn — and their turns overlap! When Person B reaches the spreading station, Person A hasn't finished bringing the bread yet.
The solution: Person B has to wait (stall) at the spreading station doing nothing for one extra turn. Meanwhile, Person A finishes the pantry trip. Now when Person B finally spreads, the bread is ready.
A "forwarding path" is like Person A handing the bread directly to Person B instead of putting it on the table first — it saves time, but only if Person A already has the bread! We can't forward something that doesn't exist yet, so we must wait.
Connections
- Data hazards and forwarding — Load-use is a special case forwarding can't fully solve
- Pipeline control signals — Stalls force ID/EX control to zero
- Hazard detection unit — Hardware that detects load-use conditions
- Compiler instruction scheduling — Software technique to avoid stalls
- Memory hierarchy and cache — Faster memory reduces load penalty
- Five-stage MIPS pipeline — Architectural context for all hazards
- Branch hazards and prediction — Control hazards also cause stalls
#flashcards/hardware
What is a load-use data hazard?
Why can't forwarding alone solve load-use hazards?
What three conditions trigger a load-use stall?
How is a pipeline stall implemented in hardware?
How many cycles does a load-use hazard stall cost?
Why doesn't a load followed by a store create a hazard?
If 20% of instructions are loads and 40% of loads have immediate dependencies, what is the CPI?
What compiler optimization reduces load-use stalls?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, yaha pe baat samajhne wali ye hai ki jab hum lw (load word) instruction chalate hain, toh memory se data aane mein time lagta hai. Data MEM stage ke end mein ready hota hai (cycle 4 ke khatam pe), lekin jo next instruction hai — jaise add jo usi register ko use karta hai — usko wo data apne EX stage ke shuru mein chahiye hota hai, aur wo bhi cycle 4 mein hi. Matlab data available hone se pehle hi zaroorat pad jaati hai. Aur bhai, hum time mein peeche nahi ja sakte, yaani data ko backward forward nahi kar sakte! Isiliye ise load-use hazard kehte hain.
Ab yaha pe forwarding kaam nahi karti, jaisa ki normal ALU-to-ALU hazards mein karti hai. Kyunki forwarding tabhi kaam karti hai jab data pehle se exist karta ho — par yaha toh data abhi bana hi nahi hai! Isliye hardware ko ek stall ya bubble daalna padta hai, jo basically ek forced NOP (no-operation) hota hai. Ye ek cycle ka delay create karta hai — PC aur IF/ID register ko freeze kar dete hain, aur ID/EX ke control signals ko zero kar dete hain taaki EX stage kuch na kare. Ek cycle ruk jaane ke baad, load ka data ready ho jaata hai aur ab forwarding se aaram se pass ho jaata hai.
Ye cheez isliye important hai kyunki real processors mein ye stalls performance ko affect karte hain — har stall ka matlab ek wasted cycle. Compiler writers isiliye instructions ko smartly reorder karte hain taaki load ke turant baad dependent instruction na aaye, aur stall avoid ho jaaye. Exam ke point of view se bhi, tumhe wo teen stall detection conditions yaad rakhni chahiye: MemRead=1 (load hai), register match ho raha ho, aur register $0 na ho. Ye samajh lo toh pipelining ka poora hazard concept clear ho jaayega.