Skip to content

JIT: fix profile weights when folding switches with self-edges - #137702

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:jit-fix-switch-profile-137642
Nov 17, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:jit-fix-switch-profile-137642

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

When folding a constant switch whose block is also one of its own successors, the self-edge is visited while subtracting edge weights, which lowers the block's weight before the remaining edges are processed. Later successors then use the reduced weight, so removed blocks keep stale weight and the retained target ends up too heavy (e.g. 0.05 / 1.05 instead of 0 / 1).

Snapshot the block weight once before adjusting successors.

The regression test is a plain correctness test; the bad weights are transient (the fold marks the profile inconsistent and post-morph repair resynthesizes), so the test doesn't check them directly. Verified the fix via JIT dump with DOTNET_JitSynthesizeCounts=1.

Resolves #137642

Note

This PR description was generated with GitHub Copilot.

Use the original block weight for each removed edge. Add a regression test.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c2f2ea71-2c5f-41c9-b7ed-b1d9af3d1ba7
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 17, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo ptal
fyi @dotnet/jit-contrib

@AndyAyersMS
AndyAyersMS requested review from EgorBo and a balanced review from Copilot November 17, 2026 00:53
@EgorBo

EgorBo commented Nov 17, 2026

Copy link
Copy Markdown
Member

@AndyAyersMS it seems there are similar places in JIT like this, my local AI run found these: 643cff9

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the change addressing traversal-order-dependent profile weights in constant switches with self-edges.

No open issues 🎉

What changed in this PR

This PR corrects JIT profile-weight adjustments when folding a constant switch with a self-edge, preventing successor weights from depending on traversal order.

Changes:

  • Snapshot the switch block’s weight before subtracting outgoing edge contributions.
  • Add a correctness regression test covering a constant switch with a self-edge; it does not directly assert profile weights.
File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_137642.cs Adds a self-edge switch test that checks the selected branch’s result.
src/​coreclr/​jit/​morph.cpp Uses the original block weight for all successor-weight deductions.

Review effort: Balanced

Apply the same self-edge fix to fgConvertBBToThrowBB, fgUnreachableBlock, and LowerReturnSuspend.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c2f2ea71-2c5f-41c9-b7ed-b1d9af3d1ba7
@AndyAyersMS

Copy link
Copy Markdown
Member Author

Thanks, I applied the same snapshot fix to fgConvertBBToThrowBB, fgUnreachableBlock, and Lowering::LowerReturnSuspend in a follow-up commit. SuperPMI asmdiffs were clean across 2,836,071 contexts.

Note

This comment was generated with GitHub Copilot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) fgFoldConditional leaves inconsistent block weights when a folded switch case targets the switch block itself

3 participants