Branch Weights in Inlining Decisions

Hi everyone,

As probably most people know already, there are intrinsics that one can use
to influence the weight of a branch, like __builtin_expect() [1]. I assumed that using
such intrinsics “just works” in LLVM. For example, let’s say that a branch surrounds a function
call. If the branch is cold, I assumed the inliner would probably not inline that. OTOH, if it’s
hot, it probably will.

However, by looking at the code, it seems that at least for “canonical” cases, these
weights do not impact at all the inliner. When I say “canonical” case, I mean e.g.,
a branch surrounding a function call and we put a __builtin_expect() in its condition.

More specifically, the place that I think such an intrinsic would impact the
inliner is here [2]. If we look at the code of isColdSite() [3],
we’ll see that it either needs ProfileSummaryInfo (PSI) or BlockFrequencyInfo
(BFI). PSI is not available because we don’t have any profile (i.e. when __builtin_expect()
is lowered, it just puts a “branch_weights” MD in the branch, it doesn’t create any kind of
artificial profile).

What is weirder is that we don’t have BFI either, which, when I put it myself, solved the problem.
We don’t have it because of here [4]. GetBFI is just passed as nullptr and I’m not sure why. It
seems to me it’d help.

Does anybody have any idea about any of that ? Do I use __builtin_expect() incorrectly ?
Is GetBFI supposed to be passed as nullptr?

Thanks,
Stefanos

[1] https://llvm.org/docs/BranchWeightMetadata.html
[2] https://github.com/llvm/llvm-project/blob/42652c1d6e21345173f5dd971cd453520aa5a7ef/llvm/lib/Analysis/InlineCost.cpp#L1550
[3] https://github.com/llvm/llvm-project/blob/42652c1d6e21345173f5dd971cd453520aa5a7ef/llvm/lib/Analysis/InlineCost.cpp#L1422
[4] https://github.com/llvm/llvm-project/blob/42652c1d6e21345173f5dd971cd453520aa5a7ef/llvm/lib/Transforms/IPO/InlineSimple.cpp#L71