Skip to content

Fix cross-assembly COM projections and shared helpers - #1870

Merged
Jevan Saks (jevansaks) merged 16 commits into
mainfrom
jevansaks-external-com-interfaces
Nov 5, 2026
Merged

Jevan Saks (jevansaks) merged 16 commits into
mainfrom
jevansaks-external-com-interfaces

Conversation

@jevansaks

@jevansaks Jevan Saks (jevansaks) commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Fix projections when Windows metadata references COM interfaces defined in a separate WinMD, such as a D3D12 provider. DML_BUFFER_BINDING.Buffer uses the referenced ID3D12Resource with the correct pointer or managed interface shape.

Key changes:

  • Resolve interface classification and COM inheritance using the metadata file that owns each definition.
  • Preserve managed fields in recursive DRED structures and SafeHandle-friendly shared-handle COM overloads across metadata files. Keep backing-type safety checks for handles that cannot fit in IntPtr.
  • Keep factory APIs under Windows.Win32.PInvoke when metadata files share a projection root.
  • Emit shared helpers and inline-array types once, preserving distinct array overloads.
  • Update to published Win32Metadata 71.0.45-preview and use its package-selected Windows/D3D12 baseline for native, marshaled, and COM source-generator regression coverage. No checked-in WinMDs or local SDK reproduction tooling are added.

Input selection supplies exactly one D3D12 provider. There is no per-type occlusion or name-based replacement logic. Agility coverage will use the published SDK package.

Resolve interface definitions and COM ancestry with their owning generator and metadata reader. Add reciprocal metadata fixtures covering native pointers, marshaled arrays, and non-COM ancestry.

Co-authored-by: Copilot App <[email protected]>
Run the SDK COM source generator on GUID-bearing reciprocal fixtures and compile its output. Fix inherited CCW property inspection to use the method-owning metadata reader.

Co-authored-by: Copilot App <[email protected]>
Verify the SDK-generated compilation emits successfully, then normalize only third-party generated trees for the existing cross-platform test formatting assertions.

Co-authored-by: Copilot App <[email protected]>
Register compatible replacement types across inputs, forward local and external dependencies, and replace Apis members individually. Cover complete enum upgrades, architecture variants, nested types and real COM/LibraryImport generation in the three supported modes.

Co-authored-by: Copilot App <[email protected]>
@jevansaks Jevan Saks (jevansaks) changed the title Fix COM interface classification across metadata assemblies Prefer supplemental metadata types and resolve cross-assembly COM interfaces Nov 1, 2026
Emit common SliceAtNull and GeneratedCode attribute once across metadata inputs while preserving distinct per-array ReadOnlyItemRef overloads. Cover both generation orders with authored array fixtures.

Co-authored-by: Copilot App <[email protected]>
…overage

Use complete public Windows metadata70.0.11 and a reproducible public Agility1.619.3 fixture. Exercise real DXGI/D3D/DirectML requests in native, marshaled and actual COM-source-generated modes, both request orders, baseline precedence and target architectures. Remove all ExternalCom/Occlusion fixtures added by this work.

Co-authored-by: Copilot App <[email protected]>
Use existing inline-array namespace/name cache keys to select one same-root declaration at emission without changing transactional generation. Cover real D3D11 and D3D12 sampler border arrays in all three modes and both request orders, including retained ReadOnlyItemRef calls.

Co-authored-by: Copilot App <[email protected]>
Remove type, method and constant winner registration and local-definition forwarding. Keep qualified COM resolution, shared projection roots and helper ownership. Test full public split Windows metadata with one baseline or Agility D3D12 provider, neutral identities, all three projection modes, request/input orders, samplers and failed-generation retry.

Co-authored-by: Copilot App <[email protected]>
@jevansaks Jevan Saks (jevansaks) changed the title Prefer supplemental metadata types and resolve cross-assembly COM interfaces Fix cross-assembly COM projections and shared helpers Nov 3, 2026
Keep cross-assembly regression coverage on the split Windows SDK baseline and defer Agility coverage to the published SDK package.

Co-authored-by: Copilot App <[email protected]>
@jevansaks
Jevan Saks (jevansaks) marked this pull request as ready for review November 4, 2026 08:37
Update to Win32Metadata 71.0.45-preview and remove the checked-in Windows/D3D12 pair. Collect and copy package-selected test metadata after provider selection.

Co-authored-by: Copilot App <[email protected]>
Honor AvoidWinmdRootAlias for type definitions as well as references, so inherited IInspectable friendly methods keep Windows HSTRING locals correctly qualified.

Co-authored-by: Copilot App <[email protected]>
@jevansaks
Jevan Saks (jevansaks) enabled auto-merge (squash) November 5, 2026 17:12
@jevansaks
Jevan Saks (jevansaks) merged commit fa921cd into main Nov 5, 2026
28 of 36 checks passed
@jevansaks
Jevan Saks (jevansaks) deleted the jevansaks-external-com-interfaces branch November 5, 2026 18:54
Nick Eubanks (nieubank) pushed a commit to microsoft/WindowsML that referenced this pull request Nov 17, 2026
…35)

