use unpopped::kernel::Stmt;
use crate::decline::collective_is_ruled;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Coverage {
Lowered,
Row(&'static str),
Patent,
Nvidia(&'static str),
}
#[must_use]
pub fn coverage_in(lift_subgroup: bool, stmt: &Stmt) -> Coverage {
if lift_subgroup {
if let Stmt::Collective { kind, scope, .. } = stmt {
if collective_is_ruled(kind, *scope) {
return Coverage::Lowered;
}
}
}
coverage(stmt)
}
#[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 { .. }
| Stmt::Assume { .. }
| Stmt::ScheduleHint { .. } => 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::TileLoad { .. }
| Stmt::TileMma { .. }
| Stmt::TileFill { .. }
| Stmt::TileStore { .. } => Coverage::Row(
"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)",
),
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 { .. }
| Stmt::LaunchGrid { .. } => 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 if matches!(stmt, Stmt::LaunchGrid { .. }) => Some(
"refused by name for possible patent exposure (board 539), see `declined_by_name`"
.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 the_tile_statements_wait_on_row_8j_and_say_the_patent_hold() {
use unpopped::ir::ScalarExpr;
let fill = Stmt::TileFill {
d: 0,
value: ScalarExpr::ConstInt(1, unpopped_vocab::ElementKind::U32),
};
assert!(
matches!(coverage(&fill), Coverage::Row(why) if why.contains("row 8j")),
"{fill:?}"
);
let n = not_lowered_note(&fill).expect("a note");
assert!(
n.contains("US12405770B2") && n.contains("no transpose option"),
"{n}"
);
}
#[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}");
}
}
#[cfg(test)]
mod collective_coverage_tests {
use super::*;
use unpopped::exec::Scope;
use unpopped::ir::{ReduceOp, ScalarExpr};
use unpopped::kernel::{CollectiveKind, ShuffleMode, VoteKind};
fn every_kind() -> Vec<CollectiveKind> {
let ops = [
ReduceOp::Sum,
ReduceOp::Mean,
ReduceOp::Max,
ReduceOp::Min,
ReduceOp::Prod,
ReduceOp::And,
ReduceOp::Or,
ReduceOp::Xor,
];
let mut kinds = Vec::new();
for mode in [
ShuffleMode::Idx,
ShuffleMode::Up,
ShuffleMode::Down,
ShuffleMode::Xor,
] {
kinds.push(CollectiveKind::Shuffle {
mode,
operand: ScalarExpr::ConstInt(1, unpopped_vocab::ElementKind::U32),
});
}
for v in [VoteKind::All, VoteKind::Any, VoteKind::Ballot] {
kinds.push(CollectiveKind::Vote(v));
}
kinds.push(CollectiveKind::MatchAny);
for op in ops {
kinds.push(CollectiveKind::Reduce(op));
for exclusive in [false, true] {
kinds.push(CollectiveKind::Scan { op, exclusive });
}
}
kinds.push(CollectiveKind::Broadcast {
lane: ScalarExpr::ConstInt(3, unpopped_vocab::ElementKind::U32),
});
kinds.push(CollectiveKind::ReduceWith { combine: 0 });
kinds.push(CollectiveKind::ScanWith { combine: 0 });
kinds
}
const SCOPES: [Scope; 7] = [
Scope::Thread,
Scope::Subgroup,
Scope::Warpgroup,
Scope::Block,
Scope::Cluster,
Scope::Device,
Scope::System,
];
fn collective(kind: &CollectiveKind, scope: Scope) -> Stmt {
Stmt::Collective {
kind: kind.clone(),
value: ScalarExpr::ConstInt(0, unpopped_vocab::ElementKind::U32),
result: 0,
scope,
}
}
#[test]
fn coverage_in_follows_collective_is_ruled_for_every_kind_and_scope() {
let kinds = every_kind();
let mut ruled = 0;
for kind in &kinds {
for scope in SCOPES {
let stmt = collective(kind, scope);
let want = if collective_is_ruled(kind, scope) {
ruled += 1;
Coverage::Lowered
} else {
Coverage::Patent
};
assert_eq!(
coverage_in(true, &stmt),
want,
"{kind:?} at {scope:?}, lifting"
);
assert_eq!(
coverage_in(false, &stmt),
Coverage::Patent,
"{kind:?} at {scope:?}, not lifting"
);
assert_eq!(
coverage(&stmt),
Coverage::Patent,
"{kind:?} at {scope:?}: the dialect-agnostic answer"
);
}
}
assert_eq!(
ruled,
15,
"ruled (kind, scope) pairs of {}",
kinds.len() * SCOPES.len()
);
}
#[test]
fn a_collective_tuple_stays_patent_for_every_emitter() {
let stmt = Stmt::CollectiveTuple {
kind: CollectiveKind::ReduceWith { combine: 0 },
values: vec![ScalarExpr::ConstInt(0, unpopped_vocab::ElementKind::U32)],
results: vec![0],
scope: Scope::Subgroup,
};
assert_eq!(coverage_in(true, &stmt), Coverage::Patent);
assert_eq!(coverage_in(false, &stmt), Coverage::Patent);
}
}