A scalar pipeline is a CPU pipeline that processes one instruction per cycle (ideally) through sequential pipeline stages such as:
It increases performance by overlapping the execution of multiple instructions.
However, overlapping instructions can cause data dependencies, which lead to pipeline hazards.
A data dependency (also called data hazard) occurs when one instruction depends on the data or result produced by another instruction.
In pipelines, data dependencies can cause incorrect execution or delays (stalls) because instructions are in different stages of execution simultaneously.
There are three types of data dependencies, often asked in exams:
A RAW dependency occurs when an instruction needs to read a value that a previous instruction has not yet written.
I1: R1 = R2 + R3
I2: R4 = R1 + 5 ; Needs result of I1
Here, I2 depends on I1. If the pipeline executes without care, I2 may read the old value of R1 → incorrect execution.
A WAR dependency occurs when a later instruction wants to write to a register before an earlier instruction reads it.
I1: R5 = R2 + R3 ; Reads R5
I2: R5 = R4 - 1 ; Writes R5 early
If I2 writes R5 before I1 reads it → incorrect.
A WAW dependency occurs when two instructions want to write to the same register.
I1: R6 = R1 + R2 ; Writes R6
I2: R6 = R3 + R4 ; Also writes R6
If I2 writes before I1, the result of I1 is lost.
| Dependency | Full Name | Meaning | Common? | Solution |
|---|---|---|---|---|
| RAW | Read After Write | True dependency | Yes | Forwarding, Stalling |
| WAR | Write After Read | Anti-dependency | Only out-of-order | Register renaming |
| WAW | Write After Write | Output dependency | Only out-of-order | Register renaming |
Data dependencies lead to data hazards, specifically:
Because data isn't ready in time.
Scalar pipelines mainly face RAW hazards.
Pause pipeline until data is ready.
Send the result directly from one pipeline stage to another before it's written to the register file.
Example:
This is the most important technique in scalar pipelines.
Avoids WAR and WAW hazards using extra registers.
Both reduce pipeline stalls.
Consider:
I1: R1 = R2 + R3
I2: R4 = R1 + 1
Pipeline timeline without forwarding:
| Cycle | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
| I1 | 1 | 2 | 3 | 4 | 5 |
| I2 | 2 | 3 | STALL | STALL | 6 |
I2 must wait because R1 is written in cycle 5.
With forwarding → only one stall or zero stalls depending on hardware.
Data dependencies affect:
In summary:
Data dependencies are the main challenge for achieving one instruction per cycle in pipelines.
Scalar pipeline: Executes one instruction at a time through pipeline stages.
Data dependency: When one instruction depends on the result of another.
Types:
Hazards: RAW causes stalls unless forwarding is used.
Solutions: Forwarding, stalling, register renaming, compiler scheduling.
Open this section to load past papers