▲ | hansvm 2 days ago | |
Semantically it's just a table from instruction location to branch probability. Some nuances exist in: - Table overflow mitigation: multi-leveled tables, not wasting space on 100% predicted branches, etc - Table eviction: Rolling counts are actually impossible without space consumption; do you have space wasted, periodic flushing, exponential moving averages, etc - Table initialization: When do you start caring about a branch (and wasting table space), how conservative are the initial parameters, etc - Table overflow: What do you do when a branch doesn't fit in the table but should As a rule of thumb, no extra information/context is used for branch prediction. If a program over the course of a few thousand instructions hits a branch X% of the time, then X will be the branch prediction. If you have context you want to use to influence the prediction, you need to manifest that context as additional lines of assembly the predictor can use in its lookup table. As another rule of thumb, if the hot path has more than a few thousand branches (on modern architectures, often just a few thousand <100% branches (you want the assembly to generate the jump-if-not-equal in the right direction for that architecture though, else you'll get a 100% misprediction rate instead)) then you'll hit slow paths -- multi-leveled search, mispredicted branches, etc. It's reasonably interesting, and given that it's hardware it's definitely clever, but it's not _that_ clever from a software perspective. Is there anything in particular you're curious about? | ||
▲ | aclindsa 2 days ago | parent | next [-] | |
> As a rule of thumb, no extra information/context is used for branch prediction. As you'll find in the results of the recent branch prediction competition, global branch history (a record of which branches have recently been taken) is a piece of context critical to achieving the high accuracy of modern branch predictors: https://ericrotenberg.wordpress.ncsu.edu/cbp2025-workshop-pr... | ||
▲ | NobodyNada 2 days ago | parent | prev [-] | |
> If a program over the course of a few thousand instructions hits a branch X% of the time, then X will be the branch prediction. This is not completely true - modern branch predictors can recognize patterns such as "this branch is taken every other time", or "every 5th time", etc. They also can, in some cases, recognize correlations between nearby branches. However, they won't use factors like register or memory contents to predict branches, because that would require waiting for that data to be available to make the prediction -- which of course defeats the point of branch prediction. |