* Add C# Windows ML samples over CsWin32 Runtime and Task bindings

Rebase the C# image classification sample onto Windows ML 2.8, whose pinned
package ships the Runtime Win32 metadata, and add two more C# samples.

- Shared C# helpers (Samples/Runtime/shared/csharp): one props file for every
  C# sample, plus helpers matching the C++ ones for --device cpu|gpu|npu, --ep
  (Windows ML execution-provider catalog + ONNX Runtime registration),
  --performance/--efficiency (DXCore adapter ranking), and --verbose (ONNX
  Runtime logging and IWinMLStageExecutionDiagnostics).
- Image classification (C#): now at parity with the C++ sample's options and
  output, and built by build.ps1 -Sample all.
- Hello LanguageModel (C#): ONNX decoder with state pairs or GGUF through
  llama.cpp, streaming a greedy reply.
- Model-backed text generation Task (C#): model provider route and explicit
  llama.cpp route with optional speculative decoding.
- Run scripts take -Language cpp|csharp; GetTopK orders ties by class index.

Co-authored-by: Copilot App <[email protected]>

* Build C# samples with the .NET SDK and address review feedback

build.ps1 in Samples\Runtime and Samples\Tasks builds C++ projects first and C# projects with dotnet build; -Sample all skips the C# samples with a warning when the .NET SDK is missing. Document the .NET 8 prerequisite, skip adapters whose hardware ID can't be read, and guard the IUnknown argument helper.

Co-authored-by: Copilot App <[email protected]>

* Attribute the IUnknown parameter workaround to CsWin32

Co-authored-by: Copilot App <[email protected]>

* Update CsWin32 to 0.3.369 and drop the IUnknown argument workaround

CsWin32 0.3.369 (microsoft/CsWin32#1870) generates IUnknown* parameters
from the Windows SDK metadata as pointers, so the samples pass the DXCore
adapter to CreateExecutionTargetFromAdapter directly. The docs now require
0.3.369 or later.

Co-authored-by: Copilot App <[email protected]>

* Match the Windows ML 2.9 C++ samples in the C# samples

Pin --ep webgpu through IWinMLOrtCompatibility like the other providers,
since the Runtime now acquires and registers WebGPU for the pinned target,
and add --cache-weights to the C# text generation sample.

Co-authored-by: Copilot App <[email protected]>

* Exit the C# image classification sample with the failure HRESULT

The C++ sample returns the failing HRESULT from wmain. The C# sample now
does the same, and the shared C# provider helpers throw the HRESULTs the
C++ helpers return (ERROR_NOT_SUPPORTED and ERROR_NOT_FOUND).

Co-authored-by: Copilot App <[email protected]>

* Format the GetTopK tie-break with clang-format

Co-authored-by: Copilot App <[email protected]>

* Detect the .NET SDK, not just dotnet.exe, before building C# samples

The .NET runtime alone also installs dotnet.exe. The build scripts now
look for a .NET 8 or later SDK with dotnet --list-sdks, so -Sample all
skips the C# samples with a warning on runtime-only machines.

Co-authored-by: Copilot App <[email protected]>

* Add an idiomatic C# sample wrapper and port the C# samples to it

The C# samples called the Runtime C API through raw CsWin32 bindings, so
their code read like interop: factory functions with pointer out
parameters, unsafe blocks, and hand-mapped HRESULT checks.

Add a source-only wrapper under Samples/Runtime/shared/csharp/Runtime that
gives the API an idiomatic C# surface over the same bindings and the
published package:
- IDisposable objects with deterministic release; objects a property
  returns stay owned by their parent.
- Exceptions that keep the HRESULT; success codes never throw.
- .NET names, strings, and spans.
- Zero-copy access to CPU tensors through TensorLock<T>, with a read-only
  span for read locks and Commit for writes to device tensors.
- DXCore adapter ranking and execution target helpers.
- Text generation as an IAsyncEnumerable with cancellation, run on a
  dedicated session thread, plus an allocation-free synchronous loop.

All unsafe code stays inside the wrapper. It is sample code, not a
supported API. Its names follow .NET conventions so the samples could
move to a C# projection with few changes if one ships.

Port the image classification, Hello LanguageModel, and text generation
samples to the wrapper. Their options, output, and exit codes are
unchanged. Image decoding uses Windows.Graphics.Imaging.

Tested on x64 CPU and on an ARM64 NVIDIA GPU against the C++ samples:
image classification on CPU, GPU, and pinned WebGPU; Hello LanguageModel
with GGUF and ONNX models; model-provider text generation with a program
cache; and llama.cpp with and without a draft model. NPU wasn't tested.

Co-authored-by: Copilot App <[email protected]>

---------

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants