Skip to content

Commit 16782a4

Browse files
JIT: Recompute test block weights after loop inversion (#112197)
Fixes #112196.
1 parent aeda1de commit 16782a4

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/coreclr/jit/optimizer.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,13 +2240,6 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
22402240
//
22412241
bNewCond->inheritWeight(block);
22422242

2243-
const weight_t totalWeight = bTest->bbWeight;
2244-
2245-
if (haveProfileWeights)
2246-
{
2247-
bTest->decreaseBBProfileWeight(block->bbWeight);
2248-
}
2249-
22502243
// Move all predecessor edges that look like loop entry edges to point to the new cloned condition
22512244
// block, not the existing condition block. The idea is that if we only move `block` to point to
22522245
// `bNewCond`, but leave other `bTest` predecessors still pointing to `bTest`, when we eventually
@@ -2281,14 +2274,6 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
22812274
case BBJ_SWITCH:
22822275
case BBJ_EHFINALLYRET:
22832276
fgReplaceJumpTarget(predBlock, bTest, bNewCond);
2284-
2285-
// Redirect profile weight, too
2286-
if (haveProfileWeights)
2287-
{
2288-
const weight_t edgeWeight = predEdge->getLikelyWeight();
2289-
bNewCond->increaseBBProfileWeight(edgeWeight);
2290-
bTest->decreaseBBProfileWeight(edgeWeight);
2291-
}
22922277
break;
22932278

22942279
case BBJ_EHCATCHRET:
@@ -2302,13 +2287,21 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
23022287
}
23032288
}
23042289

2305-
const weight_t loopWeight = bTest->bbWeight;
2306-
const weight_t nonLoopWeight = bNewCond->bbWeight;
2307-
if (haveProfileWeights && !fgProfileWeightsConsistent(totalWeight, loopWeight + nonLoopWeight))
2290+
if (haveProfileWeights)
23082291
{
2309-
JITDUMP("Redirecting flow from " FMT_BB " to " FMT_BB " introduced inconsistency. Data %s inconsistent.\n",
2310-
bTest->bbNum, bNewCond->bbNum, fgPgoConsistent ? "is now" : "was already");
2311-
fgPgoConsistent = false;
2292+
// The above change should have moved some flow out of 'bTest', and into 'bNewCond'.
2293+
// Check that no extraneous flow was lost or gained in the process.
2294+
//
2295+
const weight_t totalWeight = bTest->bbWeight;
2296+
bTest->setBBProfileWeight(bTest->computeIncomingWeight());
2297+
bNewCond->setBBProfileWeight(bNewCond->computeIncomingWeight());
2298+
2299+
if (!fgProfileWeightsConsistent(totalWeight, bTest->bbWeight + bNewCond->bbWeight))
2300+
{
2301+
JITDUMP("Redirecting flow from " FMT_BB " to " FMT_BB " introduced inconsistency. Data %s inconsistent.\n",
2302+
bTest->bbNum, bNewCond->bbNum, fgPgoConsistent ? "is now" : "was already");
2303+
fgPgoConsistent = false;
2304+
}
23122305
}
23132306

23142307
#ifdef DEBUG

0 commit comments

Comments
 (0)