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.
Inlining and the limited partial inlining are of course heavily influenced by it but it does a lot more than that. If you marked a lot of your functions cold / hot and added branch metadata to every if statement with __builtin_expect_with_probability you could get most manually.
1
If you really wanted you could add your own instrumentation and then turn it into metadata within the source code to have the benefits regardless of compilation method. It will easily bitrot and end up with bad coverage and inaccurate metadata unless it's small + mature codebase.
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
Show replies

