[CSSPGO][llvm-profgen] Compress recursive cycles in calling context
authorwlei <wlei@fb.com>
Fri, 29 Jan 2021 23:00:08 +0000 (15:00 -0800)
committerwlei <wlei@fb.com>
Thu, 4 Feb 2021 02:50:14 +0000 (18:50 -0800)
commit0609f257dc2e2c3e4c7cd30fe2ffd520117e706b
tree757b9f303328b3d65412db498d09575a95430e7b
parentc95c0db2eb687edb91afa628655bb5025f7e863b
[CSSPGO][llvm-profgen] Compress recursive cycles in calling context

This change compresses the context string by removing cycles due to recursive function for CS profile generation. Removing recursion cycles is a way to normalize the calling context which will be better for the sample aggregation and also make the context promoting deterministic.
Specifically for implementation, we recognize adjacent repeated frames as cycles and deduplicated them through multiple round of iteration.
For example:
Considering a input context string stack:
[“a”, “a”, “b”, “c”, “a”, “b”, “c”, “b”, “c”, “d”]
For first iteration,, it removed all adjacent repeated frames of size 1:
[“a”, “b”, “c”, “a”, “b”, “c”, “b”, “c”, “d”]
For second iteration, it removed all adjacent repeated frames of size 2:
[“a”, “b”, “c”, “a”, “b”, “c”, “d”]
So in the end, we get compressed output:
[“a”, “b”, “c”, “d”]

Compression will be called in two place: one for sample's context key right after unwinding, one is for the eventual context string id in the ProfileGenerator.
Added a switch `compress-recursion` to control the size of duplicated frames, default -1 means no size limit.
Added unit tests and regression test for this.

Differential Revision: https://reviews.llvm.org/D93556
16 files changed:
llvm/test/tools/llvm-profgen/Inputs/recursion-compression-noprobe.perfbin [new file with mode: 0755]
llvm/test/tools/llvm-profgen/Inputs/recursion-compression-noprobe.perfscript [new file with mode: 0644]
llvm/test/tools/llvm-profgen/Inputs/recursion-compression-pseudoprobe.perfbin [new file with mode: 0755]
llvm/test/tools/llvm-profgen/Inputs/recursion-compression-pseudoprobe.perfscript [new file with mode: 0644]
llvm/test/tools/llvm-profgen/recursion-compression-noprobe.test [new file with mode: 0644]
llvm/test/tools/llvm-profgen/recursion-compression-pseudoprobe.test [new file with mode: 0644]
llvm/tools/llvm-profgen/PerfReader.cpp
llvm/tools/llvm-profgen/ProfileGenerator.cpp
llvm/tools/llvm-profgen/ProfileGenerator.h
llvm/tools/llvm-profgen/ProfiledBinary.cpp
llvm/tools/llvm-profgen/ProfiledBinary.h
llvm/tools/llvm-profgen/PseudoProbe.cpp
llvm/tools/llvm-profgen/PseudoProbe.h
llvm/unittests/tools/CMakeLists.txt
llvm/unittests/tools/llvm-profgen/CMakeLists.txt [new file with mode: 0644]
llvm/unittests/tools/llvm-profgen/ContextCompressionTest.cpp [new file with mode: 0644]