Skip to content

Range check elimination for multi-dimensional array #35056

Open
@kunalspathak

Description

@kunalspathak

For Jagged array, it is not possible to hoist the column length check out of the loop, however it should be possible for 2D array or if we are doing loop cloning we should be able to generate a faster version of loop that doesn't have these checks.

static int Calculate(int[,] b, int col)
{
    int  result = 0;
    for (int i = 1; i < 10; i++)
    {
        result = result + b[i,col];
    }
    return result;
}
G_M3911_IG01:
       4883EC28             sub      rsp, 40
                                                ;; bbWeight=1    PerfScore 0.25
G_M3911_IG02:
       33C0                 xor      eax, eax
       41B801000000         mov      r8d, 1
                                                ;; bbWeight=1    PerfScore 0.50
G_M3911_IG03:
       458BC8               mov      r9d, r8d
       442B4918             sub      r9d, dword ptr [rcx+24]
       443B4910             cmp      r9d, dword ptr [rcx+16]
       732E                 jae      SHORT G_M3911_IG05            # <-- can be outside the loop
       448BD2               mov      r10d, edx
       442B511C             sub      r10d, dword ptr [rcx+28]
       443B5114             cmp      r10d, dword ptr [rcx+20]
       7321                 jae      SHORT G_M3911_IG05           # <-- can be outside the loop
       448B5914             mov      r11d, dword ptr [rcx+20]
       4D0FAFD9             imul     r11, r9
       4D8BCA               mov      r9, r10
       4D03CB               add      r9, r11
       4203448920           add      eax, dword ptr [rcx+4*r9+32]
       41FFC0               inc      r8d
       4183F80A             cmp      r8d, 10
       7CCA                 jl       SHORT G_M3911_IG03
                                                ;; bbWeight=4    PerfScore 74.00
G_M3911_IG04:
       4883C428             add      rsp, 40
       C3                   ret
                                                ;; bbWeight=1    PerfScore 1.25
G_M3911_IG05:
       E8445D4A5F           call     CORINFO_HELP_RNGCHKFAIL
       CC                   int3

category:cq
theme:range-check
skill-level:expert
cost:large

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

Status

Backlog (General)

Relationships

None yet

Development

No branches or pull requests

Issue actions