Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Support for IL interpreter in CoreRT #5011

Open

Description

@tonerdo

We should consider porting Mono's .NET interpreter (http://www.mono-project.com/news/2017/11/13/mono-interpreter/) over to CoreRT, to support runtime IL generation and execution within native code

Activity

  1. jkotas commented on Nov 23, 2017

    @jkotas
    Member

    The interpreter implementation is always tighly coupled with the underlying runtime implementation details. It cannot be easily ported from runtime to runtime. This would be more like a rewrite, maybe using some of the ideas from the other implementations. We would want to rewrite it in C# for CoreRT. Implementing it in C/C++ for CoreRT would be much harder than implementing it in C#.

    BTW: We have interpreter in CoreCLR as well: https://github.com/dotnet/coreclr/blob/master/src/vm/interpreter.cpp . It is not shipping in .NET Core because of .NET Core would not benefit from it. It has been used for new platform bringups, and various experiments.

    There are two distinct pieces:

    • IL interpreter: The interpreter should not be tightly coupled with Reflection.Emit. It should be able to interpret any IL, e.g. even IL comming from regular IL .dlls. The idea is that we would have pluggable execution strategy for the IL. Some of the early plumbing for this has been done in our System.Private.Jit prototype - look for MethodExecutionStrategy. The interpreter can live in say System.Private.Interpreter and be pluged in as execution strategy. A good first step would be to put all scafolding in place that is able to interpret a super simple method that just returns "42" end-to-end. Let's use this issue to track further dicusssion on this.

    • Refleflection.Emit: Reflection.Emit should be independent part as described in: https://github.com/dotnet/corefx/issues/4491#issuecomment-189756092 . Let's use this corefx issue for further discussion on Reflection.Emit. The "Run" flavor of Reflection.Emit should communicate with the runtime by passing PE files down. In case of DynamicMethods, it can pass down just baked IL method bodies as performance optimization since producing a PE file per dynamic method would be too expensive.

  2. changed the title [-]Support for Reflection.Emit in CoreRT[/-] [+]Support for IL interpreter in CoreRT[/+] on Nov 23, 2017
  3. tonerdo commented on Apr 22, 2018

    @tonerdo
    ContributorAuthor

    @jkotas looking to take a first pass at this. However, I have a couple of questions:

    • The OnEntryPoint method returns an IntPtr which I'm guessing points to the native compiled code. How will this work in the context of the interpreter.
    • Considering CoreRT ships without a JIT, at what point is the RyuJitExecutionStrategy used? Where in the code can I find it?
  4. jkotas commented on Apr 22, 2018

    @jkotas
    Member

    The OnEntryPoint method returns an IntPtr

    CoreRT has capability to manufacture entrypoints. Look for CallConverterThunk.MakeThunk for an example. The call converter is sort of a mini-interpreter that just changes once calling convention to a different one.

    RyuJitExecutionStrategy

    Code under src\System.Private.Jit was a prototype for JIT support in CoreRT. It was able to run a few methods, nothing extensive. It does not ship, and it is not used in the default CoreRT config and it has likely bit-rotten and does not work anymore.

  5. tonerdo commented on Apr 22, 2018

    @tonerdo
    ContributorAuthor

    Thanks for the quick response. I have more stuff I'm curious about

    What happens when MethodInfo.Invoke(object, args) is called? I'm guessing there's a transition from managed code to unmanaged code at some point but I can't seem to find it (I've looked through coreclr, corefx and corert)

    I ask because I'm trying my best to avoid using reflection and instead leverage the underlying runtime capabilities. Specifically how info like the value of static and instance fields are retrieved from a handle to a managed object (*Object?) that already exists in memory

  6. mattwarren commented on Apr 23, 2018

    @mattwarren
    Contributor

    What happens when MethodInfo.Invoke(object, args) is called? I'm guessing there's a transition from managed code to unmanaged code at some point but I can't seem to find it (I've looked through coreclr, corefx and corert)

    I've written about this scenario, see the section on 'How does Reflection work?' (referring to the CoreCLR)

  7. tonerdo commented on Apr 23, 2018

    @tonerdo
    ContributorAuthor

    Sweet! thanks Matt! Will take a look.

  8. tonerdo commented on Apr 23, 2018

    @tonerdo
    ContributorAuthor

    Yup, that definitely answers my question, knew there had to be an FCALL somewhere

  9. mattwarren commented on Apr 23, 2018

    @mattwarren
    Contributor

    I've looked into the equivalent flow for CoreRT (mostly just for my own interest) and unless I'm missing something it's all in C#, which is pretty cool (I know that's one of the goals of CoreRT, it's just interesting to see it in action)!

    At this point there are no C# method bodies, but after a bit of digging, it seems that the [McgIntrinsics] attribute is a placeholder and the method bodies are wired up via ILProvider - CreateMethodIL(MethodDesc method) and finally CalliIntrinsic - EmitIL(MethodDesc target) (using OpCodes.Calli)

  10. tonerdo commented on Apr 23, 2018

    @tonerdo
    ContributorAuthor

    You should write a blog post on this 😃

  11. tonerdo commented on Apr 23, 2018

    @tonerdo
    ContributorAuthor

    Here's also for my second question on getting field info from an object reference:

  12. morganbr commented on Apr 23, 2018

    @morganbr
    Contributor

    It looks like you're on the right track with the implementation details in System.Private.Reflection.Execution (no FCALLs in CoreRT). @MichalStrehovsky or @davidwrighton, do you have any details to fill in?

  13. mattwarren commented on Apr 25, 2018

    @mattwarren
    Contributor

    Just out of interest, from reading this post there seems to be 2 possible approaches to allowing runtime code generation in CoreRT:

    1. Implement an IL Interpreter
    2. Use RyuJIT (i.e. System.Private.Jit)

    I was wondering what's the preferred approach? Or in an ideal world would you have both? (leaving aside the cost to implement, engineering time, other priorities, etc)

    Is 2) less preferable because it's no longer 'AOT compiled' or is that not really a concern? I assume that 1) is slower, but does that matter?

  14. jkotas commented on Apr 25, 2018

    @jkotas
    Member

    Interpreter is good to have for places where (Ryu)JIT is not available, not allowed (e.g. Apple devices), or not desirable (e.g. environments locked down for security).

    It may be also used as part of tiered code generation strategy.

  15. mattwarren commented on Apr 25, 2018

    @mattwarren
    Contributor

    @jkotas Thanks for the info

    @tonerdo BTW you may find these 2 blog posts help you out and/or give you something to think about when working on this issue:

  16. 166 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions