-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
area-System.Runtime.InteropServicestrackingThis issue is tracking the completion of other related issues.This issue is tracking the completion of other related issues.
Milestone
Description
The Roslyn source generator feature has created an opportunity for the Interop team to consider the generation of the P/Invoke ILStub at compile time instead of during run time. A proposal for this work can be found here.
Prototyping is occurring in the dotnet/runtimelab.
Goals
- Remove run time overhead of ILStub generation.
- No observable degradation in performance of replaced P/Invoke calls.
- Generated code is semantically equivalent to build-in ILStub code gen.
- Support >= 80 % of P/Invoke patterns in
NetCoreApp.- Stretch goal is to support 100 % in
NetCoreApp.
- Stretch goal is to support 100 % in
- Cross-platform support.
- Ability to innovate in support of modern .NET types (e.g.,
ReadOnlySpan<T>/Span<T>). - Analyzer to ensure consumption success.
- Have a scenario agnostic code generation (Roslyn APIs) library for marshalling.
- Testable
- Ability to create comprehensive test matrix.
- High confidence in generated code through quantifiable unit testing metrics not "most common scenario" testing.
Non-Goals
- Support P/Invoke signatures not found in
NetCoreApp. - Alter built-in ILStub generation.
- Support COM interop.
Prototype exit criteria
- Proposal for new runtime APIs - see list below.
- Source generator and Analyzer in NuGet package that can be consumed by
NetCoreAppin a local build.- 80 % of P/Invokes in
NetCoreAppcan be updated with confidence. - Accounting of potential tenet deficiencies (e.g., performance, functional failures, semantic inconsistencies with built-in system).
- 80 % of P/Invokes in
- Add one high-value marshaller that does not exist today (e.g.,
Span<T>). Implement marshallers for Span and ReadOnlySpan runtimelab#1222 - Tooling to examine .NET assembly and indicate issues relying on source generator.
- Comprehensive unit test and integration matrix for all supported scenarios.
- Design documents contained within
designs/folder in repository. - Determine the size impact of the generated code on
NetCoreApp- ~550kB (SPCL is not entirely converted)
- Validate performance impact to IDE scenarios for projects in
NetCoreApp. See Disable EventSource generator in design-time builds #50741 for concern.- We will need to move to the Incremental Generator V2 API to get the required performance for large projects - Move to using the new Roslyn IIncrementalGenerator API for better in-VS performance runtimelab#1374
- Create a plan for transitioning from the prototype to a shipping release - Plan for .NET 7 integration runtimelab#1378.
- Share a local build of
NetCoreAppusing the source generator prototype with stakeholders.
Source generator prototype
All work is current taking place in dotnet/runtimelab.
- Collect needed scope of support in
NetCoreApp- Get P/Invoke information from assembly metadata runtimelab#146 - Authoring of needed marshallers.
- Blittable types
- Non-Blittable types - Implement the non-blittable type marshalling proposal runtimelab#302
-
bool -
Delegate -
SafeHandle- SafeHandle marshalling runtimelab#133 -
char- Add Char marshaller runtimelab#212 -
string- String marshallers for UTF-16 and UTF-8 runtimelab#249, Add string marshallers for ANSI and platform-defined runtimelab#288 -
Array- - by value in, all by ref, return - Array marshalling support runtimelab#272
- - by value out (e.g.,
[Out] Foo[]) - Add handling for [In, Out] attributes. runtimelab#380
- Experimental - (e.g.,
Span<T>orReadOnlySpan<T>) - Implement marshallers for Span and ReadOnlySpan runtimelab#1222 - Explicitly handle all
DllImportAttributeoptions (support, pass-through, or error).-
PreserveSig- Handle PreserveSig=false runtimelab#323 -
BestFitMapping,ThrowOnUnmappableChar, andLCIDConversion- Report diagnostic forBestFitMapping,ThrowOnUnmappableChar, andLCIDConversionruntimelab#291 -
SetLastError- Handle SetLastError=true runtimelab#360 -
ExactSpelling,EntryPoint,CharSet,CallingConvention- Pass through to blittableDllImportP/Invoke.
-
- Stub performance follow-ups
- Diagnostics:
- Properly update Roslyn Diagnostics - Report diagnostics in generator runtimelab#158
- What are the development mechanisms for diagnosing/debugging issue?
- Adorn all generated source with the
// <auto-generated/>comment. - Comment in source that source file is auto-generated. runtimelab#1209
- Testing:
- Integration testing. - Create test projects runtimelab#149
- XUnit for Integration testing is the default, but other methods should be possible.
- Use test runner in Visual Studio/VSCode to debug and run all tests. - Create test projects runtimelab#149
- Run individual test outside of testing harness -
F5to treat test like an "exe". -Demoproject
- Leverage
DNNEto avoid native build system support. - Performance testing using
BenchmarkDotNet.- Test performance against the built-in DllImport stubs - Add Benchmarks project. runtimelab#538
- Create assembly for P/Invoke targets.
- Create project of tests destined for https://github.com/dotnet/performance.
- Add
EventSourceevents for indicating DllImport source generator begin/end. - Add event for marking the Start/Stop of source generation runtimelab#258
- Deployment logistics
- Arcade – Integrate with Add standalone template for experiments that don't need runtime features runtimelab#238 Set up CI/CD pipeline based on the standalone template runtimelab#308
- NuGet - Publish as “Non-shipping” - Disable packing Demo app, tools, and test assets. runtimelab#327
- Analyzer vs Source Generator vs new Attributes
- Determine where do these go. Moving into dotnet/runtime - see guidelines.
- Investigate separation for COM generation option. Refactor DllImportGenerator project for easier extensibility runtimelab#1119.
- Porting support
-
MarshalEx=>Marshal. - Implement support for configurable generator options. runtimelab#458 - "Generate Forwarder" configured at runtime instead of a
#define. - Implement support for configurable generator options. runtimelab#458 - Analyzer/Fixer to help switch from
DllImport<=>GeneratedDllImport.
-
New .NET APIs
New APIs that will need to be introduced - remember to consider Linker semantics:
- Ability to set the thread-centric error code (e.g.,
SetLastError()). Expose setting/getting of system error code #46843. - Instantiation of
SafeHandles. Expose interop manipulation of SafeHandle #46830. -
GeneratedDllImportattribute and analyzer. LibraryImportAttribute for p/invoke source generation #46822. - Struct marshalling attributes and analyzer for usage support. Attributes and Analyzer Diagnostics to support custom struct marshalling in the DllImport Source Generator #46838.
- Provide mechanism to avoid relying on the
CallingConventionenumeration. Extensible Calling Conventions for native callee functions #51156.
All above APIs have inefficient but semantically correct implementations in the Ancillary.Interop project.
Once prototyping has completed the above APIs will go through an official API review in dotnet/runtime.
GSPP, juepiezhongren, AraHaan and nxrightheretannergooding, mjsabby, tibel, sos-dll, CoffeeFlux and 7 more
Metadata
Metadata
Assignees
Labels
area-System.Runtime.InteropServicestrackingThis issue is tracking the completion of other related issues.This issue is tracking the completion of other related issues.