Skip to content

JIT: clone array loops with stride > 57#129349

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:array-large-stride-cloning
Open

JIT: clone array loops with stride > 57#129349
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:array-large-stride-cloning

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Drop the blanket stride cap (stride < 58) in optDeriveLoopCloningConditions that previously rejected array loops whose post-step IV could exceed INT_MAX given Array.MaxLength. For larger strides emit a runtime arr.Length <= INT_MAX - s + 1 cloning condition; for small strides the implicit Array.MaxLength bound still suffices and no extra check is added.

Builds on similar work we added for spans in #129309.

Drop the blanket stride cap (`stride < 58`) in optDeriveLoopCloningConditions
that previously rejected array loops whose post-step IV could exceed INT_MAX
given Array.MaxLength. For larger strides emit a runtime
`arr.Length <= INT_MAX - s + 1` cloning condition; for small strides the
implicit Array.MaxLength bound still suffices and no extra check is added.

Reuses the Phase 2 span overflow guard for the HasInvariantLocalLimit case
and extends it with a HasArrayLengthLimit branch so the fast clone is taken
only for arrays short enough that the IV stays in int.

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

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib PTAL

There are no SPMI hits for this, but it feels like we should take this so the array and span cases are on an even footing (not "exact" because arrays have a smaller size limit and so can omit a check for smaller strides).

Eg if you span-wrap an array we should not suddenly start optimizing differently.

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 updates CoreCLR JIT loop cloning to allow array loops with strides ≥ 58 by replacing the previous blanket rejection with a runtime overflow guard (arr.Length <= INT_MAX - s + 1, adjusted for <= tests and limit offsets) when needed.

Changes:

  • Replace the prior stride cap for array-length loops with a conditional overflow guard for large strides (and keep span overflow guarding behavior consistent).
  • Rework the array-length limit handling to compute/reuse a single ArrIndex for deref/zero-trip/overflow/limit conditions.
  • Add new JIT tests covering array loops with large strides (e.g., 60 and 256) and a few related limit forms.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/loopcloning.cpp Removes the hard stride rejection and emits an additional runtime overflow condition for large strides where necessary.
src/tests/JIT/opt/Cloning/LargeStride.cs Adds functional tests exercising array loops with strides > 57 and a few related loop shapes.
src/tests/JIT/opt/Cloning/LargeStride.csproj Adds the corresponding test project file following the existing pattern in this directory.

Comment on lines +1229 to +1233
// For arrays the per-access cloning condition only bounds `limit` by
// Array.MaxLength (0x7FFFFFC7), which leaves room for the post-step IV
// up to `limit + s - 1` to fit in INT_MAX as long as `s <= 58`. Larger
// strides need an explicit overflow guard, same shape as the one used
// for spans (where Span<>.Length can reach INT_MAX even at small s).
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.

2 participants