Skip to main content

vulkane_kernel_text/
decline.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Statements this emitter refuses **by name** because lowering them to a hardware operation on non-NVIDIA
3//! hardware risks infringing NVIDIA's patents. Twelve names: the four of board 434 C plus `CollectiveTuple` (CireSnave 2026-11-13) and the
4//! seven of its extension (CireSnave 2026-11-16).
5//!
6//! - Board 434 C, *"emitters may lower AsyncWait/CopyAsync/AsyncCommit/Collective for NVIDIA devices ... and decline by
7//!   name for non-NVIDIA devices"* (CireSnave 2026-11-13, "434: C."): `CopyAsync`, `AsyncCommit`, `AsyncWait` (NVIDIA
8//!   US11294713B2 and US12118382B2, an API that blocks a thread's instructions around an asynchronous shared-memory
9//!   copy; `unpopped` `docs/PATENT-SWEEP.md` hit 22) and `Collective`, and `CollectiveTuple`. `CollectiveTuple` is the tuple form of `Collective`: 434 C read as covering it; not named in the
10//!   ruling's list (pm-kernels 2026-11-17, PM no objection; rockfish, bonefish and fishstringer decline the family).
11//!
12//! CireSnave 2026-11-16, verbatim: *"For that patent, we add a note about the potential for infringement if
13//! non-Nvidia-specific emitters lower those operations to hardware ops and we ensure our projects' emitters for
14//! other manufacturers' hardware know about that and we consider ourselves not infringing."* and, for the second
15//! patent, *"I think we treat that patent like the others that are Nvidia-only. Add a note and specifically notify
16//! our non-Nvidia-emitter builders about the potential for infringement and not to."*
17//!
18//! Board 434 C extended (2026-11-16):
19//! - NVIDIA US11803380B2 (a synchronization barrier with an arrival counter, a phase and a hardware copy operator):
20//!   `MbarInit`, `MbarArrive`, `MbarCompleteTx`, `MbarWait`, `CopyBulk` (completes on an mbarrier) and
21//!   `Tcgen05Commit` (arrives on an mbarrier).
22//! - NVIDIA US12204897B2 and its continuation US12710961B2 (a wait on a count of pending matrix-multiply
23//!   operations): `WgmmaWait`.
24//!
25//! **Do not lower any of these to a hardware operation in this crate.** The walk below matches every [`Stmt`]
26//! variant explicitly and has no wildcard arm, so a new statement fails to compile here until someone decides which
27//! side of this list it is on.
28
29use unpopped::backend::LowerError;
30use unpopped::exec::Scope;
31use unpopped::ir::ReduceOp;
32use unpopped::kernel::{Block, CollectiveKind, KernelFn, Stmt};
33
34/// The reason text the four board 434 C refusals carry, so a caller and a test can recognise it.
35pub const DECLINED_434C: &str = "declined: possible exposure, board 434 C";
36/// The reason text the seven board 434 C extended refusals carry.
37pub const DECLINED_434C_EXT: &str = "declined: possible exposure, board 434 C extended";
38
39/// The name of the first declined statement in `block` and the ruling text it carries, looking inside `If`, `For`
40/// and `While` at any depth.
41fn find(block: &Block, lift: bool) -> Option<Declined> {
42    block.0.iter().find_map(|s| find_stmt(s, lift))
43}
44
45/// A declined statement's name and the ruling text its refusal carries.
46type Declined = (&'static str, &'static str);
47
48fn find_stmt(stmt: &Stmt, lift: bool) -> Option<Declined> {
49    declined_434c(stmt, lift)
50        .or_else(|| declined_extension(stmt))
51        .or_else(|| nested(stmt, lift))
52}
53
54/// The five of board 434 C. The wildcard is fine here: exhaustiveness is enforced by [`nested`].
55fn declined_434c(stmt: &Stmt, lift: bool) -> Option<Declined> {
56    // Board 506 "A" (narrowed): a dialect that lowers subgroup collectives leaves the RULED kinds alone; every other `Collective`
57    // (a scan, a user combine, a block scope) keeps the 434 C marker, and so does every `CollectiveTuple`.
58    if let Stmt::Collective { kind, scope, .. } = stmt {
59        if lift && collective_is_ruled(kind, *scope) {
60            return None;
61        }
62    }
63    let name = match stmt {
64        Stmt::CopyAsync { .. } => "CopyAsync",
65        Stmt::AsyncCommit => "AsyncCommit",
66        Stmt::AsyncWait { .. } => "AsyncWait",
67        Stmt::Collective { .. } => "Collective",
68        Stmt::CollectiveTuple { .. } => "CollectiveTuple",
69        _ => return None,
70    };
71    Some((name, DECLINED_434C))
72}
73
74/// The seven of the 2026-11-16 extension, and `CopyUnpack` (unpopped 0.58: its own doc says the `CopyBulk` patent note applies and
75/// emitters for other manufacturers decline it).
76fn declined_extension(stmt: &Stmt) -> Option<Declined> {
77    let name = match stmt {
78        Stmt::MbarInit { .. } => "MbarInit",
79        Stmt::MbarArrive { .. } => "MbarArrive",
80        Stmt::MbarCompleteTx { .. } => "MbarCompleteTx",
81        Stmt::MbarWait { .. } => "MbarWait",
82        Stmt::CopyBulk { .. } => "CopyBulk",
83        Stmt::Tcgen05Commit { .. } => "Tcgen05Commit",
84        Stmt::WgmmaWait { .. } => "WgmmaWait",
85        Stmt::CopyUnpack { .. } => "CopyUnpack",
86        _ => return None,
87    };
88    Some((name, DECLINED_434C_EXT))
89}
90
91/// Look inside `If`, `For` and `While`. **This match has no wildcard arm on purpose**: it names every [`Stmt`] variant,
92/// so a new statement in `unpopped` fails to compile here until someone decides which side of the declined list it is
93/// on. The declined ones are listed (and answered `None`: they were already found by the two functions above).
94fn nested(stmt: &Stmt, lift: bool) -> Option<Declined> {
95    match stmt {
96        Stmt::If { then, els, .. } => find(then, lift).or_else(|| find(els, lift)),
97        Stmt::For { body, .. } | Stmt::While { body, .. } => find(body, lift),
98        // The declined statements (found above, and none holds a block).
99        Stmt::CopyAsync { .. }
100        | Stmt::AsyncCommit
101        | Stmt::AsyncWait { .. }
102        | Stmt::Collective { .. }
103        | Stmt::CollectiveTuple { .. }
104        | Stmt::MbarInit { .. }
105        | Stmt::MbarArrive { .. }
106        | Stmt::MbarCompleteTx { .. }
107        | Stmt::MbarWait { .. }
108        | Stmt::CopyBulk { .. }
109        | Stmt::Tcgen05Commit { .. }
110        | Stmt::WgmmaWait { .. }
111        | Stmt::CopyUnpack { .. }
112        // Every other statement is not declined by name here.
113        | Stmt::Let { .. }
114        | Stmt::Load { .. }
115        | Stmt::Assign { .. }
116        | Stmt::Store { .. }
117        | Stmt::MaskedLoad { .. }
118        | Stmt::MaskedStore { .. }
119        | Stmt::Break
120        | Stmt::Continue
121        | Stmt::Return
122        | Stmt::Barrier { .. }
123        | Stmt::AtomicRmw { .. }
124        | Stmt::AtomicCas { .. }
125        | Stmt::MulticastLoadReduce { .. }
126        | Stmt::GridDepWait
127        | Stmt::GridDepLaunchDependents
128        | Stmt::ProxyFence { .. }
129        | Stmt::Fence { .. }
130        | Stmt::Mma { .. }
131        | Stmt::MatrixLoad { .. }
132        | Stmt::MatrixStore { .. }
133        | Stmt::Wgmma { .. }
134        | Stmt::WgmmaCommit
135        | Stmt::Tcgen05Mma { .. }
136        | Stmt::Tcgen05Ld { .. }
137        | Stmt::Tcgen05Alloc { .. }
138        | Stmt::Tcgen05Dealloc { .. }
139        | Stmt::Tcgen05RelinquishPermit
140        | Stmt::Call { .. }
141        | Stmt::CallTuple { .. }
142        | Stmt::ExternCall { .. } => None,
143    }
144}
145
146/// Whether a `Collective` of `kind` over `scope` is in the set CireSnave ruled liftable on non-NVIDIA targets (board 506 "A", narrowed,
147/// 2026-11-26): subgroup-scope **reductions** (sum, product, max, min, and, or, xor), **shuffles**, **votes** and **broadcasts**.
148/// Everything else stays declined by name with the 434 C marker: scans (inclusive or exclusive, any op), user combines
149/// (`ReduceWith`, `ScanWith`), `CollectiveTuple`, and every `Block`-scope collective.
150///
151/// Lifted by CireSnave 2026-11-26 (board 506 A) as his risk call: "outside as read" is a reading of public documents, not a clearance;
152/// no attorney; findings, not advice.
153#[must_use]
154pub fn collective_is_ruled(kind: &CollectiveKind, scope: Scope) -> bool {
155    if scope != Scope::Subgroup {
156        return false;
157    }
158    match kind {
159        CollectiveKind::Reduce(op) => matches!(
160            op,
161            ReduceOp::Sum
162                | ReduceOp::Prod
163                | ReduceOp::Max
164                | ReduceOp::Min
165                | ReduceOp::And
166                | ReduceOp::Or
167                | ReduceOp::Xor
168        ),
169        CollectiveKind::Shuffle { .. }
170        | CollectiveKind::Vote(_)
171        | CollectiveKind::Broadcast { .. } => true,
172        _ => false,
173    }
174}
175
176/// The by-name refusal of the first declined statement in `kernel` (at any depth), or `None`.
177///
178/// `backend` is the name used in the text (`glsl`, `wgsl`). Every `Collective` is declined; see [`declined_by_name_with`] for a
179/// target that lowers the ruled subgroup collectives.
180#[must_use]
181pub fn declined_by_name(backend: &str, kernel: &KernelFn) -> Option<LowerError> {
182    declined_by_name_with(false, backend, kernel)
183}
184
185/// As [`declined_by_name`], for a target that **lowers the ruled subgroup collectives** (`lift_subgroup`: see
186/// [`collective_is_ruled`]); the other collectives, and every other declined statement, are still refused by name.
187#[must_use]
188pub fn declined_by_name_with(
189    lift_subgroup: bool,
190    backend: &str,
191    kernel: &KernelFn,
192) -> Option<LowerError> {
193    let (name, ruling) = find(&kernel.body, lift_subgroup)?;
194    Some(LowerError::UnsupportedKernel {
195        detail: format!(
196            "backend `{backend}` refuses `Stmt::{name}` by name ({ruling}); it is not lowered to a hardware operation on non-NVIDIA hardware (kernel `{}`)",
197            kernel.name
198        ),
199    })
200}