mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 20:23:11 +01:00
db7fa377e4
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC. This change supports context-sensitive profile data generation into llvm-profgen. With simultaneous sampling for LBR and call stack, we can identify leaf of LBR sample with calling context from stack sample . During the process of deriving fall through path from LBR entries, we unwind LBR by replaying all the calls and returns (including implicit calls/returns due to inlining) backwards on top of the sampled call stack. Then the state of call stack as we unwind through LBR always represents the calling context of current fall through path. we have two types of virtual unwinding 1) LBR unwinding and 2) linear range unwinding. Specifically, for each LBR entry which can be classified into call, return, regular branch, LBR unwinding will replay the operation by pushing, popping or switching leaf frame towards the call stack and since the initial call stack is most recently sampled, the replay should be in anti-execution order, i.e. for the regular case, pop the call stack when LBR is call, push frame on call stack when LBR is return. After each LBR processed, it also needs to align with the next LBR by going through instructions from previous LBR's target to current LBR's source, which we named linear unwinding. As instruction from linear range can come from different function by inlining, linear unwinding will do the range splitting and record counters through the range with same inline context. With each fall through path from LBR unwinding, we aggregate each sample into counters by the calling context and eventually generate full context sensitive profile (without relying on inlining) to driver compiler's PGO/FDO. A breakdown of noteworthy changes: - Added `HybridSample` class as the abstraction perf sample including LBR stack and call stack * Extended `PerfReader` to implement auto-detect whether input perf script output contains CS profile, then do the parsing. Multiple `HybridSample` are extracted * Speed up by aggregating `HybridSample` into `AggregatedSamples` * Added VirtualUnwinder that consumes aggregated `HybridSample` and implements unwinding of calls, returns, and linear path that contains implicit call/return from inlining. Ranges and branches counters are aggregated by the calling context. Here calling context is string type, each context is a pair of function name and callsite location info, the whole context is like `main:1 @ foo:2 @ bar`. * Added PorfileGenerater that accumulates counters by ranges unfolding or branch target mapping, then generates context-sensitive function profile including function body, inferring callee's head sample, callsite target samples, eventually records into ProfileMap. * Leveraged LLVM build-in(`SampleProfWriter`) writer to support different serialization format with no stop - `getCanonicalFnName` for callee name and name from ELF section - Added regression test for both unwinding and profile generation Test Plan: ninja & ninja check-llvm Reviewed By: hoy, wenlei, wmi Differential Revision: https://reviews.llvm.org/D89723
97 lines
3.1 KiB
C++
97 lines
3.1 KiB
C++
//===-- ProfileGenerator.h - Profile Generator -----------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_PROGEN_PROFILEGENERATOR_H
|
|
#define LLVM_TOOLS_LLVM_PROGEN_PROFILEGENERATOR_H
|
|
#include "ErrorHandling.h"
|
|
#include "PerfReader.h"
|
|
#include "ProfiledBinary.h"
|
|
#include "llvm/ProfileData/SampleProfWriter.h"
|
|
|
|
using namespace llvm;
|
|
using namespace sampleprof;
|
|
|
|
namespace llvm {
|
|
namespace sampleprof {
|
|
|
|
class ProfileGenerator {
|
|
|
|
public:
|
|
ProfileGenerator(){};
|
|
virtual ~ProfileGenerator() = default;
|
|
static std::unique_ptr<ProfileGenerator>
|
|
create(const BinarySampleCounterMap &SampleCounters,
|
|
enum PerfScriptType SampleType);
|
|
virtual void generateProfile() = 0;
|
|
|
|
// Use SampleProfileWriter to serialize profile map
|
|
void write();
|
|
|
|
protected:
|
|
/*
|
|
For each region boundary point, mark if it is begin or end (or both) of
|
|
the region. Boundary points are inclusive. Log the sample count as well
|
|
so we can use it when we compute the sample count of each disjoint region
|
|
later. Note that there might be multiple ranges with different sample
|
|
count that share same begin/end point. We need to accumulate the sample
|
|
count for the boundary point for such case, because for the example
|
|
below,
|
|
|
|
|<--100-->|
|
|
|<------200------>|
|
|
A B C
|
|
|
|
sample count for disjoint region [A,B] would be 300.
|
|
*/
|
|
void findDisjointRanges(RangeSample &DisjointRanges,
|
|
const RangeSample &Ranges);
|
|
|
|
// Used by SampleProfileWriter
|
|
StringMap<FunctionSamples> ProfileMap;
|
|
};
|
|
|
|
class CSProfileGenerator : public ProfileGenerator {
|
|
const BinarySampleCounterMap &BinarySampleCounters;
|
|
|
|
public:
|
|
CSProfileGenerator(const BinarySampleCounterMap &Counters)
|
|
: BinarySampleCounters(Counters){};
|
|
|
|
public:
|
|
void generateProfile() override {
|
|
// Fill in function body samples
|
|
populateFunctionBodySamples();
|
|
|
|
// Fill in boundary sample counts as well as call site samples for calls
|
|
populateFunctionBoundarySamples();
|
|
|
|
// Fill in call site value sample for inlined calls and also use context to
|
|
// infer missing samples. Since we don't have call count for inlined
|
|
// functions, we estimate it from inlinee's profile using the entry of the
|
|
// body sample.
|
|
populateInferredFunctionSamples();
|
|
}
|
|
|
|
private:
|
|
// Helper function for updating body sample for a leaf location in
|
|
// FunctionProfile
|
|
void updateBodySamplesforFunctionProfile(FunctionSamples &FunctionProfile,
|
|
const FrameLocation &LeafLoc,
|
|
uint64_t Count);
|
|
// Lookup or create FunctionSamples for the context
|
|
FunctionSamples &getFunctionProfileForContext(StringRef ContextId);
|
|
void populateFunctionBodySamples();
|
|
void populateFunctionBoundarySamples();
|
|
void populateInferredFunctionSamples();
|
|
};
|
|
|
|
} // end namespace sampleprof
|
|
} // end namespace llvm
|
|
|
|
#endif
|