You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
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.
// 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.
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.
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.
Activity
pgavlin commentedon Jan 11, 2016
@jkotas
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_BEGINwill perform.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.
jkotas commentedon Jan 11, 2016
Yes.
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.
pgavlin commentedon Jan 11, 2016
Ah, right.
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.
jkotas commentedon May 3, 2016
Work in progress: https://github.com/pgavlin/corert/tree/PInvokeHelpers
jkotas commentedon Jun 24, 2016
This is done.