Skip to content

JIT does not eliminate bounds check with 64-bit indexed loop #104659

@jakobbotsch

Description

@jakobbotsch
private static int Sum(int[] arr)
{
    int sum = 0;
    for (nint i = 0; i < arr.Length; i++)
    {
        sum += arr[i];
    }

    return sum;
}

Codegen:

G_M17360_IG03:  ;; offset=0x0020
       cmp      rdx, r8
       jae      SHORT G_M17360_IG05
       add      eax, dword ptr [rcx+4*rdx+0x10]
       inc      rdx
       cmp      r8, rdx
       jg       SHORT G_M17360_IG03
						;; size=17 bbWeight=4 PerfScore 23.00
G_M17360_IG04:  ;; offset=0x0031
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M17360_IG05:  ;; offset=0x0036
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
						;; size=6 bbWeight=0 PerfScore 0.00

we should be able to eliminate this bounds check even with the 64-bit index.

Metadata

Metadata

Assignees

No one assigned

    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