Data Hazards in Pipelining: Types, Solutions and Worked Examples

Follow one three-instruction trace to see exactly when a load-use pair stalls and when ALU forwarding works. Then compare the same trace without forwarding.

KnowledgeGate Team

Exam prep & CS education

Updated 14 Nov 20266 min read
Data Hazards in Pipelining: Types, Solutions and Worked Examples

Knowing the five pipeline stages is not enough when one instruction depends on another. Should it move, stall, or receive a forwarded value? Test each dependency, trace the cycle-accurate example, and distinguish RAW, WAR, and WAW. Calculate each count with a classic five-stage pipeline and stated timing rules.

Data hazards start with dependencies, not pipeline boxes

A data dependency comes from what the program must compute. A data hazard appears when the pipeline cannot yet satisfy that dependency safely.

For example:

Code
I1: ADD R1, R2, R3
I2: SUB R4, R1, R5

I2 always depends on the R1 produced by I1. Forwarding or enough instruction separation may satisfy it without a stall, so a dependency does not always cause a delay.

The model is IF -> ID -> EX -> MEM -> WB. Fetch happens in IF, register reads in ID, ALU work in EX, memory access in MEM, and register writes in WB. For a broader subject route, use the CS Fundamentals category.

Do not mix data hazards with the other two classes. Two operations competing for one memory port create a structural hazard. Waiting for a branch decision creates a control hazard.

RAW, WAR and WAW: classify by access order

Read the instructions in program order and focus on one register at a time.

Type

Access order

Meaning

Example

RAW

Earlier write, later read

True dependency

ADD R1, R2, R3, then SUB R4, R1, R5

WAR

Earlier read, later write

Anti-dependency

SUB R4, R1, R5, then ADD R1, R2, R3

WAW

Earlier write, later write

Output dependency

MUL R1, R2, R3, then ADD R1, R4, R5

RAW carries a value from the earlier instruction to the later one, so renaming cannot remove it. In WAR, the earlier instruction must read the old value before the later write. In WAW, the final write order must match program order.

A simple single-issue, in-order five-stage pipeline normally exposes RAW hazards, not WAR or WAW hazards. Its reads and writes remain ordered. WAR and WAW become important with out-of-order execution or unequal completion times.

The solution toolbox: stall, forward, schedule or rename

A hardware interlock holds an instruction whose operand is unavailable and injects a bubble. A compiler may place an independent instruction in that gap. A literal NOP is the fallback when no useful move is safe.

Forwarding sends a result from a later pipeline register directly to EX instead of waiting for WB. Common paths are EX/MEM -> EX and MEM/WB -> EX.

Forwarding does not eliminate every stall. Here, a load produces data at the end of MEM, one cycle too late for an immediate consumer's original EX slot. Register renaming solves false WAR and WAW name conflicts, not true RAW timing.

Worked example: one load-use stall, then ALU forwarding

Lock the assumptions before drawing a timing table:

  • Single issue and in-order execution

  • Stages IF-ID-EX-MEM-WB, one cycle each

  • Separate instruction and data access

  • Full forwarding to EX

  • Load data available at the end of MEM

  • ALU data available at the end of EX

  • No cache miss

  • Register WB occurs early enough for an ID read in the same cycle

Use this sequence:

Code
I1: LW  R1, 0(R2)    R2 = 100, Mem[100] = 7
I2: ADD R3, R1, R4   R4 = 5
I3: SUB R5, R3, R6   R6 = 2

The architectural results are:

  1. I1 loads R1 = Mem[100] = 7.

  2. I2 computes R3 = R1 + R4 = 7 + 5 = 12.

  3. I3 computes R5 = R3 - R6 = 12 - 2 = 10.

Pipelining in Computer Architecture explains the equal-stage baseline. For finite-batch clocks and a four-instruction load-use chain, use Pipelining Basics in Computer Architecture: Clock Cycle, Speedup and Hazard Worked Examples. Pipelining Cross-Concept Problems: CPI, Hazards and Cache Stalls combines the same dependency chain with branch and cache penalties. The three-instruction values below isolate RAW timing and compare full forwarding with no forwarding.

