Skip to content

Commit ab562a5

Browse files
amanasifkhalidmatouskozak
authored andcommitted
JIT: Use likelihoods in block reordering decision (dotnet#101132)
Follow-up to dotnet#99736.
1 parent f43ffa1 commit ab562a5

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/coreclr/jit/fgopt.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,54 +3587,43 @@ bool Compiler::fgReorderBlocks(bool useProfile)
35873587
{
35883588
noway_assert(bPrev->KindIs(BBJ_COND));
35893589
//
3590-
// We will reverse branch if the taken-jump to bDest ratio (i.e. 'takenRatio')
3591-
// is more than 51%
3590+
// We will reverse branch if the true edge's likelihood is more than 51%.
35923591
//
3593-
// We will setup profHotWeight to be maximum bbWeight that a block
3594-
// could have for us not to want to reverse the conditional branch
3592+
// We will set up profHotWeight to be maximum bbWeight that a block
3593+
// could have for us not to want to reverse the conditional branch.
35953594
//
35963595
// We will consider all blocks that have less weight than profHotWeight to be
3597-
// uncommonly run blocks as compared with the hot path of bPrev taken-jump to bDest
3596+
// uncommonly run blocks compared to the weight of bPrev's true edge.
35983597
//
3599-
// We will check that the weight of the bPrev to bDest edge
3600-
// is more than twice the weight of the bPrev to block edge.
3598+
// We will check if bPrev's true edge weight
3599+
// is more than twice bPrev's false edge weight.
36013600
//
3602-
// bPrev --> [BB04, weight 31]
3601+
// bPrev --> [BB04, weight 100]
36033602
// | \.
3604-
// edgeToBlock -------------> O \.
3605-
// [min=8,max=10] V \.
3606-
// block --> [BB05, weight 10] \.
3603+
// falseEdge ---------------> O \.
3604+
// [likelihood=0.33] V \.
3605+
// block --> [BB05, weight 33] \.
36073606
// \.
3608-
// edgeToDest ----------------------------> O
3609-
// [min=21,max=23] |
3607+
// trueEdge ------------------------------> O
3608+
// [likelihood=0.67] |
36103609
// V
3611-
// bDest ---------------> [BB08, weight 21]
3610+
// bDest ---------------> [BB08, weight 67]
36123611
//
36133612
assert(bPrev->FalseTargetIs(block));
3614-
FlowEdge* edgeToDest = bPrev->GetTrueEdge();
3615-
FlowEdge* edgeToBlock = bPrev->GetFalseEdge();
3616-
noway_assert(edgeToDest != nullptr);
3617-
noway_assert(edgeToBlock != nullptr);
3618-
//
3619-
// Calculate the taken ratio
3620-
// A takenRatio of 0.10 means taken 10% of the time, not taken 90% of the time
3621-
// A takenRatio of 0.50 means taken 50% of the time, not taken 50% of the time
3622-
// A takenRatio of 0.90 means taken 90% of the time, not taken 10% of the time
3623-
//
3624-
double takenCount = edgeToDest->getLikelyWeight();
3625-
double notTakenCount = edgeToBlock->getLikelyWeight();
3626-
double totalCount = takenCount + notTakenCount;
3613+
FlowEdge* trueEdge = bPrev->GetTrueEdge();
3614+
FlowEdge* falseEdge = bPrev->GetFalseEdge();
3615+
noway_assert(trueEdge != nullptr);
3616+
noway_assert(falseEdge != nullptr);
36273617

3628-
// If the takenRatio (takenCount / totalCount) is greater or equal to 51% then we will reverse
3629-
// the branch
3630-
if (takenCount < (0.51 * totalCount))
3618+
// If we take the true branch more than half the time, we will reverse the branch.
3619+
if (trueEdge->getLikelihood() < 0.51)
36313620
{
36323621
reorderBlock = false;
36333622
}
36343623
else
36353624
{
36363625
// set profHotWeight
3637-
profHotWeight = edgeToBlock->getLikelyWeight() - 1;
3626+
profHotWeight = falseEdge->getLikelyWeight() - 1;
36383627
}
36393628
}
36403629
}

0 commit comments

Comments
 (0)