Skip to content

JIT: Bounds check not hoisted or eliminated for simple InlineArray iteration #119693

@BoyBaykiller

Description

@BoyBaykiller

https://godbolt.org/z/Mqa1rEs38

[InlineArray(3)]
struct Data
{
    int[] _element;
}

private static void Test1(Data data, int axis)
{
    for (int i = 0; i < data[axis].Length; i++)
    {
        _ = data[axis][i];
    }
}

Both bounds check (for axis against 3, and i against .Length) exist.

Main codegen
G_M7138_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25
G_M7138_IG02:  ;; offset=0x0004
       xor      eax, eax
       mov      r8d, edx
       jmp      SHORT G_M7138_IG04
       align    [5 bytes for IG03]
						;; size=12 bbWeight=1 PerfScore 2.50
G_M7138_IG03:  ;; offset=0x0010
       cmp      eax, dword ptr [r10+0x08]
       jae      SHORT G_M7138_IG06
       inc      eax
						;; size=8 bbWeight=4 PerfScore 17.00
G_M7138_IG04:  ;; offset=0x0018
       cmp      edx, 3
       jae      SHORT G_M7138_IG06
       mov      r10, gword ptr [rcx+8*r8]
       cmp      dword ptr [r10+0x08], eax
       jg       SHORT G_M7138_IG03
						;; size=15 bbWeight=8 PerfScore 58.00
G_M7138_IG05:  ;; offset=0x0027
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M7138_IG06:  ;; offset=0x002C
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
						;; size=6 bbWeight=0 PerfScore 0.00

Now with JitStressModeNames=STRESS_NO_OLD_PROMOTION + JitSynthesizeCounts=1 the axis bounds check gets hoisted, but the other one persists. And the load isnt hoisted.

Main + STRESS_NO_OLD_PROMOTION + JitSynthesizeCounts codegen
G_M7138_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25
G_M7138_IG02:  ;; offset=0x0004
       xor      eax, eax
       cmp      edx, 3
       jae      SHORT G_M7138_IG05
       mov      edx, edx
       mov      rcx, gword ptr [rcx+8*rdx]
       cmp      dword ptr [rcx+0x08], 0
       jle      SHORT G_M7138_IG04
       align    [9 bytes for IG03]
						;; size=28 bbWeight=1 PerfScore 8.00
G_M7138_IG03:  ;; offset=0x0020
       mov      rdx, rcx
       cmp      eax, dword ptr [rdx+0x08]
       jae      SHORT G_M7138_IG05
       inc      eax
       cmp      dword ptr [rcx+0x08], eax
       jg       SHORT G_M7138_IG03
						;; size=15 bbWeight=9.00 PerfScore 76.50
G_M7138_IG04:  ;; offset=0x002F
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M7138_IG05:  ;; offset=0x0034
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
						;; size=6 bbWeight=0 PerfScore 0.00

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions