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 (`Assume` by omission: it emits nothing).
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 { .. }
40        // Lowered BY OMISSION (unpopped 0.64, U50): a value fact that reads and writes nothing; no emitted text.
41        | Stmt::Assume { .. } => Coverage::Lowered,
42        Stmt::Call { .. } | Stmt::CallTuple { .. } | Stmt::ExternCall { .. } => {
43            Coverage::Row("ROADMAP row 8g: device function calls are not lowered yet")
44        }
45        Stmt::Mma { .. } | Stmt::MatrixLoad { .. } | Stmt::MatrixStore { .. } => Coverage::Row(
46            "ROADMAP row 8j: matrix multiply-accumulate waits on a device with the extension (SPIR-V, GLSL) and on Unpopped's tile statements (Metal)",
47        ),
48        Stmt::TileLoad { .. }
49        | Stmt::TileMma { .. }
50        | Stmt::TileFill { .. }
51        | Stmt::TileStore { .. } => Coverage::Row(
52            "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)",
53        ),
54        Stmt::CopyAsync { .. }
55        | Stmt::AsyncCommit
56        | Stmt::AsyncWait { .. }
57        | Stmt::Collective { .. }
58        | Stmt::CollectiveTuple { .. }
59        | Stmt::MbarInit { .. }
60        | Stmt::MbarArrive { .. }
61        | Stmt::MbarCompleteTx { .. }
62        | Stmt::MbarWait { .. }
63        | Stmt::CopyBulk { .. }
64        | Stmt::Tcgen05Commit { .. }
65        | Stmt::WgmmaWait { .. }
66        | Stmt::CopyUnpack { .. } => Coverage::Patent,
67        Stmt::Wgmma { .. } | Stmt::WgmmaCommit => Coverage::Nvidia(
68            "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",
69        ),
70        Stmt::Tcgen05Mma { .. }
71        | Stmt::Tcgen05Ld { .. }
72        | Stmt::Tcgen05Alloc { .. }
73        | Stmt::Tcgen05Dealloc { .. }
74        | Stmt::Tcgen05RelinquishPermit => Coverage::Nvidia(
75            "a fifth-generation tensor-core operation on tensor memory (PTX `tcgen05`, Blackwell); no target language of this repository has tensor memory",
76        ),
77        Stmt::ProxyFence { .. } => Coverage::Nvidia(
78            "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",
79        ),
80        Stmt::GridDepWait | Stmt::GridDepLaunchDependents => Coverage::Nvidia(
81            "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",
82        ),
83        Stmt::MulticastLoadReduce { .. } => Coverage::Nvidia(
84            "an NVLink multicast load-reduce (PTX `multimem`) over several devices' memory; no target language of this repository addresses another device's memory",
85        ),
86    }
87}
88
89/// The sentence an emitter adds to its "not lowered" refusal of `stmt`: the row it waits for, the by-name ruling or
90/// the NVIDIA-specific reason, or `None` for a statement the emitters lower (a refusal of one is about its operands).
91#[must_use]
92pub fn not_lowered_note(stmt: &Stmt) -> Option<String> {
93    match coverage(stmt) {
94        Coverage::Lowered => None,
95        Coverage::Row(why) => Some(why.to_string()),
96        Coverage::Patent => Some(
97            "refused by name for possible patent exposure (board 434 C), see `declined_by_name`"
98                .to_string(),
99        ),
100        Coverage::Nvidia(why) => Some(format!("NVIDIA-specific: {why}")),
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use unpopped::exec::Scope;
108    use unpopped::kernel::{BarrierKind, Stmt};
109
110    #[test]
111    fn the_cheap_to_build_variants_are_classified_as_documented() {
112        assert_eq!(coverage(&Stmt::Break), Coverage::Lowered);
113        assert_eq!(
114            coverage(&Stmt::Barrier {
115                scope: Scope::Block,
116                kind: BarrierKind::Full
117            }),
118            Coverage::Lowered
119        );
120        assert_eq!(coverage(&Stmt::AsyncCommit), Coverage::Patent);
121        for s in [
122            Stmt::WgmmaCommit,
123            Stmt::GridDepWait,
124            Stmt::GridDepLaunchDependents,
125        ] {
126            assert!(
127                matches!(coverage(&s), Coverage::Nvidia(why) if !why.is_empty()),
128                "{s:?}"
129            );
130        }
131    }
132
133    #[test]
134    fn the_tile_statements_wait_on_row_8j_and_say_the_patent_hold() {
135        use unpopped::ir::ScalarExpr;
136        let fill = Stmt::TileFill {
137            d: 0,
138            value: ScalarExpr::ConstInt(1, unpopped_vocab::ElementKind::U32),
139        };
140        assert!(
141            matches!(coverage(&fill), Coverage::Row(why) if why.contains("row 8j")),
142            "{fill:?}"
143        );
144        let n = not_lowered_note(&fill).expect("a note");
145        assert!(
146            n.contains("US12405770B2") && n.contains("no transpose option"),
147            "{n}"
148        );
149    }
150
151    #[test]
152    fn a_lowered_statement_has_no_note_and_the_others_say_why() {
153        assert_eq!(not_lowered_note(&Stmt::Return), None);
154        let n = not_lowered_note(&Stmt::GridDepWait).expect("a note");
155        assert!(
156            n.contains("NVIDIA-specific") && n.contains("dependent launch"),
157            "{n}"
158        );
159        let p = not_lowered_note(&Stmt::AsyncCommit).expect("a note");
160        assert!(p.contains("board 434 C"), "{p}");
161    }
162}