vulkane_kernel_text/lib.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! The shared text walker for Vulkane's kernel emitters (ROADMAP rows 8a, 8b and 8g for GLSL and WGSL).
3//!
4//! [`lower_kernel`] takes an Unpopped kernel-layer [`KernelFn`] and a
5//! [`Dialect`] and writes shader source. The walk (scope checks, statements, control flow, the
6//! structure of a `For`) is written once; a dialect supplies only the spellings that differ between
7//! GLSL and WGSL: declarations, the entry point, literals, operators, barriers.
8//!
9//! ## What it lowers (v1)
10//!
11//! Integer kernels (`I32`/`U32`): buffer parameters, static `Shared` arrays, locals, `Let`, `Load`,
12//! `Assign`, `Store`, `MaskedLoad` and `MaskedStore` (ROADMAP 9e: a guarded plain access, so the memory is not touched
13//! where the mask is false), `Barrier` and `Fence`, `If`, `For`, `While`, `Break`, `Continue`, `Return`, and
14//! the expressions `Local`, `ConstInt`, `Exec` (thread, block, `BlockDim`, `GridDim`), `Add`, `Sub`,
15//! `Mul`, the comparisons, `BitAnd`/`BitOr`/`BitXor`, `Shl`/`Shr` (the count masked to the width, as
16//! the SPIR-V emitter does) and `Select`. Anything else is a typed
17//! [`UnsupportedKernel`](unpopped::backend::LowerError::UnsupportedKernel) naming it.
18//!
19//! ## Semantics shared with the SPIR-V emitter
20//!
21//! A comparison gives `1` or `0` in the compute dtype. A `For` evaluates its bounds once; a zero step
22//! leaves the loop at once (a shader cannot trap). A barrier under thread-divergent control is refused
23//! by `check_kernel` (`BarrierDivergent`) before any text is written.
24//!
25//! ## Refused by name
26//!
27//! The twelve NVIDIA-only statements of board 434 C and its extension (`CopyAsync`, `Collective`,
28//! `MbarInit`, `WgmmaWait`, ...) are refused by name, at any nesting depth, before the walk starts.
29
30mod decline;
31mod dialect;
32mod expr;
33mod scalarize;
34mod scope;
35mod stmt;
36
37pub use decline::{DECLINED_434C, DECLINED_434C_EXT, declined_by_name};
38pub use dialect::{BIT_OPS, COMPARISONS, Decl, Dialect, Layout, operator};
39pub use scalarize::scalarize;
40
41use unpopped::backend::{GeneratedKernel, LowerError};
42use unpopped::kernel::KernelFn;
43
44/// Lower `kernel` to source in `dialect`, or say why it cannot.
45///
46/// # Errors
47///
48/// [`LowerError::UnsupportedKernel`] naming what is not lowered, with the by-name patent refusal
49/// first.
50pub fn lower_kernel(
51 dialect: &dyn Dialect,
52 kernel: &KernelFn,
53) -> Result<GeneratedKernel, LowerError> {
54 if let Some(e) = declined_by_name(dialect.name(), kernel) {
55 return Err(e);
56 }
57 // Packed values (vectors) become scalars first: `lower_dag` has no seam for them.
58 let scalarized = scalarize(kernel)?;
59 let kernel = scalarized.as_ref().unwrap_or(kernel);
60 let sc = scope::check(dialect, kernel)?;
61 // The body first: a rounding conversion in it adds a declaration to the prologue.
62 let mut body = String::new();
63 stmt::emit_body(dialect, kernel, &sc, &mut body)?;
64 let mut out = dialect.prologue(&sc.layout(kernel));
65 out.push_str(&body);
66 out.push_str(dialect.epilogue());
67 Ok(GeneratedKernel::new(kernel.name.clone(), out))
68}