Skip to content

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

@noclowns
Copy link
Contributor Author

@aaupov could you please take a look when you get a chance? Thanks.

Copy link
Contributor

@maksfb maksfb left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Could you please add a test case?

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.

3 participants