Skip to main content

vulkane_kernel_text/
coverage.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Where every [`Stmt`] variant stands in this repository's emitters (ROADMAP row 9f).
3
4use unpopped::kernel::Stmt;
5
6/// What the emitters of this repository do with a statement.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum Coverage {
9    /// Lowered by the shared walker and by the SPIR-V emitter.
10    Lowered,
11    /// Not lowered yet, and a ROADMAP row says what it waits for (the text).
12    Row(&'static str),
13    /// Refused by name for possible patent exposure (board 434 C, and its extension): see `declined_by_name`.
14    Patent,
15    /// An NVIDIA-specific operation with no spelling in the target languages of this repository; the reason.
16    Nvidia(&'static str),
17}
18
19/// The coverage of `stmt`. **This match has no wildcard arm on purpose**: it names every [`Stmt`] variant, so a new
20/// statement in `unpopped` fails to compile here until someone decides which of the four it is.
21#[must_use]
22pub fn coverage(stmt: &Stmt) -> Coverage {
23    match stmt {
24        Stmt::Let { .. }
25        | Stmt::Load { .. }
26        | Stmt::Assign { .. }
27        | Stmt::Store { .. }
28        | Stmt::MaskedLoad { .. }
29        | Stmt::MaskedStore { .. }
30        | Stmt::If { .. }
31        | Stmt::For { .. }
32        | Stmt::While { .. }
33        | Stmt::Break
34        | Stmt::Continue
35        | Stmt::Return
36        | Stmt::Barrier { .. }
37        | Stmt::Fence { .. }
38        | Stmt::AtomicRmw { .. }
39        | Stmt::AtomicCas { .. } => Coverage::Lowered,
40        Stmt::Call { .. } | Stmt::CallTuple { .. } | Stmt::ExternCall { .. } => {
41            Coverage::Row("ROADMAP row 8g: device function calls are not lowered yet")
42        }
43        Stmt::Mma { .. } | Stmt::MatrixLoad { .. } | Stmt::MatrixStore { .. } => Coverage::Row(
44            "ROADMAP row 8j: matrix multiply-accumulate waits on a device with the extension (SPIR-V, GLSL) and on Unpopped's tile statements (Metal)",
45        ),
46        Stmt::TileLoad { .. }
47        | Stmt::TileMma { .. }
48        | Stmt::TileFill { .. }
49        | Stmt::TileStore { .. } => Coverage::Row(
50            "ROADMAP row 8j: the tile statements (unpopped 0.62) are declined by name until a device with the cooperative-matrix extension (SPIR-V, GLSL) or simdgroup matrices (Metal) can run them; no saturating variant and no transpose option (US12405770B2 hold)",
51        ),
52        Stmt::CopyAsync { .. }
53        | Stmt::AsyncCommit
54        | Stmt::AsyncWait { .. }
55        | Stmt::Collective { .. }
56        | Stmt::CollectiveTuple { .. }
57        | Stmt::MbarInit { .. }
58        | Stmt::MbarArrive { .. }
59        | Stmt::MbarCompleteTx { .. }
60        | Stmt::MbarWait { .. }
61        | Stmt::CopyBulk { .. }
62        | Stmt::Tcgen05Commit { .. }
63        | Stmt::WgmmaWait { .. }
64        | Stmt::CopyUnpack { .. } => Coverage::Patent,
65        Stmt::Wgmma { .. } | Stmt::WgmmaCommit => Coverage::Nvidia(
66            "a warpgroup-level asynchronous matrix multiply-accumulate (PTX `wgmma`, Hopper); none of SPIR-V, GLSL, HLSL, WGSL or MSL has a construct with its asynchronous warpgroup semantics",
67        ),
68        Stmt::Tcgen05Mma { .. }
69        | Stmt::Tcgen05Ld { .. }
70        | Stmt::Tcgen05Alloc { .. }
71        | Stmt::Tcgen05Dealloc { .. }
72        | Stmt::Tcgen05RelinquishPermit => Coverage::Nvidia(
73            "a fifth-generation tensor-core operation on tensor memory (PTX `tcgen05`, Blackwell); no target language of this repository has tensor memory",
74        ),
75        Stmt::ProxyFence { .. } => Coverage::Nvidia(
76            "an async-proxy fence (PTX `fence.proxy.async`, as unpopped documents `ProxyFence`): it orders generic-proxy writes against the asynchronous proxy that `CopyBulk` and `wgmma` use; no target language of this repository has memory proxies",
77        ),
78        Stmt::GridDepWait | Stmt::GridDepLaunchDependents => Coverage::Nvidia(
79            "CUDA programmatic dependent launch (a kernel waiting on, or releasing, the next kernel of a stream); a Vulkan dispatch has no in-kernel dependency on another dispatch",
80        ),
81        Stmt::MulticastLoadReduce { .. } => Coverage::Nvidia(
82            "an NVLink multicast load-reduce (PTX `multimem`) over several devices' memory; no target language of this repository addresses another device's memory",
83        ),
84    }
85}
86
87/// The sentence an emitter adds to its "not lowered" refusal of `stmt`: the row it waits for, the by-name ruling or
88/// the NVIDIA-specific reason, or `None` for a statement the emitters lower (a refusal of one is about its operands).
89#[must_use]
90pub fn not_lowered_note(stmt: &Stmt) -> Option<String> {
91    match coverage(stmt) {
92        Coverage::Lowered => None,
93        Coverage::Row(why) => Some(why.to_string()),
94        Coverage::Patent => Some(
95            "refused by name for possible patent exposure (board 434 C), see `declined_by_name`"
96                .to_string(),
97        ),
98        Coverage::Nvidia(why) => Some(format!("NVIDIA-specific: {why}")),
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use unpopped::exec::Scope;
106    use unpopped::kernel::{BarrierKind, Stmt};
107
108    #[test]
109    fn the_cheap_to_build_variants_are_classified_as_documented() {
110        assert_eq!(coverage(&Stmt::Break), Coverage::Lowered);
111        assert_eq!(
112            coverage(&Stmt::Barrier {
113                scope: Scope::Block,
114                kind: BarrierKind::Full
115            }),
116            Coverage::Lowered
117        );
118        assert_eq!(coverage(&Stmt::AsyncCommit), Coverage::Patent);
119        for s in [
120            Stmt::WgmmaCommit,
121            Stmt::GridDepWait,
122            Stmt::GridDepLaunchDependents,
123        ] {
124            assert!(
125                matches!(coverage(&s), Coverage::Nvidia(why) if !why.is_empty()),
126                "{s:?}"
127            );
128        }
129    }
130
131    #[test]
132    fn the_tile_statements_wait_on_row_8j_and_say_the_patent_hold() {
133        use unpopped::ir::ScalarExpr;
134        let fill = Stmt::TileFill {
135            d: 0,
136            value: ScalarExpr::ConstInt(1, unpopped_vocab::ElementKind::U32),
137        };
138        assert!(
139            matches!(coverage(&fill), Coverage::Row(why) if why.contains("row 8j")),
140            "{fill:?}"
141        );
142        let n = not_lowered_note(&fill).expect("a note");
143        assert!(
144            n.contains("US12405770B2") && n.contains("no transpose option"),
145            "{n}"
146        );
147    }
148
149    #[test]
150    fn a_lowered_statement_has_no_note_and_the_others_say_why() {
151        assert_eq!(not_lowered_note(&Stmt::Return), None);
152        let n = not_lowered_note(&Stmt::GridDepWait).expect("a note");
153        assert!(
154            n.contains("NVIDIA-specific") && n.contains("dependent launch"),
155            "{n}"
156        );
157        let p = not_lowered_note(&Stmt::AsyncCommit).expect("a note");
158        assert!(p.contains("board 434 C"), "{p}");
159    }
160}