Skip to content

JIT: fix edge likelihoods for loop cloning - #129646

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:cloning-cond-chain-likelihood
Jun 22, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:cloning-cond-chain-likelihood

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

We were over-estimating the edge likelihoods leading to the fast clone, causing profiles to inflate. The old code was not aware of the total number of checks that need to pass to reach the fast clone, so each edge's likelihood was slightly too high.

This shows up prominently when we clone a set of nested loops.

Fix by counting how many conditional branches we must pass through before reaching the fast clone, and use that to compute the proper likelihoods.

We were over-estimating the edge likelihoods leading to the fast
clone, causing profiles to inflate. The old code was not aware
of the total number of checks that need to pass to reach the fast
clone, so each edge's likelihood was slightly too high.

This shows up prominently when we clone a set of nested loops.

Fix by counting how many conditional branches we must pass through
before reaching the fast clone, and use that to compute the
proper likelihoods.
Copilot AI review requested due to automatic review settings June 20, 2026 01:49
@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 Jun 20, 2026
@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.

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.

Pull request overview

This PR adjusts how loop cloning assigns edge likelihoods for the fast-path gating condition chain so that the cumulative probability of reaching the fast clone matches the intended fastPathWeightScaleFactor, even when the condition chain is constructed across multiple CondToStmtInBlock calls (e.g., block/deref conditions plus cloning conditions).

Changes:

  • Extend LoopCloneContext::CondToStmtInBlock to accept totalCondsInChain and use it to compute the per-conditional likelihood (Nth-root model) against the full chain length.
  • In Compiler::optInsertLoopChoiceConditions, pre-count total condition blocks to be inserted and pass that count to each CondToStmtInBlock call.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/coreclr/jit/loopcloning.h Updates the CondToStmtInBlock declaration and documents the new totalCondsInChain parameter.
src/coreclr/jit/loopcloning.cpp Uses the full chain condition count to compute per-branch likelihoods; counts and threads the total through optInsertLoopChoiceConditions.

@AndyAyersMS

AndyAyersMS commented Jun 20, 2026 •

Copy link
Copy Markdown
Member Author

@jakobbotsch PTAL
fyi @dotnet/jit-contrib

Definitely affects perf scores in some cases... still evaluating diffs, but what we were doing before was wrong.

benchmarks.run_pgo.windows has only ~200 code diffs locally, but −301,305,052 (−41.67% of base) perf score diff. Diffs look mostly to be layout changes.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

diffs

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@jakobbotsch PTAL

@AndyAyersMS
AndyAyersMS merged commit 8216cbe into dotnet:main Jun 22, 2026
140 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
We were over-estimating the edge likelihoods leading to the fast clone,
causing profiles to inflate. The old code was not aware of the total
number of checks that need to pass to reach the fast clone, so each
edge's likelihood was slightly too high.

This shows up prominently when we clone a set of nested loops.

Fix by counting how many conditional branches we must pass through
before reaching the fast clone, and use that to compute the proper
likelihoods.
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants