Skip to content

NativeAOT: devirtualize isinst for monomorphic cases #80828

@EgorBo

Description

@EgorBo
public interface IMyInterface {}

public class MyProgram : IMyInterface
{
    static void Main() => IsMyInterface(new MyProgram ());

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsMyInterface(object o) => o is IMyInterface;
}

Since IMyInterface has only one implementation (monomorphic cases aren't rare) we can optimize isinst to basically o is MyProgram and inline it.

Current NativeAOT codegen:

; Method Program:IsMyInterface(System.Object):bool
       4883EC28             sub      rsp, 40
       488BD1               mov      rdx, rcx
       488D0D00000000       lea      rcx, [(reloc 0x40000000004234f8)]
       E800000000           call     CORINFO_HELP_ISINSTANCEOFINTERFACE
       4885C0               test     rax, rax
       0F95C0               setne    al
       0FB6C0               movzx    rax, al      
       4883C428             add      rsp, 40
       C3                   ret      
; Total bytes of code: 33

Expected NativeAOT codegen:

; Method Program:IsMyInterface(System.Object):bool
       4885C9               test     rcx, rcx
       740E                 je       SHORT G_M18292_IG05
       488D0500000000       lea      rax, [(reloc 0x40000000004221d8)]
       483901               cmp      qword ptr [rcx], rax
       7402                 je       SHORT G_M18292_IG05
       33C9                 xor      rcx, rcx
G_M18292_IG05:
       33C0                 xor      eax, eax
       4885C9               test     rcx, rcx
       0F95C0               setne    al
       C3                   ret      
; Total bytes of code: 28

cc @MichalStrehovsky

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions