[SampleFDO] Persist profile staleness metrics into binary
authorwlei <wlei@fb.com>
Tue, 25 Oct 2022 07:31:46 +0000 (00:31 -0700)
committerwlei <wlei@fb.com>
Thu, 10 Nov 2022 06:34:33 +0000 (22:34 -0800)
commit47b0758049eab549c51e77c8f8b5560498af5b80
treef99b360ec1ea2dbf108c12a2cb77d3734349ba0c
parent97842fcba9e3a3fa4db52a47215641f9141fea63
[SampleFDO] Persist profile staleness metrics into binary

With https://reviews.llvm.org/D136627, now we have the metrics for profile staleness based on profile statistics, monitoring the profile staleness in real-time can help user quickly identify performance issues. For a production scenario, the build is usually incremental and if we want the real-time metrics, we should store/cache all the old object's metrics somewhere and pull them in a post-build time. To make it more convenient, this patch add an option to persist them into the object binary, the metrics can be reported right away by decoding the binary rather than polling the previous stdout/stderrs from a cache system.

For implementation, it writes the statistics first into a new metadata section(llvm.stats) then encode into a special ELF `.llvm_stats` section. The section data is formatted as a list of key/value pair so that future statistics can be easily extended. This is also under a new switch(`-persist-profile-staleness`)

In terms of size overhead, the metrics are computed at module level, so the size overhead should be small, measured on one of our internal service, it costs less than < 1MB for a 10GB+ binary.

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D136698
llvm/include/llvm/IR/MDBuilder.h
llvm/include/llvm/MC/MCObjectFileInfo.h
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
llvm/lib/IR/MDBuilder.cpp
llvm/lib/MC/MCObjectFileInfo.cpp
llvm/lib/Transforms/IPO/SampleProfile.cpp
llvm/test/Transforms/SampleProfile/profile-mismatch.ll
llvm/test/Transforms/SampleProfile/pseudo-probe-profile-mismatch.ll