Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Repair profile after tail-merging #110656

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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: 12 additions & 5 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4657,11 +4657,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_CLONE_FINALLY, &Compiler::fgCloneFinally);

// Drop back to just checking profile likelihoods.
//
activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE;
activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS;

// Do some flow-related optimizations
//
if (opts.OptimizationEnabled())
Expand All @@ -4672,6 +4667,11 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
return fgHeadTailMerge(true);
});

// Drop back to just checking profile likelihoods.
//
activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE;
activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS;

// Merge common throw blocks
//
DoPhase(this, PHASE_MERGE_THROWS, &Compiler::fgTailMergeThrows);
Expand All @@ -4680,6 +4680,13 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_EARLY_UPDATE_FLOW_GRAPH, &Compiler::fgUpdateFlowGraphPhase);
}
else
{
// Drop back to just checking profile likelihoods.
//
activePhaseChecks &= ~PhaseChecks::CHECK_PROFILE;
activePhaseChecks |= PhaseChecks::CHECK_LIKELIHOODS;
}

// Promote struct locals
//
Expand Down
11 changes: 11 additions & 0 deletions src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,18 @@ PhaseStatus Compiler::fgTailMergeThrows()
case BBJ_SWITCH:
{
JITDUMP("*** " FMT_BB " now branching to " FMT_BB "\n", predBlock->bbNum, canonicalBlock->bbNum);
FlowEdge* prevEdge = fgGetPredForBlock(nonCanonicalBlock, predBlock);
weight_t removedWeight = prevEdge->getLikelyWeight();
fgReplaceJumpTarget(predBlock, nonCanonicalBlock, canonicalBlock);

// In practice, when we have true profile data, we can repair it locally here, since the no-return
// call means that there is no contribution from nonCanonicalBlock to any of its successors.
// Note that this might not be the case if we have profile data from e.g. synthesis, so this
// repair is best-effort only.
if (canonicalBlock->hasProfileWeight())
{
canonicalBlock->setBBProfileWeight(canonicalBlock->bbWeight + removedWeight);
Copy link
Member

Choose a reason for hiding this comment

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

If you enable profile consistency checks for throw merging, I suspect this will cause asserts; since we don't convert canonicalBlock into a BBJ_THROW block until morph, the post-phase checks will see new weight into canonicalBlock that isn't being propagated to its successors. If you add some logic to make fgPgoConsistent false here, I think you'll be able to enable profile checks after this phase without issue (though I'm ok with you leaving them off for now, I can enable them in another PR).

Also, sorry for creating merge conflicts here

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, the comment above explains the situation. In practice this local update keeps global consistency, but in synthetic cases it does not. That's why I kept the profile checking disabled for this phase. Do you prefer to move the disablement further in favor of setting fgPgoConsistent = false here? If so I'll do that.

Copy link
Member

@amanasifkhalid amanasifkhalid Jan 6, 2025

Choose a reason for hiding this comment

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

Up to you -- I have the checks enabled in a draft PR (#111047), so I'll have to rebase that on top of this one either way

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's just close this one in favor of #111047

}
updated = true;
}
break;
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6576,6 +6576,13 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
FlowEdge* const newEdge = fgAddRefPred(crossJumpTarget, predBlock);
predBlock->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
}

// For tail merge we have a common successor of predBlock and
// crossJumpTarget, so the profile update can be done locally.
if (crossJumpTarget->hasProfileWeight() && predBlock->hasProfileWeight())
{
crossJumpTarget->setBBProfileWeight(crossJumpTarget->bbWeight + predBlock->bbWeight);
}
}

// We changed things
Expand Down
Loading