Skip to content

Commit d65ba58

Browse files
Maintain profile through switch lowering
1 parent 56f19da commit d65ba58

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/coreclr/jit/compiler.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,11 +5148,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
51485148
DoPhase(this, PHASE_SWITCH_RECOGNITION, &Compiler::optSwitchRecognition);
51495149
}
51505150

5151-
// Drop back to just checking profile likelihoods.
5152-
//
5153-
activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE;
5154-
activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS;
5155-
51565151
#ifdef DEBUG
51575152
// Stash the current estimate of the function's size if necessary.
51585153
if (verbose && opts.OptimizationEnabled())
@@ -5267,7 +5262,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
52675262

52685263
// The common phase checks and dumps are no longer relevant past this point.
52695264
//
5270-
activePhaseChecks = PhaseChecks::CHECK_PROFILE;
5265+
activePhaseChecks = PhaseChecks::CHECK_NONE;
52715266
activePhaseDumps = PhaseDumps::DUMP_NONE;
52725267

52735268
// Generate code

src/coreclr/jit/lower.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,28 @@ GenTree* Lowering::LowerSwitch(GenTree* node)
12601260
JITDUMP("Zero weight switch block " FMT_BB ", distributing likelihoods equally per case\n",
12611261
afterDefaultCondBlock->bbNum);
12621262
// jumpCnt-1 here because we peeled the default after copying this value.
1263-
weight_t const newLikelihood = 1.0 / (jumpCnt - 1);
1263+
weight_t const newLikelihood = 1.0 / (jumpCnt - 1);
1264+
bool profileInconsistent = false;
12641265
for (unsigned i = 0; i < successors.numDistinctSuccs; i++)
12651266
{
1266-
FlowEdge* const edge = successors.nonDuplicates[i];
1267+
FlowEdge* const edge = successors.nonDuplicates[i];
1268+
weight_t const oldEdgeWeight = edge->getLikelyWeight();
12671269
edge->setLikelihood(newLikelihood * edge->getDupCount());
1270+
weight_t const newEdgeWeight = edge->getLikelyWeight();
1271+
1272+
if (afterDefaultCondBlock->hasProfileWeight())
1273+
{
1274+
BasicBlock* const targetBlock = edge->getDestinationBlock();
1275+
targetBlock->increaseBBProfileWeight(newEdgeWeight - oldEdgeWeight);
1276+
profileInconsistent |= (targetBlock->NumSucc() > 0);
1277+
}
1278+
}
1279+
1280+
if (profileInconsistent)
1281+
{
1282+
JITDUMP("Switch lowering: Flow out of " FMT_BB " needs to be propagated. Data %s inconsistent.\n",
1283+
afterDefaultCondBlock->bbNum, comp->fgPgoConsistent ? "is now" : "was already");
1284+
comp->fgPgoConsistent = false;
12681285
}
12691286
}
12701287
else
@@ -1447,6 +1464,22 @@ bool Lowering::TryLowerSwitchToBitTest(FlowEdge* jumpTable[],
14471464

14481465
bbSwitch->SetCond(case1Edge, case0Edge);
14491466

1467+
//
1468+
// Update profile
1469+
//
1470+
if (bbSwitch->hasProfileWeight())
1471+
{
1472+
bbCase0->setBBProfileWeight(bbCase0->computeIncomingWeight());
1473+
bbCase1->setBBProfileWeight(bbCase1->computeIncomingWeight());
1474+
1475+
if ((bbCase0->NumSucc() > 0) || (bbCase1->NumSucc() > 0))
1476+
{
1477+
JITDUMP("TryLowerSwitchToBitTest: Flow out of " FMT_BB " needs to be propagated. Data %s inconsistent.\n",
1478+
bbSwitch->bbNum, comp->fgPgoConsistent ? "is now" : "was already");
1479+
comp->fgPgoConsistent = false;
1480+
}
1481+
}
1482+
14501483
var_types bitTableType = (bitCount <= (genTypeSize(TYP_INT) * 8)) ? TYP_INT : TYP_LONG;
14511484
GenTree* bitTableIcon = comp->gtNewIconNode(bitTable, bitTableType);
14521485

0 commit comments

Comments
 (0)