KEMBAR78
NativeAOT: improve getExactClasses to support classes as base types · Issue #88547 · dotnet/runtime · GitHub
Skip to content

NativeAOT: improve getExactClasses to support classes as base types #88547

@EgorBo

Description

@EgorBo

Currently, getExactClasses gives up if the base type is not an interface so if we have something like this it won't be optimized:

using System.Runtime.CompilerServices;

public class ClassA
{
    public virtual int GetValue() => 42;
}

public class ClassB : ClassA
{
    // we don't even need to override GetValue here
}

class MyClass
{
    static void Main()
    {
        Test(new ClassB());
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Test(ClassA c) => c.GetValue();
}

Current codegen of Test on NativeAOT:

; Method MyClass:Test(ClassA):int (FullOpts)
       sub      rsp, 40
       mov      rax, qword ptr [rcx]
       call     [rax+30H]ClassA:GetValue():int:this
       nop      
       add      rsp, 40
       ret      
; Total bytes of code: 16

Expected codegen:

; Method MyClass:Test(ClassA):int (FullOpts)
       cmp      byte  ptr [rcx], cl ; nullcheck
       mov      eax, 42
       ret      
; Total bytes of code: 8

Presumably, this needs no changes on the jit side, only that getExactClasses should be improved on the NAOT side.

cc @dotnet/ilc-contrib

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions