KEMBAR78
Unnecessary call to `CORINFO_HELP_RNGCHKFAIL` with subtraction and bitwise and · Issue #96046 · dotnet/runtime · GitHub
Skip to content

Unnecessary call to CORINFO_HELP_RNGCHKFAIL with subtraction and bitwise and #96046

@hamarb123

Description

@hamarb123

Reproduction:

static ref readonly byte X(int a) => ref _0xFFThen0x00_512[64 - (a & 63)];
static ReadOnlySpan<byte> _0xFFThen0x00_512
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get => MemoryMarshal.AsBytes<ulong>
    ([
        0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
        0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
        0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
        0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
    ]);
}

Godbolt link: https://godbolt.org/z/8q9fq6Ksn

Codegen:

C:X(int):byref (FullOpts):
G_M48249_IG01:  ;; offset=0x0000
       push     rax
G_M48249_IG02:  ;; offset=0x0001
       and      edi, 63
       mov      eax, edi
       neg      eax
       add      eax, 64
       cmp      eax, 128
       jae      SHORT G_M48249_IG04
       mov      rcx, 0x7FB1C7369BD0      ; static handle
       add      rax, rcx
G_M48249_IG03:  ;; offset=0x001F
       add      rsp, 8
       ret      
G_M48249_IG04:  ;; offset=0x0024
       call     CORINFO_HELP_RNGCHKFAIL
       int3     

The range check fail call is unnecessary since a & 63 is within 0->63, and then 64 - (a & 63) is then clearly within 1->64, which is clearly within the 0->127.

The issue seems to be caused by the 64 - as opposed to any other part of it.

/cc @EgorBo whomst I was discussing this on Discord with.

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