Skip to content

Investigate R2R issue when inlining methods with EH #113742

Open

Description

@AndyAyersMS

See #112998 (comment) for context.

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Mar 20, 2025
  2. AndyAyersMS commented on Mar 20, 2025

    @AndyAyersMS
    MemberAuthor

    To repro, comment out this bit of code

    // Do not inline pinvoke stubs with EH.
    //
    if ((methAttr & CORINFO_FLG_PINVOKE) != 0)
    {
    inlineResult->NoteFatal(InlineObservation::CALLEE_HAS_EH);
    return;
    }

    and then run the R2R version resilience tests.

  3. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Mar 20, 2025
  4. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    and removed on May 23, 2025
  5. dotnet-policy-service commented on May 23, 2025

    @dotnet-policy-service
    Contributor

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

  6. jkotas commented on May 23, 2025

    @jkotas
    Member

    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 (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.

  7. removed
    untriagedNew issue has not been triaged by the area owner
    on May 28, 2025
  8. added this to the 10.0.0 milestone on May 28, 2025
  9. 4 remaining items

  10. AndyAyersMS commented on Jul 28, 2025

    @AndyAyersMS
    MemberAuthor

    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  
    
  11. AndyAyersMS commented on Jul 28, 2025

    @AndyAyersMS
    MemberAuthor

    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.

  12. AndyAyersMS commented on Jul 28, 2025

    @AndyAyersMS
    MemberAuthor

    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)
    

    I will look into some NAOT cases next.

  13. jkotas commented on Jul 28, 2025

    @jkotas
    Member

    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?

  14. added a commit that references this issue on Jul 28, 2025
    84b502d
  15. jkotas commented on Jul 28, 2025

    @jkotas
    Member

    Or are there PInvokes in CoreLib where marshaling required returns true?

  16. AndyAyersMS commented on Jul 28, 2025

    @AndyAyersMS
    MemberAuthor

    Or are there PInvokes in CoreLib where marshaling required returns true?

    Will let you know shortly.

  17. AndyAyersMS commented on Jul 28, 2025

    @AndyAyersMS
    MemberAuthor

    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.

  18. AndyAyersMS commented on Jul 30, 2025

    @AndyAyersMS
    MemberAuthor

    Will revisit this in .NET 11.

  19. modified the milestones: 10.0.0, 11.0.0 on Jul 30, 2025
  20. added a commit that references this issue on May 30, 2026
    0863150
  21. AndyAyersMS commented on Jul 30, 2026

    @AndyAyersMS
    MemberAuthor

    I looked into this some but am still not clear what the right fix is. Going to move this to .NET 12.

  22. modified the milestones: 11.0.0, 12.0.0 on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions