Skip to content

Migrate DllImport P/Invokes to LibraryImport - #162

Open
craig-b wants to merge 5 commits into
zhongkaifu:mainfrom
craig-b:chore/libraryimport-migration
Open

Migrate DllImport P/Invokes to LibraryImport#162
craig-b wants to merge 5 commits into
zhongkaifu:mainfrom
craig-b:chore/libraryimport-migration

Conversation

@craig-b

@craig-b craig-b commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Converts all ~440 [DllImport] declarations (CUDA, GGML, MLX, CPU surfaces) to [LibraryImport] + UnmanagedCallConv — the source-generated replacement .NET has recommended since .NET 7. Five per-surface commits so each backend can be reviewed (or dropped) separately. Signatures, calling conventions, and entry points are unchanged. The MLX assembly gets [assembly: DisableRuntimeMarshalling] so its MlxOptional* structs (bool fields) keep the exact mlx-c layout, and the Qwen3.5 decode contract test now asserts the new attributes.

The practical benefit is compile-time marshalling checks: neither of the interop bugs I sent earlier would have compiled — #143's unannotated out bool is rejected outright, and #144's CharSet.Ansi can't happen because StringMarshalling has to be stated explicitly. Runtime performance is unchanged.

Verified: solution builds clean, environment-independent test lane 1141 passed / 0 failed on current main, native smoke on my Vulkan box (model load + generation through the GGML surface).

Mechanical DllImport -> source-generated LibraryImport conversion.
TS_Std/TS_Var normByN now marshals explicitly as I1 (1-byte C++ bool);
the previous default was a 4-byte BOOL, equivalent for a by-value
register argument on the supported ABIs.
GgmlOps imports keep Cdecl via UnmanagedCallConv; the kernel32/libc
memory-pool imports keep platform-default conventions. The UTF-8
string parameters move from per-parameter LPUTF8Str to
StringMarshalling.Utf8.
The MlxOptional* structs cross by value with bool fields, so the
assembly disables runtime marshalling; their managed layout then
matches the mlx-c definitions exactly (1-byte bool).
cuModuleGetFunction's kernel-name parameter now marshals explicitly
as UTF-8.
Covers _exit (CLI), madvise (ModelBase), and mlock/munlock
(GgufReader). The Qwen35 decode contract test now asserts the
LibraryImport/UnmanagedCallConv attributes instead of DllImport.
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.

1 participant