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