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 }}
Repository navigation
Investigate R2R issue when inlining methods with EH #113742
I have not removed the workaround in the JIT, Removing the workaround produces large code deltas. It is not clear to me whether the code delta is net positive. Back to you to take a look.
Also, there seems to be odd interaction between P/Invoke inlining (pInvokeMarshalingRequired returns false) and inlining of the IL stub. The JIT does some work on inlining the IL stub (generating the IL for the IL stub is not free), but then throws it away and takes the inlined P/Invoke path.
I get one method with a diff: System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller+ManagedToUnmanagedIn:Free()
Is this expected diff? It looks like we lost tail-call optimization in the process.
The stub IL has the call in the try of a try/finally where the finally is empty. This blocks the JIT's early tail call candidate recognition, and when the JIT cleans up the finally and subsequent flow it does not realize the call is now in tail position.
I am not sure how hard it would be to detect new tail call candidates outside of the importer. It seems like it shouldn't be too difficult, but as far as I know, that's not something we have ever considered. Might be worth a quick look to see how often it comes up.
Invoking compiler for the inlinee method Interop+Ucrtbase:free(ptr) :
IL to import:
IL_0000 02 ldarg.0
IL_0001 28 01 00 00 0a call 0xA000001
IL_0006 dd 01 00 00 00 leave 1 (IL_000c)
IL_000b dc endfinally
IL_000c 2a ret
Is it expected that PGO data will make us skip pInvokeMarshalingRequired check and fallback to analyzing the IL instead?
I think we're just missing a check. If impCheckForPInvokeCall (which calls pInvokeMarshalingRequired) marks the call as GTF_CALL_UNMANAGED the JIT should bail out of the inline candidacy checks early on, before it gets around to asking for the IL.
With a fix for the missing check we now try and inline pinvoke IL at 42 call sites in SPC. 36 of those succeed, 7 fail as unprofitable, and one fails scanning IL. In 31 cases this inlining does not lead to diffs as the code to invoke the stub is the same as the code to invoke the native method.
There are 5 where we lose tail calls if we allow inlining of pinvoke stubs with EH, all are similar to the above where the unmanaged call is in a try of a try/finally with empty finally and is not recognized as a tail call by the JIT.
;; code ends up slightly larger
System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller+ManagedToUnmanagedIn:Free()
;; code ends up slightly smaller
System.Diagnostics.Tracing.NativeRuntimeEventSource:ThreadPoolWorkerThreadStart(uint,uint,ushort)
System.Diagnostics.Tracing.NativeRuntimeEventSource:ThreadPoolWorkerThreadStop(uint,uint,ushort)
System.Diagnostics.Tracing.NativeRuntimeEventSource:ThreadPoolWorkerThreadAdjustmentSample(double,ushort)
System.Diagnostics.Tracing.NativeRuntimeEventSource:ThreadPoolWorkerThreadAdjustmentAdjustment(double,uint,uint,ushort)
With a fix for the missing check we now try and inline pinvoke IL at 42 call sites in SPC.
I assumed that the missing check is going to stop all attempts to inline pinvoke IL in SPC. Why do we try to inline IL when marshalling required returns false?
Looks like the JIT is just being internally inconsistent. If the pinvoke site is cold we traditionally have preferred to call the stub as it is smaller code. If so, we don't inquire about marshalling.
Since the call remains a managed call we also independently look at the stub IL and decide it is worth inlining and are able to inline it, so we do.
Seems like this inconsistency is a pre-existing thing; relaxing the EH constraints has just highlighted a few cases of it.
Activity
AndyAyersMS commentedon Mar 20, 2025
To repro, comment out this bit of code
runtime/src/coreclr/jit/importercalls.cpp
Lines 7778 to 7784 in dc88476
and then run the R2R version resilience tests.
[crossgen2] Prevent inlining of PInvoke marshalling accross version b…
[crossgen2] Prevent inlining of PInvoke marshalling accross version b…
dotnet-policy-service commentedon May 23, 2025
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
jkotas commentedon May 23, 2025
I have fixed the issue in crossgen2 driver.
I have not removed the workaround in the JIT, Removing the workaround produces large code deltas. It is not clear to me whether the code delta is net positive. Back to you to take a look.
Also, there seems to be odd interaction between P/Invoke inlining (
pInvokeMarshalingRequiredreturns false) and inlining of the IL stub. The JIT does some work on inlining the IL stub (generating the IL for the IL stub is not free), but then throws it away and takes the inlined P/Invoke path.[crossgen2] Prevent inlining of PInvoke marshalling accross version b…
4 remaining items
AndyAyersMS commentedon Jul 28, 2025
The stub IL has the call in the try of a try/finally where the finally is empty. This blocks the JIT's early tail call candidate recognition, and when the JIT cleans up the finally and subsequent flow it does not realize the call is now in tail position.
I am not sure how hard it would be to detect new tail call candidates outside of the importer. It seems like it shouldn't be too difficult, but as far as I know, that's not something we have ever considered. Might be worth a quick look to see how often it comes up.
AndyAyersMS commentedon Jul 28, 2025
I think we're just missing a check. If
impCheckForPInvokeCall(which callspInvokeMarshalingRequired) marks the call asGTF_CALL_UNMANAGEDthe JIT should bail out of the inline candidacy checks early on, before it gets around to asking for the IL.AndyAyersMS commentedon Jul 28, 2025
With a fix for the missing check we now try and inline pinvoke IL at 42 call sites in SPC. 36 of those succeed, 7 fail as unprofitable, and one fails scanning IL. In 31 cases this inlining does not lead to diffs as the code to invoke the stub is the same as the code to invoke the native method.
There are 5 where we lose tail calls if we allow inlining of pinvoke stubs with EH, all are similar to the above where the unmanaged call is in a try of a try/finally with empty finally and is not recognized as a tail call by the JIT.
I will look into some NAOT cases next.
jkotas commentedon Jul 28, 2025
I assumed that the missing check is going to stop all attempts to inline pinvoke IL in SPC. Why do we try to inline IL when marshalling required returns false?
JIT: enable inlining of IL stubs with EH
jkotas commentedon Jul 28, 2025
Or are there PInvokes in CoreLib where marshaling required returns true?
AndyAyersMS commentedon Jul 28, 2025
Will let you know shortly.
AndyAyersMS commentedon Jul 28, 2025
Looks like the JIT is just being internally inconsistent. If the pinvoke site is cold we traditionally have preferred to call the stub as it is smaller code. If so, we don't inquire about marshalling.
Since the call remains a managed call we also independently look at the stub IL and decide it is worth inlining and are able to inline it, so we do.
Seems like this inconsistency is a pre-existing thing; relaxing the EH constraints has just highlighted a few cases of it.
AndyAyersMS commentedon Jul 30, 2025
Will revisit this in .NET 11.
JIT: replace R2R EH-inlining workaround for PInvoke stubs
AndyAyersMS commentedon Jul 30, 2026
I looked into this some but am still not clear what the right fix is. Going to move this to .NET 12.