Skip to content
class

MetalKernel

MetalKernel

RAII handle for a compiled Metal compute pipeline.

Wraps a MTLComputePipelineState plus its dedicated MTLCommandQueue (both CFRetain-ed) so that one pipeline can be invoked many times without re-compiling MSL source. The Obj-C types are erased to void* here to keep this header includable from plain C++. Non-copyable, move-only.

See Also

compile_metal_kernel — factory that produces these. run_metal_kernel — invokes the compiled pipeline.

Attributes

pipeline_statevoid*
Opaque id<MTLComputePipelineState>. nullptr if compilation failed.
command_queuevoid*
Opaque id<MTLCommandQueue>. Owned independently from any other queue so kernel launches do not contend with MLX's own queue.
namestd::string
The MSL function name compiled into pipeline_state. Useful for debug logs.

Notes

Released in release_. Move constructors null out the source so the destructor only releases once.

Constructors

1
ctor

MetalKernel

Default-construct an invalid kernel handle.

Equivalent to a moved-from state — is_valid returns false until populated by compile_metal_kernel.

Destructor

1

Operators

1
op

operator=

MetalKernel & operator=(const MetalKernel &)

Methods

2
fnconst

is_valid

→bool
bool is_valid()

Return true iff compilation produced a usable pipeline.

A false return means either Metal is unavailable on this system or compile_metal_kernel failed silently (e.g. device offline). Hard MSL syntax errors throw rather than returning here.

Returns

bool

true iff pipeline_state != nullptr.

fn

release_

void release_()

Release the retained Metal objects and clear the handle fields.

Notes

Idempotent — safe to call on an already-released kernel.