Skip to main content

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 coverage;
31mod decline;
32mod dialect;
33mod expr;
34mod scalarize;
35mod scope;
36mod stmt;
37
38pub use coverage::{Coverage, coverage, not_lowered_note};
39pub use decline::{
40    DECLINED_434C, DECLINED_434C_EXT, collective_is_ruled, declined_by_name, declined_by_name_with,
41};
42pub use dialect::{BIT_OPS, COMPARISONS, Decl, Dialect, Layout, SubgroupCollective, operator};
43pub use scalarize::scalarize;
44
45use unpopped::backend::{GeneratedKernel, LowerError};
46use unpopped::kernel::KernelFn;
47
48/// Lower `kernel` to source in `dialect`, or say why it cannot.
49///
50/// # Errors
51///
52/// [`LowerError::UnsupportedKernel`] naming what is not lowered, with the by-name patent refusal
53/// first.
54pub fn lower_kernel(
55    dialect: &dyn Dialect,
56    kernel: &KernelFn,
57) -> Result<GeneratedKernel, LowerError> {
58    if let Some(e) = declined_by_name_with(dialect.subgroup_collectives(), dialect.name(), kernel) {
59        return Err(e);
60    }
61    // Packed values (vectors) become scalars first: `lower_dag` has no seam for them.
62    let scalarized = scalarize(kernel)?;
63    let kernel = scalarized.as_ref().unwrap_or(kernel);
64    let sc = scope::check(dialect, kernel)?;
65    // The body first: a rounding conversion in it adds a declaration to the prologue.
66    let mut body = String::new();
67    stmt::emit_body(dialect, kernel, &sc, &mut body)?;
68    let mut out = dialect.prologue_for(&sc.layout(kernel), stmt::uses_collective(&kernel.body));
69    out.push_str(&body);
70    out.push_str(dialect.epilogue());
71    Ok(GeneratedKernel::new(kernel.name.clone(), out))
72}