Skip to content

[wasm][coreclr] Avoid using calli thunks for delegates #121222

Closed

Description

@radekdoulik

Try to not use calli thunks for code like this:

class Program
{
    static void Main(string[] args)
    {
        CountDelegate countDelegate = new CountDelegate(getCount);
        Console.WriteLine($"count: {countDelegate()}");
        Console.WriteLine($"count: {countDelegate()}");
    }

    static double getCount()
    {
        return counter++;
    }

    static double counter = 0;

    delegate double CountDelegate();
}

We hit an assert, but then the output follows and is as expected, so I think we don't need to use the thunk here and possibly just try to get it during interpreter compilation?

ASSERT FAILED
	Expression: false && "GetCookieForCalliSig: unknown thunk signature"
	Location:   line 632 in /Users/rodo/git/runtime-main/src/coreclr/vm/wasm/helpers.cpp
	Function:   GetCookieForCalliSig
	Process:    42
Frame (InlinedCallFrame): 0x4fdf7c
    Skipping 0x4fdf7c
Frame (InterpreterFrame): 0x4feca0
   0) dynamicClass::IL_STUB_PInvoke, IR_0038
   1) System.Delegate::DelegateConstruct, IR_0043
   2) Program::Main, IR_000e
count: 0
count: 1

Activity

  1. added this to the Future milestone on Oct 30, 2025
  2. dotnet-policy-service commented on Oct 30, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  3. jkotas commented on Oct 31, 2025

    @jkotas
    Member

    We hit an assert, but then the output follows and is as expected

    That because we take this path during the interpreter execution:

    #ifdef FEATURE_PORTABLE_ENTRYPOINTS
    // WASM-TODO: We may end up here with native JIT helper entrypoint without MethodDesc
    // that CALL_INTERP_METHOD is not able to handle. This is a potential problem for
    // interpreter<->native code stub generator.
    // https://github.com/dotnet/runtime/pull/119516#discussion_r2337631271
    if (!PortableEntryPoint::HasNativeEntryPoint(calliFunctionPointer))
    {
    targetMethod = PortableEntryPoint::GetMethodDesc(calliFunctionPointer);
    goto CALL_INTERP_METHOD;
    }
    #endif // FEATURE_PORTABLE_ENTRYPOINTS

    possibly just try to get it during interpreter compilation

    I think it should be the other way around: We should get the thunk during interpreter execution only once it is actually needed to invoke native code here:

    InvokeCalliStub(calliFunctionPointer, cookie, callArgsAddress, returnValueAddress);
    . The thunk should be cached in the portable entrypoint so that we do not need to fetch it every time.

  4. dotnet-policy-service commented on Oct 31, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
    See info in area-owners.md if you want to be subscribed.

  5. radekdoulik commented on Nov 3, 2025

    @radekdoulik
    MemberAuthor

    possibly just try to get it during interpreter compilation

    I think it should be the other way around: We should get the thunk during interpreter execution only once it is actually needed to invoke native code here:

    runtime/src/coreclr/vm/interpexec.cpp

    Line 2482 in 3683bd5
    InvokeCalliStub(calliFunctionPointer, cookie, callArgsAddress, returnValueAddress);
    . The thunk should be cached in the portable entrypoint so that we do not need to fetch it every time.

    I forgot to finish my comment, what I mean is that we try to get it only in case we need it. Doing it lazily during execution phase can be even better, I will take a look at details.

    Currently we hit the assert during compilation.

  6. radekdoulik commented on Apr 1, 2026

    @radekdoulik
    MemberAuthor

    Implemented in #125455

  7. locked and limited conversation to collaborators on May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions