Skip to main content

rlx_runtime/
registry.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Backend registry — a single registration point for all backends.
6//!
7//! Adding a new backend (CUDA, ROCm, wgpu, WASM, TPU) is now a self-contained
8//! change in its own crate:
9//!
10//! ```ignore
11//! // in rlx-cuda/src/lib.rs
12//! #[cfg(feature = "cuda")]
13//! pub fn register() {
14//!     rlx_runtime::register_backend(Device::Cuda,
15//!         || Box::new(CudaBackend) as Box<dyn Backend>);
16//! }
17//! ```
18//!
19//! `Session::compile` consults the registry instead of a hardcoded `match`,
20//! so the runtime crate has no compile-time knowledge of which backends are
21//! available — each enables itself via its Cargo feature.
22
23use crate::backend::Backend;
24use rlx_driver::Device;
25use std::collections::HashMap;
26use std::sync::{OnceLock, RwLock};
27
28/// Factory closure that constructs a fresh backend instance.
29///
30/// Called once per `Session::compile`. Implementations are typically
31/// stateless (e.g. unit struct `CpuBackend`); the per-graph state lives
32/// inside the returned `Box<dyn Backend>`.
33pub type BackendFactory = fn() -> Box<dyn Backend>;
34
35struct Registry {
36    factories: RwLock<HashMap<Device, BackendFactory>>,
37}
38
39fn registry() -> &'static Registry {
40    static REGISTRY: OnceLock<Registry> = OnceLock::new();
41    REGISTRY.get_or_init(|| {
42        let r = Registry {
43            factories: RwLock::new(HashMap::new()),
44        };
45        register_builtin(&r);
46        r
47    })
48}
49
50/// Register builtin backends compiled into `rlx-runtime`. External
51/// backends (in their own crates) call `register_backend` from their
52/// own init path.
53#[allow(unused_mut, unused_variables)]
54fn register_builtin(r: &Registry) {
55    let mut map = r.factories.write().expect("registry poisoned");
56
57    #[cfg(feature = "cpu")]
58    map.insert(Device::Cpu, || {
59        Box::new(crate::backend::cpu_backend::CpuBackend) as Box<dyn Backend>
60    });
61
62    #[cfg(all(feature = "metal", target_vendor = "apple", not(target_os = "watchos")))]
63    map.insert(Device::Metal, || {
64        Box::new(crate::backend::metal_backend::MetalBackend) as Box<dyn Backend>
65    });
66
67    #[cfg(all(feature = "mlx", rlx_mlx_host))]
68    map.insert(Device::Mlx, || {
69        Box::new(crate::backend::mlx_backend::MlxBackend) as Box<dyn Backend>
70    });
71
72    #[cfg(all(
73        feature = "coreml",
74        target_vendor = "apple",
75        not(target_os = "watchos")
76    ))]
77    map.insert(Device::Ane, || {
78        Box::new(crate::backend::coreml_backend::CoremlBackend) as Box<dyn Backend>
79    });
80
81    #[cfg(feature = "gpu")]
82    map.insert(Device::Gpu, || {
83        Box::new(crate::backend::wgpu_backend::WgpuBackend) as Box<dyn Backend>
84    });
85
86    // Browser-facing WebGPU label — same wgpu backend as `Device::Gpu`.
87    #[cfg(feature = "webgpu")]
88    map.insert(Device::WebGpu, || {
89        Box::new(crate::backend::wgpu_backend::WgpuBackend) as Box<dyn Backend>
90    });
91
92    #[cfg(feature = "opengl")]
93    map.insert(Device::OpenGl, || {
94        Box::new(crate::backend::webgl_backend::WebglBackend) as Box<dyn Backend>
95    });
96
97    #[cfg(feature = "vulkan")]
98    map.insert(Device::Vulkan, || {
99        Box::new(crate::backend::vulkan_backend::VulkanBackend) as Box<dyn Backend>
100    });
101
102    #[cfg(feature = "cuda")]
103    map.insert(Device::Cuda, || {
104        Box::new(crate::backend::cuda_backend::CudaBackend) as Box<dyn Backend>
105    });
106
107    #[cfg(feature = "rocm")]
108    map.insert(Device::Rocm, || {
109        Box::new(crate::backend::rocm_backend::RocmBackend) as Box<dyn Backend>
110    });
111
112    #[cfg(feature = "oneapi")]
113    map.insert(Device::OneApi, || {
114        Box::new(crate::backend::oneapi_backend::OneApiBackend) as Box<dyn Backend>
115    });
116
117    #[cfg(feature = "tpu")]
118    map.insert(Device::Tpu, || {
119        Box::new(crate::backend::tpu_backend::TpuBackend) as Box<dyn Backend>
120    });
121
122    #[cfg(feature = "qnn")]
123    map.insert(Device::Hexagon, || {
124        Box::new(crate::backend::qnn_backend::QnnBackend) as Box<dyn Backend>
125    });
126
127    // XDNA NPU: registers the seam so `backend_for(Device::Xdna)` resolves and a
128    // dispatch report reads "0 ops supported". `is_available(Xdna)` stays honest
129    // (false without a runtime), so this never runs unless explicitly forced,
130    // in which case `XdnaBackend::compile` returns rlx_xdna's clear diagnostic.
131    #[cfg(feature = "xdna")]
132    map.insert(Device::Xdna, || {
133        Box::new(crate::backend::xdna_backend::XdnaBackend) as Box<dyn Backend>
134    });
135}
136
137/// Register a backend factory for `device`. External backend crates
138/// (rlx-cuda, rlx-rocm, rlx-wgpu, rlx-wasm, …) call this once at startup
139/// (typically from a `pub fn register()` in their lib.rs that the user
140/// invokes — or via a constructor attribute if they use `ctor`/`inventory`).
141///
142/// Re-registering a device replaces the prior factory, so a custom backend
143/// can override a builtin (useful for swap-in alternatives like a tuned
144/// CPU backend).
145pub fn register_backend(device: Device, factory: BackendFactory) {
146    let r = registry();
147    let mut map = r.factories.write().expect("registry poisoned");
148    map.insert(device, factory);
149}
150
151/// Look up a backend factory and instantiate. Returns `None` if no backend
152/// is registered for `device`.
153pub fn backend_for(device: Device) -> Option<Box<dyn Backend>> {
154    let r = registry();
155    let map = r.factories.read().expect("registry poisoned");
156    map.get(&device).map(|f| f())
157}
158
159/// All currently registered devices (deterministic snapshot).
160pub fn registered_devices() -> Vec<Device> {
161    let r = registry();
162    let map = r.factories.read().expect("registry poisoned");
163    let mut out: Vec<Device> = map.keys().copied().collect();
164    out.sort_by_key(|d| format!("{d:?}"));
165    out
166}