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.

Generate PInvoke transitions in the JIT #610

Closed

Description

@jkotas

The JIT needs to generate the following code around PInvoke calls, the existing logic to check for PInvoke calls is in Compiler::impCheckForPInvokeCall should work:

    PInvokeTransitionFrame frame; // opaque local - the size is returned via JIT-EE interface

…
    lea arg0, &frame
    call JIT_PINVOKE_BEGIN // new JIT helper – needs to be added to corinfo.h
    // no managed references can be in registers between BEGIN/END callouts
    <argument setup>
    call target
    <store return value>
    lea arg0, &frame
    call JIT_PINVOKE_END // new JIT helper
…

There is some of this in the RyuJIT already – the difference between the above and what’s there today are:

  • JIT_PINVOKE_BEGIN callback does not exist, instead the JIT has knowledge of the CoreCLR-specific EE datastructures and conventions involved and inlines the required checks into the code
  • JIT_PINVOKE_END callback sort of exists (it is called CORINFO_HELP_STOP_FOR_GC), but the hot part of it is again inlined into the code

The sequence proposed above avoids the tight coupling between codegen and runtime at the cost of two calls. If this overhead will prove to be a problem, we will worry about optimizing it later.

Activity

  1. modified the milestones: , on Jan 8, 2016
  2. assigned and unassigned on Jan 11, 2016
  3. pgavlin commented on Jan 11, 2016

    @pgavlin

    @jkotas

    // no managed references can be in registers between BEGIN/END callouts
    

    Would it be more precise to say "no managed references may be live only in registers?" I assume that this is due to the GC transition that JIT_PINVOKE_BEGIN will perform.

    <argument setup>
    call target
    <store return value>
    

    Similarly, I assume that argument setup and the return value store don't need to be tightly positioned around the call to the target so long as they respect the condition above.

  4. jkotas commented on Jan 11, 2016

    @jkotas
    MemberAuthor

    Would it be more precise to say "no managed references may be live only in registers?"

    Yes.

    I assume that argument setup and the return value store don't need to be tightly positioned around the call to the target so long as they respect the condition above.

    This depends on the calling convention of these JIT helpers. If they are going to have regular calling convention, then the argument setup (I mean putting the right values into the right argument registers) has to be after the call to the helper. We can explore a custom calling convention if it would be too hard to use the regular calling convention for these helpers in the JIT, but I would prefer if they can use the regular calling convention.

  5. pgavlin commented on Jan 11, 2016

    @pgavlin

    This depends on the calling convention of these JIT helpers. If they are going to have regular calling convention, then the argument setup has to be after the call to the helper.

    Ah, right.

    We can explore a custom calling convention if it would be too hard to use the regular calling convention for these in the JIT.

    Sure. I'm thinking of LLVM in particular (via LLIILC), which currently performs GC transitions immediately before and after the instruction that calls unmanaged code in order to ensure that the preemptive mode invariants are respected.

  6. assigned and unassigned on May 3, 2016
  7. jkotas commented on Jun 24, 2016

    @jkotas
    MemberAuthor

    This is done.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions