[llvm] [Bolt] Fix integer division bug in computeEdgeWeights fallback weights (PR #152880)

Slava Gurevich via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 14:22:46 PDT 2025


https://github.com/noclowns created https://github.com/llvm/llvm-project/pull/152880

The fallback edge weight calculation in Bolt computeEdgeWeights() incorrectly used integer division when computing weights as `1 / numChildren`. This caused zero weights for fallback cases when `numChildren > 1`, leading to incorrect edge weight assignments in the absence of profile data.

This fix improves correctness and stability of edge weight computations by changing the divisor to floating-point to ensure correct fallback weights calculation.

>From 1bd6ef57e7571fe36318ab394417dc62af488552 Mon Sep 17 00:00:00 2001
From: Slava Gurevich <sgurevich at gmail.com>
Date: Sat, 9 Aug 2025 14:21:03 -0700
Subject: [PATCH] [Bolt] Fix integer division bug in computeEdgeWeights
 fallback weights

The fallback edge weight calculation in computeEdgeWeights
incorrectly used integer division when computing weights as `1 / numChildren`. This caused zero weights for fallback cases when `numChildren > 1`, leading to incorrect edge weight assignments in the absence of profile data.

This fix improves correctness and stability of edge weight computations by changing the divisor to floating-point
to ensure correct fallback weights.
---
 bolt/lib/Passes/MCF.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Passes/MCF.cpp b/bolt/lib/Passes/MCF.cpp
index 4f3a964fd3230..38350efa2a730 100644
--- a/bolt/lib/Passes/MCF.cpp
+++ b/bolt/lib/Passes/MCF.cpp
@@ -153,7 +153,7 @@ void computeEdgeWeights(BinaryBasicBlock *BB, EdgeWeightMap &EdgeWeights) {
                                           E = GraphT::child_end(BB);
        CI != E; ++CI) {
     typename GraphT::NodeRef Child = *CI;
-    double Weight = 1 / (GraphT::child_end(BB) - GraphT::child_begin(BB));
+    double Weight = 1.0 / (GraphT::child_end(BB) - GraphT::child_begin(BB));
     if (TotalChildrenCount != 0.0)
       Weight = ChildrenExecCount[ChildIndex] / TotalChildrenCount;
     updateEdgeWeight<NodeT>(EdgeWeights, BB, Child, Weight);



More information about the llvm-commits mailing list