Skip to content

Regression: Extra range check generated #115090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rameel opened this issue Apr 27, 2025 · 3 comments
Open

Regression: Extra range check generated #115090

rameel opened this issue Apr 27, 2025 · 3 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@rameel
Copy link
Contributor

rameel commented Apr 27, 2025

In the latest .NET build, additional range checks are introduced when slicing a span.

godbolt

A repro:

public static int Test(ReadOnlySpan<char> s)
{
    while (s.Length != 0)
    {
        if (s[^1] != ' ')
            break;

        s = s.Slice(0, s.Length - 1);
    }
    
    return s.Length;
}

trunk-20250427+cb4581af6e:

Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):
       push     rbp
       mov      rbp, rsp
       test     esi, esi
       je       SHORT G_M44937_IG04
       align    [8 bytes for IG03]
G_M44937_IG03:  ;; offset=0x0010
       lea      eax, [rsi-0x01]
       mov      ecx, eax
       cmp      ecx, esi                   ; <---- extra check
       jae      SHORT G_M44937_IG07        ; <----
       cmp      word  ptr [rdi+2*rcx], 32
       jne      SHORT G_M44937_IG04
       cmp      eax, esi
       ja       SHORT G_M44937_IG06
       mov      esi, eax
       test     esi, esi
       jne      SHORT G_M44937_IG03
G_M44937_IG04:  ;; offset=0x002A
       mov      eax, esi
       pop      rbp
       ret      
G_M44937_IG06:  ;; offset=0x002E
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
G_M44937_IG07:  ;; offset=0x0035
       call     CORINFO_HELP_RNGCHKFAIL
       int3     

9.0.2:

Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):
       push     rbp
       mov      rbp, rsp
G_M44937_IG02:  ;; offset=0x0004
       test     esi, esi
       je       SHORT G_M44937_IG04
       lea      eax, [rsi-0x01]
       mov      ecx, eax
       cmp      word  ptr [rdi+2*rcx], 32
       jne      SHORT G_M44937_IG04
       cmp      eax, esi
       ja       SHORT G_M44937_IG06
       mov      esi, eax
       jmp      SHORT G_M44937_IG02
G_M44937_IG04:  ;; offset=0x001C
       mov      eax, esi
       pop      rbp
       ret      
G_M44937_IG06:  ;; offset=0x0020
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 27, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 27, 2025
Copy link
Contributor

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

@rameel
Copy link
Contributor Author

rameel commented Apr 27, 2025

If the loop condition is slightly modified, .NET 9 is able to eliminate all unnecessary checks, while in the latest build there are still two redundant range checks.

godbolt

public static int Test(ReadOnlySpan<char> s)
{
    while ((uint)s.Length - 1 < (uint)s.Length) // loop condition changed
    {
        if (s[^1] != ' ')
            break;

        s = s.Slice(0, s.Length - 1);
    }
    
    return s.Length;
}
Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):
       push     rbp
       mov      rbp, rsp
G_M44937_IG02:  ;; offset=0x0004
       lea      eax, [rsi-0x01]
       cmp      eax, esi
       jae      SHORT G_M44937_IG04
       mov      ecx, eax
       cmp      word  ptr [rdi+2*rcx], 32
       jne      SHORT G_M44937_IG04
       mov      esi, eax
       jmp      SHORT G_M44937_IG02
G_M44937_IG04:  ;; offset=0x0018
       mov      eax, esi
       pop      rbp
       ret      

@JulieLeeMSFT JulieLeeMSFT removed the untriaged New issue has not been triaged by the area owner label May 29, 2025
@JulieLeeMSFT JulieLeeMSFT added this to the Future milestone May 29, 2025
@JulieLeeMSFT
Copy link
Member

CC @dotnet/jit-contrib.

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

No branches or pull requests

2 participants