Skip to content

Commit 059fad8

Browse files
committed
Make sure total loop body weight is preserved in loop peeling
Summary: Regardless how the loop body weight is distributed, we should preserve total loop body weight. i.e. we should have same weight reaching the body of the loop or its duplicates in peeled and unpeeled case. Reviewers: mkuper, davidxl, anemet Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D28179 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290833 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 12ec8e1 commit 059fad8

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/Transforms/Utils/LoopUnrollPeel.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,27 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
335335
unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
336336

337337
uint64_t TrueWeight, FalseWeight;
338-
uint64_t ExitWeight = 0, BackEdgeWeight = 0;
338+
uint64_t ExitWeight = 0, CurHeaderWeight = 0;
339339
if (LatchBR->extractProfMetadata(TrueWeight, FalseWeight)) {
340340
ExitWeight = HeaderIdx ? TrueWeight : FalseWeight;
341-
BackEdgeWeight = HeaderIdx ? FalseWeight : TrueWeight;
341+
// The # of times the loop body executes is the sum of the exit block
342+
// weight and the # of times the backedges are taken.
343+
CurHeaderWeight = TrueWeight + FalseWeight;
342344
}
343345

344346
// For each peeled-off iteration, make a copy of the loop.
345347
for (unsigned Iter = 0; Iter < PeelCount; ++Iter) {
346348
SmallVector<BasicBlock *, 8> NewBlocks;
347349
ValueToValueMapTy VMap;
348350

349-
// The exit weight of the previous iteration is the header entry weight
350-
// of the current iteration. So this is exactly how many dynamic iterations
351-
// the current peeled-off static iteration uses up.
351+
// Subtract the exit weight from the current header weight -- the exit
352+
// weight is exactly the weight of the previous iteration's header.
352353
// FIXME: due to the way the distribution is constructed, we need a
353354
// guard here to make sure we don't end up with non-positive weights.
354-
if (ExitWeight < BackEdgeWeight)
355-
BackEdgeWeight -= ExitWeight;
355+
if (ExitWeight < CurHeaderWeight)
356+
CurHeaderWeight -= ExitWeight;
356357
else
357-
BackEdgeWeight = 1;
358+
CurHeaderWeight = 1;
358359

359360
cloneLoopBlocks(L, Iter, InsertTop, InsertBot, Exit,
360361
NewBlocks, LoopBlocks, VMap, LVMap, LI);
@@ -388,6 +389,14 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
388389

389390
// Adjust the branch weights on the loop exit.
390391
if (ExitWeight) {
392+
// The backedge count is the difference of current header weight and
393+
// current loop exit weight. If the current header weight is smaller than
394+
// the current loop exit weight, we mark the loop backedge weight as 1.
395+
uint64_t BackEdgeWeight = 0;
396+
if (ExitWeight < CurHeaderWeight)
397+
BackEdgeWeight = CurHeaderWeight - ExitWeight;
398+
else
399+
BackEdgeWeight = 1;
391400
MDBuilder MDB(LatchBR->getContext());
392401
MDNode *WeightNode =
393402
HeaderIdx ? MDB.createBranchWeights(ExitWeight, BackEdgeWeight)

test/Transforms/LoopUnroll/peel-loop-pgo.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ for.end: ; preds = %for.cond.for.end_cr
4343
;CHECK: !1 = !{!"branch_weights", i32 900, i32 101}
4444
;CHECK: !2 = !{!"branch_weights", i32 540, i32 360}
4545
;CHECK: !3 = !{!"branch_weights", i32 162, i32 378}
46-
;CHECK: !4 = !{!"branch_weights", i32 560, i32 162}
46+
;CHECK: !4 = !{!"branch_weights", i32 1399, i32 162}
4747

0 commit comments

Comments
 (0)