diff --git a/src/coreclr/jit/compiler.cpp b/src/coreclr/jit/compiler.cpp index 41986d0320b5b..e2beac699bbfb 100644 --- a/src/coreclr/jit/compiler.cpp +++ b/src/coreclr/jit/compiler.cpp @@ -4609,11 +4609,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl // Record "start" values for post-inlining cycles and elapsed time. RecordStateAtEndOfInlining(); - // Drop back to just checking profile likelihoods. - // - activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE; - activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS; - if (opts.OptimizationEnabled()) { // Build post-order and remove dead blocks @@ -4658,6 +4653,11 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl // DoPhase(this, PHASE_CLONE_FINALLY, &Compiler::fgCloneFinally); + // Drop back to just checking profile likelihoods. + // + activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE; + activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS; + // Do some flow-related optimizations // if (opts.OptimizationEnabled()) diff --git a/src/coreclr/jit/fgehopt.cpp b/src/coreclr/jit/fgehopt.cpp index bb277f1b6212e..4d1fe732234ea 100644 --- a/src/coreclr/jit/fgehopt.cpp +++ b/src/coreclr/jit/fgehopt.cpp @@ -1038,8 +1038,27 @@ PhaseStatus Compiler::fgCloneFinally() const unsigned finallyTryIndex = firstBlock->bbTryIndex; BasicBlock* insertAfter = nullptr; BlockToBlockMap blockMap(getAllocator()); - unsigned cloneBBCount = 0; - weight_t const originalWeight = firstBlock->hasProfileWeight() ? firstBlock->bbWeight : BB_ZERO_WEIGHT; + unsigned cloneBBCount = 0; + weight_t originalWeight; + + // When distributing weight between the original and cloned regions, + // ensure only weight from region entries is considered. + // Flow from loop backedges within the region should not influence the weight distribution ratio. + if (firstBlock->hasProfileWeight()) + { + originalWeight = firstBlock->bbWeight; + for (BasicBlock* const predBlock : firstBlock->PredBlocks()) + { + if (!predBlock->KindIs(BBJ_CALLFINALLY)) + { + originalWeight = max(0.0, originalWeight - predBlock->bbWeight); + } + } + } + else + { + originalWeight = BB_ZERO_WEIGHT; + } for (BasicBlock* block = firstBlock; block != nextBlock; block = block->Next()) { diff --git a/src/coreclr/jit/flowgraph.cpp b/src/coreclr/jit/flowgraph.cpp index dd4d95ab8356d..10f929a9cd74c 100644 --- a/src/coreclr/jit/flowgraph.cpp +++ b/src/coreclr/jit/flowgraph.cpp @@ -1401,15 +1401,8 @@ void Compiler::fgAddSyncMethodEnterExit() // Create a block for the start of the try region, where the monitor enter call // will go. BasicBlock* const tryBegBB = fgSplitBlockAtEnd(fgFirstBB); - BasicBlock* const tryNextBB = tryBegBB->Next(); BasicBlock* const tryLastBB = fgLastBB; - // If we have profile data the new block will inherit the next block's weight - if (tryNextBB->hasProfileWeight()) - { - tryBegBB->inheritWeight(tryNextBB); - } - // Create a block for the fault. // It gets an artificial ref count. diff --git a/src/coreclr/jit/objectalloc.cpp b/src/coreclr/jit/objectalloc.cpp index ed68aa9323197..e8c85d7ca48bc 100644 --- a/src/coreclr/jit/objectalloc.cpp +++ b/src/coreclr/jit/objectalloc.cpp @@ -673,6 +673,9 @@ unsigned int ObjectAllocator::MorphAllocObjNodeIntoStackAlloc( if (predBlock->hasProfileWeight()) { block->setBBProfileWeight(predBlock->bbWeight); + JITDUMP("Profile weight into " FMT_BB " needs to be propagated to successors. Profile %s inconsistent.\n", + block->bbNum, comp->fgPgoConsistent ? "is now" : "was already"); + comp->fgPgoConsistent = false; } // Just lop off the JTRUE, the rest can clean up later