JIT: eliminate redundant bounds checks for span.Slice(span.Length - cns)#127488
Merged
EgorBo merged 2 commits intodotnet:mainfrom Apr 28, 2026
Merged
JIT: eliminate redundant bounds checks for span.Slice(span.Length - cns)#127488EgorBo merged 2 commits intodotnet:mainfrom
EgorBo merged 2 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Two complementary changes: * VN: add identity 'x - (x + a) == -a' so that 'length - (length - cns)' folds to 'cns', eliminating the BinaryPrimitives length check. * RangeCheck: in GetRangeFromAssertionsWorker, recognize the correlated relop 'ADD(A, K) op A' (K constant < 0) and fold it when A's lower bound proves ADD(A, K) does not wrap, eliminating the Slice 'start > length' check. Fixes dotnet#127486 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
366d859 to
490ce9b
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves CoreCLR JIT optimizations to eliminate redundant bounds checks in the common pattern span.Slice(span.Length - cns) by strengthening value numbering identities and range-check assertion reasoning, enabling subsequent checks (including in BinaryPrimitives.ReadInt32BigEndian) to fold away.
Changes:
- Extend VN math identities to recognize
x - (x + a)/x - (a + x)as-a(non-floating, non-overflowing case). - Enhance RangeCheck’s assertion-based relop folding by proving
ADD(A, K) < Afor negativeKwhenA’s lower bound is high enough to prevent wraparound.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/coreclr/jit/valuenum.cpp | Adds a new subtraction identity and cleans up ADD/SUB VNFunc usage to improve VN simplification for length - (length - cns) patterns. |
| src/coreclr/jit/rangecheck.cpp | Adds correlated relop folding logic to eliminate the Slice(start) (uint)start > (uint)_length check when proven false via assertions. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Apr 28, 2026
Open
Member
Author
|
PTAL @jakobbotsch @dotnet/jit-contrib Diffs |
jakobbotsch
approved these changes
Apr 28, 2026
Member
Author
|
/ba-g filed #127500 for ios/android failure (there are hits in other PRs) |
EgorBo
added a commit
that referenced
this pull request
Apr 28, 2026
…ns (#127485) Replaces `Unsafe.ReadUnaligned` / `Unsafe.Add` / `MemoryMarshal.GetReference` usage in `IBinaryInteger<T>.TryReadBigEndian` (Byte, SByte, Char, Int16/UInt16, Int32/UInt32, Int64/UInt64, IntPtr/UIntPtr, Int128/UInt128) with safe equivalents (`BinaryPrimitives.ReadXxxBigEndian` and span indexing). The JIT is expected to eliminate the resulting bounds checks given the existing length gates. > [!NOTE] > This PR was authored with assistance from Copilot CLI. --- ~~UPD: blocked by #127486 (improved in JIT via #127488) [Diffs](MihuBot/runtime-utils#1867) --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Closes #127486
For the snippet from the issue:
Two changes:
x - (x + a) == -aso thatlength - (length - cns)value-numbers tocns. This eliminates theBinaryPrimitives.ReadInt32BigEndianlength check afterSlice.GetRangeFromAssertionsWorker, recognize the correlated relopADD(A, K) op A(constantK < 0) and fold it whenA's lower bound is>= -K(so the add cannot wrap, signed or unsigned). This eliminates theSlice(start)(uint)start > (uint)_lengthcheck.