Skip to content

Commit

Permalink
JIT: Add helpers for increasing/decreasing block weights (#111135)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanasifkhalid authored Jan 7, 2025
1 parent 6d250c7 commit 2d7f45b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
16 changes: 16 additions & 0 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,22 @@ struct BasicBlock : private LIR::Range
}
}

// increaseBBProfileWeight -- Increase the profile-derived weight for a basic block
// and update the run rarely flag as appropriate.
void increaseBBProfileWeight(weight_t weight)
{
assert(weight >= BB_ZERO_WEIGHT);
setBBProfileWeight(bbWeight + weight);
}

// decreaseBBProfileWeight -- Decrease the profile-derived weight for a basic block,
// ensuring it remains non-negative, and update the run rarely flag as appropriate.
void decreaseBBProfileWeight(weight_t weight)
{
assert(weight >= BB_ZERO_WEIGHT);
setBBProfileWeight(max(BB_ZERO_WEIGHT, bbWeight - weight));
}

// this block will inherit the same weight and relevant bbFlags as bSrc
//
void inheritWeight(BasicBlock* bSrc)
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6549,7 +6549,6 @@ class Compiler


void WalkSpanningTree(SpanningTreeVisitor* visitor);
void fgSetProfileWeight(BasicBlock* block, weight_t weight);
void fgApplyProfileScale();
bool fgHaveSufficientProfileWeights();
bool fgHaveTrustedProfileWeights();
Expand Down
12 changes: 4 additions & 8 deletions src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ PhaseStatus Compiler::fgRemoveEmptyTry()
// Remove profile weight into the continuation block
if (continuation->hasProfileWeight())
{
continuation->setBBProfileWeight(max(0.0, continuation->bbWeight - leave->bbWeight));
continuation->decreaseBBProfileWeight(leave->bbWeight);
}

// (3) Convert the callfinally to a normal jump to the handler
Expand Down Expand Up @@ -717,7 +717,7 @@ PhaseStatus Compiler::fgRemoveEmptyTry()
// Propagate profile weight into the continuation block
if (continuation->hasProfileWeight())
{
continuation->setBBProfileWeight(continuation->bbWeight + block->bbWeight);
continuation->increaseBBProfileWeight(block->bbWeight);
}
}
}
Expand Down Expand Up @@ -2208,16 +2208,12 @@ bool Compiler::fgRetargetBranchesToCanonicalCallFinally(BasicBlock* block,
//
if (callFinally->hasProfileWeight())
{
weight_t const newCallFinallyWeight =
callFinally->bbWeight > block->bbWeight ? callFinally->bbWeight - block->bbWeight : BB_ZERO_WEIGHT;
callFinally->setBBProfileWeight(newCallFinallyWeight);
callFinally->decreaseBBProfileWeight(block->bbWeight);
}

if (leaveBlock->hasProfileWeight())
{
weight_t const newLeaveWeight =
leaveBlock->bbWeight > block->bbWeight ? leaveBlock->bbWeight - block->bbWeight : BB_ZERO_WEIGHT;
leaveBlock->setBBProfileWeight(newLeaveWeight);
leaveBlock->decreaseBBProfileWeight(block->bbWeight);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ PhaseStatus Compiler::fgPostImportationCleanup()

JITDUMP("Updating block weight for now-reachable try entry " FMT_BB " via " FMT_BB "\n",
fromBlock->bbNum, fgFirstBB->bbNum);
fromBlock->setBBProfileWeight(fromBlock->bbWeight + entryWeight);
fromBlock->increaseBBProfileWeight(entryWeight);

// We updated the weight of fromBlock above.
//
Expand Down Expand Up @@ -2277,7 +2277,7 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock*
//
weight_t targetWeight = target->bbWeight;
weight_t blockWeight = block->bbWeight;
target->setBBProfileWeight(max(0.0, targetWeight - blockWeight));
target->decreaseBBProfileWeight(blockWeight);
JITDUMP("Decreased " FMT_BB " profile weight from " FMT_WT " to " FMT_WT "\n", target->bbNum, targetWeight,
target->bbWeight);
}
Expand Down Expand Up @@ -2944,11 +2944,10 @@ bool Compiler::fgOptimizeSwitchJumps()

// Update profile data
//
const weight_t fraction = newBlock->GetSwitchTargets()->bbsDominantFraction;
const weight_t blockToTargetWeight = block->bbWeight * fraction;
const weight_t blockToNewBlockWeight = block->bbWeight - blockToTargetWeight;
const weight_t fraction = newBlock->GetSwitchTargets()->bbsDominantFraction;
const weight_t blockToTargetWeight = block->bbWeight * fraction;

newBlock->setBBProfileWeight(blockToNewBlockWeight);
newBlock->decreaseBBProfileWeight(blockToTargetWeight);

blockToTargetEdge->setLikelihood(fraction);
blockToNewBlockEdge->setLikelihood(max(0.0, 1.0 - fraction));
Expand Down
21 changes: 3 additions & 18 deletions src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2963,21 +2963,6 @@ PhaseStatus Compiler::fgIncorporateProfileData()
return PhaseStatus::MODIFIED_EVERYTHING;
}

//------------------------------------------------------------------------
// fgSetProfileWeight: set profile weight for a block
//
// Arguments:
// block -- block in question
// profileWeight -- raw profile weight (not accounting for inlining)
//
// Notes:
// Does inlinee scaling.
//
void Compiler::fgSetProfileWeight(BasicBlock* block, weight_t profileWeight)
{
block->setBBProfileWeight(profileWeight);
}

//------------------------------------------------------------------------
// fgIncorporateBlockCounts: read block count based profile data
// and set block weights
Expand Down Expand Up @@ -3006,7 +2991,7 @@ bool Compiler::fgIncorporateBlockCounts()

if (fgGetProfileWeightForBasicBlock(block->bbCodeOffs, &profileWeight))
{
fgSetProfileWeight(block, profileWeight);
block->setBBProfileWeight(profileWeight);
}
}

Expand Down Expand Up @@ -3784,7 +3769,7 @@ void EfficientEdgeCountReconstructor::Propagate()
{
BlockInfo* const info = BlockToInfo(block);
assert(info->m_weightKnown);
m_comp->fgSetProfileWeight(block, info->m_weight);
block->setBBProfileWeight(info->m_weight);

const unsigned nSucc = block->NumSucc(m_comp);
if (nSucc == 0)
Expand Down Expand Up @@ -5117,7 +5102,7 @@ void Compiler::fgRepairProfileCondToUncond(BasicBlock* block,

if (target->hasProfileWeight())
{
target->setBBProfileWeight(target->bbWeight + weight);
target->increaseBBProfileWeight(weight);
}
else
{
Expand Down

0 comments on commit 2d7f45b

Please sign in to comment.