use unpopped::kernel::Stmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Coverage {
Lowered,
Row(&'static str),
Patent,
Nvidia(&'static str),
}
#[must_use]
pub fn coverage(stmt: &Stmt) -> Coverage {
match stmt {
Stmt::Let { .. }
| Stmt::Load { .. }
| Stmt::Assign { .. }
| Stmt::Store { .. }
| Stmt::MaskedLoad { .. }
| Stmt::MaskedStore { .. }
| Stmt::If { .. }
| Stmt::For { .. }
| Stmt::While { .. }
| Stmt::Break
| Stmt::Continue
| Stmt::Return
| Stmt::Barrier { .. }
| Stmt::Fence { .. }
| Stmt::AtomicRmw { .. }
| Stmt::AtomicCas { .. } => Coverage::Lowered,
Stmt::Call { .. } | Stmt::CallTuple { .. } | Stmt::ExternCall { .. } => {
Coverage::Row("ROADMAP row 8g: device function calls are not lowered yet")
}
Stmt::Mma { .. } | Stmt::MatrixLoad { .. } | Stmt::MatrixStore { .. } => Coverage::Row(
"ROADMAP row 8j: matrix multiply-accumulate waits on a device with the extension (SPIR-V, GLSL) and on Unpopped's tile statements (Metal)",
),
Stmt::CopyAsync { .. }
| Stmt::AsyncCommit
| Stmt::AsyncWait { .. }
| Stmt::Collective { .. }
| Stmt::CollectiveTuple { .. }
| Stmt::MbarInit { .. }
| Stmt::MbarArrive { .. }
| Stmt::MbarCompleteTx { .. }
| Stmt::MbarWait { .. }
| Stmt::CopyBulk { .. }
| Stmt::Tcgen05Commit { .. }
| Stmt::WgmmaWait { .. }
| Stmt::CopyUnpack { .. } => Coverage::Patent,
Stmt::Wgmma { .. } | Stmt::WgmmaCommit => Coverage::Nvidia(
"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",
),
Stmt::Tcgen05Mma { .. }
| Stmt::Tcgen05Ld { .. }
| Stmt::Tcgen05Alloc { .. }
| Stmt::Tcgen05Dealloc { .. }
| Stmt::Tcgen05RelinquishPermit => Coverage::Nvidia(
"a fifth-generation tensor-core operation on tensor memory (PTX `tcgen05`, Blackwell); no target language of this repository has tensor memory",
),
Stmt::ProxyFence { .. } => Coverage::Nvidia(
"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",
),
Stmt::GridDepWait | Stmt::GridDepLaunchDependents => Coverage::Nvidia(
"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",
),
Stmt::MulticastLoadReduce { .. } => Coverage::Nvidia(
"an NVLink multicast load-reduce (PTX `multimem`) over several devices' memory; no target language of this repository addresses another device's memory",
),
}
}
#[must_use]
pub fn not_lowered_note(stmt: &Stmt) -> Option<String> {
match coverage(stmt) {
Coverage::Lowered => None,
Coverage::Row(why) => Some(why.to_string()),
Coverage::Patent => Some(
"refused by name for possible patent exposure (board 434 C), see `declined_by_name`"
.to_string(),
),
Coverage::Nvidia(why) => Some(format!("NVIDIA-specific: {why}")),
}
}
#[cfg(test)]
mod tests {
use super::*;
use unpopped::exec::Scope;
use unpopped::kernel::{BarrierKind, Stmt};
#[test]
fn the_cheap_to_build_variants_are_classified_as_documented() {
assert_eq!(coverage(&Stmt::Break), Coverage::Lowered);
assert_eq!(
coverage(&Stmt::Barrier {
scope: Scope::Block,
kind: BarrierKind::Full
}),
Coverage::Lowered
);
assert_eq!(coverage(&Stmt::AsyncCommit), Coverage::Patent);
for s in [
Stmt::WgmmaCommit,
Stmt::GridDepWait,
Stmt::GridDepLaunchDependents,
] {
assert!(
matches!(coverage(&s), Coverage::Nvidia(why) if !why.is_empty()),
"{s:?}"
);
}
}
#[test]
fn a_lowered_statement_has_no_note_and_the_others_say_why() {
assert_eq!(not_lowered_note(&Stmt::Return), None);
let n = not_lowered_note(&Stmt::GridDepWait).expect("a note");
assert!(
n.contains("NVIDIA-specific") && n.contains("dependent launch"),
"{n}"
);
let p = not_lowered_note(&Stmt::AsyncCommit).expect("a note");
assert!(p.contains("board 434 C"), "{p}");
}
}