Forwarding Can Save Ticks When We Have a Data Hazard
What is the problem?
- Look at the example that begins on page 304 of the book
- It is a sequence of 4 R-format instructions followed by a sw
- The first instruction (sub) writes into $2
- Each of the following instructions reads $2
- This causes a problem in two of the instructions:
- The sub instruction writes $2 in its tick 5
- Each of the other instructions reads $2 in its tick 2
- Tick 2 of the immediately following instruction (and)
coincides with tick 3 of the sub instruction
- Tick 2 of the next instruction (or) coincides with tick 4 of the sub instruction
- So the and and the or are reading $2 before
the sub can write to it
- This is called a data hazard
- The last two instructions of the example don't have this problem
- Forwarding can help alleviate this problem
- In some cases, forwarding eliminates the problem completely
- In other cases, forwarding reduces the effect of the problem but does not eliminate it completely
When do we need forwarding?
- An instruction writes into a register
- One of the next two instructions reads from that same register
Here's a short example:
- the sub instruction writes into $18
- the and instruction writes into $5
- three succeeding instructions read from $18 (arrows)
- two succeeding instructions read from $5 (arrows)
- the red arrows mark places where forwarding will be necessary
- the green arrows mark places where forwarding will not be necessary
- the difference is that the red arrows point no more than two instructions down
from the instruction that wrote into the register;
the green arrows point at least three instructions down