Skip to content

Commit f779ec7

Browse files
authored
[BPI] Cache LoopExitBlocks to improve compile time (#93451)
The `LoopBlock` stored in `LoopWorkList` consist of basic block and its loop data information. When iterate `LoopWorkList`, if estimated weight of a loop is not stored in `EstimatedLoopWeight`, `getLoopExitBlocks()` is called to get all exit blocks of the loop. The estimated weight of a loop is calculated by iterating over edges leading from basic block to all exit blocks of the loop. If at least one edge has unknown estimated weight, the estimated weight of loop is unknown and will not be stored in `EstimatedLoopWeight`. `LoopWorkList` can contain different blocks in a same loop, so there is wasted work that calls `getLoopExitBlocks()` for same loop multiple times. Since computing the exit blocks of loop is expensive and the loop structure is not mutated in Branch Probability Analysis, we can cache the result and improve compile time. With this change, the overall compile time for a file containing a very large loop is dropped by around 82%.
1 parent d7d2d4f commit f779ec7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ void BranchProbabilityInfo::computeEestimateBlockWeight(
810810
const Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
811811
SmallVector<BasicBlock *, 8> BlockWorkList;
812812
SmallVector<LoopBlock, 8> LoopWorkList;
813+
SmallDenseMap<LoopData, SmallVector<BasicBlock *, 4>> LoopExitBlocks;
813814

814815
// By doing RPO we make sure that all predecessors already have weights
815816
// calculated before visiting theirs successors.
@@ -828,12 +829,14 @@ void BranchProbabilityInfo::computeEestimateBlockWeight(
828829
do {
829830
while (!LoopWorkList.empty()) {
830831
const LoopBlock LoopBB = LoopWorkList.pop_back_val();
831-
832-
if (EstimatedLoopWeight.count(LoopBB.getLoopData()))
832+
const LoopData LD = LoopBB.getLoopData();
833+
if (EstimatedLoopWeight.count(LD))
833834
continue;
834835

835-
SmallVector<BasicBlock *, 4> Exits;
836-
getLoopExitBlocks(LoopBB, Exits);
836+
auto Res = LoopExitBlocks.try_emplace(LD);
837+
SmallVectorImpl<BasicBlock *> &Exits = Res.first->second;
838+
if (Res.second)
839+
getLoopExitBlocks(LoopBB, Exits);
837840
auto LoopWeight = getMaxEstimatedEdgeWeight(
838841
LoopBB, make_range(Exits.begin(), Exits.end()));
839842

@@ -842,7 +845,7 @@ void BranchProbabilityInfo::computeEestimateBlockWeight(
842845
if (LoopWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
843846
LoopWeight = static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);
844847

845-
EstimatedLoopWeight.insert({LoopBB.getLoopData(), *LoopWeight});
848+
EstimatedLoopWeight.insert({LD, *LoopWeight});
846849
// Add all blocks entering the loop into working list.
847850
getLoopEnterBlocks(LoopBB, BlockWorkList);
848851
}

0 commit comments

Comments
 (0)