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
impl KernelRegistry
Sourcepub fn new() -> Self
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.
Sourcepub fn register_source(&mut self, name: impl Into<String>, source: &'static str)
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).
Sourcepub fn get_pipeline(
&mut self,
name: &str,
device: &DeviceRef,
) -> Result<&ComputePipelineState>
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.
Sourcepub fn is_cached(&self, name: &str) -> bool
pub fn is_cached(&self, name: &str) -> bool
Check if a pipeline for the given name is already compiled and cached.
Sourcepub fn cached_count(&self) -> usize
pub fn cached_count(&self) -> usize
Number of compiled pipelines currently in the cache.
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Number of registered shader sources.