BIT151 ยท 2082 ยท 5 marks
What are different pipelining hazards? Explain along with their solution.
What are different pipelining hazards? Explain along with their solution. [5]
Pipelining Hazards
Definition
Pipelining hazards are situations that prevent the next instruction in the instruction stream from executing during its designated clock cycle, causing the pipeline to stall.
Types of Pipelining Hazards
1. Structural Hazards
Definition: Arise when the hardware cannot support all possible combinations of instructions in simultaneous overlapped execution (resource conflict).
Example: Two instructions needing the same memory unit or ALU at the same time.
IF ID EX MEM WB
IF ID EX MEM WB
^
Both need memory at same time --> CONFLICT
Solutions:
- Resource duplication: Use separate instruction memory and data memory (Harvard architecture).
- Pipeline stalling (bubble insertion): Insert a NOP (no-operation) cycle to delay one instruction.
2. Data Hazards
Definition: Arise when an instruction depends on the result of a previous instruction that has not yet completed execution.
Types:
| Type | Description |
|---|---|
| RAW (Read After Write) | Instruction reads a register before a previous instruction writes to it (most common) |
| WAR (Write After Read) | Instruction writes before a previous one reads |
| WAW (Write After Write) | Two instructions write to the same register out of order |
Example (RAW):
ADD R1, R2, R3 ; writes R1
SUB R4, R1, R5 ; reads R1 -- hazard!
Solutions:
- Forwarding (Data Bypassing): Route the result directly from the output of one stage to the input of another without waiting for WB stage.
- Pipeline Stalling / Bubbles: Insert NOP cycles (stall) until the required data is available.
- Compiler-based reordering: Reorder instructions so that independent instructions fill the delay slots.
3. Control Hazards (Branch Hazards)
Definition: Arise due to branch instructions (conditional/unconditional jumps). The pipeline fetches the next sequential instruction before knowing whether a branch is taken, causing incorrect instructions to enter the pipeline.
Example:
BEQ R1, R2, LABEL ; branch instruction
ADD R3, R4, R5 ; fetched but may not be needed
Solutions:
- Branch Prediction: Predict whether a branch will be taken (static or dynamic) and fetch accordingly.
- Static: Always predict taken or always predict not taken.
- Dynamic: Use a branch history table (BHT) to predict based on past behavior.
- Delayed Branching: Execute one or more instructions after the branch (delay slot) that are independent of the branch outcome.
- Flushing the Pipeline: If a wrong instruction is fetched, flush (discard) it and fetch the correct one (costly but correct).
- Branch Target Buffer (BTB): Cache the target address of branches to reduce delay.
Summary Table
| Hazard | Cause | Solution |
|---|---|---|
| Structural | Resource conflict | Resource duplication, stalling |
| Data (RAW/WAR/WAW) | Data dependency | Forwarding, stalling, instruction reordering |
| Control | Branch instructions | Branch prediction, delayed branching, flushing |
Key Point: Hazards reduce pipeline efficiency. The goal of all solutions is to minimize CPI (Cycles Per Instruction) and keep the pipeline as full as possible.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.