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