use unpopped::backend::LowerError;
use unpopped::exec::Scope;
use unpopped::ir::ReduceOp;
use unpopped::kernel::{Block, CollectiveKind, KernelFn, Stmt};
pub const DECLINED_434C: &str = "declined: possible exposure, board 434 C";
pub const DECLINED_434C_EXT: &str = "declined: possible exposure, board 434 C extended";
fn find(block: &Block, lift: bool) -> Option<Declined> {
block.0.iter().find_map(|s| find_stmt(s, lift))
}
type Declined = (&'static str, &'static str);
fn find_stmt(stmt: &Stmt, lift: bool) -> Option<Declined> {
declined_434c(stmt, lift)
.or_else(|| declined_extension(stmt))
.or_else(|| nested(stmt, lift))
}
fn declined_434c(stmt: &Stmt, lift: bool) -> Option<Declined> {
if let Stmt::Collective { kind, scope, .. } = stmt {
if lift && collective_is_ruled(kind, *scope) {
return None;
}
}
let name = match stmt {
Stmt::CopyAsync { .. } => "CopyAsync",
Stmt::AsyncCommit => "AsyncCommit",
Stmt::AsyncWait { .. } => "AsyncWait",
Stmt::Collective { .. } => "Collective",
Stmt::CollectiveTuple { .. } => "CollectiveTuple",
_ => return None,
};
Some((name, DECLINED_434C))
}
fn declined_extension(stmt: &Stmt) -> Option<Declined> {
let name = match stmt {
Stmt::MbarInit { .. } => "MbarInit",
Stmt::MbarArrive { .. } => "MbarArrive",
Stmt::MbarCompleteTx { .. } => "MbarCompleteTx",
Stmt::MbarWait { .. } => "MbarWait",
Stmt::CopyBulk { .. } => "CopyBulk",
Stmt::Tcgen05Commit { .. } => "Tcgen05Commit",
Stmt::WgmmaWait { .. } => "WgmmaWait",
Stmt::CopyUnpack { .. } => "CopyUnpack",
_ => return None,
};
Some((name, DECLINED_434C_EXT))
}
fn nested(stmt: &Stmt, lift: bool) -> Option<Declined> {
match stmt {
Stmt::If { then, els, .. } => find(then, lift).or_else(|| find(els, lift)),
Stmt::For { body, .. } | Stmt::While { body, .. } => find(body, lift),
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::Let { .. }
| Stmt::Load { .. }
| Stmt::Assign { .. }
| Stmt::Store { .. }
| Stmt::MaskedLoad { .. }
| Stmt::MaskedStore { .. }
| Stmt::Break
| Stmt::Continue
| Stmt::Return
| Stmt::Barrier { .. }
| Stmt::AtomicRmw { .. }
| Stmt::AtomicCas { .. }
| Stmt::MulticastLoadReduce { .. }
| Stmt::GridDepWait
| Stmt::GridDepLaunchDependents
| Stmt::ProxyFence { .. }
| Stmt::Fence { .. }
| Stmt::Mma { .. }
| Stmt::TileLoad { .. }
| Stmt::TileMma { .. }
| Stmt::TileFill { .. }
| Stmt::TileStore { .. }
| Stmt::MatrixLoad { .. }
| Stmt::MatrixStore { .. }
| Stmt::Wgmma { .. }
| Stmt::WgmmaCommit
| Stmt::Tcgen05Mma { .. }
| Stmt::Tcgen05Ld { .. }
| Stmt::Tcgen05Alloc { .. }
| Stmt::Tcgen05Dealloc { .. }
| Stmt::Tcgen05RelinquishPermit
| Stmt::Call { .. }
| Stmt::CallTuple { .. }
| Stmt::ExternCall { .. } => None,
}
}
#[must_use]
pub fn collective_is_ruled(kind: &CollectiveKind, scope: Scope) -> bool {
if scope != Scope::Subgroup {
return false;
}
match kind {
CollectiveKind::Reduce(op) => matches!(
op,
ReduceOp::Sum
| ReduceOp::Prod
| ReduceOp::Max
| ReduceOp::Min
| ReduceOp::And
| ReduceOp::Or
| ReduceOp::Xor
),
CollectiveKind::Shuffle { .. }
| CollectiveKind::Vote(_)
| CollectiveKind::Broadcast { .. } => true,
_ => false,
}
}
#[must_use]
pub fn declined_by_name(backend: &str, kernel: &KernelFn) -> Option<LowerError> {
declined_by_name_with(false, backend, kernel)
}
#[must_use]
pub fn declined_by_name_with(
lift_subgroup: bool,
backend: &str,
kernel: &KernelFn,
) -> Option<LowerError> {
let (name, ruling) = find(&kernel.body, lift_subgroup)?;
Some(LowerError::UnsupportedKernel {
detail: format!(
"backend `{backend}` refuses `Stmt::{name}` by name ({ruling}); it is not lowered to a hardware operation on non-NVIDIA hardware (kernel `{}`)",
kernel.name
),
})
}