Skip to content

Consecutive range checks are not combined #79490

@svick

Description

@svick

Description

Consider the following code (inspired by dotnet/csharplang#4082 (comment)):

static int ArrayAccess(int[] abcd)
{
    return abcd[0] + abcd[1] + abcd[2] + abcd[3];
}

Note that it accesses the array multiple times at increasing indexes, which I think is a relatively common pattern. This results in one range check for every array access:

L0000: mov eax, [ecx+4]
L0003: test eax, eax
L0005: je short L0025
L0007: mov edx, [ecx+8]
L000a: cmp eax, 1
L000d: jbe short L0025
L000f: add edx, [ecx+0xc]
L0012: cmp eax, 2
L0015: jbe short L0025
L0017: add edx, [ecx+0x10]
L001a: cmp eax, 3
L001d: jbe short L0025
L001f: mov eax, edx
L0021: add eax, [ecx+0x14]
L0024: ret
L0025: call 0x71efa060
L002a: int3

This is annoying, because only the check for abcd[3] is necessary, since there are no side-effects between the array accesses. Even though these range checks are likely to be well predicted by the CPU, I think it would be nice if they could be elided by the JIT.

This may very well be a duplicate, but I couldn't find it. Or it may not be worth tracking, in which case, feel free to close.

Configuration

Current SharpLab: Core CLR 7.0.22.51805 on x86

Regression?

Not that I know of.

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions