Skip to main content

qualia_core_db/wgsl_forge/emit/
mod.rs

1pub mod coopmat;
2pub mod cuda_c;
3pub mod cuda_c_fused;
4pub mod cuda_graph;
5pub mod df64;
6pub mod dxc;
7pub mod dxc_cache;
8pub mod graph_hlsl;
9pub mod graph_msl;
10pub mod hlsl;
11pub mod hlsl_wave;
12pub mod msl;
13pub mod ptx;
14pub mod spirv;
15pub mod wgsl;
16
17use serde::{Deserialize, Serialize};
18
19use super::{ForgeError, KernelSpec, Schedule};
20pub use coopmat::{matmul_tc_wgsl, matmul_tc_wgsl_tiled, MATMUL_TC_TILED_ENTRY};
21pub use cuda_c::emit_cuda_c;
22pub use cuda_graph::{emit_graph_cuda_c, graph_cuda_entry, CudaCLowerer};
23pub use df64::{GEMM_DF64_ENTRY, GEMM_DF64_WGSL};
24pub use dxc::compile_hlsl_to_spirv;
25pub use dxc_cache::{clear_dxc_cache, compile_hlsl_to_spirv_cached, dxc_cache_len};
26pub use graph_hlsl::{conv2d_hlsl, emit_graph_hlsl, HlslLowerer};
27pub use graph_msl::{conv2d_msl, emit_graph_msl, MslLowerer};
28pub use hlsl::emit_hlsl;
29pub use msl::emit_msl;
30pub use ptx::emit_ptx;
31pub use spirv::{decode_spirv_words, emit_spirv, emit_spirv_patched, patch_spirv_workgroup_size};
32pub use wgsl::{emit_graph_wgsl, emit_wgsl};
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
35#[serde(rename_all = "snake_case")]
36pub enum TargetBackend {
37    Wgsl,
38    Msl,
39    Hlsl,
40    Ptx,
41    /// CUDA-C compiled to PTX by NVRTC at runtime (mirrors HLSL -> DXC).
42    CudaC,
43    /// Binary SPIR-V, produced from the generated WGSL via naga's `spv-out`
44    /// backend. The words are stored in `GeneratedShader::source` as a
45    /// `;`-joined decimal string (see [`spirv`]).
46    Spirv,
47}
48
49impl Default for TargetBackend {
50    fn default() -> Self {
51        Self::Wgsl
52    }
53}
54
55impl std::str::FromStr for TargetBackend {
56    type Err = String;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        match s.to_lowercase().as_str() {
60            "wgsl" => Ok(Self::Wgsl),
61            "msl" => Ok(Self::Msl),
62            "hlsl" => Ok(Self::Hlsl),
63            "ptx" => Ok(Self::Ptx),
64            "cuda" | "cuda-c" | "cuda_c" => Ok(Self::CudaC),
65            "spirv" | "spir-v" | "spv" => Ok(Self::Spirv),
66            _ => Err(format!("unknown target backend: {}", s)),
67        }
68    }
69}
70
71#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72pub struct GeneratedShader {
73    pub kernel_id: String,
74    pub semantic_hash: String,
75    pub source_hash: String,
76    pub schedule: Schedule,
77    pub source: String,
78}
79
80pub fn emit_shader(
81    kernel: &KernelSpec,
82    schedule: Schedule,
83    target: TargetBackend,
84) -> Result<GeneratedShader, ForgeError> {
85    match target {
86        TargetBackend::Wgsl => emit_wgsl(kernel, schedule),
87        TargetBackend::Msl => emit_msl(kernel, schedule),
88        TargetBackend::Hlsl => emit_hlsl(kernel, schedule),
89        TargetBackend::Ptx => emit_ptx(kernel, schedule),
90        TargetBackend::CudaC => emit_cuda_c(kernel, schedule),
91        TargetBackend::Spirv => emit_spirv(kernel, schedule),
92    }
93}