Skip to content

Commit

Permalink
JIT: don't allow negative edge weights (#56651)
Browse files Browse the repository at this point in the history
Due to rounding errors or inconsistencies we may end up with unexpected
ratios of profile counts. Fix one place where that was leading to a negative
edge weight.

Fixes #56647.
  • Loading branch information
AndyAyersMS authored Aug 2, 2021
1 parent f35b474 commit d01dae4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,8 @@ bool flowList::setEdgeWeightMaxChecked(BasicBlock::weight_t newWeight,
void flowList::setEdgeWeights(BasicBlock::weight_t theMinWeight, BasicBlock::weight_t theMaxWeight, BasicBlock* bDst)
{
assert(theMinWeight <= theMaxWeight);
assert(theMinWeight >= 0.0f);
assert(theMaxWeight >= 0.0f);

JITDUMP("Setting edge weights for " FMT_BB " -> " FMT_BB " to [" FMT_WT " .. " FMT_WT "]\n", getBlock()->bbNum,
bDst->bbNum, theMinWeight, theMaxWeight);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4467,7 +4467,7 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
// Note "next" is the loop top block, not bTest's bbNext,
// we'll call this latter block "after".
//
BasicBlock::weight_t const testToNextLikelihood = weightNext / weightTest;
BasicBlock::weight_t const testToNextLikelihood = min(1.0f, weightNext / weightTest);
BasicBlock::weight_t const testToAfterLikelihood = 1.0f - testToNextLikelihood;

// Adjust edges out of bTest (which now has weight weightNext)
Expand Down

0 comments on commit d01dae4

Please sign in to comment.