Instruction

C1

C2

C3

C4

C5

C6

C7

C8

I1 LW

IF

ID

EX

MEM

WB

I2 ADD

IF

ID

ST

EX

MEM

WB

I3 SUB

IF

ST

ID

EX

MEM

WB

I1 cannot deliver 7 for I2's original EX slot in C4. After one stall, I2 uses it in C5, produces 12, and forwards that result through EX/MEM -> EX to I3 in C6. No second stall is needed.

Cycle grid C1 to C8 for LW, ADD and SUB, with one stall before I2 executes and forwarding arrows carrying R1 = 7, R3 = 12 and R5 = 10.

Count cycles and compare forwarding with no forwarding

For n = 3 instructions and k = 5 stages, the ideal short-trace count is:

n + k - 1 = 3 + 5 - 1 = 7 cycles

The load-use bubble raises the total to 8 cycles. Trace CPI is 8/3 = 2.666..., approximately 2.67. This includes fill and drain, not steady-state operation.

Without forwarding, each consumer waits until its producer writes the register. Under the same-cycle WB-to-ID convention, the schedule is:

Instruction

C1

C2

C3

C4

C5

C6

C7

C8

C9

C10

C11

I1 LW

IF

ID

EX

MEM

WB

I2 ADD

IF

ID

ST

ST

EX

MEM

WB

I3 SUB

IF

ST

ST

ID

ST

ST

EX

MEM

WB

I2 reads R1 when I1 writes it in C5 and enters EX in C6. I3 reaches ID in C6, waits for the R3 write in C8, enters EX in C9, and completes in C11.

No-forwarding trace CPI is 11/3 = 3.666..., approximately 3.67. The speedup is 11/8 = 1.375x. If same-cycle WB and ID are forbidden, the schedule changes, so state the convention first.

Full-forwarding schedule finishing in 8 cycles beside the no-forwarding schedule finishing in 11, with CPI 2.67 versus 3.67.

Traps that cause wrong hazard answers

  • Do not label every repeated register as RAW. Check whether the earlier access is a read or write and whether the later access is a read or write.

  • Do not assume full forwarding means zero stalls. The immediate load-use pair above still needs one bubble.

  • Do not add a second stall between I2 and I3. The ALU result is ready for the next EX cycle.

  • Do not count only bubbles when total cycles are requested. Include fill and drain.

  • Keep the held instruction separate from the bubble injected into EX.

Mark when each producer makes its value available and when each consumer needs it. Compare those cycles instead of memorising a stall count.

How exam questions test data hazards

Practice usually takes four useful shapes: identify dependency pairs, choose a forwarding path, complete a cycle table, or calculate stalls, cycles, CPI, and speedup under the timing rules a question states.

Try this compact classification drill:

Code
I1: ADD R7, R2, R3
I2: MUL R8, R7, R4
I3: SUB R7, R5, R6
  • I1 -> I2 is RAW on R7: I1 writes it, then I2 reads it.

  • I2 -> I3 is WAR on R7: I2 reads it, then I3 writes it.

  • I1 -> I3 is WAW on R7: both instructions write it.

In the simple in-order pipeline, only the RAW pair can require a runtime wait. An out-of-order design must also preserve program meaning around the WAR and WAW name dependencies, often through register renaming.

The short version and the next study step

  • RAW carries a value that a later instruction truly needs.

  • WAR and WAW are ordering constraints caused by reused register names.

  • Forwarding reduces waiting, but an immediate load-use pair can still stall.

  • A cycle count is valid only under its stated timing rules.

If your goal is GATE preparation, GATE Guidance by Sanchit Sir provides the structured route. Redraw both schedules above without looking at the tables, then change one timing assumption and trace the consequences.