Skip to content

Commit

Permalink
JIT: fix exit edge likelihood computation for capped loops (#84817)
Browse files Browse the repository at this point in the history
The initial version of this only really worked for very simple cases.
Generalize to handle loops with multiple exits better.

The rough idea is to compute how much additional profile flow needs
to come out of the loop to match the capped probability, and then
try and find one exit block where we can adjust likelhoods to cause
exactly this amount of extra flow to leave the loop.

Closes #84789.
  • Loading branch information
AndyAyersMS authored Apr 14, 2023
1 parent 87ba6ea commit 3182dfb
Showing 1 changed file with 88 additions and 32 deletions.
120 changes: 88 additions & 32 deletions src/coreclr/jit/fgprofilesynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,12 +1140,16 @@ void ProfileSynthesis::ComputeCyclicProbabilities(SimpleLoop* loop)
//
if (cyclicWeight > cappedLikelihood)
{
JITDUMP("Cyclic weight " FMT_WT " > " FMT_WT "(cap) -- will reduce to cap\n", cyclicWeight, cappedLikelihood);
capped = true;
cyclicWeight = cappedLikelihood;
m_cappedCyclicProbabilities++;
}

weight_t cyclicProbability = 1.0 / (1.0 - cyclicWeight);
// Note this value is not actually a probability; it is the expected
// iteration count of the loop.
//
weight_t const cyclicProbability = 1.0 / (1.0 - cyclicWeight);

JITDUMP("For loop at " FMT_BB " cyclic weight is " FMT_WT " cyclic probability is " FMT_WT "%s\n",
loop->m_head->bbNum, cyclicWeight, cyclicProbability, capped ? " [capped]" : "");
Expand All @@ -1154,50 +1158,102 @@ void ProfileSynthesis::ComputeCyclicProbabilities(SimpleLoop* loop)

// Try and adjust loop exit likelihood to reflect capping.
// If there are multiple exits we just adjust the first one we can. This is somewhat arbitrary.
// If there are no exits, there's nothing we can do.
//
if (capped)
if (capped && (loop->m_exitEdges.size() > 0))
{
bool adjustedExit = false;
// Figure out how much flow exits the loop with the capped probablility
// and current block frequencies and exit likelihoods.
//
weight_t cappedExitWeight = 0.0;

for (FlowEdge* const exitEdge : loop->m_exitEdges)
{
BasicBlock* const exitBlock = exitEdge->getSourceBlock();
BasicBlock* const exitBlock = exitEdge->getSourceBlock();
weight_t const exitBlockFrequency = exitBlock->bbWeight;
weight_t const exitBlockWeight = exitBlockFrequency * cyclicProbability;
weight_t const exitWeight = exitEdge->getLikelihood() * exitBlockWeight;
cappedExitWeight += exitWeight;
JITDUMP("Exit from " FMT_BB " has weight " FMT_WT "\n", exitBlock->bbNum, exitWeight);
}

JITDUMP("Total exit weight " FMT_WT "\n", cappedExitWeight);

// We should end up with a value less than one since we input one unit of flow into the
// loop and are artificially capping the iteration count of the loop, so less weight is
// now flowing out than in. However because of rounding we might end up near or a bit over 1.0.
//
if ((cappedExitWeight + epsilon) < 1.0)
{
// We want to increase the exit likelihood of one exit block to create
// additional flow out of the loop. Figure out how much we need.
//
weight_t const missingExitWeight = 1.0 - cappedExitWeight;
JITDUMP("Loop exit flow deficit from capping is " FMT_WT "\n", missingExitWeight);

if ((exitBlock->bbJumpKind == BBJ_COND) &&
!Compiler::fgProfileWeightsEqual(exitBlock->bbWeight, 0.0, epsilon))
bool adjustedExit = false;

for (FlowEdge* const exitEdge : loop->m_exitEdges)
{
JITDUMP("Will adjust likelihood of the exit edge from loop exit block " FMT_BB
" to reflect capping; current likelihood is " FMT_WT "\n",
exitBlock->bbNum, exitEdge->getLikelihood());

BasicBlock* const jump = exitBlock->bbJumpDest;
BasicBlock* const next = exitBlock->bbNext;
FlowEdge* const jumpEdge = m_comp->fgGetPredForBlock(jump, exitBlock);
FlowEdge* const nextEdge = m_comp->fgGetPredForBlock(next, exitBlock);
weight_t const continueLikelihood = cappedLikelihood / exitBlock->bbWeight;
weight_t const breakLikelihood = 1 - continueLikelihood;

if (jumpEdge == exitEdge)
{
jumpEdge->setLikelihood(breakLikelihood);
nextEdge->setLikelihood(continueLikelihood);
}
else
// Does this block have enough weight that it can supply all the missing weight?
//
BasicBlock* const exitBlock = exitEdge->getSourceBlock();
weight_t const exitBlockFrequency = exitBlock->bbWeight;
weight_t const exitBlockWeight = exitBlockFrequency * cyclicProbability;
weight_t const currentExitWeight = exitEdge->getLikelihood() * exitBlockWeight;

// TODO: we might also want to exclude edges that are exiting from child loops here,
// or think harder about what might be appropriate in those cases. Seems like we ought
// to adjust an edge's likelihoods at most once.
//
// Currently we don't know which edges do this.
//
if ((exitBlock->bbJumpKind == BBJ_COND) && (exitBlockWeight > (missingExitWeight + currentExitWeight)))
{
assert(nextEdge == exitEdge);
jumpEdge->setLikelihood(continueLikelihood);
nextEdge->setLikelihood(breakLikelihood);
JITDUMP("Will adjust likelihood of the exit edge from loop exit block " FMT_BB
" to reflect capping; current likelihood is " FMT_WT "\n",
exitBlock->bbNum, exitEdge->getLikelihood());

BasicBlock* const jump = exitBlock->bbJumpDest;
BasicBlock* const next = exitBlock->bbNext;
FlowEdge* const jumpEdge = m_comp->fgGetPredForBlock(jump, exitBlock);
FlowEdge* const nextEdge = m_comp->fgGetPredForBlock(next, exitBlock);
weight_t const exitLikelihood = (missingExitWeight + currentExitWeight) / exitBlockWeight;
weight_t const continueLikelihood = 1.0 - exitLikelihood;

// We are making it more likely that the loop exits, so the new exit likelihood
// should be greater than the old.
//
assert(exitLikelihood > exitEdge->getLikelihood());

if (jumpEdge == exitEdge)
{
jumpEdge->setLikelihood(exitLikelihood);
nextEdge->setLikelihood(continueLikelihood);
}
else
{
assert(nextEdge == exitEdge);
jumpEdge->setLikelihood(continueLikelihood);
nextEdge->setLikelihood(exitLikelihood);
}
adjustedExit = true;

JITDUMP("New likelihood is " FMT_WT "\n", exitEdge->getLikelihood());
break;
}
adjustedExit = true;
}

JITDUMP("New likelihood is " FMT_WT "\n", exitEdge->getLikelihood());
break;
if (!adjustedExit)
{
// Possibly we could have fixed things up by adjusting more than one exit?
//
JITDUMP("Unable to find suitable exit to carry off capped flow\n");
}
}

if (!adjustedExit)
else
{
JITDUMP("Unable to find suitable exit to carry off capped flow\n");
JITDUMP("Exit weight comparable or above 1.0, leaving as is\n");
}
}
}
Expand Down

0 comments on commit 3182dfb

Please sign in to comment.