pub mod dead_expr;
pub mod df64_rewrite;
pub mod fma_fusion;
pub mod spv_emit;
mod validation_harness;
pub mod wgsl_emit;
use crate::device::capabilities::DeviceCapabilities;
#[derive(Debug, Clone)]
pub struct ValidatedSpirv {
words: Vec<u32>,
}
impl ValidatedSpirv {
pub(crate) fn from_validated(words: Vec<u32>) -> Self {
Self { words }
}
#[must_use]
pub fn words(&self) -> &[u32] {
&self.words
}
}
#[derive(Debug)]
pub enum SovereignOutput {
Spirv(ValidatedSpirv),
}
#[derive(Debug, Clone, Default)]
pub struct CompileStats {
pub fma_fusions: usize,
pub dead_exprs_eliminated: usize,
}
pub struct SovereignCompiler {
_caps: DeviceCapabilities,
}
impl SovereignCompiler {
#[must_use]
pub fn new(caps: DeviceCapabilities) -> Self {
Self { _caps: caps }
}
pub fn compile_to_wgsl(&self, wgsl: &str) -> Result<(String, CompileStats), SovereignError> {
let (module, info, stats) = self.parse_optimize_validate(wgsl)?;
let mut optimized_wgsl = wgsl_emit::emit_wgsl(&module, &info)?;
for ep in &module.entry_points {
let original = &ep.name;
let fn_original = format!("fn {original}(");
if optimized_wgsl.contains(&fn_original) {
continue;
}
let renamed = format!("{original}_");
let fn_renamed = format!("fn {renamed}(");
if optimized_wgsl.contains(&fn_renamed) {
optimized_wgsl = optimized_wgsl.replacen(&fn_renamed, &fn_original, 1);
}
}
Ok((optimized_wgsl, stats))
}
pub fn compile(&self, wgsl: &str) -> Result<(SovereignOutput, CompileStats), SovereignError> {
let (module, info, stats) = self.parse_optimize_validate(wgsl)?;
let validated = spv_emit::emit_spirv(&module, &info)?;
Ok((SovereignOutput::Spirv(validated), stats))
}
fn parse_optimize_validate(
&self,
wgsl: &str,
) -> Result<(naga::Module, naga::valid::ModuleInfo, CompileStats), SovereignError> {
let mut stats = CompileStats::default();
let mut module =
naga::front::wgsl::parse_str(wgsl).map_err(|e| SovereignError::Parse(e.to_string()))?;
for (_handle, func) in module.functions.iter_mut() {
stats.fma_fusions += fma_fusion::fuse_multiply_add(&mut func.expressions);
}
for ep in &mut module.entry_points {
stats.fma_fusions += fma_fusion::fuse_multiply_add(&mut ep.function.expressions);
}
for (_handle, func) in module.functions.iter_mut() {
stats.dead_exprs_eliminated += dead_expr::eliminate(&mut func.expressions, &func.body);
}
for ep in &mut module.entry_points {
stats.dead_exprs_eliminated +=
dead_expr::eliminate(&mut ep.function.expressions, &ep.function.body);
}
let info = {
let mut validator = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
);
validator
.validate(&module)
.map_err(|e| SovereignError::Validation(e.to_string()))?
};
Ok((module, info, stats))
}
}
#[derive(Debug, thiserror::Error)]
pub enum SovereignError {
#[error("WGSL parse failed: {0}")]
Parse(String),
#[error("Module validation failed after optimization: {0}")]
Validation(String),
#[error("SPIR-V emission failed: {0}")]
SpirvEmit(String),
#[error("WGSL emission failed: {0}")]
WgslEmit(String),
}
#[cfg(test)]
mod tests {
use super::*;
fn test_caps() -> DeviceCapabilities {
use crate::device::vendor::VENDOR_NVIDIA;
DeviceCapabilities {
device_name: "Test GPU".into(),
device_type: wgpu::DeviceType::DiscreteGpu,
max_buffer_size: 1024 * 1024 * 1024,
max_workgroup_size: (256, 256, 64),
max_compute_workgroups: (65_535, 65_535, 65_535),
max_compute_invocations_per_workgroup: 1024,
max_storage_buffers_per_shader_stage: 8,
max_uniform_buffers_per_shader_stage: 12,
max_bind_groups: 4,
backend: wgpu::Backend::Vulkan,
vendor: VENDOR_NVIDIA,
gpu_dispatch_threshold_override: None,
subgroup_min_size: 32,
subgroup_max_size: 32,
has_subgroups: false,
f64_shaders: true,
f64_shared_memory: false,
f64_capabilities: None,
}
}
#[test]
fn test_sovereign_compiles_trivial_shader() {
let wgsl = 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(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let idx = gid.x;
output[idx] = input[idx] * 2.0;
}
";
let compiler = SovereignCompiler::new(test_caps());
let (output, stats) = compiler.compile(wgsl).expect("should compile");
match output {
SovereignOutput::Spirv(validated) => {
assert!(!validated.words().is_empty(), "SPIR-V should not be empty");
assert_eq!(validated.words()[0], 0x07230203, "SPIR-V magic number");
}
}
assert_eq!(stats.fma_fusions, 0);
}
#[test]
fn test_sovereign_rejects_invalid_wgsl() {
let compiler = SovereignCompiler::new(test_caps());
let result = compiler.compile("this is not wgsl");
assert!(result.is_err());
}
#[test]
fn test_sovereign_fma_fusion_roundtrip() {
let wgsl = r"
@group(0) @binding(0) var<storage, read> a_buf: array<f32>;
@group(0) @binding(1) var<storage, read> b_buf: array<f32>;
@group(0) @binding(2) var<storage, read> c_buf: array<f32>;
@group(0) @binding(3) var<storage, read_write> out: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
let a = a_buf[i];
let b = b_buf[i];
let c = c_buf[i];
let product = a * b;
let result = product + c;
out[i] = result;
}
";
let compiler = SovereignCompiler::new(test_caps());
let (output, stats) = compiler.compile(wgsl).expect("should compile with FMA");
assert!(
stats.fma_fusions >= 1,
"expected FMA fusion, got {}",
stats.fma_fusions
);
match output {
SovereignOutput::Spirv(validated) => {
assert!(!validated.words().is_empty());
assert_eq!(validated.words()[0], 0x07230203);
}
}
}
#[test]
fn test_sovereign_complex_shader_roundtrip() {
let wgsl = r"
struct Params {
n: u32,
scale: f32,
}
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var<storage, read> input: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let idx = gid.x;
if (idx >= params.n) {
return;
}
let x = input[idx];
let scaled = x * params.scale;
let shifted = scaled + 1.0;
let clamped = max(shifted, 0.0);
output[idx] = clamped;
}
";
let compiler = SovereignCompiler::new(test_caps());
let (output, _stats) = compiler
.compile(wgsl)
.expect("complex shader should compile");
match output {
SovereignOutput::Spirv(validated) => {
assert!(
validated.words().len() > 10,
"SPIR-V should have substantial content"
);
assert_eq!(validated.words()[0], 0x07230203);
}
}
}
#[test]
fn test_sovereign_preserves_correctness_after_fusion() {
let wgsl = r"
@group(0) @binding(0) var<storage, read> data: array<f32>;
@group(0) @binding(1) var<storage, read_write> out: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
let a = data[i];
let b = data[i + 1u];
let c = data[i + 2u];
// Two independent FMA opportunities
let p1 = a * b;
let r1 = p1 + c;
let p2 = b * c;
let r2 = p2 + a;
out[i] = r1 + r2;
}
";
let compiler = SovereignCompiler::new(test_caps());
let (output, stats) = compiler.compile(wgsl).expect("should compile");
assert!(stats.fma_fusions >= 1, "expected FMA fusions");
match output {
SovereignOutput::Spirv(validated) => {
assert_eq!(validated.words()[0], 0x07230203);
}
}
}
}