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

[Mono]: Library mode enhancements. #84165

Merged

Conversation

lateralusX
Copy link
Member

This PR does a couple of enhancements to library mode builds.

Add support for direct pinvoke charset on Windows.

NativeAOT direct pinvoke support handles pinvoke charset on Windows:

private IEnumerable<string> EntryPointNameVariations(string name, PInvokeFlags flags)

This PR adds the same logic to Mono when targeting Windows, so it will follow the same charset rules as NativeAOT.

direct pinvoke wrappers on none full AOT builds.

Library mode builds will have option to create a self contained static or shared library, including all needed resources, embedding assemblies and static link all external dependencies (using direct pinvoke). Library mode also exports UnamangedCallersOnly methods that can be called by application loading the library. Currently the wrappers needed for UnmanagedCallersOnly methods will be generated when doing none full AOT builds (741fa51), but not pinvoke wrappers. This PR adds support to generate the pinvoke wrappers, but only for the methods described by direct pinvoke AOT compiler arguments, so for none full AOT builds not using direct pinvoke arguments, no pinvoke wrappers will be generated, inline with current none full AOT behavior.

direct pinvoke wrappers on profiled AOT builds.

Enhanced the profile-only option doing profile AOT to emit managed-to-native and native-to-managed wrappers. Needed in order for library build to work together with profiled AOT builds.

wrappers-only AOT option.

On platforms like Android, we still run majority of code using JIT. Since library mode should produce a self contained library, including pinvokes (for all direct pinvoked functions) as well as UnmanagedCallersOnly wrappers, Android would need to at least run none full AOT build of all used assemblies. By default, that will AOT a lot more methods that is really needed and there is no way to reduce the AOT:ed code and still run as much as possible using JIT. The profile-only option opens up ability to reduce what managed methods that will be AOT:ed, but still generates a number of inflated methods not needed by UnmanagedCallersOnly and direct pinvoke wrappers and piggy back on an existing option with additional functionality is not optimal. This PR adds an undocumented AOT option intended to be used by library builder, wrappers-only, and that will only include wrappers in AOT object giving library builder the ability to build a self contained library on Android that still runs majority of managed code using JIT.

Add ability to managele direct pinvoke symbols based on charset
on Windows. Aligning with NativeAOT behavior.
Library mode has a use case to support UnmanagedCallersOnly
(native-to-managed) and direct pinvokes (managed-to-native) and
still fallback to JIT for majority of managed methods. This commit
adds an undocumented AOT option to only generate wrappers and when
running that option together with partial AOT, only
native-to-managed and managed-to-native wrappers are generated.
@lateralusX lateralusX force-pushed the lateralusX/direct-pinvoke-enhancements branch from 0472ebc to 16cec6e Compare April 3, 2023 08:18
@lateralusX
Copy link
Member Author

Failures are known issues.

@lateralusX lateralusX merged commit a1954cc into dotnet:main Apr 4, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants