JIT: add profitability heuristic for loop cloning#128870
Conversation
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>
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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::optCloningHeuristicto 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
LoopsRejectedForInsufficientBenefitto 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. |
|
@jakobbotsch PTAL 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>

Introduce JitCloneLoopsMinPerCallRatio, a release-config integer that determines whether a loop is cloned based on estimated per-call benefit ratio:
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.