Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use write barriers for byref-like struct copies #102316

Merged
merged 4 commits into from
May 17, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented May 16, 2024

Byref-like structs are stack only so we can omit write barriers for them. It looks like JIT already knew that, but only if such structs had ref fields, which they might not have.

void Test(ref MyStruct dst, MyStruct src) => dst = src;

ref struct MyStruct
{
    object a;
    long b;
}

Current codegen for Test:

; Method ConsoleApp35.Program:Test(byref,ConsoleApp35.MyStruct):this (FullOpts)
       push     rdi
       push     rsi
       mov      rdi, rdx
       mov      rsi, r8
       call     CORINFO_HELP_ASSIGN_BYREF
       movsq    
       pop      rsi
       pop      rdi
       ret      
; Total bytes of code: 18

New codegen for Test:

; Method ConsoleApp35.Program:Test(byref,ConsoleApp35.MyStruct):this (FullOpts)
       vmovdqu  xmm0, xmmword ptr [r8]         ;; NOTE: nogc region so SIMD is fine
       vmovdqu  xmmword ptr [rdx], xmm0
       ret      
; Total bytes of code: 10

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 16, 2024
@EgorBo
Copy link
Member Author

EgorBo commented May 16, 2024

@MihuBot

@EgorBo EgorBo marked this pull request as ready for review May 16, 2024 21:03
@EgorBo
Copy link
Member Author

EgorBo commented May 16, 2024

PTAL @jakobbotsch @kunalspathak @dotnet/jit-contrib diffs are relatively small since there are not so many byref structs without ref fields.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

{
unsigned slots = GetSlotCount();
for (unsigned i = 0; i < slots; i++)
// Byref-like structs are stack only
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a documentation that I can read more about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, e.g. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct Basically, Span itself is the most popular example

@EgorBo EgorBo merged commit 0b10f4b into dotnet:main May 17, 2024
107 checks passed
@EgorBo EgorBo deleted the no-wb-byreflike branch May 17, 2024 08:45
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2024
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