use super::backend::{BufferBinding, DispatchDescriptor, GpuBackend};
use crate::env_keys;
use crate::error::{BarracudaError, Result};
use std::sync::Arc;
#[cfg(feature = "sovereign-dispatch")]
use std::collections::HashMap;
#[cfg(feature = "sovereign-dispatch")]
const CONSERVATIVE_GPR_COUNT: u32 = 128;
#[cfg(feature = "sovereign-dispatch")]
const DEFAULT_WORKGROUP: [u32; 3] = [64, 1, 1];
#[cfg(feature = "sovereign-dispatch")]
static GPR_COUNT: std::sync::LazyLock<u32> = std::sync::LazyLock::new(|| {
std::env::var(env_keys::BARRACUDA_GPR_COUNT)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(CONSERVATIVE_GPR_COUNT)
});
#[cfg(feature = "sovereign-dispatch")]
static RESOLVED_DEFAULT_WORKGROUP: std::sync::LazyLock<[u32; 3]> = std::sync::LazyLock::new(|| {
let x = std::env::var(env_keys::BARRACUDA_DEFAULT_WORKGROUP_X)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(DEFAULT_WORKGROUP[0]);
[x, 1, 1]
});
#[cfg(feature = "sovereign-dispatch")]
use super::sovereign_dispatch_wire::{IpcBufferBinding, ShaderDispatchInfo};
#[derive(Debug, Clone, Copy)]
pub struct SovereignBuffer {
#[cfg(feature = "sovereign-dispatch")]
id: u64,
size: u64,
}
pub struct SovereignDevice {
name: Arc<str>,
#[cfg(feature = "sovereign-dispatch")]
compiler_available: bool,
#[cfg(feature = "sovereign-dispatch")]
dispatch_addr: Option<String>,
#[cfg(feature = "sovereign-dispatch")]
binary_cache: std::sync::Mutex<HashMap<u64, CachedBinary>>,
#[cfg(feature = "sovereign-dispatch")]
staged_buffers: std::sync::Mutex<HashMap<u64, bytes::BytesMut>>,
}
#[cfg(feature = "sovereign-dispatch")]
#[derive(Clone)]
struct CachedBinary {
binary: bytes::Bytes,
gpr_count: u32,
workgroup: [u32; 3],
shared_mem_bytes: u32,
barrier_count: u32,
}
impl std::fmt::Debug for SovereignDevice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SovereignDevice")
.field("name", &self.name)
.finish()
}
}
impl SovereignDevice {
#[cfg(feature = "sovereign-dispatch")]
#[must_use]
pub fn new() -> Self {
let compiler_available = crate::device::coral_compiler::is_coral_available();
let dispatch_addr = super::sovereign_discovery::detect_dispatch_addr();
if let Some(ref addr) = dispatch_addr {
tracing::info!(addr = %addr, "discovered compute.dispatch endpoint");
}
Self {
name: Arc::from("sovereign-ipc (shader.compile + compute.dispatch)"),
compiler_available,
dispatch_addr,
binary_cache: std::sync::Mutex::new(HashMap::new()),
staged_buffers: std::sync::Mutex::new(HashMap::new()),
}
}
#[cfg(not(feature = "sovereign-dispatch"))]
pub fn new_disabled() -> Result<Self> {
Err(BarracudaError::Device(
"SovereignDevice: enable the 'sovereign-dispatch' feature — \
cargo build --features sovereign-dispatch"
.into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
pub fn with_auto_device() -> Result<Self> {
Ok(Self::new())
}
#[cfg(not(feature = "sovereign-dispatch"))]
pub fn with_auto_device() -> Result<Self> {
Err(BarracudaError::Device(
"SovereignDevice: enable the 'sovereign-dispatch' feature".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
#[must_use]
pub fn has_compiler(&self) -> bool {
self.compiler_available
}
#[cfg(feature = "sovereign-dispatch")]
#[must_use]
pub fn has_dispatch(&self) -> bool {
self.dispatch_addr.is_some()
}
#[cfg(not(feature = "sovereign-dispatch"))]
#[must_use]
pub fn has_dispatch(&self) -> bool {
false
}
fn try_coral_cache(shader_source: &str) -> Option<bytes::Bytes> {
use crate::device::coral_compiler::{cache::cached_native_binary_any_arch, shader_hash};
let hash = shader_hash(shader_source);
cached_native_binary_any_arch(&hash).map(|b| b.binary)
}
#[cfg(feature = "sovereign-dispatch")]
fn query_dispatch_arch(&self) -> Option<String> {
let addr = self.dispatch_addr.as_ref()?;
super::sovereign_discovery::query_dispatch_arch(addr)
}
#[cfg(feature = "sovereign-dispatch")]
fn select_target(&self, archs: &[String]) -> Result<String> {
if let Ok(env_target) = std::env::var(env_keys::BARRACUDA_TARGET_ARCH) {
if archs.iter().any(|a| a == &env_target) {
return Ok(env_target);
}
tracing::warn!(
requested = %env_target,
available = ?archs,
"BARRACUDA_TARGET_ARCH not in supported_archs — falling back"
);
}
if let Some(dispatch_arch) = self.query_dispatch_arch() {
if archs.iter().any(|a| a == &dispatch_arch) {
tracing::info!(
arch = %dispatch_arch,
"target selected from dispatch primal capabilities"
);
return Ok(dispatch_arch);
}
tracing::warn!(
dispatch_arch = %dispatch_arch,
compiler_archs = ?archs,
"dispatch primal arch not in compiler's supported_archs — falling back"
);
}
match archs {
[] => Err(BarracudaError::Device(
"SovereignDevice: compiler reports no supported architectures".into(),
)),
[single] => Ok(single.clone()),
[first, ..] => {
tracing::warn!(
available = ?archs,
selected = %first,
"multiple target architectures — set BARRACUDA_TARGET_ARCH to disambiguate"
);
Ok(first.clone())
}
}
}
#[cfg(feature = "sovereign-dispatch")]
pub(crate) fn build_precision_advice(
f64_shader: bool,
df64_shader: bool,
) -> Option<crate::device::coral_compiler::types::PrecisionAdvice> {
if !f64_shader && !df64_shader {
return None;
}
let tier = if df64_shader { "DF64" } else { "F64" };
Some(crate::device::coral_compiler::types::PrecisionAdvice {
tier: tier.to_owned(),
needs_transcendental_lowering: f64_shader,
df64_naga_poisoned: df64_shader,
domain: None,
})
}
#[cfg(feature = "sovereign-dispatch")]
fn live_compile(
&self,
shader_source: &str,
advice: Option<crate::device::coral_compiler::types::PrecisionAdvice>,
) -> Result<CachedBinary> {
use crate::device::coral_compiler::{
GLOBAL_CORAL, cache::cache_native_binary, shader_hash,
};
let handle = tokio::runtime::Handle::try_current().map_err(|_| {
BarracudaError::Device("SovereignDevice: no tokio runtime for live compilation".into())
})?;
let source = shader_source.to_owned();
let fp64_software = advice
.as_ref()
.is_some_and(|a| a.needs_transcendental_lowering);
tokio::task::block_in_place(|| {
handle.block_on(async {
let archs = GLOBAL_CORAL.supported_archs().await.ok_or_else(|| {
BarracudaError::Device(
"SovereignDevice: compiler unreachable — no supported_archs".into(),
)
})?;
let target = self.select_target(&archs)?;
tracing::info!(
target = %target,
has_advice = advice.is_some(),
"live compile-on-dispatch: compiling WGSL for {target}"
);
let binary = if let Some(adv) = advice {
GLOBAL_CORAL
.compile_wgsl_with_advice(&source, &target, fp64_software, adv)
.await
} else {
GLOBAL_CORAL
.compile_wgsl_direct(&source, &target, false)
.await
};
let binary = binary.ok_or_else(|| {
BarracudaError::Device(format!(
"SovereignDevice: live compilation failed for target {target}"
))
})?;
let hash = shader_hash(&source);
cache_native_binary(&hash, &target, binary.clone());
tracing::info!(
target = %target,
size = binary.binary.len(),
"live compile-on-dispatch: compiled and cached ({} bytes)",
binary.binary.len()
);
Ok(CachedBinary {
binary: binary.binary,
gpr_count: binary.gpr_count.unwrap_or(*GPR_COUNT),
workgroup: binary.workgroup.unwrap_or(*RESOLVED_DEFAULT_WORKGROUP),
shared_mem_bytes: binary.shared_mem_bytes.unwrap_or(0),
barrier_count: binary.barrier_count.unwrap_or(0),
})
})
})
}
#[cfg(feature = "sovereign-dispatch")]
fn submit_dispatch(
&self,
binary: &[u8],
workgroups: (u32, u32, u32),
bindings: &[IpcBufferBinding],
hardware_hint: super::backend::HardwareHint,
shader_info: ShaderDispatchInfo,
) -> Result<()> {
let Some(ref addr) = self.dispatch_addr else {
return Err(BarracudaError::Device(
"SovereignDevice: no compute.dispatch endpoint discovered — \
ensure a dispatch primal is running and advertising compute.dispatch"
.into(),
));
};
super::sovereign_dispatch_wire::submit_dispatch(
addr,
&self.staged_buffers,
binary,
workgroups,
bindings,
hardware_hint,
shader_info,
)
}
}
#[cfg(feature = "sovereign-dispatch")]
impl Default for SovereignDevice {
fn default() -> Self {
Self::new()
}
}
impl GpuBackend for SovereignDevice {
type Buffer = SovereignBuffer;
fn name(&self) -> &str {
&self.name
}
fn has_f64_shaders(&self) -> bool {
true
}
fn is_lost(&self) -> bool {
false
}
#[cfg(feature = "sovereign-dispatch")]
fn alloc_buffer(&self, _label: &str, size: u64) -> Result<SovereignBuffer> {
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if let Ok(mut staged) = self.staged_buffers.lock() {
staged.insert(id, bytes::BytesMut::zeroed(size as usize));
}
Ok(SovereignBuffer { id, size })
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn alloc_buffer(&self, _label: &str, _size: u64) -> Result<SovereignBuffer> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
fn alloc_buffer_init(&self, label: &str, contents: &[u8]) -> Result<SovereignBuffer> {
let buf = self.alloc_buffer(label, contents.len() as u64)?;
self.upload(&buf, 0, contents);
Ok(buf)
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn alloc_buffer_init(&self, _label: &str, _contents: &[u8]) -> Result<SovereignBuffer> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
fn alloc_uniform(&self, label: &str, contents: &[u8]) -> Result<SovereignBuffer> {
self.alloc_buffer_init(label, contents)
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn alloc_uniform(&self, _label: &str, _contents: &[u8]) -> Result<SovereignBuffer> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
fn upload(&self, buffer: &SovereignBuffer, offset: u64, data: &[u8]) {
if let Ok(mut staged) = self.staged_buffers.lock() {
if let Some(buf) = staged.get_mut(&buffer.id) {
let start = offset as usize;
let end = start + data.len();
if end <= buf.len() {
buf[start..end].copy_from_slice(data);
}
}
}
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn upload(&self, _buffer: &SovereignBuffer, _offset: u64, _data: &[u8]) {}
#[cfg(feature = "sovereign-dispatch")]
fn download(&self, buffer: &SovereignBuffer, _size: u64) -> Result<bytes::Bytes> {
let staged = self.staged_buffers.lock().map_err(|e| {
BarracudaError::Device(format!("SovereignDevice: staged lock poisoned: {e}"))
})?;
staged
.get(&buffer.id)
.map(|b| bytes::Bytes::copy_from_slice(b))
.ok_or_else(|| {
BarracudaError::Device(format!(
"SovereignDevice: buffer {} not found in staging",
buffer.id
))
})
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn download(&self, _buffer: &SovereignBuffer, _size: u64) -> Result<bytes::Bytes> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
fn dispatch_compute(&self, desc: DispatchDescriptor<'_, Self>) -> Result<()> {
use std::collections::hash_map::Entry;
use std::hash::{Hash, Hasher};
let mut hasher = std::hash::DefaultHasher::new();
desc.shader_source.hash(&mut hasher);
let key = hasher.finish();
let mut cache = self.binary_cache.lock().map_err(|e| {
BarracudaError::Device(format!("SovereignDevice: cache lock poisoned: {e}"))
})?;
let cached = match cache.entry(key) {
Entry::Occupied(e) => {
tracing::debug!("sovereign cache hit — using pre-compiled native binary");
e.into_mut()
}
Entry::Vacant(e) => {
if let Some(binary) = Self::try_coral_cache(desc.shader_source) {
tracing::debug!("coral compiler cache hit — using pre-compiled native binary");
e.insert(CachedBinary {
binary,
gpr_count: *GPR_COUNT,
workgroup: *RESOLVED_DEFAULT_WORKGROUP,
shared_mem_bytes: 0,
barrier_count: 0,
})
} else {
let advice = Self::build_precision_advice(desc.f64_shader, desc.df64_shader);
let cached = self.live_compile(desc.shader_source, advice)?;
e.insert(cached)
}
}
};
let binary = cached.binary.clone();
let info = ShaderDispatchInfo {
gpr_count: cached.gpr_count,
workgroup: cached.workgroup,
shared_mem_bytes: cached.shared_mem_bytes,
barrier_count: cached.barrier_count,
};
drop(cache);
let ipc_bindings: Vec<IpcBufferBinding> = desc
.bindings
.iter()
.map(|b| IpcBufferBinding {
index: b.index,
buffer_id: b.buffer.id,
size: b.buffer.size,
read_only: b.read_only,
})
.collect();
self.submit_dispatch(
&binary,
desc.workgroups,
&ipc_bindings,
desc.hardware_hint,
info,
)
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn dispatch_compute(&self, _desc: DispatchDescriptor<'_, Self>) -> Result<()> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
#[cfg(feature = "sovereign-dispatch")]
fn dispatch_binary(
&self,
binary: &[u8],
bindings: Vec<BufferBinding<'_, Self>>,
workgroups: (u32, u32, u32),
_entry_point: &str,
) -> Result<()> {
let ipc_bindings: Vec<IpcBufferBinding> = bindings
.iter()
.map(|b| IpcBufferBinding {
index: b.index,
buffer_id: b.buffer.id,
size: b.buffer.size,
read_only: b.read_only,
})
.collect();
self.submit_dispatch(
binary,
workgroups,
&ipc_bindings,
super::backend::HardwareHint::Compute,
ShaderDispatchInfo {
gpr_count: *GPR_COUNT,
workgroup: *RESOLVED_DEFAULT_WORKGROUP,
shared_mem_bytes: 0,
barrier_count: 0,
},
)
}
#[cfg(not(feature = "sovereign-dispatch"))]
fn dispatch_binary(
&self,
_binary: &[u8],
_bindings: Vec<BufferBinding<'_, Self>>,
_workgroups: (u32, u32, u32),
_entry_point: &str,
) -> Result<()> {
Err(BarracudaError::Device(
"sovereign-dispatch not enabled".into(),
))
}
}
#[cfg(test)]
#[path = "sovereign_device_tests.rs"]
mod tests;