Skip to content

Commit

Permalink
JIT: Avoid relying on bbNum to find lexical loop boundaries (#101724)
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

Co-authored-by: Jakob Botsch Nielsen <[email protected]>
  • Loading branch information
github-actions[bot] and jakobbotsch authored Apr 30, 2024
1 parent 64f7eca commit 274daa1
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5569,20 +5569,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 @@ -5592,20 +5593,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 274daa1

Please sign in to comment.