Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inliner: don't inline callees with pinvokes inside EH #79125

Open
EgorBo opened this issue Dec 2, 2022 · 4 comments
Open

Inliner: don't inline callees with pinvokes inside EH #79125

EgorBo opened this issue Dec 2, 2022 · 4 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Dec 2, 2022

[DllImport("user32.dll")]
static extern byte MessageBeep(uint uType);

static void Foo()
{
    MessageBeep(42);
}

static void Test()
{
    try
    {
        Foo();
    }
    catch
    {
    }
}

Inliner should not inline callees with pinvokes to try/catch handlers because we never inline pinvoke machinery in JIT for those. So ^ snippet ends up generating:

  21: JIT compiled (dynamicClass):IL_STUB_PInvoke(uint):ubyte [FullOpts, IL size=34, code size=152, hash=0xeb066932]

but if we mark Foo as [MethodImpl(MethodImplOptions.NoInlining)] we won't emit IL_STUB_PInvoke

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Dec 2, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 2, 2022
@ghost
Copy link

ghost commented Dec 2, 2022

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

Issue Details
[DllImport("user32.dll")]
static extern byte MessageBeep(uint uType);

static void Foo()
{
    MessageBeep(42);
}

static void Test()
{
    try
    {
        Foo();
    }
    catch
    {
    }
}

Inliner should not inline callees with pinvokes to try/catch handlers because we never inline pinvoke machinery in JIT for those. So ^ snippet ends up generating:

  21: JIT compiled (dynamicClass):IL_STUB_PInvoke(uint):ubyte [FullOpts, IL size=34, code size=152, hash=0xeb066932]

but if we mark Foo as [MethodImpl(MethodImplOptions.NoInlining)] we won't emit IL_STUB_PInvoke

Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo EgorBo added this to the Future milestone Dec 2, 2022
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2022
@jkoritzinsky
Copy link
Member

Related issue around enabling inlining a P/Invoke inside a try block with a catch block (amongst other things around P/Invokes and exception handling): #70109

@EgorBo
Copy link
Member Author

EgorBo commented Dec 2, 2022

@jkoritzinsky thanks, didn't notice that - do you think it's worth doing as a quick workaround (I'd expect it to be a simple change) until we support what you suggested in #70109?

@jkoritzinsky
Copy link
Member

Yes, I think it's definitely worth doing! It might also be worth avoiding inlining calls to functions that contain P/Invokes in finally blocks, as it has the same issue.

If we do both cases (don't inline call to function with P/Invoke in try or finally blocks), I think we can finally complete #69758 and remove the P/Invoke stub emitter support from crossgen2 without causing any regressions for common use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

No branches or pull requests

2 participants