What is and What is Speculative Prediction in CPUs for?

Speculative Prediction

One of the most unknown parts due to the fact that marketing does not usually talk about them are the jump prediction units and the speculative execution of the processors . These drives are important because they are an essential part of performance in many processor architectures found in all types of computing devices. We explain quickly and easily what their role is and what advantages they provide.

In this world nothing is certain, everything is pure uncertainty, even for a poor CPU that all it does is execute code sequentially until it unexpectedly gets a jump instruction.

Jump prediction on a CPU

CPU jump prediction

The CPUs when executing a program do it sequentially; this means that once they have read and executed an instruction, they go to the next line at the next memory address.

The most common way to save code through recursion are jumps, instructions that change the value of the program counter that indicates the memory address of the RAM to be read by the CPU, causing the processor to execute the code located at starting from that new memory address that we have marked with the jump instruction.

When a jump occurs, the time between the instruction executed prior to the jump and the first instruction after the jump is longer than moving from one instruction to another sequentially.

What the jump prediction unit does is act as a forward and check if the jump is going to take place or not and go ahead. In the event that the jump is mandatory and does not have a conditional instruction in advance then the jump unit prepares the jump as quickly as possible and leaves the situation ready to perform it.

Conditional jumps

conditional jumps

With the conditional jumps the thing is different, because you cannot know if the jump is going to take place or not, since you have to execute the condition first. Prediction units cannot read the code, they only foresee the jump through certain conditions and there are times that they fail completely causing the CPU to lose a large number of cycles in previously jumping because the prediction unit has completely failed .

Imagine that the jump prediction unit is a person who encounters the dilemma of the three doors or Motley Fool, does not know which to choose and is completely blocked because he needs to know in advance which of the three doors to take; This is where speculative execution comes in, allowing the CPU to choose the most suitable option.

Speculative Execution

Bingo

The idea of ​​speculative execution is that all the possible options in a jump are executed in a separate execution branch, or in other words, that the CPU does not choose one of the three doors but all three at the same time and then once know which branch of code is running discard the two unused ones.

This means that the CPU takes the jump as if it were already fulfilled beforehand, independently of whether it is going to be fulfilled or not, and executes it internally. If the conditional jump occurs, then the CPU has gained an advantage over time and has advanced correctly, if it is not fulfilled then that branch of the code is completely discarded and in theory it does not affect the main branch.

Therefore, speculative execution is the execution of code by the CPU that does not have to be necessary a priori, but with this there is a problem and that is that said code is valid for the CPU as long as it does not know the result of the condition that activates it and it will continue to be valid or cease to be valid as soon as the CPU knows if the condition is met or not.