use unpopped::backend::LowerError;
use unpopped::kernel::{Block, 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) -> Option<Declined> {
block.0.iter().find_map(find_stmt)
}
type Declined = (&'static str, &'static str);
fn find_stmt(stmt: &Stmt) -> Option<Declined> {
declined_434c(stmt)
.or_else(|| declined_extension(stmt))
.or_else(|| nested(stmt))
}
fn declined_434c(stmt: &Stmt) -> Option<Declined> {
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) -> Option<Declined> {
match stmt {
Stmt::If { then, els, .. } => find(then).or_else(|| find(els)),
Stmt::For { body, .. } | Stmt::While { body, .. } => find(body),
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::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 declined_by_name(backend: &str, kernel: &KernelFn) -> Option<LowerError> {
let (name, ruling) = find(&kernel.body)?;
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
),
})
}