This is an archive of the discontinued LLVM Phabricator instance.

Implement callsite-hotness based inline cost for Sample-based PGO
ClosedPublic

Authored by danielcdh on Jul 7 2016, 4:07 PM.

Details

Summary

For sample-based PGO, using BFI to calculate callsite count is sometime not accurate. This is because with sampling based approach, if a callsite resides in a hot loop deeply nested in a bunch of cold branches, the callsite's BFI frequency would be inaccurately calculated due to lack of samples in the cold branch.

E.g.

if (A1 && A2 && A3 && ..... && A10) {

for (i=0; i < 100000000; i++) {
  callsite();
}

}

Assume that A1 to A100 are all 100% taken, and callsite has 1000 samples and thus is considerred hot. Because the loop's trip count is huge, it's normal that all branches outside the loop has no sample at all. As a result, we can only use static branch probability to derive the the frequency of the loop header. Assuming that static heuristic thinks each branch is 50% taken, then the count calculated from BFI will be 1/(2^10) of the actual value.

In order to get more accurate callsite count, we directly annotate the weight on the call instruction, and directly use it when checking callsite hotness.

Note that this mechanism can also be shared by instrumentation based callsite hotness analysis. The side benefit is that it breaks the dependency from Inliner to BFI as call count is embedded in the IR.

Diff Detail

Event Timeline

danielcdh updated this revision to Diff 63154.Jul 7 2016, 4:07 PM
danielcdh retitled this revision from to Implement callsite-hotness based inline cost for Sample-based PGO.
danielcdh updated this object.
danielcdh added reviewers: davidxl, eraman, dnovillo.
danielcdh added a subscriber: llvm-commits.
dnovillo accepted this revision.Jul 11 2016, 7:48 AM
dnovillo edited edge metadata.

LGTM with a couple of small changes.

lib/IR/Metadata.cpp
1323

This will leave TotalVal in an undefined state. Perhaps initialize it to 0 at the start?

1327

Likewise.

This revision is now accepted and ready to land.Jul 11 2016, 7:48 AM
danielcdh updated this revision to Diff 63525.Jul 11 2016, 9:33 AM
danielcdh edited edge metadata.

integrate Diego's comments.

danielcdh closed this revision.Jul 11 2016, 9:56 AM