Skip to content

JIT: Rarely run exception block is not placed at the end, regression #117577

Closed
@BoyBaykiller

Description

@BoyBaykiller

In the Get function the rarely run exception throwing block is not placed after the vector access causing a jmp on every call. This is a regression to .NET 9. In GetFixed I rewrote the if statement which fixes it.
https://godbolt.org/z/M4dvTv1ah

static float Get(Vector2 vec, int index)
{
    if (index > 1 || index < 0)
    {
        ThrowOutOfRangeException("You tried to access this vector at index: {0}", index);
    }

    return *(&vec.X + index);
}

static float GetFixed(Vector2 vec, int index)
{
    if ((uint)index > 1)
    {
        ThrowOutOfRangeException("You tried to access this vector at index: {0}", index);
    }

    return *(&vec.X + index);
}
Program:Get(Program+Vector2,int):float (FullOpts):
       push     rbx
       sub      rsp, 16
       vmovsd   qword ptr [rsp], xmm0
       vmovss   dword ptr [rsp+0x08], xmm1
       mov      ebx, edi
       cmp      ebx, 1
       jbe      SHORT G_M37698_IG04
       mov      edi, 1
       mov      rsi, 0x7F996EC7D118
       call     [CORINFO_HELP_STRCNS]
       mov      rdi, rax
       mov      esi, ebx
       call     [Program:ThrowOutOfRangeException[int](System.String,int)]
       int3     
G_M37698_IG04:  ;; offset=0x0038
       lea      rax, bword ptr [rsp]
       mov      ecx, ebx
       vmovss   xmm0, dword ptr [rax+4*rcx]
       add      rsp, 16
       pop      rbx
       ret      

Program:GetFixed(Program+Vector2,int):float (FullOpts):
       push     rbx
       sub      rsp, 16
       vmovsd   qword ptr [rsp], xmm0
       vmovss   dword ptr [rsp+0x08], xmm1
       mov      ebx, edi
       cmp      ebx, 1
       ja       SHORT G_M29684_IG04
       lea      rdi, bword ptr [rsp]
       mov      esi, ebx
       vmovss   xmm0, dword ptr [rdi+4*rsi]
       add      rsp, 16
       pop      rbx
       ret      
G_M29684_IG04:  ;; offset=0x0028
       mov      edi, 91
       mov      rsi, 0x7F996EC7D118
       call     [CORINFO_HELP_STRCNS]
       mov      rdi, rax
       mov      esi, ebx
       call     [Program:ThrowOutOfRangeException[int](System.String,int)]
       int3     

Metadata

Metadata

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