Skip to content

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 20, 2024

This PR enables more memset-like unrollings for these APIs. Also, it fixes some potential regressions from #98623 which removes Unsafe.InitBlockUnaligned from SpanHelpers.ClearWithoutReferences and Span.Fill.

Example of what it does compared to Main/.NET 8.0:

static void Test(Span<int> span)
{
    span.Slice(0, 10).Fill(0);
}

Current codegen:

; Assembly listing for method Prog:Test(System.Span`1[int]) (FullOpts)
G_M6718_IG01:
       sub      rsp, 40
G_M6718_IG02:
       cmp      dword ptr [rcx+0x08], 10
       jb       SHORT G_M6718_IG04
       mov      rcx, bword ptr [rcx]
       mov      edx, 10
       xor      r8d, r8d
       call     [System.SpanHelpers:Fill[int](byref,ulong,int)]
       nop      
G_M6718_IG03:
       add      rsp, 40
       ret      
G_M6718_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
; Total bytes of code 40

New codegen

; Assembly listing for method Prog:Test(System.Span`1[int]) (FullOpts)
G_M6718_IG01:
       sub      rsp, 40
G_M6718_IG02:
       cmp      dword ptr [rcx+0x08], 10
       jb       SHORT G_M6718_IG04
       mov      rax, bword ptr [rcx]
       vxorps   ymm0, ymm0, ymm0
       vmovdqu  ymmword ptr [rax], ymm0
       vmovdqu  xmmword ptr [rax+0x18], xmm0
G_M6718_IG03:
       add      rsp, 40
       ret      
G_M6718_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
; Total bytes of code 38

@ghost ghost assigned EgorBo Feb 20, 2024
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 20, 2024
@ghost
Copy link

ghost commented Feb 20, 2024

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

This PR enables more memset-like unrollings for these APIs. Also, it fixes some potential regressions from #98623 which removes Unsafe.InitBlockUnaligned from SpanHelpers.ClearWithoutReferences and Span.Fill.

Example of what it does compared to Main/.NET 7.0:

static void Test(Span<int> span)
{
    span.Slice(0, 10).Fill(0);
}

Current codegen:

; Assembly listing for method Prog:Test(System.Span`1[int]) (FullOpts)
G_M6718_IG01:
       sub      rsp, 40
G_M6718_IG02:
       cmp      dword ptr [rcx+0x08], 10
       jb       SHORT G_M6718_IG04
       mov      rcx, bword ptr [rcx]
       mov      edx, 10
       xor      r8d, r8d
       call     [System.SpanHelpers:Fill[int](byref,ulong,int)]
       nop      
G_M6718_IG03:
       add      rsp, 40
       ret      
G_M6718_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
; Total bytes of code 40

New codegen

; Assembly listing for method Prog:Test(System.Span`1[int]) (FullOpts)
G_M6718_IG01:
       sub      rsp, 40
G_M6718_IG02:
       cmp      dword ptr [rcx+0x08], 10
       jb       SHORT G_M6718_IG04
       mov      rax, bword ptr [rcx]
       vxorps   ymm0, ymm0, ymm0
       vmovdqu  ymmword ptr [rax], ymm0
       vmovdqu  xmmword ptr [rax+0x18], xmm0
G_M6718_IG03:
       add      rsp, 40
       ret      
G_M6718_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
; Total bytes of code 38
Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Feb 20, 2024

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Feb 22, 2024

@jakobbotsch @dotnet/jit-contrib PTAL. This is needed to unblock #98623 which removes unrollable Unsafe.InitBlock from two places.

This impl is similar to LowerMemmove that @jakobbotsch reviewed in the past

@EgorBo EgorBo requested a review from jakobbotsch February 22, 2024 10:20
@EgorBo
Copy link
Member Author

EgorBo commented Feb 22, 2024

@jakobbotsch I've addressed your feedback (thanks, it made it simpler!) PTAL

@EgorBo EgorBo merged commit 973ceee into dotnet:main Feb 23, 2024
@EgorBo EgorBo deleted the lower-memset branch February 23, 2024 15:11
@EgorBo
Copy link
Member Author

EgorBo commented Feb 23, 2024

Grr.. clicked "Update branch" instead of "Merge" 😠

@DrewScoggins
Copy link
Member

Possible improvements (Only the clear related benchmarks):
dotnet/perf-autofiling-issues#29847
dotnet/perf-autofiling-issues#29821
dotnet/perf-autofiling-issues#29805

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants