Skip to content

[Bolt] Fix integer division bug in computeEdgeWeights fallback weights #152880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

noclowns
Copy link
Contributor

@noclowns noclowns commented Aug 9, 2025

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 division to floating-point to ensure correct fallback weights calculation.

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.
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-bolt

Author: Slava Gurevich (noclowns)

Changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants