Questions tagged [branch-prediction]

In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Why is it faster to process a sorted array than an unsorted array? Stack Overflow's highest-voted question and answer is a good introduction to the subject.


In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline.

Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Two-way branching is usually implemented with a conditional jump instruction. A conditional jump can either be "not taken" and continue execution with the first branch of code which follows immediately after the conditional jump - or it can be "taken" and jump to a different place in program memory where the second branch of code is stored.

It is not known for certain whether a conditional jump will be taken or not taken until the condition has been calculated and the conditional jump has passed the execution stage in the instruction pipeline.

Without branch prediction, the processor would have to wait until the conditional jump instruction has passed the execute stage before the next instruction can enter the fetch stage in the pipeline. The branch predictor attempts to avoid this waste of time by trying to guess whether the conditional jump is most likely to be taken or not taken. The branch that is guessed to be the most likely is then fetched and speculatively executed. If it is later detected that the guess was wrong then the speculatively executed or partially executed instructions are discarded and the pipeline starts over with the correct branch, incurring a delay.

The time that is wasted in case of a branch misprediction is equal to the number of stages in the pipeline from the fetch stage to the execute stage. Modern microprocessors tend to have quite long pipelines so that the misprediction delay is between 10 and 20 clock cycles. The longer the pipeline the greater the need for a good branch predictor.

Source: http://en.wikipedia.org/wiki/Branch_predictor


The Spectre security vulnerability revolves around branch prediction:


Other resources

Special-purpose predictors: Return Address Stack for call/ret. ret is effectively an indirect branch, setting program-counter = return address. This would be hard to predict on its own, but calls are normally made with a special instruction so modern CPUs can match call/ret pairs with an internal stack.

