Skip to content

Source generation of ILStub for DllImport #43060

@AaronRobinsonMSFT

Description

@AaronRobinsonMSFT

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.
  • 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 NetCoreApp in a local build.
    • 80 % of P/Invokes in NetCoreApp can be updated with confidence.
    • Accounting of potential tenet deficiencies (e.g., performance, functional failures, semantic inconsistencies with built-in system).
  • 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.
  • Create a plan for transitioning from the prototype to a shipping release - Plan for .NET 7 integration runtimelab#1378.
  • Share a local build of NetCoreApp using the source generator prototype with stakeholders.

Source generator prototype

All work is current taking place in dotnet/runtimelab.

New .NET APIs

New APIs that will need to be introduced - remember to consider Linker semantics:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions