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
__builtin_expect_with_probability allows you to specify the probability yourself instead of having a standard default. __attribute__((cold)) and __attribute__((hot)) are relevant too, and the compilers will usually infer that a branch calling a cold function is itself cold, etc.
1
Getting branch metadata for ordering the branches is one of the most important parts of PGO since branch predictors heavily use the order of branches. They can also output certain kinds of hints. The rest is largely knowing which blocks / functions are hot and how to group them.
They can lay out functions near where they're called and similarly can group / order blocks in a sensible way. It all depends on the compiler sophistication. LLVM's optimizations really aren't that complex at a high level, it's really hard to implement conceptually simple things.
1
Inlining and partial inlining can also use that information on code hotness and where it's called from too. You can get the benefits through manual optimization but you may need to do partial inlining, etc. yourself because you can't communicate all of the metadata manually.
1

