Supplement instr profile with sample profile.
authorWei Mi <wmi@google.com>
Wed, 8 Jul 2020 22:19:44 +0000 (15:19 -0700)
committerWei Mi <wmi@google.com>
Tue, 28 Jul 2020 03:17:40 +0000 (20:17 -0700)
commita23f62343cb79a3306fa64545db1d61c2d76b9ca
tree8f6dcf7a8c865338265e1cd5a9fb4379210aed17
parent23d6525cbdc9de7cbfe7640d1e9e4f25a0c5dd85
Supplement instr profile with sample profile.

PGO profile is usually more precise than sample profile. However, PGO profile
needs to be collected from loadtest and loadtest may not be representative
enough to the production workload. Sample profile collected from production
can be used as a supplement -- for functions cold in loadtest but warm/hot
in production, we can scale up the related function in PGO profile if the
function is warm or hot in sample profile.

The implementation contains changes in compiler side and llvm-profdata side.
Given an instr profile and a sample profile, for a function cold in PGO
profile but warm/hot in sample profile, llvm-profdata will either mark
all the counters in the profile to be -1 or scale up the max count in the
function to be above hot threshold, depending on the zero counter ratio in
the profile. The assumption is if there are too many counters being zero
in the function profile, the profile is more likely to cause harm than good,
then llvm-profdata will mark all the counters to be -1 indicating the
function is hot but the profile is unaccountable. In compiler side, if a
function profile with all -1 counters is seen, the function entry count will
be set to be above hot threshold but its internal profile will be dropped.

In the long run, it may be useful to let compiler support using PGO profile
and sample profile at the same time, but that requires more careful design
and more substantial changes to make two profiles work seamlessly. The patch
here serves as a simple intermediate solution.

Differential Revision: https://reviews.llvm.org/D81981
15 files changed:
llvm/docs/CommandGuide/llvm-profdata.rst
llvm/include/llvm/ProfileData/InstrProf.h
llvm/include/llvm/ProfileData/InstrProfWriter.h
llvm/lib/ProfileData/InstrProf.cpp
llvm/lib/ProfileData/InstrProfWriter.cpp
llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
llvm/test/Transforms/PGOProfile/Inputs/sample-profile.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/suppl-profile.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/suppl-profile.ll [new file with mode: 0644]
llvm/test/tools/llvm-profdata/Inputs/mix_instr.proftext [new file with mode: 0644]
llvm/test/tools/llvm-profdata/Inputs/mix_sample.proftext [new file with mode: 0644]
llvm/test/tools/llvm-profdata/overflow-instr.test
llvm/test/tools/llvm-profdata/suppl-instr-with-sample.test [new file with mode: 0644]
llvm/tools/llvm-profdata/llvm-profdata.cpp