Skip to content

Commit

Permalink
JIT: Avoid relying on bbNum to find lexical loop boundaries (#101714)
Browse files Browse the repository at this point in the history
Switch `FlowGraphNaturalLoop::GetLexicallyTopMostBlock` and
`FlowGraphNaturalLoop::GetLexicallyBottomMostBlock` to more robust
implementations that scan the basic block list forwards (and backwards)
to find the boundary blocks.

Fix #101695
  • Loading branch information
jakobbotsch committed Apr 30, 2024
1 parent 9fa1235 commit 017593d
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/coreclr/jit/flowgraph.cpp
Expand Up @@ -5555,20 +5555,21 @@ bool FlowGraphNaturalLoop::InitBlockEntersLoopOnTrue(BasicBlock* initBlock)
// the loop.
//
// Returns:
// Block with highest bbNum.
// First block in block order contained in the loop.
//
// Remarks:
// Mostly exists as a quirk while transitioning from the old loop
// representation to the new one.
//
BasicBlock* FlowGraphNaturalLoop::GetLexicallyTopMostBlock()
{
BasicBlock* top = m_header;
VisitLoopBlocks([&top](BasicBlock* loopBlock) {
if (loopBlock->bbNum < top->bbNum)
top = loopBlock;
return BasicBlockVisit::Continue;
});
BasicBlock* top = m_dfsTree->GetCompiler()->fgFirstBB;

while (!ContainsBlock(top))
{
top = top->Next();
assert(top != nullptr);
}

return top;
}
Expand All @@ -5578,20 +5579,21 @@ BasicBlock* FlowGraphNaturalLoop::GetLexicallyTopMostBlock()
// within the loop.
//
// Returns:
// Block with highest bbNum.
// Last block in block order contained in the loop.
//
// Remarks:
// Mostly exists as a quirk while transitioning from the old loop
// representation to the new one.
//
BasicBlock* FlowGraphNaturalLoop::GetLexicallyBottomMostBlock()
{
BasicBlock* bottom = m_header;
VisitLoopBlocks([&bottom](BasicBlock* loopBlock) {
if (loopBlock->bbNum > bottom->bbNum)
bottom = loopBlock;
return BasicBlockVisit::Continue;
});
BasicBlock* bottom = m_dfsTree->GetCompiler()->fgLastBB;

while (!ContainsBlock(bottom))
{
bottom = bottom->Prev();
assert(bottom != nullptr);
}

return bottom;
}
Expand Down

0 comments on commit 017593d

Please sign in to comment.