# How to use BlockFrequency in inter-procedural context?

The BlockFrequency analysis has been useful for machine block placement,
register allocation and other function-level optimizations. How could we
extend it for use in an inter-procedural (whole-program) context? For
example, if we would like to compare the hotness of two call sites in
different functions, or calculate the hotness of two global variables
referenced in multiple functions.

If the ratio of a block BB frequency to the entry block frequency is the
expected number of times the block BB will execute per entry to the
function (according to LLVM Block Frequency Terminology page), would the
multiplication of that ratio to the profile count of the function be a
reasonable approximation of BB total execution count?

Thanks,
Ivan

The BlockFrequency analysis has been useful for machine block placement,
register allocation and other function-level optimizations. How could we
extend it for use in an inter-procedural (whole-program) context? For
example, if we would like to compare the hotness of two call sites in
different functions, or calculate the hotness of two global variables
referenced in multiple functions.

BlockFrequency can not be used for inter-procedural analysis.

If the ratio of a block BB frequency to the entry block frequency is the
expected number of times the block BB will execute per entry to the
function (according to LLVM Block Frequency Terminology page), would the
multiplication of that ratio to the profile count of the function be a
reasonable approximation of BB total execution count?

The answer depends on how BB frequency is computed. If BB frequency is
directly scaled from BB profile count (Execution count), then the answer is
'yes'. In the current implementation, the answer is 'no'. LLVM only keeps
branch probability information from the profile data, and BB frequency is
computed from branch probabilities: There are known limitations of the
frequency propagation algorithm to make the resulting frequency
'distorted'. To name a few: loop scale is capped at 4096; branch weight is
incremented by 1 (leading to issues such as computed loop trip count as low
as half of the actual count); limitation of handling irreducible loops etc.
In fact, frequency can not be reliably be used for comparison across
different loop nests.

We have plans to address those issues to improve PGO.

thanks,

David

David

Is this true for static heuristics as well ?

If the bb freqs are scaled wrt to the entry block freq and a) use such scaled freqs for the bb’s that have calls b) propagate this info topologically over the call graph, how representative will be the info if one just wants to use in a comparative sense ?

-Dibyendu

David

Is this true for static heuristics as well ?

If the bb freqs are scaled wrt to the entry block freq and a) use such
scaled freqs for the bb's that have calls b) propagate this info
topologically over the call graph, how representative will be the info if
one just wants to use in a comparative sense ?

The main issue with inter-procedural propagation of static frequencies is
the existence of indirect calls -- their targets are usually not resolvable
statically, not to mention their frequency distributions. Another issue is
with loops -- static heuristics tend to estimate loop trip count very
conservatively, and it favors BBs in deep loop nests more. The global
'hotness' info computed can be misleading and harmful.

David

Thanks David for the insight. I was thinking whether we can use some of the learning algorithms like Support Vector Machine to estimate freqs in a non-pgo setup. And these can use the pgo-based results for supervised learning.