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

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 14:23:16 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: Slava Gurevich (noclowns)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/152880.diff


1 Files Affected:

- (modified) bolt/lib/Passes/MCF.cpp (+1-1) 


``````````diff
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);

``````````

</details>


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


More information about the llvm-commits mailing list