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

JIT: Devirtualization: "this" should always have exact/final class. #50420

Closed
EgorBo opened this issue Mar 30, 2021 · 2 comments
Closed

JIT: Devirtualization: "this" should always have exact/final class. #50420

EgorBo opened this issue Mar 30, 2021 · 2 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue untriaged New issue has not been triaged by the area owner
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Mar 30, 2021

Contributes to #7541

using System;

public class C : IDisposable
{
    public void Dispose() {}

    public void CallDispose(IDisposable d) => d.Dispose();
    
    public void Test() => CallDispose(this);
}

Current codegen for Test:

C.Test()
    L0000: mov r11, 0x7ffc31027028
    L000a: mov rax, [0x7ffc31027028]
    L0011: jmp rax

Expected codegen for Test:

C.Test()
    L0000: ret

From jitdump:

impDevirtualizeCall: Trying to devirtualize interface call:
    class for 'this' is Program (attrib 20000000)
    base method is System.IDisposable::Dispose
    base class is interface
    devirt to Program::Dispose -- final method
               [000003] --C-G-------              *  CALLV stub void   System.IDisposable.Dispose
               [000000] ------------ this in rcx  \--*  LCL_VAR   ref    V00 arg0         
    Class not final or exact

In this case it should not matter if C is sealed or not - this should mean the class is exact and Dispose method should be devirtualized (inlined as a no-op).

@EgorBo EgorBo added the tenet-performance Performance related issue label Mar 30, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Mar 30, 2021
@EgorBo EgorBo added this to the Future milestone Mar 30, 2021
@EgorBo EgorBo added this to Needs Triage in .NET Core CodeGen via automation Mar 30, 2021
@EgorBo EgorBo moved this from Needs Triage to Backlog (General) in .NET Core CodeGen Mar 30, 2021
@EgorBo EgorBo moved this from Backlog (General) to Optimizations in .NET Core CodeGen Mar 30, 2021
@jkotas
Copy link
Member

jkotas commented Mar 30, 2021

What about:

public class C : IDisposable
{
    public void Dispose() {}

    public void CallDispose(IDisposable d) => d.Dispose();
    
    public void Test() => CallDispose(this);
}

public class D : C, IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("I am here");
    }
}

new D().Test(); has to print "I am here" in this case.

@EgorBo
Copy link
Member Author

EgorBo commented Mar 30, 2021

Oops, indeed 😪

@EgorBo EgorBo closed this as completed Mar 30, 2021
.NET Core CodeGen automation moved this from Optimizations to Done Mar 30, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Apr 29, 2021
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 tenet-performance Performance related issue untriaged New issue has not been triaged by the area owner
Projects
Archived in project
Development

No branches or pull requests

2 participants