Skip to content

Commit 4903a7b

Browse files
authored
[ctxprof][nfc] Move profile annotator to Analysis (#135871)
This moves the utility that propagates counter values such that we can reuse it elsewhere. Specifically, in a subsequent patch, it'll be used to guide ICP: we need to prioritize promoting indirect calls that dominate larger portions of the dynamic instruction count. We can compare them based on the dynamic count of IR instructions, and we can get that early with this counter propagation logic. The patch is mostly a move of the existing logic, with a pimpl - style implementation to hide all the current complexity.
1 parent 4aca20c commit 4903a7b

File tree

3 files changed

+424
-351
lines changed

3 files changed

+424
-351
lines changed

llvm/include/llvm/Analysis/CtxProfAnalysis.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ class CtxProfAnalysisPrinterPass
157157
const PrintMode Mode;
158158
};
159159

160+
/// Utility that propagates counter values to each basic block and to each edge
161+
/// when a basic block has more than one outgoing edge, using an adaptation of
162+
/// PGOUseFunc::populateCounters.
163+
// FIXME(mtrofin): look into factoring the code to share one implementation.
164+
class ProfileAnnotatorImpl;
165+
class ProfileAnnotator {
166+
std::unique_ptr<ProfileAnnotatorImpl> PImpl;
167+
168+
public:
169+
ProfileAnnotator(const Function &F, ArrayRef<uint64_t> RawCounters);
170+
uint64_t getBBCount(const BasicBlock &BB) const;
171+
172+
// Finds the true and false counts for the given select instruction. Returns
173+
// false if the select doesn't have instrumentation or if the count of the
174+
// parent BB is 0.
175+
bool getSelectInstrProfile(SelectInst &SI, uint64_t &TrueCount,
176+
uint64_t &FalseCount) const;
177+
// Clears Profile and populates it with the edge weights, in the same order as
178+
// they need to appear in the MD_prof metadata. Also computes the max of those
179+
// weights an returns it in MaxCount. Returs false if:
180+
// - the BB has less than 2 successors
181+
// - the counts are 0
182+
bool getOutgoingBranchWeights(BasicBlock &BB,
183+
SmallVectorImpl<uint64_t> &Profile,
184+
uint64_t &MaxCount) const;
185+
~ProfileAnnotator();
186+
};
187+
160188
/// Assign a GUID to functions as metadata. GUID calculation takes linkage into
161189
/// account, which may change especially through and after thinlto. By
162190
/// pre-computing and assigning as metadata, this mechanism is resilient to such

0 commit comments

Comments
 (0)