Skip to content

JIT: Redundant bounds check when iterating over many spans with equal length #122696

@BoyBaykiller

Description

@BoyBaykiller

This is like #114744 (which had nice diffs) but for Span. count of both Spans is equal so both bounds check could be elided, yet only the one thats used as the loop upper limit is.
https://godbolt.org/z/74aqo6Mn3

void TransformMesh(int[] first, float[] second, int start, int count)
{
    Span<int> vertexPositions = first.AsSpan(start, count);
    Span<float> vertices = second.AsSpan(start, count);

    for (int i = 0; i < vertices.Length; i++)
    {
        //vertices[i] = default;
        vertexPositions[i] = default; // redundant bounds check
    }
}
C:TransformMesh(int[],float[],int,int):this (FullOpts):
       push     rbp
       mov      rbp, rsp
       test     rsi, rsi
       je       G_M29678_IG13
       mov      eax, ecx
       mov      edi, r8d
       add      rax, rdi
       mov      edi, dword ptr [rsi+0x08]
       cmp      rax, rdi
       ja       SHORT G_M29678_IG12
       mov      eax, ecx
       lea      rax, bword ptr [rsi+4*rax+0x10]
       mov      edi, r8d
G_M29678_IG04:  ;; offset=0x0027
       test     rdx, rdx
       je       SHORT G_M29678_IG06
       mov      ecx, ecx
       mov      esi, r8d
       add      rcx, rsi
       mov      edx, dword ptr [rdx+0x08]
       cmp      rcx, rdx
       ja       SHORT G_M29678_IG12
       jmp      SHORT G_M29678_IG07
       align    [12 bytes for IG09]
G_M29678_IG06:  ;; offset=0x004A
       or       ecx, r8d
       jne      SHORT G_M29678_IG12
       xor      r8d, r8d
G_M29678_IG07:  ;; offset=0x0052
       xor      ecx, ecx
       test     r8d, r8d
       je       SHORT G_M29678_IG10
       cmp      r8d, edi
       jg       SHORT G_M29678_IG11
       xor      ecx, ecx
G_M29678_IG09:  ;; offset=0x0060
       xor      edi, edi
       mov      dword ptr [rax+rcx], edi
       add      rcx, 4
       dec      r8d
       jne      SHORT G_M29678_IG09
G_M29678_IG10:  ;; offset=0x006E
       pop      rbp
       ret      
G_M29678_IG11:  ;; offset=0x0070
       cmp      ecx, edi
       jae      SHORT G_M29678_IG14
       mov      edx, ecx
       xor      esi, esi
       mov      dword ptr [rax+4*rdx], esi
       inc      ecx
       cmp      ecx, r8d
       jl       SHORT G_M29678_IG11
       jmp      SHORT G_M29678_IG10
G_M29678_IG12:  ;; offset=0x0084
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
G_M29678_IG13:  ;; offset=0x008B
       mov      eax, ecx
       or       eax, r8d
       jne      SHORT G_M29678_IG12
       xor      rax, rax
       xor      edi, edi
       jmp      SHORT G_M29678_IG04
G_M29678_IG14:  ;; offset=0x0098
       call     CORINFO_HELP_RNGCHKFAIL
       int3     

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions