14#ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCE_H
15#define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCE_H
124 using Edge = std::pair<const BasicBlockT *, const BasicBlockT *>;
132 :
F(
F), Successors(Successors), SampleBlockWeights(SampleBlockWeights) {}
140 createFlowFunction(
const std::vector<const BasicBlockT *> &BasicBlocks,
146 void findUnlikelyJumps(
const std::vector<const BasicBlockT *> &BasicBlocks,
162template <
typename BT>
174 for (
const auto &BB :
F) {
184 std::vector<const BasicBlockT *> BasicBlocks;
186 BasicBlocks.reserve(Reachable.
size());
187 for (
const auto &BB :
F) {
188 if (Reachable.
count(&BB) && InverseReachable.
count(&BB)) {
189 BlockIndex[&BB] = BasicBlocks.
size();
190 BasicBlocks.push_back(&BB);
194 BlockWeights.
clear();
196 bool HasSamples =
false;
197 for (
const auto *BB : BasicBlocks) {
198 auto It = SampleBlockWeights.find(BB);
199 if (It != SampleBlockWeights.end() && It->second > 0) {
201 BlockWeights[BB] = It->second;
205 if (BasicBlocks.size() <= 1 || !HasSamples) {
210 FlowFunction Func = createFlowFunction(BasicBlocks, BlockIndex);
218 for (
const auto *BB : BasicBlocks) {
219 BlockWeights[BB] = Func.Blocks[BlockIndex[BB]].Flow;
221 for (
auto &Jump : Func.Jumps) {
222 Edge E = std::make_pair(BasicBlocks[Jump.Source], BasicBlocks[Jump.Target]);
223 EdgeWeights[
E] = Jump.Flow;
228 for (
auto &
I : BlockWeights) {
232 for (
auto &
I : EdgeWeights) {
236 InverseReachable.
contains(
I.first.second));
241template <
typename BT>
243 const std::vector<const BasicBlockT *> &BasicBlocks,
246 Func.Blocks.reserve(BasicBlocks.size());
248 for (
const auto *BB : BasicBlocks) {
250 auto It = SampleBlockWeights.find(BB);
251 if (It != SampleBlockWeights.end()) {
252 Block.HasUnknownWeight =
false;
253 Block.Weight = It->second;
255 Block.HasUnknownWeight =
true;
262 for (
const auto *BB : BasicBlocks) {
263 for (
auto *Succ : Successors[BB]) {
264 if (!BlockIndex.
count(Succ))
267 Jump.Source = BlockIndex[BB];
268 Jump.Target = BlockIndex[Succ];
269 Func.Jumps.push_back(Jump);
272 for (
auto &Jump :
Func.Jumps) {
275 Func.Blocks[Src].SuccJumps.push_back(&Jump);
276 Func.Blocks[Dst].PredJumps.push_back(&Jump);
280 findUnlikelyJumps(BasicBlocks, Successors, Func);
283 for (
size_t I = 0;
I <
Func.Blocks.size();
I++) {
284 if (
Func.Blocks[
I].isEntry()) {
289 assert(
Func.Entry == 0 &&
"incorrect index of the entry block");
292 auto &EntryBlock =
Func.Blocks[
Func.Entry];
293 if (EntryBlock.Weight == 0 && !EntryBlock.HasUnknownWeight) {
294 EntryBlock.Weight = 1;
295 EntryBlock.HasUnknownWeight =
false;
301template <
typename BT>
302inline void SampleProfileInference<BT>::findUnlikelyJumps(
303 const std::vector<const BasicBlockT *> &BasicBlocks,
304 BlockEdgeMap &Successors, FlowFunction &Func) {}
306template <
typename BT>
307inline bool SampleProfileInference<BT>::isExit(
const BasicBlockT *BB) {
308 return BB->succ_empty();
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Sample profile inference pass.
std::remove_pointer_t< NodeRef > BasicBlockT
DenseMap< const BasicBlockT *, uint64_t > BlockWeightMap
typename GraphTraits< FT * >::NodeRef NodeRef
DenseMap< const BasicBlockT *, SmallVector< const BasicBlockT *, 8 > > BlockEdgeMap
DenseMap< Edge, uint64_t > EdgeWeightMap
std::pair< const BasicBlockT *, const BasicBlockT * > Edge
void apply(BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights)
Apply the profile inference algorithm for a given function.
SampleProfileInference(FunctionT &F, BlockEdgeMap &Successors, BlockWeightMap &SampleBlockWeights)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
bool contains(ConstPtrType Ptr) const
NodeAddr< FuncNode * > Func
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
iterator_range< idf_ext_iterator< T, SetTy > > inverse_depth_first_ext(const T &G, SetTy &S)
void applyFlowInference(const ProfiParams &Params, FlowFunction &Func)
Apply the profile inference algorithm for a given function and provided profi options.
A wrapper of a binary basic block.
bool isEntry() const
Check if it is the entry block in the function.
bool isExit() const
Check if it is an exit block in the function.
std::vector< FlowJump * > PredJumps
std::vector< FlowJump * > SuccJumps
A wrapper of binary function with basic blocks and jumps.
std::vector< FlowJump > Jumps
Jumps between the basic blocks.
std::vector< FlowBlock > Blocks
Basic blocks in the function.
uint64_t Entry
The index of the entry block.
A wrapper of a jump between two basic blocks.
typename GraphType::UnknownGraphTypeError NodeRef
Various thresholds and options controlling the behavior of the profile inference algorithm.
unsigned CostJumpUnknownFTInc
The cost of increasing an unknown fall-through jump's count by one.
unsigned CostBlockInc
The cost of increasing a block's count by one.
unsigned CostJumpFTInc
The cost of increasing a fall-through jump's count by one.
bool RebalanceUnknown
Evenly re-distribute flow among unknown subgraphs.
const int64_t CostUnlikely
The cost of taking an unlikely block/jump.
unsigned CostJumpDec
The cost of decreasing a jump's count by one.
bool JoinIslands
Join isolated components having positive flow.
unsigned CostBlockZeroInc
The cost of increasing a count of zero-weight block by one.
unsigned CostBlockEntryDec
The cost of decreasing the entry block's count by one.
unsigned CostJumpInc
The cost of increasing a jump's count by one.
unsigned CostJumpUnknownInc
The cost of increasing an unknown jump's count by one.
unsigned CostBlockUnknownInc
The cost of increasing an unknown block's count by one.
unsigned CostJumpFTDec
The cost of decreasing a fall-through jump's count by one.
unsigned CostBlockDec
The cost of decreasing a block's count by one.
unsigned CostBlockEntryInc
The cost of increasing the entry block's count by one.
bool EvenFlowDistribution
Evenly distribute flow when there are multiple equally likely options.