Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ struct FlowEdge
// Convenience flag for phases that need to track edge visitation
bool m_visited;

// Indicates if m_likelihood was determined using profile synthesis's heuristics
bool m_heuristicBasedLikelihood;

// True if likelihood has been set
INDEBUG(bool m_likelihoodSet);

Expand All @@ -605,6 +608,7 @@ struct FlowEdge
, m_likelihood(0)
, m_dupCount(0)
, m_visited(false)
, m_heuristicBasedLikelihood(false)
#ifdef DEBUG
, m_likelihoodSet(false)
#endif // DEBUG
Expand Down Expand Up @@ -661,7 +665,8 @@ struct FlowEdge

void clearLikelihood()
{
m_likelihood = 0.0;
m_likelihood = 0.0;
m_heuristicBasedLikelihood = false;
INDEBUG(m_likelihoodSet = false);
}

Expand Down Expand Up @@ -706,6 +711,16 @@ struct FlowEdge
assert(visited());
m_visited = false;
}

bool isHeuristicBased() const
{
return m_heuristicBasedLikelihood;
}

void setHeuristicBased()
{
m_heuristicBasedLikelihood = true;
}
};

//------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ void Compiler::fgConvertBBToThrowBB(BasicBlock* block)
}
}

// Reduce the heuristics-derived edge likelihoods into 'block' to indicate exceptional behavior
for (FlowEdge* const predEdge : block->PredEdges())
{
if (predEdge->isHeuristicBased() && predEdge->getSourceBlock()->KindIs(BBJ_COND))
{
BasicBlock* const condBlock = predEdge->getSourceBlock();
FlowEdge* const otherEdge =
condBlock->TrueEdgeIs(predEdge) ? condBlock->GetFalseEdge() : condBlock->GetTrueEdge();

// We should not have degenerate branches
assert(predEdge != otherEdge);
assert(otherEdge->isHeuristicBased());

// If the predecessor can jump to a non-throw block, bias the likelihoods to that path
if (!otherEdge->getDestinationBlock()->KindIs(BBJ_THROW))
{
predEdge->setLikelihood(0.0);
otherEdge->setLikelihood(1.0);
}
// If both branches are to throw blocks, consider them equally likely
else
{
predEdge->setLikelihood(0.5);
otherEdge->setLikelihood(0.5);
}

profileInconsistent = true;
Copy link

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert on otherEdge->isHeuristicBased() may trigger if the opposite edge wasn’t marked heuristic-based, causing a debug-only failure. Instead of asserting, guard the adjustment logic with an if (otherEdge->isHeuristicBased()) check and skip edges that don’t meet the criteria.

Suggested change
assert(otherEdge->isHeuristicBased());
// If the predecessor can jump to a non-throw block, bias the likelihoods to that path
if (!otherEdge->getDestinationBlock()->KindIs(BBJ_THROW))
{
predEdge->setLikelihood(0.0);
otherEdge->setLikelihood(1.0);
}
// If both branches are to throw blocks, consider them equally likely
else
{
predEdge->setLikelihood(0.5);
otherEdge->setLikelihood(0.5);
}
profileInconsistent = true;
// Proceed only if the other edge is heuristic-based
if (otherEdge->isHeuristicBased())
{
// If the predecessor can jump to a non-throw block, bias the likelihoods to that path
if (!otherEdge->getDestinationBlock()->KindIs(BBJ_THROW))
{
predEdge->setLikelihood(0.0);
otherEdge->setLikelihood(1.0);
}
// If both branches are to throw blocks, consider them equally likely
else
{
predEdge->setLikelihood(0.5);
otherEdge->setLikelihood(0.5);
}
profileInconsistent = true;
}

Copilot uses AI. Check for mistakes.
}
}

if (profileInconsistent)
{
JITDUMP("Flow removal of " FMT_BB " needs to be propagated. Data %s inconsistent.\n", block->bbNum,
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/fgprofilesynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ void ProfileSynthesis::AssignLikelihoods()

case BBJ_COND:
// Two successor cases
block->GetTrueEdge()->setHeuristicBased();
block->GetFalseEdge()->setHeuristicBased();
AssignLikelihoodCond(block);
break;

Expand Down
Loading