Skip to content

[CoreCLR] Consider replacing [DllImport] with [LibraryImport] for p/invokes #10762

@simonrozsival

Description

@simonrozsival

Android framework version

net11.0-android (Preview)

Affected platform version

.NET 11

Description

Docs: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

When it's not using source generation, the built-in interop system in the .NET runtime generates an IL stub—a stream of IL instructions that is JIT-ed—at runtime to facilitate the transition from managed to unmanaged. [...] Since this IL stub is generated at runtime, it isn't available for ahead-of-time (AOT) compiler or IL trimming scenarios.

We should consider replacing our [DllImport] with [LibraryImport] to reduce the amount of code that needs to be JITted at runtime and allow R2R to precompile more code into native code.

We should focus only on P/Invokes into CoreCLR host code. We do not need to make changes to Mono P/Invokes.

Steps to Reproduce

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Labels

Area: CoreCLRIssues that only occur when using CoreCLR.needs-triageIssues that need to be assigned.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions