I have read that in some pipelined architectures, memory access requires more than just one clock cycle. In that case how does processor handles the next instruction if the next instruction tries to access something the previous memory instruction has yet to write to its register files?
1 Answer
There are many ways this kind of situation be handled.
- Clever compilers
If you are are writing the contents of, lets say register 5 to the memory location 0x12345, and the next instruction is reading from the memory location 0x12345 and the variable allocated at memory location 0x12345 is not "volatile", compiler will optimize your code so that the next instruction will use the contents of register 5, instead of reading data from the memory.
- Instruction reordering
Ordering of the instructions are changed so that the next instruction is pushed down, as far as required without compromising the outcome of the program. This way, more non-dependent instruction can be executed until the memory write is complete.
- Multiple threads
When the scheduler detects that there is a read after write dependency, it can pick another thread for thread for the IF stage. That way, the current thread will stay inactive until the memory write is complete. Once completed it becomes active again, and will get picked up by the scheduler when a similar situation arises for the current thread.
- Bubbles
And of course, you can have many bubbles in the pipeline, until the write is complete :-). BTW, this is a bad idea.
-
$\begingroup$ By bubbles you mean pipeline stalls right? $\endgroup$ Jul 29, 2018 at 15:25
-
1
-
$\begingroup$ There is also store-to-load forwarding (which is similar to ordinary forwarding of result but instead of comparing destination register name with source register name destination memory address is compared with source memory address. In theory, the dependency relationship can even be predicted (or analytically proven, e.g., a store to x+8 where x is not yet known could forward to a load from x+8), but this seems unlikely to be helpful (implementation overhead, rare utility, possibly significant mispredict rate, significant misprediction cost). $\endgroup$– user4577May 20, 2019 at 10:34