Is there a document somewhere that describes what optimizations PGO opens up? Like I imagine it can say "don't inline this, it doesn't get called" and "inline this, it gets called a bunch", but what else?
Conversation
Replying to
It provides branch metadata so it knows how to order branches or add hints for better branch prediction. Can also decide when to use cmov, etc. that way. Since it has metadata on function call sites, callees and branches it can also split out functions into hit/cold regions, etc.
2
Replying to
Is there a doc with all of this? I actually don't even know how code tells a branch predictor "this is the likely branch", although I know that gcc had a 'likely' builtin iirc.
I see you mentioned a builtin, I'll check that out too.
1
Replying to
Look at llvm.org/docs/BranchWei.
__builtin_expect is the traditional GNU C built-in. It's traditionally used this way:
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
__builtin_expect_with_probability was introduced by Clang.
2
Replying to
Thanks. I'm not sure I understand what you mean by ordering of branches though?
2
Replying to
Ordering the block taken in the likely case before the branch is an incredibly strong hint that it's a hot path because that's how a loop looks. Similarly, if both are after the code, the one that's closer is a slight hint. It will stick all the cold paths out of line at bottom.
1
1
You can see the difference yourself by marking a branch as likely and then unlikely and looking at the disassembled optimized code. It will tend to put all the cold paths at the bottom of the function or if it's really smart could even hoist them into their own functions, etc.
1
1
It will optimize the cold blocks / functions for size and try to group them in cold sections together, and similarly will try to group hot code together. PGO can provide more information it could use beyond just hot/cold to group code that's used together in sections together.
Replying to
Awesome, ok got it. Thanks, this gives me a bunch of stuff I can start looking at.
1

