Migrate DllImport P/Invokes to LibraryImport - #162
Open
craig-b wants to merge 5 commits into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 itsMlxOptional*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 boolis rejected outright, and #144'sCharSet.Ansican't happen becauseStringMarshallinghas 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).