Skip to content

[JIT] Unnecessary stack spills when copying large struct from array/ref to array. #121890

@omariom

Description

@omariom

.NET 10

static void Copy(WideStruct[] target, WideStruct[] source)
{
    target[0] = source[1];
}

static void Copy(WideStruct[] target, ref WideStruct source)
{
    target[0] = source;
}

[StructLayout(LayoutKind.Sequential, Size = 64)]
struct WideStruct
{
}

Disasm:

; Assembly listing for method TestJITConsoleApp.Program:Copy(TestJITConsoleApp.WideStruct[],TestJITConsoleApp.WideStruct[]) (FullOpts)
; Emitting BLENDED_CODE for generic X64 + VEX on Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data

G_M000_IG01:                ;; offset=0x0000
       sub      rsp, 104
 
G_M000_IG02:                ;; offset=0x0004
       cmp      dword ptr [rdx+0x08], 1
       jbe      SHORT G_M000_IG04
       vmovdqu  ymm0, ymmword ptr [rdx+0x50]
       vmovdqu  ymmword ptr [rsp+0x28], ymm0  ; <---- spill
       vmovdqu  ymm0, ymmword ptr [rdx+0x70]
       vmovdqu  ymmword ptr [rsp+0x48], ymm0
       cmp      dword ptr [rcx+0x08], 0
       jbe      SHORT G_M000_IG04
       vmovdqu  ymm0, ymmword ptr [rsp+0x28]
       vmovdqu  ymmword ptr [rcx+0x10], ymm0
       vmovdqu  ymm0, ymmword ptr [rsp+0x48]
       vmovdqu  ymmword ptr [rcx+0x30], ymm0
 
G_M000_IG03:                ;; offset=0x003C
       vzeroupper 
       add      rsp, 104
       ret      
 
G_M000_IG04:                ;; offset=0x0044
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
 
; Total bytes of code 74


; Assembly listing for method TestJITConsoleApp.Program:Copy(TestJITConsoleApp.WideStruct[],byref) (FullOpts)
; Emitting BLENDED_CODE for generic X64 + VEX on Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data

G_M000_IG01:                ;; offset=0x0000
       sub      rsp, 104
 
G_M000_IG02:                ;; offset=0x0004
       vmovdqu  ymm0, ymmword ptr [rdx]
       vmovdqu  ymmword ptr [rsp+0x28], ymm0
       vmovdqu  ymm0, ymmword ptr [rdx+0x20]
       vmovdqu  ymmword ptr [rsp+0x48], ymm0
       cmp      dword ptr [rcx+0x08], 0
       jbe      SHORT G_M000_IG04
       vmovdqu  ymm0, ymmword ptr [rsp+0x28]
       vmovdqu  ymmword ptr [rcx+0x10], ymm0
       vmovdqu  ymm0, ymmword ptr [rsp+0x48]
       vmovdqu  ymmword ptr [rcx+0x30], ymm0
 
G_M000_IG03:                ;; offset=0x0035
       vzeroupper 
       add      rsp, 104
       ret      
 
G_M000_IG04:                ;; offset=0x003D
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
 
; Total bytes of code 67

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