From 2d7f45b9650caa5045ebf5d6381e10bc88df159c Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Tue, 7 Jan 2025 13:30:29 +0000 Subject: [PATCH] JIT: Add helpers for increasing/decreasing block weights (#111135) --- src/coreclr/jit/block.h | 16 ++++++++++++++++ src/coreclr/jit/compiler.h | 1 - src/coreclr/jit/fgehopt.cpp | 12 ++++-------- src/coreclr/jit/fgopt.cpp | 11 +++++------ src/coreclr/jit/fgprofile.cpp | 21 +++------------------ 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/coreclr/jit/block.h b/src/coreclr/jit/block.h index 23cf1f4fa44546..30fec37fcf122f 100644 --- a/src/coreclr/jit/block.h +++ b/src/coreclr/jit/block.h @@ -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) diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index e88f27c9928822..ecf3ffa090bd4f 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -6549,7 +6549,6 @@ class Compiler void WalkSpanningTree(SpanningTreeVisitor* visitor); - void fgSetProfileWeight(BasicBlock* block, weight_t weight); void fgApplyProfileScale(); bool fgHaveSufficientProfileWeights(); bool fgHaveTrustedProfileWeights(); diff --git a/src/coreclr/jit/fgehopt.cpp b/src/coreclr/jit/fgehopt.cpp index 54c0adcbdeb3ab..12d91d7e127497 100644 --- a/src/coreclr/jit/fgehopt.cpp +++ b/src/coreclr/jit/fgehopt.cpp @@ -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 @@ -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); } } } @@ -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); } } diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index 627802c2b69001..deb6c0f48d39d7 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -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. // @@ -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); } @@ -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)); diff --git a/src/coreclr/jit/fgprofile.cpp b/src/coreclr/jit/fgprofile.cpp index 0b8f4378c48270..3349f2ff1a320e 100644 --- a/src/coreclr/jit/fgprofile.cpp +++ b/src/coreclr/jit/fgprofile.cpp @@ -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 @@ -3006,7 +2991,7 @@ bool Compiler::fgIncorporateBlockCounts() if (fgGetProfileWeightForBasicBlock(block->bbCodeOffs, &profileWeight)) { - fgSetProfileWeight(block, profileWeight); + block->setBBProfileWeight(profileWeight); } } @@ -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) @@ -5117,7 +5102,7 @@ void Compiler::fgRepairProfileCondToUncond(BasicBlock* block, if (target->hasProfileWeight()) { - target->setBBProfileWeight(target->bbWeight + weight); + target->increaseBBProfileWeight(weight); } else {