Skip to content
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

JIT: Detect and handle imprecision in 3-opt layout cost computation #110069

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4927,21 +4927,33 @@ Compiler::ThreeOptLayout::ThreeOptLayout(Compiler* comp)
// endPos - The inclusive ending index of the region
//
// Returns:
// The region's layout cost
// The region's layout cost, or 'BB_MAX_WEIGHT' for pathalogically costly layouts
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The region's layout cost, or 'BB_MAX_WEIGHT' for pathalogically costly layouts
// The region's layout cost, or 'BB_MAX_WEIGHT' for pathologically costly layouts

//
weight_t Compiler::ThreeOptLayout::GetLayoutCost(unsigned startPos, unsigned endPos)
{
assert(startPos <= endPos);
assert(endPos < numCandidateBlocks);
weight_t layoutCost = BB_ZERO_WEIGHT;

// As 'layoutCost' grows, we may begin to incur noticeable floating-point imprecision,
// which complicates comparing layout costs.
// Detect this, and return 'BB_MAX_WEIGHT'.
auto sumAndCheckForImprecision = [&layoutCost](weight_t cost) -> bool {
assert(cost >= BB_ZERO_WEIGHT);
const weight_t oldLayoutCost = layoutCost;
layoutCost += cost;
return !Compiler::fgProfileWeightsEqual(cost, layoutCost - oldLayoutCost, 0.001);
};

for (unsigned position = startPos; position < endPos; position++)
{
layoutCost += GetCost(blockOrder[position], blockOrder[position + 1]);
if (sumAndCheckForImprecision(GetCost(blockOrder[position], blockOrder[position + 1])))
{
return BB_MAX_WEIGHT;
}
}

layoutCost += blockOrder[endPos]->bbWeight;
return layoutCost;
return sumAndCheckForImprecision(blockOrder[endPos]->bbWeight) ? BB_MAX_WEIGHT : layoutCost;
}
#endif // DEBUG

Expand Down
Loading