Computer architecture details about branch prediction / speculative execution, and its effects on pipelined CPUs

  • Why is it faster to process a sorted array than an unsorted array?
  • Branch prediction - Dan Luu's article on branch prediction, adapted from a talk. With diagrams. Good introduction to why it's needed, and some basic implementations used in early CPUs, building up to more complicated predictors. And at the end, a link to TAGE branch predictors used on modern Intel CPUs. (Too complicated for that article to explain, though!)
  • Slow jmp-instruction - even unconditional direct jumps (like x86's jmp) need to be predicted, to avoid stalls in the very first stage of the pipeline: fetching blocks of machine code from I-cache. After fetching one block, you need to know which block to fetch next, before (or at best in parallel with) decoding the block you just fetched. A large sequence of jmp next_instruction will overwhelm branch prediction and expose the cost of misprediction in this part of the pipeline. (Many high-end modern CPUs have a queue after fetch before decode, to hide bubbles, so some blocks of non-branchy code can allow the queue to refill.)
  • Branch target prediction in conjunction with branch prediction?
  • What branch misprediction does the Branch Target Buffer detect?

Cost of a branch miss


Modern TAGE predictors (in Intel CPUs for example) can "learn" amazingly long patterns, because they index based on past branch history. (So the same branch can get different predictions depending on the path leading up to it. A single branch can have its prediction data scattered over many bits in the branch predictor table). This goes a long way to solving the problem of indirect branches in an interpreter almost always mispredicting (X86 prefetching optimizations: "computed goto" threaded code and Branch prediction and the performance of interpreters — Don't trust folklore), or for example a binary search on the same data with the same input can be really efficient.

Static branch prediction on newer Intel processors - according to experimental evidence, it appears Nehalem and earlier do sometimes use static prediction at some point in the pipeline (backwards branches default to predicted-taken, forward to not-taken.) But Sandybridge and newer seem to be always dynamic based on some history, whether it's from this branch or one that aliases it. Why did Intel change the static branch prediction mechanism over these years?

Cases where TAGE does "amazingly" well


Assembly code layout: not so much for branch prediction, but because not-taken branches are easier on the front-end than taken branches. Better I-cache code density if the fast-path is just a straight line, and taken branches mean the part of a fetch block after the branch isn't useful.

Superscalar CPUs fetch code in blocks, e.g. aligned 16 byte blocks, containing multiple instructions. In non-branching code, including not-taken conditional branches, all of those bytes are useful instruction bytes.


Branchless code: using cmov or other tricks to avoid branches

This is the asm equivalent of replacing if (c) a=b; with a = c ? b : a;. If b doesn't have side-effects, and a isn't a potentially-shared memory location, compilers can do "if-conversion" to do the conditional with a data dependency on c instead of a control dependency.

(C compilers can't introduce a non-atomic read/write: that could step on another thread's modification of the variable. Writing your code as always rewriting a value tells compilers that it's safe, which sometimes enables auto-vectorization: AVX-512 and Branching)

Potential downside to cmov in scalar code: the data dependency can become part of a loop-carried dependency chain and become a bottleneck, while branch prediction + speculative execution hide the latency of control dependencies. The branchless data dependency isn't predicted or speculated, which makes it good for unpredictable cases, but potentially bad otherwise.

363 questions
1
vote
2 answers

How can I get my CPU's branch target buffer(BTB) size?

It's useful when execute this routine when LOOPS > BTB_SIZE, eg, from int n = 0; for (int i = 0; i < LOOPS; i++) n++; to int n = 0; int loops = LOOPS / 2; for(int i = 0; i < loops; i+=2) n += 2; can reduce branch misses. BTB…
superK
  • 3,932
  • 6
  • 30
  • 54
1
vote
0 answers

Two-bit branch prediction should give higher percentage

I have small program that is supposed to calculate percentage of successfull predicition of a 2-bit branch predictor. I have it all done but my output isn't what I have expected, the percantage stops at about 91% instead of what I think should be at…
user6722
  • 21
  • 5
0
votes
1 answer

Static Branch prediction for the ARM with __builtin_expect is not functional!!?

Im doing the optimization in the C code running in the Cortex-R4. first of all I haven't seen any change in the assembly code output when I indicated the "__builtin_expect" in condition check. It seem like the compiler generate the unnecessary…
Aravind Nadumane
  • 170
  • 1
  • 11
0
votes
0 answers

__builtin_expect_with_probability use in gcc

The use of builtin_expect_with_probability gcc function is for condition check with probability like in below example __builtin_expect_with_probability(!!(x),1,1.0) can someone tell me what is the purpose of "!!" in above function?
Naval
  • 1
0
votes
1 answer

How much does a mispredicted conditional branch cost?

On x86-64 whatever micro architecture and ARM64 devices, how many clock cycles does a mispredicted conditional branch cost? And I suppose I should also ask what the figure is for a successfully predicted branch taken/not taken ? I can try and find…
Cecil Ward
  • 597
  • 2
  • 13
0
votes
1 answer

How can I cause indirect (function pointer) call to be correctly jump/branch predicted?

let's say I have a function that accepts a callback argument (example given in rust and C) void foo(void (*bar)(int)) { // lots of computation bar(3); } fn foo(bar: fn(u32)) { // lots of computation bar(3) } can I rely on the…
ajp
  • 1,723
  • 14
  • 22
0
votes
0 answers

how do i get job id of batch prediction job on vertex AI?

i need to get the prediction details of batch prediction job which are stored on google cloud storage, however to get that i need to get JOB ID from BatchPredictionJob i tired to write the results to a json file as shown below but its failing with…
0
votes
0 answers

What is the depth of CPU branch prediction?

If CPU is already in the path of a branch A speculatively, will it continue to speculatively execute the next branch B? or wait until branch A retire? if (A) { /* body of branch A */ if(B) { /* body of branch B */ } } else { …
Changbin Du
  • 501
  • 5
  • 11
0
votes
0 answers

Optimizing a branch like a jump table?

I was wondering if I have a branch bool condition = x > y; // just an example if(condition) { // do the thing... } else { // do the other thing... } It can be optimized to something like this because condition will be either 0 or 1, and rest of…
0
votes
1 answer

How debuggers deal with out-of-order execution and branch prediction

I know that modern CPUs do OoO execution and got advanced branch predictors that may fail, how does the debugger deal with that? So, if the cpu fails in predicting a branch how does the debugger know that? I don't know if debuggers execute…
0
votes
3 answers

How good is the Visual Studio compiler at branch-prediction for simple if-statements?

Here is some c++ pseudo-code as an example: bool importantFlag = false; for (SomeObject obj : arr) { if (obj.someBool) { importantFlag = true; } obj.doSomethingUnrelated(); } Obviously, once the if-statement evaluates as true…
Greg
  • 61
  • 5
0
votes
0 answers

can you produce BEQL MIPS instruction with C code?

So I have this code snippet in C int unit_test_case08(int a, int b) { int success = 1336; if(a != b) { success = 1337; } else { success = -1; } return success; } Which translates in MIPS…
0
votes
1 answer

How to profile branch prediction hitrate in Java

Is there a tool available to profile java applications regarding branch (mis)prediction statistics for if statements? I know VisualVM and JDK Mission Control but did not find such functionality.
Mahatma_Fatal_Error
  • 720
  • 1
  • 10
  • 26
0
votes
0 answers

Branch prediction does not improve the performance

I executed the code from this famous topic Why is processing a sorted array faster than processing an unsorted array? On my Mac OS Mojave: //file test.cpp #include #include #include int main() { // Generate data …
Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47
0
votes
0 answers

Optimize a loop for static predict-not-taken? Which prediction problems exist for that in a normal loop?

Which problems arise in the following assembly loop, if Predict Not Taken is chosen by default? Optimize the example to Predict not Taken. addi $s1, $zero, 1024 // s1 := 1024 loop: addi $s1, $s1, -1 // s1-- jal subroutine // call subroutine() bne…
Iwan5050
  • 165
  • 6