diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -18,6 +18,11 @@ cl::ZeroOrMore, cl::desc("Print unwinder output")); +static cl::opt CSProfProbeStackDepth( + "csprof-probe-stack-depth", cl::init(-1), cl::ZeroOrMore, + cl::desc("Cut off probe stack from the bottom and keep it up to a given " + "depth. No cut is made if the input is -1.")); + extern cl::opt ShowDisassemblyOnly; extern cl::opt ShowSourceLocations; @@ -104,11 +109,18 @@ std::shared_ptr ProbeStack::getContextKey() { std::shared_ptr ProbeBasedKey = std::make_shared(); + auto &P = ProbeBasedKey->Probes; for (auto CallProbe : Stack) { - ProbeBasedKey->Probes.emplace_back(CallProbe); + P.emplace_back(CallProbe); + } + CSProfileGenerator::compressRecursionContext(P); + if (CSProfProbeStackDepth >= 0 && + static_cast(CSProfProbeStackDepth) < P.size()) { + std::copy(P.begin() + P.size() - static_cast(CSProfProbeStackDepth), + P.end(), P.begin()); + P.resize(CSProfProbeStackDepth); } - CSProfileGenerator::compressRecursionContext( - ProbeBasedKey->Probes); + ProbeBasedKey->genHashCode(); return ProbeBasedKey; }