In computer architecture,
what is difference between (branch) prediction and speculation??
These seems very similar, but i think there is a subtle distinction between them.
Branch prediction is done by the processor to try to determine where the execution will continue after a conditional jump, so that it can read the next instruction(s) from memory.
Speculative execution goes one step further and determines what the result would be from executing the next instruction(s). If the branch prediction was correct, the result is used, otherwise it is discarded.
Note that speculative execution can be applied even if there isn't an actual conditional branch in the code. The processor can determine the result from several instructions that normally would execute in succession, but the execution could be stopped for example by an arithmetic overflow interrupt.
If you want to speculatively do something, you increase your chance of it being useful by accurately predicting which path to speculate on.
In some cases, the prediction is trivial (e.g. predict that loads/stores won't segfault). In other cases, it's hard (branch prediction).
In either case, you need to be able to revert / throw away the speculative calculations if an exception occurs.
It is possible to speculate without prediction, by speculatively executing instructions from both directions of a branch, and only keeping the result from the line that's later found to have been the right path.
Current hardware doesn't make use of this for branches, but the same sort of thing happens on a much smaller local scale for stuff like parallel decoding of x86 instructions. Decoders start decoding at every possible instruction boundary, and only find out which start position was correct once the decode of the previous instruction determines the length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With