Skip to content

Remove bounds checks for 'i + cns < len' pattern#124242

Merged
EgorBo merged 9 commits intodotnet:mainfrom
EgorBo:optimize-rng-chck-3
Feb 11, 2026
Merged

Remove bounds checks for 'i + cns < len' pattern#124242
EgorBo merged 9 commits intodotnet:mainfrom
EgorBo:optimize-rng-chck-3

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 10, 2026

Closes #121262

public static bool Test(ReadOnlySpan<char> span)
{
    for (int i = 0; i < span.Length; i++)
    {
        if (span[i] == '%' && (uint)(i + 2) < (uint)span.Length)
        {
            if (span[i + 1] == 'F' && // Bounds check
                span[i + 2] == 'F')
            {
                return true;
            }
        }
    }
    return false;
}

New codegen:

; Method Prog:Test(System.ReadOnlySpan`1[char]):bool (FullOpts)
       mov      rax, bword ptr [rcx]
       mov      ecx, dword ptr [rcx+0x08]
       xor      edx, edx
       test     ecx, ecx
       je       SHORT G_M967_IG06
       align    [0 bytes for IG03]
G_M967_IG03:
       cmp      word  ptr [rax+2*rdx], 37
       jne      SHORT G_M967_IG05
       lea      r8d, [rdx+0x02]
       cmp      r8d, ecx
       jae      SHORT G_M967_IG05
       lea      r10d, [rdx+0x01]
       cmp      word  ptr [rax+2*r10], 70
       jne      SHORT G_M967_IG05
       cmp      word  ptr [rax+2*r8], 70
       je       SHORT G_M967_IG08
G_M967_IG05:
       inc      edx
       cmp      edx, ecx
       jl       SHORT G_M967_IG03
G_M967_IG06:
       xor      eax, eax
       ret      
G_M967_IG08:
       mov      eax, 1
       ret      
; Total bytes of code: 63

Diffs

Copilot AI review requested due to automatic review settings February 10, 2026 20:36
@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 Feb 10, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

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 range-check analysis to recognize unsigned compare patterns like (uint)(i + cns) < (uint)len and use them to tighten inferred ranges, enabling elimination of redundant bounds checks for nearby indices (e.g., i+1, i+2) in common Span/array loops.

Changes:

  • Extend RangeCheck::MergeEdgeAssertions to derive a range for a base value number (normalLclVN) from an assertion on an offset form (normalLclVN + cns).
  • Prefer deductions that relate the base induction variable to the “preferred” checked bound VN (typically the length VN).

Copilot AI review requested due to automatic review settings February 10, 2026 21:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings February 10, 2026 22:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@dotnet dotnet deleted a comment from Copilot AI Feb 10, 2026
Copilot AI review requested due to automatic review settings February 11, 2026 00:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@EgorBo EgorBo marked this pull request as ready for review February 11, 2026 12:00
@EgorBo
Copy link
Member Author

EgorBo commented Feb 11, 2026

PTAL @jakobbotsch @dotnet/jit-contrib

@EgorBo EgorBo requested a review from jakobbotsch February 11, 2026 12:01
@EgorBo EgorBo enabled auto-merge (squash) February 11, 2026 15:10
@EgorBo
Copy link
Member Author

EgorBo commented Feb 11, 2026

/ba-g deadletter + infra issues + filed #124279

@EgorBo EgorBo merged commit 7c04074 into dotnet:main Feb 11, 2026
124 of 133 checks passed
EgorBo added a commit that referenced this pull request Feb 12, 2026
Bad merge of two PRs at once.
#124132 - introduced a VN cache to
find out if we have any assertions for a VN. And
#124242 introduced a VN
decomposition inside MergeEdgeAssertion loop where it tries to match
VNF_ADD(X, CNS). So the fix is to register VNF_ADD's op1 VN as well.

As the result, #124242 couldn't
find the matching assertion and the FILECHECK test failed due to bounds
check in the codegen.
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.

Bounds check after overlapping span length checks

2 participants