Skip to content
Draft
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
62 changes: 31 additions & 31 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5457,48 +5457,48 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
}
};

ArrayStack<BasicBlock*> retOrThrowBlocks(getAllocator(CMK_ArrayStack));

// Visit each block
// Tail merge predecessors
//
for (BasicBlock* const block : Blocks())
{
iterateTailMerge(block);
if (block->isEmpty())
{
continue;
}
}

if (block->KindIs(BBJ_THROW))
{
retOrThrowBlocks.Push(block);
}
else if (block->KindIs(BBJ_RETURN) && (block != genReturnBB))
// Deduplicate RETURN blocks
//
do
{
predInfo.Reset();
for (BasicBlock* const block : Blocks())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The set of eligible return and throw blocks never changes, so do we need to repeatedly walk the entire block list here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would also think we don't need to, however when I tried to hoist that it caused asserts.
I didn't look further into it because the same approach is already done in iterateTailMerge() and there are also multiple comments arround this code about improving algorithm efficiency.
So I'd prefer properly understanding the entire code and improving efficiency in a separate PR, in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

iterateTailMerge just walks the preds of a given block, not all blocks.

What asserts did you see?

Copy link
Copy Markdown
Contributor Author

@BoyBaykiller BoyBaykiller May 25, 2026

Choose a reason for hiding this comment

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

iterateTailMerge just walks the preds of a given block, not all blocks.

Yeah it happens to be only the preds here and so less of an issue but the fundamental thing of not needing to regenerate the set still applies I think.

What asserts did you see?

else // The statement is in the middle.
{
assert(stmt->GetPrevStmt() != nullptr && stmt->GetNextStmt() != nullptr);
Statement* prev = stmt->GetPrevStmt();
prev->SetNextStmt(stmt->GetNextStmt());
stmt->GetNextStmt()->SetPrevStmt(prev);
}

Took a quick look, the issue might be that we don't remove entries from predInfo after we merged them.
So it will try to merge them a second time on the second iter - and is never making any progress.
Let me see if I can fix it...

{
// Avoid splitting a return away from a possible tail call
//
if (!block->hasSingleStmt())
if (block->isEmpty())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check was here before, but I dont think we actually need it. Because we only accept RETURN or THROW blocks and these should never be empty?

{
continue;
}

if (block->KindIs(BBJ_THROW))
{
predInfo.Push(PredInfo(block, block->lastStmt()));
}
else if (block->KindIs(BBJ_RETURN) && (block != genReturnBB))
{
Statement* const lastStmt = block->lastStmt();
Statement* const prevStmt = lastStmt->GetPrevStmt();
GenTree* const prevTree = prevStmt->GetRootNode();
if (prevTree->IsCall() && prevTree->AsCall()->CanTailCall())
// Avoid splitting a return away from a possible tail call
//
if (!block->hasSingleStmt())
{
continue;
Statement* const lastStmt = block->lastStmt();
Statement* const prevStmt = lastStmt->GetPrevStmt();
GenTree* const prevTree = prevStmt->GetRootNode();
if (prevTree->IsCall() && prevTree->AsCall()->CanTailCall())
{
continue;
}
}
}

retOrThrowBlocks.Push(block);
predInfo.Push(PredInfo(block, block->lastStmt()));
}
}
}

predInfo.Reset();
for (BasicBlock* const block : retOrThrowBlocks.BottomUpOrder())
{
predInfo.Push(PredInfo(block, block->lastStmt()));
}

tailMergePreds(nullptr);
} while (tailMergePreds(nullptr));

// Work through any retries
//
Expand Down
Loading