Skip to main content

KernelRegistry

Struct KernelRegistry 

Source
pub struct KernelRegistry { /* private fields */ }
Expand description

Registry that lazily compiles and caches Metal compute pipelines from embedded MSL source.

§Usage

let mut registry = KernelRegistry::new();
let pipeline = registry.get_pipeline("elementwise_add", device.metal_device())?;
encoder.encode(&pipeline, &buffers, grid, tg);

§Thread Safety

KernelRegistry is not Sync by default (it uses &mut self for get_pipeline to allow mutable cache insertion). If you need concurrent access, wrap it in a Mutex or use one registry per thread.

Implementations§

Source§

impl KernelRegistry

Source

pub fn new() -> Self

Create a new registry with all embedded shader sources pre-registered.

No compilation happens here — shaders are compiled lazily on first use.

Source

pub fn register_source(&mut self, name: impl Into<String>, source: &'static str)

Register a shader source at runtime (useful for testing and dynamic kernel generation).

Source

pub fn get_pipeline( &mut self, name: &str, device: &DeviceRef, ) -> Result<&ComputePipelineState>

Get a compiled compute pipeline for the named kernel function.

On first call for a given name, this compiles the MSL source into a Metal library, extracts the named function, and creates a ComputePipelineState. Subsequent calls return the cached pipeline.

§Errors
  • MlxError::KernelNotFound — no source registered for this name.
  • MlxError::ShaderCompilationError — MSL compilation or pipeline creation failed.
Source

pub fn is_cached(&self, name: &str) -> bool

Check if a pipeline for the given name is already compiled and cached.

Source

pub fn cached_count(&self) -> usize

Number of compiled pipelines currently in the cache.

Source

pub fn source_count(&self) -> usize

Number of registered shader sources.

Trait Implementations§

Source§

impl Default for KernelRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.