Skip to content

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

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:invert-boost-invariant-loads
Jun 17, 2026
Merged

AndyAyersMS merged 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 <[email protected]>
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 thread src/coreclr/jit/optimizer.cpp Resolved
@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

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g persistent timeouts

@AndyAyersMS
AndyAyersMS merged commit af7b1d7 into dotnet:main Jun 17, 2026
137 of 141 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 18, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
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.

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 19, 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.

JIT: Bounds check not hoisted or eliminated for simple InlineArray iteration

3 participants