source

02 Pipelining And Hazards

Pipelining and Hazards

Prerequisites: 01-Introduction-and-Metrics Learning Goals: Understand how pipelining improves throughput, what types of hazards arise, and how they are detected and resolved.


Pipelining in a Processor

Pipelining overlaps the execution of multiple instructions to improve throughput.

5-Stage Basic Pipeline

StageDescription
FetchRetrieve instruction from memory
Read/DecodeDecode instruction and read registers
ALUExecute arithmetic/logic operation
Memory AccessLoad or store data in memory
WriteWrite result back to registers

Pipelining the instructions takes the same total time per instruction, but throughput is improved.


Pipelining CPI

In an ideal pipeline with no hazards, CPI = 1.

When an instruction must wait at a pipeline stage:

As the number of stalls (delays) increases, CPI increases.

Overall CPI Formula

Overall CPI=Ideal CPI+Stall cycles per instruction\text{Overall CPI} = \text{Ideal CPI} + \text{Stall cycles per instruction}

For branch mispredictions: Overall CPI=CPIprogram+MispredictionsInstructions×Penalty\text{Overall CPI} = \text{CPI}_\text{program} + \frac{\text{Mispredictions}}{\text{Instructions}} \times \text{Penalty}


Pipeline Stalls and Flushes

Pipeline Flush


Data Dependencies

A data dependence occurs when an instruction needs data produced by an earlier instruction.

Types of Data Dependencies

TypeAlso CalledTrue Dependency?Causes Hazard?
RAW (Read After Write)Flow / True DependenceYesYes
WAW (Write After Write)Output DependenceNo (False)Sometimes
WAR (Write After Read)Anti-DependenceNo (False)Sometimes
RAR (Read After Read)Not a dependenceNever
  • RAW = true dependency: program semantics require this ordering
  • WAW and WAR = false (name) dependencies: caused by register reuse, not by actual data flow — can be eliminated by register renaming

Control Dependencies

A control dependence arises when an instruction’s execution depends on the outcome of a branch.


Dependencies vs. Hazards

ConceptCaused ByAlways a Problem?
DependencyThe programNo — many dependencies don’t cause hazards
HazardProgram + PipelineYes — hazards cause incorrect execution

Not every dependency becomes a hazard. A RAW dependency only causes a hazard if the dependent instruction reaches its read stage before the producing instruction has finished its write stage.


Handling Hazards

Step 1: Detect

Identify dependencies that will actually cause a hazard in this pipeline.

Step 2: Remove

Three strategies:

MethodUsed ForHow It Works
FlushControl dependenciesRemove wrong instructions; fetch correct ones
StallData dependenciesHold dependent instruction until data is ready
Forward (Fix)Data dependenciesBypass: route the computed value directly to where it’s needed without waiting for register writeback

Forwarding (Option 3) does not always work — e.g., a load followed immediately by a dependent instruction still requires a 1-cycle stall (load-use hazard).


How Many Pipeline Stages?

When adding more stages:

  1. More potential hazards are introduced
  2. Penalty per hazard increases (more stages to flush/stall)
  3. Less work per stage → shorter cycle time possible

The Iron Law says: balance CPI and cycle time.

Optimization GoalIdeal Pipeline Depth
Performance only30–40 stages
Performance + Power10–15 stages

Summary

Key Takeaways:

Common Patterns:

See Also: 03-Branch-Prediction, 04-ILP-and-Register-Renaming Next: 03-Branch-Prediction