Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/Utils/LoopUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,12 @@ BranchProbability llvm::getBranchProbability(BranchInst *B,
uint64_t Weight0, Weight1;
if (!extractBranchWeights(*B, Weight0, Weight1))
return BranchProbability::getUnknown();
uint64_t Denominator = Weight0 + Weight1;
if (Denominator == 0)
return BranchProbability::getUnknown();
if (!ForFirstTarget)
std::swap(Weight0, Weight1);
return BranchProbability::getBranchProbability(Weight0, Weight0 + Weight1);
return BranchProbability::getBranchProbability(Weight0, Denominator);
}

bool llvm::setBranchProbability(BranchInst *B, BranchProbability P,
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/Transforms/LoopUnroll/zeroed-branch-weights.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; Check that zeroed branch weights do not crash or otherwise break basic
; LoopUnroll behavior when it tries to compute a probability from them.

; RUN: opt < %s -S -unroll-count=2 -passes='loop-unroll' 2>&1 | FileCheck %s

define void @test() {
entry:
br label %loop

loop:
br i1 false, label %end, label %loop, !prof !0

end:
ret void
}

!0 = !{!"branch_weights", i32 0, i32 0}

; CHECK: define void @test() {
; CHECK: entry:
; CHECK: br label %loop
; CHECK: loop:
; CHECK: br i1 false, label %end, label %loop.1, !prof !0
; CHECK: loop.1:
; CHECK: br i1 false, label %end, label %loop, !prof !0, !llvm.loop !1
; CHECK-NOT: loop.2
; CHECK: end:
; CHECK: ret void
; CHECK: }
; CHECK: !0 = !{!"branch_weights", i32 0, i32 0}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we alternatively can avoid to create all zero branch_weights metadata by setting the ElideAllZero parameter in setBranchWeights , in this case we can save some metadata memory.