Skip to content

JIT: add profitability heuristic for loop cloning#128870

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:LoopCloningHeuristic
Jun 2, 2026
Merged

JIT: add profitability heuristic for loop cloning#128870
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:LoopCloningHeuristic

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

Introduce JitCloneLoopsMinPerCallRatio, a release-config integer that determines whether a loop is cloned based on estimated per-call benefit ratio:

PerCallRatio = (cycles saved per method call) / (duplicated body nodes)

Higher threshold values are stricter and produce fewer clones; a threshold of 0 disables the heuristic. Default is 4.

The new helper optCloningHeuristic combines the benefit and cost estimates and returns a bool decision:

  • Benefit estimate: sum cycles saved across the LcOptInfos collected by loop-cloning analysis, weighted by the bbWeight of the containing block. Per-block cycle figures are 2.0 for array/span bounds checks and 3.0 for the GDV-style tests.

  • Cost estimate: counts tree nodes that would be duplicated by cloning. The count is bounded by min(JitCloneLoopsSizeLimit, ceil(benefit/minPerCallRatio)+1) so it short-circuits as soon as the body grows large enough that the heuristic will fail.

Also adds the LoopsRejectedForInsufficientBenefit JIT metric so the number of heuristic rejections is visible in SPMI details output.

Introduce JitCloneLoopsMinPerCallRatio, a release-config integer that
gates whether a loop is cloned based on its per-call benefit ratio:

    PerCallRatio = (cycles saved per method call) / (duplicated body nodes)

Higher threshold values are stricter and produce fewer clones; a
threshold of 0 disables the heuristic. Default is 4.

The new helper optCloningHeuristic combines the benefit and cost
estimates and returns a bool decision:

  - Benefit estimate: sum cycles saved across the LcOptInfos collected
    by loop-cloning analysis, weighted by the bbWeight of the containing
    block. Per-block cycle figures are 2.0 for array/span bounds checks
    and 3.0 for the GDV-style tests.

  - Cost estimate: counts tree nodes that would be duplicated by cloning.
    The count is bounded by min(JitCloneLoopsSizeLimit,
    ceil(benefit/minPerCallRatio)+1) so it short-circuits as soon as the
    body grows large enough that the heuristic will fail.

Also adds the LoopsRejectedForInsufficientBenefit JIT metric so the
number of heuristic rejections is visible in SPMI details output.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 1, 2026 23:00
@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 1, 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 adds a new profitability gate to JIT loop cloning that compares an estimated per-call cycle benefit against an estimated code-size cost (duplicated IR nodes), and exposes a new JIT metric for loops rejected by this gate.

Changes:

  • Adds Compiler::optCloningHeuristic to decide whether a loop cloning candidate should be accepted based on benefit/cost ratio.
  • Introduces JitCloneLoopsMinPerCallRatio (release config) to control the heuristic threshold (0 disables).
  • Adds LoopsRejectedForInsufficientBenefit to JIT metadata metrics and increments it when the heuristic rejects a candidate.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/jit/loopcloning.cpp Implements the new cloning profitability heuristic and wires it into the cloning pass.
src/coreclr/jit/jitmetadatalist.h Adds a new metric for heuristic rejections.
src/coreclr/jit/jitconfigvalues.h Adds a new release config integer to control the heuristic threshold.
src/coreclr/jit/compiler.h Declares the new optCloningHeuristic helper on Compiler.

Comment thread src/coreclr/jit/loopcloning.cpp Outdated
Comment thread src/coreclr/jit/loopcloning.cpp Outdated
Comment thread src/coreclr/jit/loopcloning.cpp Outdated
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@jakobbotsch PTAL
fyi @dotnet/jit-contrib

Should eliminate about 10% of clones in libraries tests (large loops with small benefit). Less in benchmarks (which more deliberately set up hot loops).

This is something we've wanted for a while, and also a prerequisite to expanding cloning based on other kinds of loop invariants.

* Normalize bbWeight by BB_UNITY_WEIGHT so the benefit estimate is in
  true per-call cycles (previously inflated 100x).
* Drop the dead if (absWeight < 0.0) clamp.
* Drop the redundant +1 in the cost cap; use floor(benefit / minPCR).
* Change bodyNodes to unsigned to match the walker's return type.
* Interpret JitCloneLoopsMinPerCallRatio in hundredths (divide by 100
  at the use site) so the existing integer config can express the
  fractional thresholds the heuristic actually needs (default 4 means
  a threshold of 0.04). Update banner and config comments accordingly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread src/coreclr/jit/loopcloning.cpp
@AndyAyersMS
Copy link
Copy Markdown
Member Author

AndyAyersMS commented Jun 2, 2026

Should eliminate about 10% of clones in libraries tests

10% was actually on windows pgo benchmarks; looks like we ended up a bit higher for other archs.

image

We can run with this and if it's too aggressive about trimming we can always dial it back.

@AndyAyersMS AndyAyersMS merged commit a2ec76b into dotnet:main Jun 2, 2026
137 of 139 checks passed
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.

3 participants