use naga::back::spv;
use vyre_foundation::ir::Program;
pub struct SpirvBackend;
impl SpirvBackend {
pub const BACKEND_ID: &'static str = super::SPIRV_BACKEND_ID;
#[must_use]
pub fn new() -> Self {
Self
}
pub fn emit_spv(module: &naga::Module) -> Result<Vec<u32>, String> {
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.validate(module)
.map_err(|e| format!("naga validate failed: {e:?}"))?;
let options = spv::Options::default();
spv::write_vec(module, &info, &options, None)
.map_err(|e| format!("spv write failed: {e:?}"))
}
pub fn program_to_spv(program: &Program) -> Result<Vec<u32>, String> {
let desc =
vyre_lower::lower::lower(program).map_err(|e| format!("vyre lower failed: {e:?}"))?;
let module = vyre_emit_naga::emit(&desc).map_err(|e| format!("naga emit failed: {e:?}"))?;
Self::emit_spv(&module)
}
#[must_use]
pub fn program_fingerprint(program: &Program) -> Vec<u32> {
vyre_driver::program_vsa_fingerprint(program)
}
#[must_use]
pub fn observability_snapshot() -> vyre_driver::observability::DriverObservability {
vyre_driver::observability::DriverObservability::snapshot()
}
#[must_use]
pub fn spv_disk_cache_dir() -> std::path::PathBuf {
std::env::var_os("VYRE_SPV_CACHE_DIR")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| std::env::temp_dir().join("vyre-spv-cache"))
}
}
impl Default for SpirvBackend {
fn default() -> Self {
Self::new()
}
}