Skip to content

[NativeAOT] Missed devirtualization opportunities due to Unsafe.As #85906

@MichalStrehovsky

Description

@MichalStrehovsky

In the below program we miss a devirtualization opportunity in Method due to the Unsafe.As.

IFace has a single implementor but RyuJIT doesn't even ask for it.

using System;
using System.Runtime.CompilerServices;

interface IFace
{
    void Frob();
}

class Program : IFace
{
    public static IFace s_inst = new Program();

    public object inst;

    public Program() => inst = s_inst;

    public void Frob() { }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public void Method() => Unsafe.As<IFace>(inst).Frob();

    static void Main()
    {
        new Program().Method();
    }
}

I noticed this in the pattern from here as I was looking why interface dispatch is so hot (2.7% of samples spent in RhpInterfaceDispatch1 alone) in BasicMinimalApis:

return Unsafe.As<IValueTaskSource<TResult>>(obj).GetStatus(_token) != ValueTaskSourceStatus.Pending;

Cc @EgorBo

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions