Skip to content

Commit 2d7f45b

Browse files
JIT: Add helpers for increasing/decreasing block weights (dotnet#111135)
1 parent 6d250c7 commit 2d7f45b

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

src/coreclr/jit/block.h

+16
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,22 @@ struct BasicBlock : private LIR::Range
12201220
}
12211221
}
12221222

1223+
// increaseBBProfileWeight -- Increase the profile-derived weight for a basic block
1224+
// and update the run rarely flag as appropriate.
1225+
void increaseBBProfileWeight(weight_t weight)
1226+
{
1227+
assert(weight >= BB_ZERO_WEIGHT);
1228+
setBBProfileWeight(bbWeight + weight);
1229+
}
1230+
1231+
// decreaseBBProfileWeight -- Decrease the profile-derived weight for a basic block,
1232+
// ensuring it remains non-negative, and update the run rarely flag as appropriate.
1233+
void decreaseBBProfileWeight(weight_t weight)
1234+
{
1235+
assert(weight >= BB_ZERO_WEIGHT);
1236+
setBBProfileWeight(max(BB_ZERO_WEIGHT, bbWeight - weight));
1237+
}
1238+
12231239
// this block will inherit the same weight and relevant bbFlags as bSrc
12241240
//
12251241
void inheritWeight(BasicBlock* bSrc)

src/coreclr/jit/compiler.h

-1
Original file line numberDiff line numberDiff line change
@@ -6549,7 +6549,6 @@ class Compiler
65496549

65506550

65516551
void WalkSpanningTree(SpanningTreeVisitor* visitor);
6552-
void fgSetProfileWeight(BasicBlock* block, weight_t weight);
65536552
void fgApplyProfileScale();
65546553
bool fgHaveSufficientProfileWeights();
65556554
bool fgHaveTrustedProfileWeights();

src/coreclr/jit/fgehopt.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ PhaseStatus Compiler::fgRemoveEmptyTry()
671671
// Remove profile weight into the continuation block
672672
if (continuation->hasProfileWeight())
673673
{
674-
continuation->setBBProfileWeight(max(0.0, continuation->bbWeight - leave->bbWeight));
674+
continuation->decreaseBBProfileWeight(leave->bbWeight);
675675
}
676676

677677
// (3) Convert the callfinally to a normal jump to the handler
@@ -717,7 +717,7 @@ PhaseStatus Compiler::fgRemoveEmptyTry()
717717
// Propagate profile weight into the continuation block
718718
if (continuation->hasProfileWeight())
719719
{
720-
continuation->setBBProfileWeight(continuation->bbWeight + block->bbWeight);
720+
continuation->increaseBBProfileWeight(block->bbWeight);
721721
}
722722
}
723723
}
@@ -2208,16 +2208,12 @@ bool Compiler::fgRetargetBranchesToCanonicalCallFinally(BasicBlock* block,
22082208
//
22092209
if (callFinally->hasProfileWeight())
22102210
{
2211-
weight_t const newCallFinallyWeight =
2212-
callFinally->bbWeight > block->bbWeight ? callFinally->bbWeight - block->bbWeight : BB_ZERO_WEIGHT;
2213-
callFinally->setBBProfileWeight(newCallFinallyWeight);
2211+
callFinally->decreaseBBProfileWeight(block->bbWeight);
22142212
}
22152213

22162214
if (leaveBlock->hasProfileWeight())
22172215
{
2218-
weight_t const newLeaveWeight =
2219-
leaveBlock->bbWeight > block->bbWeight ? leaveBlock->bbWeight - block->bbWeight : BB_ZERO_WEIGHT;
2220-
leaveBlock->setBBProfileWeight(newLeaveWeight);
2216+
leaveBlock->decreaseBBProfileWeight(block->bbWeight);
22212217
}
22222218
}
22232219

src/coreclr/jit/fgopt.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ PhaseStatus Compiler::fgPostImportationCleanup()
655655

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

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

29452945
// Update profile data
29462946
//
2947-
const weight_t fraction = newBlock->GetSwitchTargets()->bbsDominantFraction;
2948-
const weight_t blockToTargetWeight = block->bbWeight * fraction;
2949-
const weight_t blockToNewBlockWeight = block->bbWeight - blockToTargetWeight;
2947+
const weight_t fraction = newBlock->GetSwitchTargets()->bbsDominantFraction;
2948+
const weight_t blockToTargetWeight = block->bbWeight * fraction;
29502949

2951-
newBlock->setBBProfileWeight(blockToNewBlockWeight);
2950+
newBlock->decreaseBBProfileWeight(blockToTargetWeight);
29522951

29532952
blockToTargetEdge->setLikelihood(fraction);
29542953
blockToNewBlockEdge->setLikelihood(max(0.0, 1.0 - fraction));

src/coreclr/jit/fgprofile.cpp

+3-18
Original file line numberDiff line numberDiff line change
@@ -2963,21 +2963,6 @@ PhaseStatus Compiler::fgIncorporateProfileData()
29632963
return PhaseStatus::MODIFIED_EVERYTHING;
29642964
}
29652965

2966-
//------------------------------------------------------------------------
2967-
// fgSetProfileWeight: set profile weight for a block
2968-
//
2969-
// Arguments:
2970-
// block -- block in question
2971-
// profileWeight -- raw profile weight (not accounting for inlining)
2972-
//
2973-
// Notes:
2974-
// Does inlinee scaling.
2975-
//
2976-
void Compiler::fgSetProfileWeight(BasicBlock* block, weight_t profileWeight)
2977-
{
2978-
block->setBBProfileWeight(profileWeight);
2979-
}
2980-
29812966
//------------------------------------------------------------------------
29822967
// fgIncorporateBlockCounts: read block count based profile data
29832968
// and set block weights
@@ -3006,7 +2991,7 @@ bool Compiler::fgIncorporateBlockCounts()
30062991

30072992
if (fgGetProfileWeightForBasicBlock(block->bbCodeOffs, &profileWeight))
30082993
{
3009-
fgSetProfileWeight(block, profileWeight);
2994+
block->setBBProfileWeight(profileWeight);
30102995
}
30112996
}
30122997

@@ -3784,7 +3769,7 @@ void EfficientEdgeCountReconstructor::Propagate()
37843769
{
37853770
BlockInfo* const info = BlockToInfo(block);
37863771
assert(info->m_weightKnown);
3787-
m_comp->fgSetProfileWeight(block, info->m_weight);
3772+
block->setBBProfileWeight(info->m_weight);
37883773

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

51185103
if (target->hasProfileWeight())
51195104
{
5120-
target->setBBProfileWeight(target->bbWeight + weight);
5105+
target->increaseBBProfileWeight(weight);
51215106
}
51225107
else
51235108
{

0 commit comments

Comments
 (0)