Skip to content

JIT: Improve weight distribution in internal blocks produced by casts #50082

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
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
24 changes: 21 additions & 3 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17697,6 +17697,9 @@ void Compiler::fgExpandQmarkForCastInstOf(BasicBlock* block, Statement* stmt)

assert(qmark->gtFlags & GTF_QMARK_CAST_INSTOF);

const BasicBlock::weight_t currBbWeight = block->bbWeight;
const BasicBlock::weight_t nextBbWeight = (block->bbNext != nullptr) ? block->bbNext->bbWeight : currBbWeight;

// Get cond, true, false exprs for the qmark.
GenTree* condExpr = qmark->gtGetOp1();
GenTree* trueExpr = qmark->gtGetOp2()->AsColon()->ThenNode();
Expand Down Expand Up @@ -17782,11 +17785,26 @@ void Compiler::fgExpandQmarkForCastInstOf(BasicBlock* block, Statement* stmt)
cond1Block->bbJumpDest = remainderBlock;
cond2Block->bbJumpDest = remainderBlock;

// Set the weights; some are guesses.
// Currently, we don't instrument internal blocks, so the only way we can set weights to these blocks
// is to analyze successors and take a guess.
BasicBlock::weight_t castSuccessLikelihood = BB_ZERO_WEIGHT;

// We don't expand casts inside rarely executed blocks, but currently we skip only blocks
// with BBF_RUN_RARELY flag and still can hit zero weight here (in that cases all internal blocks
// will also have zero weight).
if (currBbWeight > BB_ZERO_WEIGHT)
{
castSuccessLikelihood = clamp(nextBbWeight / currBbWeight, BB_ZERO_WEIGHT, 1.0f);
}

asgBlock->inheritWeight(block);
cond1Block->inheritWeight(block);
cond2Block->inheritWeightPercentage(cond1Block, 50);
helperBlock->inheritWeightPercentage(cond2Block, 50);

// cond2Block is always taken if the cast always succeeds (helperBlock will be cold in this case)
// If it always fails the only guess we can make that it's either object is null or of a
// wrong type (50/50).
cond2Block->inheritWeightPercentage(block, (UINT32)(castSuccessLikelihood * 50.0f) + 50);
helperBlock->inheritWeightPercentage(block, 50 - (UINT32)(castSuccessLikelihood * 50.0f));

// Append cond1 as JTRUE to cond1Block
GenTree* jmpTree = gtNewOperNode(GT_JTRUE, TYP_VOID, condExpr);
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/jit/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ inline bool isPow2(T i)
return (i > 0 && ((i - 1) & i) == 0);
}

// Clamps the given value between the given lower and upper values
template <typename T>
inline bool clamp(T value, T lower, T upper)
{
return max(lower, min(value, upper));
}

// Adapter for iterators to a type that is compatible with C++11
// range-based for loops.
template <typename TIterator>
Expand Down