From 017593d90781d797df8b2241f6d1f83c236c442b Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Tue, 30 Apr 2024 18:29:30 +0200 Subject: [PATCH] JIT: Avoid relying on bbNum to find lexical loop boundaries (#101714) 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 --- src/coreclr/jit/flowgraph.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/coreclr/jit/flowgraph.cpp b/src/coreclr/jit/flowgraph.cpp index 8b73d10aa8dc1..2a185d9e47b1d 100644 --- a/src/coreclr/jit/flowgraph.cpp +++ b/src/coreclr/jit/flowgraph.cpp @@ -5555,7 +5555,7 @@ 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 @@ -5563,12 +5563,13 @@ bool FlowGraphNaturalLoop::InitBlockEntersLoopOnTrue(BasicBlock* initBlock) // 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; } @@ -5578,7 +5579,7 @@ 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 @@ -5586,12 +5587,13 @@ BasicBlock* FlowGraphNaturalLoop::GetLexicallyTopMostBlock() // 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; }