Skip to content

Unboxing+instantiating thunks #121781

@MichalStrehovsky

Description

@MichalStrehovsky

In native AOT we build instantiating+unboxing thunks that bridge the calling convention gap when calling instance methods on shared generic valuetypes virtually:

interface IFoo { void Method(); }

struct Gen<T> : IFoo
{
    // Native code for Gen<__Canon>.Method expects _reference_ to the first field of the struct as the first parameter,
    // and a "generic context" (that tells the __Canon method body what the T is) as the second parameter
    public void Method() => Console.WriteLine(typeof(T));
}

IFoo o = new Gen<string>();

// The callsite was set up as usual for interface method invocation so the first and only parameter
// is the `this` (`o` in this case)
o.Method();

We emit these as IL and use some tricks to make it compile (dotnet/corert#2566).

For runtime async, it looks like we're out of luck with this approach because we'd need to generate a method that is runtime async (doable) that calls another runtime async method while injecting the extra instantiation parameter and passing down async resume context too.

We'll probably need a RyuJIT feature.

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