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::CopyAsync { .. }
47        | Stmt::AsyncCommit
48        | Stmt::AsyncWait { .. }
49        | Stmt::Collective { .. }
50        | Stmt::CollectiveTuple { .. }
51        | Stmt::MbarInit { .. }
52        | Stmt::MbarArrive { .. }
53        | Stmt::MbarCompleteTx { .. }
54        | Stmt::MbarWait { .. }
55        | Stmt::CopyBulk { .. }
56        | Stmt::Tcgen05Commit { .. }
57        | Stmt::WgmmaWait { .. }
58        | Stmt::CopyUnpack { .. } => Coverage::Patent,
59        Stmt::Wgmma { .. } | Stmt::WgmmaCommit => Coverage::Nvidia(
60            "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",
61        ),
62        Stmt::Tcgen05Mma { .. }
63        | Stmt::Tcgen05Ld { .. }
64        | Stmt::Tcgen05Alloc { .. }
65        | Stmt::Tcgen05Dealloc { .. }
66        | Stmt::Tcgen05RelinquishPermit => Coverage::Nvidia(
67            "a fifth-generation tensor-core operation on tensor memory (PTX `tcgen05`, Blackwell); no target language of this repository has tensor memory",
68        ),
69        Stmt::ProxyFence { .. } => Coverage::Nvidia(
70            "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",
71        ),
72        Stmt::GridDepWait | Stmt::GridDepLaunchDependents => Coverage::Nvidia(
73            "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",
74        ),
75        Stmt::MulticastLoadReduce { .. } => Coverage::Nvidia(
76            "an NVLink multicast load-reduce (PTX `multimem`) over several devices' memory; no target language of this repository addresses another device's memory",
77        ),
78    }
79}
80
81/// The sentence an emitter adds to its "not lowered" refusal of `stmt`: the row it waits for, the by-name ruling or
82/// the NVIDIA-specific reason, or `None` for a statement the emitters lower (a refusal of one is about its operands).
83#[must_use]
84pub fn not_lowered_note(stmt: &Stmt) -> Option<String> {
85    match coverage(stmt) {
86        Coverage::Lowered => None,
87        Coverage::Row(why) => Some(why.to_string()),
88        Coverage::Patent => Some(
89            "refused by name for possible patent exposure (board 434 C), see `declined_by_name`"
90                .to_string(),
91        ),
92        Coverage::Nvidia(why) => Some(format!("NVIDIA-specific: {why}")),
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use unpopped::exec::Scope;
100    use unpopped::kernel::{BarrierKind, Stmt};
101
102    #[test]
103    fn the_cheap_to_build_variants_are_classified_as_documented() {
104        assert_eq!(coverage(&Stmt::Break), Coverage::Lowered);
105        assert_eq!(
106            coverage(&Stmt::Barrier {
107                scope: Scope::Block,
108                kind: BarrierKind::Full
109            }),
110            Coverage::Lowered
111        );
112        assert_eq!(coverage(&Stmt::AsyncCommit), Coverage::Patent);
113        for s in [
114            Stmt::WgmmaCommit,
115            Stmt::GridDepWait,
116            Stmt::GridDepLaunchDependents,
117        ] {
118            assert!(
119                matches!(coverage(&s), Coverage::Nvidia(why) if !why.is_empty()),
120                "{s:?}"
121            );
122        }
123    }
124
125    #[test]
126    fn a_lowered_statement_has_no_note_and_the_others_say_why() {
127        assert_eq!(not_lowered_note(&Stmt::Return), None);
128        let n = not_lowered_note(&Stmt::GridDepWait).expect("a note");
129        assert!(
130            n.contains("NVIDIA-specific") && n.contains("dependent launch"),
131            "{n}"
132        );
133        let p = not_lowered_note(&Stmt::AsyncCommit).expect("a note");
134        assert!(p.contains("board 434 C"), "{p}");
135    }
136}