5.2.7Processor Datapath & Pipelining

Load-use hazard and stalls

2,699 words12 min readdifficulty · medium

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:

  1. Load completes in MEM stage (end of cycle 4)
  2. Dependent instruction needs data at the START of its EX stage (start of cycle 4 for the very next instruction)
  3. 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:

CPIpipeline=1.0+(stall frequency)×1\text{CPI}_{\text{pipeline}} = 1.0 + (\text{stall frequency}) \times 1

If 20% of instructions are loads and 50% of loads have immediate dependencies:

CPI=1.0+(0.20×0.50)×1=1.10\text{CPI} = 1.0 + (0.20 \times 0.50) \times 1 = 1.10

This means 10% performance loss compared to an ideal pipeline.

Mitigation strategies:

  1. Compiler scheduling: Reorder independent instructions to fill load delay slots
  2. Out-of-order execution: Hardware dynamically reorders instructions (advanced processors)
  3. 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:

  1. Person A gets bread from the pantry (like lw loading from memory)
  2. 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


#flashcards/hardware

What is a load-use data hazard?
A hazard that occurs when an instruction immediately following a load depends on the loaded data. It requires a stall because the data isn't available until the end of the MEM stage, but the dependent instruction needs it at the start of its EX stage in the same cycle.
Why can't forwarding alone solve load-use hazards?
Forwarding can only route data that already exists. A load's data becomes available at the end of the MEM stage (cycle 4), but the next instruction needs it at the start of its EX stage (also cycle 4). Forwarding can't send data backwards in time.
What three conditions trigger a load-use stall?
1) ID/EX.MemRead = 1 (load in EX stage), 2) ID/EX.RegisterRt matches IF/ID.RegisterRs or RegisterRt (register dependency), 3) ID/EX.RegisterRt ≠ 0 (not the zero register).
How is a pipeline stall implemented in hardware?
1) Keep PC unchanged (don't fetch new instruction), 2) Keep IF/ID register unchanged (re-read same instruction), 3) Zero all ID/EX control signals (make EX stage a NOP), 4) Let MEM and WB stages proceed normally.
How many cycles does a load-use hazard stall cost?
Exactly 1 cycle. After the stall, forwarding from MEM/WB to EX bridges the remaining gap.
Why doesn't a load followed by a store create a hazard?
A store uses its source (store-data) register in the MEM stage (cycle 5 if it immediately follows the load), not the EX stage. The load produces its result by the end of cycle 4, so forwarding from MEM/WB to MEM works without a stall.
If 20% of instructions are loads and 40% of loads have immediate dependencies, what is the CPI?
CPI = 1.0 + (0.20 × 0.40) × 1 = 1.08. Each load-use stall adds 1 cycle, and they occur for 8% of all instructions.
What compiler optimization reduces load-use stalls?
Instruction scheduling — reordering code to place independent instructions after loads, filling the "load delay slot" with useful work instead of stalls.

Concept Map

data ready END of MEM

needs data START of EX

creates

forwarding cannot fix

requires

inserts

detects and triggers

checks

MemRead=1

Rt matches Rs or Rt

Rt not zero

freezes PC and IF/ID

then

resolves

lw loads from memory

Timing gap in cycle 4

Next instruction

Load-use hazard

Data does not exist yet

Pipeline stall / bubble

Forced NOP

Hazard detection unit

Stall conditions

Load in EX

Register used next

Real dependency

Delay 1 cycle

Forward MEM/WB to EX

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.

Go deeper — visual, from zero

Test yourself — Processor Datapath & Pipelining

Connections