From d4381b9d401061477cad77c340935d02962b577b Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Tue, 16 Apr 2024 13:58:22 -0400 Subject: [PATCH 1/2] Use likelihoods in block reordering decision --- src/coreclr/jit/fgopt.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index c1ef6f1df18dd..3757c22c0e64b 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -3615,19 +3615,9 @@ bool Compiler::fgReorderBlocks(bool useProfile) FlowEdge* edgeToBlock = bPrev->GetFalseEdge(); noway_assert(edgeToDest != nullptr); noway_assert(edgeToBlock != nullptr); - // - // Calculate the taken ratio - // A takenRatio of 0.10 means taken 10% of the time, not taken 90% of the time - // A takenRatio of 0.50 means taken 50% of the time, not taken 50% of the time - // A takenRatio of 0.90 means taken 90% of the time, not taken 10% of the time - // - double takenCount = edgeToDest->getLikelyWeight(); - double notTakenCount = edgeToBlock->getLikelyWeight(); - double totalCount = takenCount + notTakenCount; - // If the takenRatio (takenCount / totalCount) is greater or equal to 51% then we will reverse - // the branch - if (takenCount < (0.51 * totalCount)) + // If we take the true branch more than half the time, we will reverse the branch. + if (edgeToDest->getLikelihood() < 0.51) { reorderBlock = false; } From 084b6d96ca9f1e1ac6bb9a64bcba390e1931679b Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Wed, 17 Apr 2024 10:02:43 -0400 Subject: [PATCH 2/2] Rename + comments --- src/coreclr/jit/fgopt.cpp | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index 3757c22c0e64b..c1b919d37830a 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -3587,44 +3587,43 @@ bool Compiler::fgReorderBlocks(bool useProfile) { noway_assert(bPrev->KindIs(BBJ_COND)); // - // We will reverse branch if the taken-jump to bDest ratio (i.e. 'takenRatio') - // is more than 51% + // We will reverse branch if the true edge's likelihood is more than 51%. // - // We will setup profHotWeight to be maximum bbWeight that a block - // could have for us not to want to reverse the conditional branch + // We will set up profHotWeight to be maximum bbWeight that a block + // could have for us not to want to reverse the conditional branch. // // We will consider all blocks that have less weight than profHotWeight to be - // uncommonly run blocks as compared with the hot path of bPrev taken-jump to bDest + // uncommonly run blocks compared to the weight of bPrev's true edge. // - // We will check that the weight of the bPrev to bDest edge - // is more than twice the weight of the bPrev to block edge. + // We will check if bPrev's true edge weight + // is more than twice bPrev's false edge weight. // - // bPrev --> [BB04, weight 31] + // bPrev --> [BB04, weight 100] // | \. - // edgeToBlock -------------> O \. - // [min=8,max=10] V \. - // block --> [BB05, weight 10] \. + // falseEdge ---------------> O \. + // [likelihood=0.33] V \. + // block --> [BB05, weight 33] \. // \. - // edgeToDest ----------------------------> O - // [min=21,max=23] | + // trueEdge ------------------------------> O + // [likelihood=0.67] | // V - // bDest ---------------> [BB08, weight 21] + // bDest ---------------> [BB08, weight 67] // assert(bPrev->FalseTargetIs(block)); - FlowEdge* edgeToDest = bPrev->GetTrueEdge(); - FlowEdge* edgeToBlock = bPrev->GetFalseEdge(); - noway_assert(edgeToDest != nullptr); - noway_assert(edgeToBlock != nullptr); + FlowEdge* trueEdge = bPrev->GetTrueEdge(); + FlowEdge* falseEdge = bPrev->GetFalseEdge(); + noway_assert(trueEdge != nullptr); + noway_assert(falseEdge != nullptr); // If we take the true branch more than half the time, we will reverse the branch. - if (edgeToDest->getLikelihood() < 0.51) + if (trueEdge->getLikelihood() < 0.51) { reorderBlock = false; } else { // set profHotWeight - profHotWeight = edgeToBlock->getLikelyWeight() - 1; + profHotWeight = falseEdge->getLikelyWeight() - 1; } } }