Pipeline hazards in a scalar pipeline are handled through a combination of hardware mechanisms and architectural techniques to maintain correct execution while minimizing performance loss. Here's a structured breakdown:
1. Types of Pipeline Hazards
- Structural Hazards: Resource conflicts (e.g., two instructions needing the same functional unit).
- Data Hazards: Dependencies between instructions (e.g., RAW, WAR, WAW).
- Control Hazards: Branches/jumps causing incorrect instruction fetch paths.
2. Handling Structural Hazards
- Resource Duplication:
- Use separate units for different tasks (e.g., dedicated instruction/data caches in Harvard architecture to avoid memory access conflicts).
- Stalling (Pipeline Bubbles):
- If a resource conflict arises (e.g., a shared ALU), the pipeline stalls the dependent instruction until the resource is free.
- Example: A scalar pipeline with a single multiplier may stall subsequent multiply instructions until the first completes.
3. Handling Data Hazards
Data hazards (RAW, WAR, WAW) are mitigated via forwarding (bypassing) and stalling:
- Forwarding Paths:
- Add multiplexers to route results directly from later pipeline stages (e.g., EX or MEM) to earlier stages (e.g., ALU inputs).
- Example: If Instruction 2 (ADD R3, R1, R2) depends on Instruction 1 (LOAD R1, 0(R4)), the loaded value from the MEM stage of Instruction 1 is forwarded to the EX stage of Instruction 2.
- Hazard Detection Unit:
- Detects dependencies during ID stage. If forwarding cannot resolve a hazard (e.g., a load-use delay), the pipeline inserts a stall (bubble) to wait for the required data.
- Example: A load instruction followed by a dependent ALU instruction introduces a 1-cycle stall.
4. Handling Control Hazards
Control hazards from branches/jumps are addressed using branch prediction and delay slots:
- Static Branch Prediction:
- Predicts branches as "not taken" (default) or "taken" based on heuristics (e.g., backward branches in loops).
- If mispredicted, flush the pipeline and restart fetching from the correct address.
- Dynamic Branch Prediction:
- Uses a Branch History Table (BHT) to track past branch outcomes (e.g., 1-bit or 2-bit saturating counters).
- Improves prediction accuracy for frequently executed branches.
- Delayed Branches:
- The instruction after a branch (in the delay slot) is always executed, regardless of the branch outcome.
- Compiler fills the delay slot with useful instructions to avoid wasted cycles.
- Early Branch Resolution:
- Resolve branch conditions in the ID stage (using comparator logic) instead of EX to reduce misprediction penalty.
5. Example: 5-Stage Scalar Pipeline (IF, ID, EX, MEM, WB)
- Data Hazard (RAW):
ADD R1, R2, R3 → SUB R4, R1, R5
- Forwarding: Result from EX stage of
ADD is forwarded to EX stage of SUB.
- Stall: If
SUB follows a LOAD R1, which takes 2 cycles to complete (MEM stage), a 1-cycle stall is inserted.
- Control Hazard (Branch):
BEQZ R1, Label → MUL R6, R7, R8
- Prediction: If predicted "not taken," fetch
MUL but flush it if the branch is actually taken.
- Delay Slot: Execute
MUL regardless of branch outcome (filled by compiler).
6. Trade-offs in Scalar Pipelines
- Simplicity vs. Performance: Scalar pipelines prioritize simplicity over aggressive optimization (unlike superscalar designs).
- Stalls and Bubbles: Inevitable for unresolved hazards but minimized via forwarding and prediction.
- Compiler Role: Delayed branches and instruction scheduling optimize hazard avoidance.
Summary Table
| Hazard Type | Mitigation Technique | Impact on Pipeline |
|---|
| Structural | Resource duplication, stalling | Avoids resource conflicts; potential stalls |
| Data (RAW) | Forwarding + hazard detection/stalling | Reduces stalls; critical for correctness |
| Control | Branch prediction, delayed branches | Minimizes misprediction penalties |
Scalar pipelines rely on simple, efficient hazard handling to maintain high throughput despite in-order, single-issue execution. Forwarding and prediction are key to minimizing stalls, while compiler optimizations further enhance performance.