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

131 Views Asked by At

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 indirect call to bar being correctly predicted by the CPU? Assume that at the callsite of foo, the value of bar is in fact highly dynamic and unpredictable - but because of // lots of computation, my expectation is that the CPU has enough "advance warning" such that speculative/out-of-order execution can work across the function pointer boundary.

Is this the case? If not, is there anything I can do as the programmer to help the CPU out?

1

There are 1 best solutions below

0
Peter Cordes On

You can't guarantee a correct prediction.

The best you can do is what you're already doing: have the branch condition (or target) ready early, to let out-of-order exec detect a misprediction early and recover before it costs much wasted work. (So any mis-speculated instructions hopefully haven't been executed yet, and the front-end can re-steer in time to avoid losing cycles.)

See Avoid stalling pipeline by calculating conditional early

A mispredict will unavoidably cost cycles for the front-end, but a lot of code executes slower than 4 instructions per cycle (or 4 uops per cycle), so the front-end is able to get ahead of where the oldest uops are still executing, especially if there are bottlenecks like a long dependency chain without a lot of instruction-level parallelism.


Modern branch predictors are quite good even with complex patterns, e.g. IT-TAGE uses history of the past few branches to index a predictor for this one. This leads to decent performance even in the switch in an interpreter loop or something like that, unlike in older CPUs where a complex pattern for a single indirect branch was a big problem. (See How does Branch Prediction affect performance in R? for some links, especially Branch Prediction and the Performance of Interpreters - Don’t Trust Folklore (2015) by Rohou, Swamy, and Seznec.