Skip to content

JIT: Redundant bounds check for when loopUpperBound==arrayLength #114744

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

Closed
Tracked by #109677
BoyBaykiller opened this issue Apr 16, 2025 · 3 comments · Fixed by #114845
Closed
Tracked by #109677

JIT: Redundant bounds check for when loopUpperBound==arrayLength #114744

BoyBaykiller opened this issue Apr 16, 2025 · 3 comments · Fixed by #114845
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@BoyBaykiller
Copy link

Loop cloning generates a fast path without bounds check but this can be statically optimized without needing to keep the slow path: https://godbolt.org/z/3WeaEfWh3

static void Test(int count)
{
    int[] arrA = new int[count];
    float[] arrB = new float[count];
    for (int i = 0; i < count; i++)
    {
        arrA[i] = 0;     // bounds check
        arrB[i] = 0.0f;  // bounds check
    }
}
Program:Test(int) (FullOpts):
       push     rbp
       push     r15
       push     rbx
       lea      rbp, [rsp+0x10]
       mov      ebx, edi
       movsxd   rsi, ebx
       mov      rdi, 0x79E0784C8C88      ; int[]
       call     CORINFO_HELP_NEWARR_1_VC
       mov      r15, rax
       movsxd   rsi, ebx
       mov      rdi, 0x79E078711E90      ; float[]
       call     CORINFO_HELP_NEWARR_1_VC
       xor      ecx, ecx
       test     ebx, ebx
       jle      SHORT G_M29168_IG06
       cmp      dword ptr [r15+0x08], ebx
       jl       SHORT G_M29168_IG07
       cmp      dword ptr [rax+0x08], ebx
       jl       SHORT G_M29168_IG07
       mov      ecx, 16
       align    [0 bytes for IG04]
G_M29168_IG04:  ;; offset=0x0048
       xor      edx, edx
       mov      dword ptr [r15+rcx], edx
       mov      dword ptr [rax+rcx], edx
       add      rcx, 4
       dec      ebx
       jne      SHORT G_M29168_IG04
G_M29168_IG06:  ;; offset=0x0059
       pop      rbx
       pop      r15
       pop      rbp
       ret      
G_M29168_IG07:  ;; offset=0x005E
       cmp      ecx, dword ptr [r15+0x08]
       jae      SHORT G_M29168_IG09
       mov      edx, ecx
       xor      edi, edi
       mov      dword ptr [r15+4*rdx+0x10], edi
       cmp      ecx, dword ptr [rax+0x08]
       jae      SHORT G_M29168_IG09
       mov      edx, ecx
       mov      dword ptr [rax+4*rdx+0x10], edi
       inc      ecx
       cmp      ecx, ebx
       jl       SHORT G_M29168_IG07
       jmp      SHORT G_M29168_IG06
G_M29168_IG09:  ;; offset=0x0080
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 16, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 16, 2025
Copy link
Contributor

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

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

CC @BruceForstall, @dotnet/jit-contrib.

@EgorBo EgorBo self-assigned this Apr 20, 2025
@EgorBo
Copy link
Member

EgorBo commented Apr 20, 2025

The LC's checks should be foldable at VN level, we basically have:

if (x == new int[x].Length)

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 a pull request may close this issue.

3 participants