use super::WgpuDevice;
use crate::shaders::precision::compiler::source_is_f64;
impl WgpuDevice {
#[must_use]
pub fn compile_shader_raw(&self, source: &str, label: Option<&str>) -> wgpu::ShaderModule {
self.encoding_guard();
let module = self
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label,
source: wgpu::ShaderSource::Wgsl(source.into()),
});
self.encoding_complete();
module
}
#[must_use]
pub fn compile_shader(&self, source: &str, label: Option<&str>) -> wgpu::ShaderModule {
if source_is_f64(source) {
let downcast =
crate::shaders::precision::downcast_f64_to_f32_with_transcendentals(source);
return self.compile_shader_raw(&downcast, label);
}
self.compile_shader_raw(source, label)
}
#[cfg(feature = "spirv-passthrough")]
fn try_sovereign_spirv_compile(
&self,
source: &str,
label: Option<&str>,
tag: &str,
) -> Option<wgpu::ShaderModule> {
use crate::shaders::sovereign::SovereignCompiler;
let caps = crate::device::capabilities::DeviceCapabilities::from_device(self);
let sovereign = SovereignCompiler::new(caps);
match sovereign.compile(source) {
Ok((crate::shaders::sovereign::SovereignOutput::Spirv(validated), stats)) => {
if stats.fma_fusions > 0 || stats.dead_exprs_eliminated > 0 {
tracing::debug!(
"sovereign-spirv {tag}: {} FMA fusions, {} dead exprs eliminated",
stats.fma_fusions,
stats.dead_exprs_eliminated,
);
}
match barracuda_spirv::compile_spirv_passthrough(
self.device(),
validated.words(),
label,
) {
Ok(module) => Some(module),
Err(e) => {
tracing::debug!("spirv passthrough failed: {e}");
None
}
}
}
Err(e) => {
tracing::debug!("sovereign-spirv {tag} fallback to WGSL path: {e}");
None
}
}
}
#[cfg(not(feature = "spirv-passthrough"))]
fn try_sovereign_spirv_compile(
&self,
_source: &str,
_label: Option<&str>,
_tag: &str,
) -> Option<wgpu::ShaderModule> {
None
}
fn try_sovereign_wgsl_compile(
&self,
source: &str,
label: Option<&str>,
tag: &str,
) -> Option<wgpu::ShaderModule> {
use crate::shaders::sovereign::SovereignCompiler;
let caps = crate::device::capabilities::DeviceCapabilities::from_device(self);
let sovereign = SovereignCompiler::new(caps);
match sovereign.compile_to_wgsl(source) {
Ok((optimized_wgsl, stats)) => {
if stats.fma_fusions > 0 || stats.dead_exprs_eliminated > 0 {
tracing::debug!(
"sovereign-wgsl {tag}: {} FMA fusions, {} dead exprs eliminated",
stats.fma_fusions,
stats.dead_exprs_eliminated,
);
}
Some(self.compile_shader_raw(&optimized_wgsl, label))
}
Err(e) => {
tracing::debug!("sovereign-wgsl {tag} fallback to raw WGSL: {e}");
None
}
}
}
#[must_use]
pub fn compile_shader_f64(&self, source: &str, label: Option<&str>) -> wgpu::ShaderModule {
let source = &source
.replace("enable f64;", "")
.replace("enable subgroups;", "");
let caps = crate::device::capabilities::DeviceCapabilities::from_device(self);
let optimized = crate::shaders::precision::ShaderTemplate::for_device_capabilities(
source,
self.needs_f64_exp_log_workaround(),
&caps,
);
crate::device::coral_compiler::spawn_coral_compile_for_adapter(
&optimized,
self.adapter_info(),
true,
);
if let Some(module) = self.try_sovereign_spirv_compile(&optimized, label, "f64") {
return module;
}
if let Some(module) = self.try_sovereign_wgsl_compile(&optimized, label, "f64") {
return module;
}
self.compile_shader_raw(&optimized, label)
}
#[must_use]
pub fn compile_shader_df64(&self, source: &str, label: Option<&str>) -> wgpu::ShaderModule {
const DF64_CORE: &str = include_str!("../../shaders/math/df64_core.wgsl");
const DF64_TRANSCENDENTALS: &str =
include_str!("../../shaders/math/df64_transcendentals.wgsl");
let caps = crate::device::capabilities::DeviceCapabilities::from_device(self);
let naga_poisoned = caps.has_df64_spir_v_poisoning();
let full_combined = format!("{DF64_CORE}\n{DF64_TRANSCENDENTALS}\n{source}");
let full_combined = full_combined
.lines()
.filter(|l| l.trim() != "enable f64;")
.collect::<Vec<_>>()
.join("\n");
if naga_poisoned {
tracing::warn!(
device = %caps.device_name,
"DF64 SPIR-V poisoning (naga codegen) — requesting sovereign shader \
compilation to bypass naga. Falling back to arithmetic-only if unavailable."
);
crate::device::coral_compiler::spawn_coral_compile_for_adapter(
&full_combined,
self.adapter_info(),
false,
);
}
let combined = if naga_poisoned {
format!("{DF64_CORE}\n{source}")
.lines()
.filter(|l| l.trim() != "enable f64;")
.collect::<Vec<_>>()
.join("\n")
} else {
full_combined
};
let optimized = if combined.contains("@ilp_region") || combined.contains("@unroll_hint") {
use crate::shaders::optimizer::WgslOptimizer;
let optimizer = WgslOptimizer::new(caps.latency_model());
optimizer.optimize(&combined)
} else {
combined
};
if !naga_poisoned {
crate::device::coral_compiler::spawn_coral_compile_for_adapter(
&optimized,
self.adapter_info(),
false,
);
}
if let Some(module) = self.try_sovereign_spirv_compile(&optimized, label, "df64") {
return module;
}
if let Some(module) = self.try_sovereign_wgsl_compile(&optimized, label, "df64") {
return module;
}
self.compile_shader_raw(&optimized, label)
}
}
#[cfg(test)]
mod tests {
use crate::device::test_pool;
const SIMPLE_F32_SHADER: &str = r"
@group(0) @binding(0) var<storage, read> input: array<f32>;
@group(0) @binding(1) var<storage, read_write> output: array<f32>;
@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
if i >= arrayLength(&output) { return; }
output[i] = input[i] * 2.0;
}
";
const F64_SHADER: &str = r"
enable f64;
@group(0) @binding(0) var<storage, read> input: array<f64>;
@group(0) @binding(1) var<storage, read_write> output: array<f64>;
@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
if i >= arrayLength(&output) { return; }
output[i] = input[i] + f64(1.0);
}
";
const DF64_SHADER: &str = r"
@group(0) @binding(0) var<storage, read_write> out: array<f64>;
@compute @workgroup_size(1)
fn main(@builtin(global_invocation_id) _gid: vec3<u32>) {
let a = df64_from_f64(f64(3.0));
let b = df64_from_f64(f64(4.0));
let c = df64_add(a, b);
out[0] = df64_to_f64(c);
}
";
#[tokio::test]
async fn test_compile_shader_raw_f32() {
let device = test_pool::get_test_device().await;
let _module = device.compile_shader_raw(SIMPLE_F32_SHADER, Some("test_raw_f32"));
}
#[tokio::test]
async fn test_compile_shader_auto_downcast() {
let device = test_pool::get_test_device().await;
let _module = device.compile_shader(F64_SHADER, Some("test_auto_downcast"));
}
#[tokio::test]
async fn test_compile_shader_f64_tiered() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
if !device.has_f64_shaders() {
return;
}
let _module = device.compile_shader_f64(F64_SHADER, Some("test_f64_tiered"));
}
#[tokio::test]
async fn test_compile_shader_df64_prepend() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
if !device.has_f64_shaders() {
return;
}
let _module = device.compile_shader_df64(DF64_SHADER, Some("test_df64_prepend"));
}
#[tokio::test]
async fn test_compile_shader_raw_with_label() {
let device = test_pool::get_test_device().await;
let _module = device.compile_shader_raw(SIMPLE_F32_SHADER, Some("labeled_shader"));
let _module_no_label = device.compile_shader_raw(SIMPLE_F32_SHADER, None);
}
#[tokio::test]
async fn test_compile_shader_f32_passthrough() {
let device = test_pool::get_test_device().await;
let _module = device.compile_shader(SIMPLE_F32_SHADER, Some("f32_passthrough"));
}
}