Table of Contents

Class MetalMemoryPackSerializerGenerator

Namespace
DotCompute.Backends.Metal.Compilation
Assembly
DotCompute.Backends.Metal.dll

Generates MSL (Metal Shading Language) serializer/deserializer code that matches MemoryPack's exact binary format.

public sealed class MetalMemoryPackSerializerGenerator
Inheritance
MetalMemoryPackSerializerGenerator
Inherited Members

Remarks

MemoryPack binary format: - Header: 1-byte member count (0-249 = count, 255 = null) - Fields: Serialized in declaration order, no padding - Primitive types: Little-endian, direct copy - Guid: 16 bytes using ToByteArray() ordering - Nullable<T>: 1-byte has_value flag + T value (if present) - Collections: 4-byte signed int32 length (-1 = null) + elements - Strings: 4-byte signed int32 byte count (-1 = null) + UTF-8 bytes

Generated code enables GPU-side message processing without host round-trips on Apple Silicon. MSL-specific considerations: - Uses device address space for GPU memory pointers - No explicit __device__ annotation (implicit in MSL) - Metal compiler auto-vectorizes loops

Constructors

MetalMemoryPackSerializerGenerator(ILogger<MetalMemoryPackSerializerGenerator>)

Initializes a new instance of the MetalMemoryPackSerializerGenerator class.

public MetalMemoryPackSerializerGenerator(ILogger<MetalMemoryPackSerializerGenerator> logger)

Parameters

logger ILogger<MetalMemoryPackSerializerGenerator>

Logger for diagnostic output.

Methods

GenerateBatchSerializer(IEnumerable<Type>, string, bool)

Generates MSL serialization code for multiple MemoryPackable types in a single compilation unit.

[UnconditionalSuppressMessage("AOT", "IL2072", Justification = "Types passed to this method are expected to be MemoryPackable types with PublicProperties preserved")]
public string GenerateBatchSerializer(IEnumerable<Type> messageTypes, string compilationUnitName = "MemoryPackSerializers", bool skipHandlerGeneration = false)

Parameters

messageTypes IEnumerable<Type>

The message types to generate code for.

compilationUnitName string

The name of the compilation unit.

skipHandlerGeneration bool

When true, skips generating message handler (manual handler will be provided).

Returns

string

Generated MSL code for all types.

Remarks

Callers must ensure that all types in messageTypes have PublicProperties accessible. This is typically satisfied by types marked with [MemoryPackable] attribute.

Code is generated in a specific order to ensure forward declarations are not needed: 1. All struct definitions (so Response struct exists when handler is generated) 2. All deserializers 3. All serializers 4. Message handlers (one per Request/Response pair, not per type)

GenerateSerializer(Type)

Generates complete MSL serialization code for a MemoryPackable type.

public string GenerateSerializer(Type messageType)

Parameters

messageType Type

The message type to generate code for.

Returns

string

Generated MSL code including struct, deserializer, and serializer.

Exceptions

ArgumentNullException

Thrown when messageType is null.

InvalidOperationException

Thrown when code generation fails.