Skip to content

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

Closed

Description

@BoyBaykiller

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     

Activity

  1. ghost added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Apr 16, 2025
  2. dotnet-policy-service commented on Apr 16, 2025

    @dotnet-policy-service
    Contributor

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

  3. removed
    untriagedNew issue has not been triaged by the area owner
    on Apr 17, 2025
  4. added this to the Future milestone on Apr 17, 2025
  5. JulieLeeMSFT commented on Apr 17, 2025

    @JulieLeeMSFT
    Member

    CC @BruceForstall, @dotnet/jit-contrib.

  6. self-assigned this
    on Apr 20, 2025
  7. EgorBo commented on Apr 20, 2025

    @EgorBo
    Member

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

    if (x == new int[x].Length)
    
  8. modified the milestones: Future, 10.0.0 on Apr 20, 2025
  9. locked and limited conversation to collaborators on May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions