dotnet / dotnet/runtime

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

Open
#121,890 4 comments 1 reaction 0 assignees View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

.NET 10

```C#
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:
```ASM

; 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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.