Skip to content

JIT: guard IV overflow for span loops with array-length limits - #137773

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137679-span-overflow-guard
Nov 18, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137679-span-overflow-guard

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Loop cloning could remove span bounds checks when the induction variable wraps. For a Span loop whose limit is arr.Length + offset and whose stride is small (> 1 but < 58), the array-length overflow guard was skipped because it was only emitted for largeStride. Spans can be longer than Array.MaxLength and a large offset pushes the limit close to INT_MAX, so i += stride can wrap negative in the bounds-check-free clone, causing an access violation instead of IndexOutOfRangeException.

This emits the array-length guard whenever an overflow guard is required (spans or large strides). When the guard is unsatisfiable, cloning is declined; when trivially true, it is elided.

Adds a regression test to OffsetLimit.cs.

Validation:

  • Checked JIT without the fix: the new test crashes with an AccessViolationException; with the fix, it passes (OffsetLimit run=44 fail=0). Compared using matching Core_Root copies that differ only in clrjit.dll.
  • SuperPMI asmdiffs on benchmarks.run_pgo (windows x64): no asm diffs (95,454 contexts).

Resolves #137679

Note

This PR description and the change were generated with GitHub Copilot.

When a loop indexes a Span and its limit is arr.Length + offset, the
stride overflow guard was emitted only for large strides. A span can be
longer than Array.MaxLength and the offset can push the limit near
INT_MAX, so the IV can wrap in the bounds-check-free clone. Emit the
arr.Length guard whenever an overflow guard is required (spans or large
strides).

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ded1b4dc-1d7e-4eef-ab16-fc90912f191c
@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 Nov 17, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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
AndyAyersMS requested review from EgorBo and a balanced review from Copilot November 17, 2026 20:30
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo PTAL
fyi @dotnet/jit-contrib

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.

🟢 Approval recommended

No active review issues were identified for the span-loop cloning overflow-guard change.

No open issues 🎉

What changed in this PR

This PR fixes JIT loop cloning for span loops with array-length-based limits, preserving bounds checks when the induction variable could overflow.

Changes:

  • Extends the array-length overflow guard to span loops with small non-unit strides.
  • Adds a regression test that expects IndexOutOfRangeException when a stride-four index wraps negative.
File Description
src/​tests/​JIT/​opt/​Cloning/​OffsetLimit.cs Adds a near-maximum-length span regression test, skipped if allocation fails.
src/​coreclr/​jit/​loopcloning.cpp Applies the array-length guard whenever overflow protection is required, not only for large strides.

Review effort: Balanced

@AndyAyersMS
AndyAyersMS merged commit ce97120 into dotnet:main Nov 18, 2026
128 of 130 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
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

3 participants