Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between speculation and prediction

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.

like image 277
enc Avatar asked Aug 13 '12 07:08

enc


2 Answers

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.

like image 167
Guffa Avatar answered Oct 06 '22 14:10

Guffa


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.

like image 27
Peter Cordes Avatar answered Oct 06 '22 13:10

Peter Cordes