[llvm] [LoopUnroll] Fix assert fail on zeroed branch weights (PR #165938)
Joel E. Denny via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 31 16:11:22 PDT 2025
https://github.com/jdenny-ornl created https://github.com/llvm/llvm-project/pull/165938
BranchProbability fails an assert when its denominator is zero.
Reported at <https://github.com/llvm/llvm-project/pull/159163#pullrequestreview-3406318423>.
>From 99a4386fc4ce1654b72027e06660e52ca0438ee6 Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Fri, 31 Oct 2025 19:07:42 -0400
Subject: [PATCH] [LoopUnroll] Fix assert fail on zeroed branch weights
BranchProbability fails an assert when its denominator is zero.
Reported at
<https://github.com/llvm/llvm-project/pull/159163#pullrequestreview-3406318423>.
---
llvm/lib/Transforms/Utils/LoopUtils.cpp | 5 +++-
.../LoopUnroll/zeroed-branch-weights.ll | 30 +++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/LoopUnroll/zeroed-branch-weights.ll
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 8be471bee5579..6e60b94be78e3 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -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,
diff --git a/llvm/test/Transforms/LoopUnroll/zeroed-branch-weights.ll b/llvm/test/Transforms/LoopUnroll/zeroed-branch-weights.ll
new file mode 100644
index 0000000000000..4d378b0d22f7d
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/zeroed-branch-weights.ll
@@ -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}
More information about the llvm-commits
mailing list