In this lesson, you will learn how to identify the three primary types of data dependencies that create hazards in a pipelined processor.
Understanding Data Dependencies
In a single-cycle processor, an instruction completes entirely before the next begins. In a pipeline, instructions overlap. Because they overlap, a later instruction might try to use a piece of data before a previous instruction has finished "creating" it. We call this a data dependency.
There are three main types, named by the order in which they access a register or memory location:
1. Read-After-Write (RAW) - The "True" Dependency
This is the most common hazard. It occurs when Instruction B needs to read a register that Instruction A is still calculating.
- Example:
TheADD R1, R2, R3 ; R1 = R2 + R3 SUB R4, R1, R5 ; R4 = R1 - R5SUBinstruction cannot finish its calculation until theADDinstruction writes the result intoR1. IfSUBhits the register file beforeADDfinishes its write-back stage,SUBwill use "stale" (old) data.
2. Write-After-Read (WAR) - The "Anti-Dependency"
This occurs when Instruction B tries to write to a register before Instruction A has finished reading it.
- Example:
In a simple in-order pipeline, this is rarely an issue because instructions read early and write late. However, if the pipeline reorders execution, you must ensureADD R4, R1, R5 ; Needs to read R1 SUB R1, R2, R3 ; Wants to write new value to R1ADDreads the oldR1beforeSUBoverwrites it.
3. Write-After-Write (WAW) - The "Output Dependency"
This happens when both instructions write to the same register, and we must ensure the final value in the register is from the correct instruction.
- Example:
IfADD R1, R2, R3 SUB R1, R4, R5ADDfinishes afterSUB, the register file will end up with theADDresult instead of theSUBresult, causing a logic error.
Key Takeaway: Data dependencies arise when instructions share registers; RAW hazards are the most frequent, occurring when a subsequent instruction depends on the result of a preceding one currently moving through the pipeline.