Skip to content

JIT: boost loop inversion cost limit for some loops#129472

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:invert-boost-invariant-loads
Open

JIT: boost loop inversion cost limit for some loops#129472
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:invert-boost-invariant-loads

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Recognize loops where the IV increment is in the backedge source block and also might profit from loop cloning. Increase the cost limit for such cases to enable inversion (which in turn enables cloning and other opts).

Fixes #119693.

Recognize loops where the IV increment is in the backedge source
block and also might profit from loop cloning. Increase the cost
limit for such cases.

Fixes dotnet#119693.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 16, 2026 17:08
@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 16, 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 CoreCLR JIT loop inversion profitability heuristics to more often allow while/for loop inversion in cases that are likely to benefit from subsequent loop cloning (notably when the IV increment is in a backedge source block rather than the condition block).

Changes:

  • Tracks whether a loop “might benefit from cloning” outside the size-limit gate so it can be reused by later inversion-cost heuristics.
  • Adds detection for “split IV test + increment” patterns (increment in a backedge source block) and boosts the allowed duplication cost to make inversion more likely.
  • Extends verbose DEBUG diagnostics to report the new heuristic signal.

Comment on lines +2122 to 2126
if ((optInvertInfo.sharedStaticHelperCount > 0) || (optInvertInfo.arrayLengthCount > 0) ||
hasSplitIVTestAndIncrement)
{
// Calculate a new maximum cost. We might be able to early exit.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

Still working on this so no rush to review.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

Still working on this so no rush to review.

Actually no... was looking at what it would take to get rid of the in-loop bounds check, but it is out of reach (the array is a loop invariant but we'd need it to be CSEd first).

PTAL @jakobbotsch
fyi @dotnet/jit-contrib

Diff for the issue's case is:

image

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: Bounds check not hoisted or eliminated for simple InlineArray iteration

2 participants