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

Redundant write barriers around byref-like structs #111575

Closed
EgorBo opened this issue Jan 18, 2025 · 1 comment · Fixed by #111576
Closed

Redundant write barriers around byref-like structs #111575

EgorBo opened this issue Jan 18, 2025 · 1 comment · Fixed by #111576
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Jan 18, 2025

Reported in #111127 (comment) by @NinoFloris

public class Program
{
    object _obj = new();

    Test M0() => new Test() { Obj = _obj, Obj1 = _obj, Obj2 = _obj };

    void M1(ref Test ret) => ret = new Test() { Obj = _obj, Obj1 = _obj, Obj2 = _obj };

    void M2(out Test ret) => ret = new Test() { Obj = _obj, Obj1 = _obj, Obj2 = _obj };

    public ref struct Test
    {
        public object Obj;
        public object Obj1;
        public object Obj2;
    }
}

Currently all three M0, M1, M2 emit redundant write barriers, although, they're not needed due to "ByRefLike" modifier on Test struct (meaning it can live only on stack):

; Assembly listing for method Program:M0():Program+Test:this (FullOpts)
G_M000_IG01:
       push     rsi
       push     rbx
       mov      rbx, rdx
G_M000_IG02:
       mov      rsi, gword ptr [rcx+0x08]
       mov      rdx, rsi
       mov      rcx, rbx
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x08]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x10]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       mov      rax, rbx
G_M000_IG03:
       pop      rbx
       pop      rsi
       ret      
; Total bytes of code 50


; Assembly listing for method Program:M1(byref):this (FullOpts)
G_M000_IG01:
       push     rsi
       push     rbx
       mov      rbx, rdx
G_M000_IG02:
       mov      rsi, gword ptr [rcx+0x08]
       mov      rdx, rsi
       mov      rcx, rbx
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x08]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x10]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       nop      
G_M000_IG03:
       pop      rbx
       pop      rsi
       ret      
; Total bytes of code 48


; Assembly listing for method Program:M2(byref):this (FullOpts)
G_M000_IG01:
       push     rsi
       push     rbx
       mov      rbx, rdx
G_M000_IG02:
       mov      rsi, gword ptr [rcx+0x08]
       mov      rdx, rsi
       mov      rcx, rbx
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x08]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       lea      rcx, bword ptr [rbx+0x10]
       mov      rdx, rsi
       call     CORINFO_HELP_CHECKED_ASSIGN_REF
       nop      
G_M000_IG03:
       pop      rbx
       pop      rsi
       ret      
; Total bytes of code 48

Expected:

no CORINFO_HELP_CHECKED_ASSIGN_REF calls in the codegen.

@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 Jan 18, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 18, 2025
@EgorBo EgorBo self-assigned this Jan 18, 2025
@EgorBo EgorBo added this to the 10.0.0 milestone Jan 18, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 18, 2025
Copy link
Contributor

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

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant