3
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ "More than just one clock cycle" - understatement of the year. $\endgroup$
    – gnasher729
    Jun 25, 2018 at 21:34

1 Answer 1

3
$\begingroup$

There are many ways this kind of situation be handled.

  1. 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.

  1. 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.

  1. 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.

  1. Bubbles

And of course, you can have many bubbles in the pipeline, until the write is complete :-). BTW, this is a bad idea.

$\endgroup$
3
  • $\begingroup$ By bubbles you mean pipeline stalls right? $\endgroup$
    – kiner_shah
    Jul 29, 2018 at 15:25
  • 1
    $\begingroup$ @kiner_shah yes stalls $\endgroup$
    – Isu
    Jul 31, 2018 at 15:22
  • $\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$
    – user4577
    May 20, 2019 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.