Repository navigation
JIT: guard IV overflow for span loops with array-length limits - #137773
Merged
AndyAyersMS merged 1 commit intoNov 18, 2026
Merged
Conversation
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
|
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. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Member
Author
|
@EgorBo PTAL |
Contributor
There was a problem hiding this comment.
🟢 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
IndexOutOfRangeExceptionwhen 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
EgorBo
approved these changes
Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Loop cloning could remove span bounds checks when the induction variable wraps. For a
Spanloop whose limit isarr.Length + offsetand whose stride is small (> 1 but < 58), the array-length overflow guard was skipped because it was only emitted forlargeStride. Spans can be longer thanArray.MaxLengthand a large offset pushes the limit close toINT_MAX, soi += stridecan wrap negative in the bounds-check-free clone, causing an access violation instead ofIndexOutOfRangeException.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:
AccessViolationException; with the fix, it passes (OffsetLimitrun=44 fail=0). Compared using matching Core_Root copies that differ only inclrjit.dll.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.