use std::collections::BTreeSet;
use unpopped::backend::LowerError;
use unpopped::exec::{Extent, MemorySpace};
use unpopped::kernel::{BufferRole, KernelFn, ParamKind, Place, Stmt, check_kernel};
use unpopped_vocab::ElementKind;
use crate::dialect::{Decl, Dialect, Layout};
#[derive(Debug)]
pub(crate) struct Scope {
pub(crate) signed: bool,
pub(crate) narrow: bool,
pub(crate) float: bool,
pub(crate) block: [u32; 3],
pub(crate) grid: [u32; 3],
pub(crate) buffers: Vec<Decl>,
pub(crate) shared: Vec<Decl>,
pub(crate) subgroup: bool,
pub(crate) rte: std::cell::RefCell<BTreeSet<u32>>,
pub(crate) atomic: BTreeSet<String>,
pub(crate) backend: &'static str,
}
pub(crate) fn refuse(backend: &str, kernel: &KernelFn, what: impl std::fmt::Display) -> LowerError {
LowerError::UnsupportedKernel {
detail: format!("{backend} backend: {what} (kernel `{}`)", kernel.name),
}
}
impl Scope {
pub(crate) fn layout(&self, kernel: &KernelFn) -> Layout {
Layout {
name: kernel.name.clone(),
signed: self.signed,
block: self.block,
buffers: self.buffers.clone(),
shared: self.shared.clone(),
spec_locals: self
.buffers
.iter()
.filter_map(|b| b.spec_local.map(|l| (l, b.name.clone())))
.collect(),
locals: kernel.locals.len(),
local_elems: kernel.locals.iter().map(|l| l.elem).collect(),
rte: self.rte.borrow().iter().copied().collect(),
subgroup: self.subgroup,
}
}
}
fn dtype(d: &dyn Dialect, kernel: &KernelFn) -> Result<(bool, bool), LowerError> {
match kernel.compute {
ElementKind::I32 => Ok((true, false)),
ElementKind::U32 => Ok((false, false)),
ElementKind::I64 if d.wide() => Ok((true, false)),
ElementKind::U64 if d.wide() => Ok((false, false)),
ElementKind::F32 if d.float() && !d.double() && !d.half() && !d.bfloat() => {
Ok((false, true))
}
ElementKind::F16 if d.float() && d.half() => Ok((false, true)),
ElementKind::Bf16 if d.float() && d.bfloat() => Ok((false, true)),
ElementKind::F64 if d.float() && d.double() => Ok((false, true)),
other => Err(refuse(
d.name(),
kernel,
if let Some(why) = d.float_refusal().filter(|_| {
matches!(
other,
ElementKind::F16 | ElementKind::Bf16 | ElementKind::F32 | ElementKind::F64
)
}) {
format!("a {other:?} kernel is not lowered: {why}")
} else if matches!(other, ElementKind::I64 | ElementKind::U64) {
format!(
"v1 lowers integer kernels only (I32/U32 here: this dialect has no 64-bit integer spelling); compute dtype is {other:?}"
)
} else {
format!(
"v1 lowers integer kernels only (and F32/F64 where the dialect spells it); compute dtype is {other:?}"
)
},
)),
}
}
fn buffer_elem_ok(d: &dyn Dialect, kernel: &KernelFn, elem: ElementKind) -> bool {
let narrow_int = d.narrow_ints()
&& matches!(kernel.compute, ElementKind::I32 | ElementKind::U32)
&& matches!(
elem,
ElementKind::I8 | ElementKind::U8 | ElementKind::I16 | ElementKind::U16
);
let other_float = matches!(kernel.compute, ElementKind::F32 | ElementKind::F64)
&& matches!(elem, ElementKind::F32 | ElementKind::F64);
elem == kernel.compute
|| narrow_int
|| (d.float()
&& matches!(
kernel.compute,
ElementKind::F32 | ElementKind::F64 | ElementKind::F16
)
&& (other_float || matches!(elem, ElementKind::I32 | ElementKind::U32)))
}
fn narrow_reason(d: &dyn Dialect, elem: ElementKind) -> Option<String> {
if !matches!(
elem,
ElementKind::I8 | ElementKind::U8 | ElementKind::I16 | ElementKind::U16
) || d.narrow_ints()
{
return None;
}
d.narrow_refusal()
.map(|why| format!("is a {elem:?}, an 8- or 16-bit integer: {why}"))
}
fn buffer(d: &dyn Dialect, kernel: &KernelFn, i: usize) -> Result<Decl, LowerError> {
let p = &kernel.params[i];
match &p.kind {
ParamKind::Buffer { elem, role, rank }
if buffer_elem_ok(d, kernel, *elem) && rank.is_none_or(|r| r == 1) =>
{
Ok(Decl {
name: format!("b{i}"),
binding: u32::try_from(i).expect("binding fits u32"),
role: *role,
elem: *elem,
len: 0,
private: false,
atomic: false,
spec_local: None,
len_spec: None,
})
}
ParamKind::Scalar { ty, local }
if *ty == kernel.compute && sizes_array(kernel, *local) && !d.wide() && !d.float() =>
{
Ok(Decl {
name: format!("s{i}"),
binding: u32::try_from(i).expect("spec id fits u32"),
role: BufferRole::Read,
elem: kernel.compute,
len: 0,
private: false,
atomic: false,
spec_local: Some(*local),
len_spec: None,
})
}
_ => {
let narrow = match &p.kind {
ParamKind::Buffer { elem, .. } => narrow_reason(d, *elem),
_ => None,
};
Err(refuse(
d.name(),
kernel,
narrow.map_or_else(
|| {
format!(
"parameter `{}` is not a flat buffer of the compute dtype",
p.name
)
},
|why| format!("parameter `{}` {why}", p.name),
),
))
}
}
}
fn sizes_array(kernel: &KernelFn, local: u32) -> bool {
kernel
.allocs
.iter()
.any(|a| a.extent == Extent::Param(local))
}
fn spec_name(kernel: &KernelFn, local: u32) -> Option<String> {
kernel.params.iter().position(|p| {
matches!(&p.kind, ParamKind::Scalar { ty, local: l } if *l == local && *ty == kernel.compute)
}).map(|i| format!("s{i}"))
}
fn dynamic_shared(
d: &dyn Dialect,
kernel: &KernelFn,
i: usize,
local: u32,
) -> Result<Decl, LowerError> {
let Some(name) = spec_name(kernel, local) else {
return Err(refuse(
d.name(),
kernel,
format!(
"allocation `{}` has a dynamic extent from local {local}, which is no scalar parameter of the compute dtype",
kernel.allocs[i].name
),
));
};
Ok(Decl {
name: format!("a{i}"),
binding: 0,
role: BufferRole::ReadWrite,
elem: kernel.compute,
len: 0,
private: false,
atomic: false,
spec_local: None,
len_spec: Some(name),
})
}
fn shared(d: &dyn Dialect, kernel: &KernelFn, i: usize) -> Result<Decl, LowerError> {
let a = &kernel.allocs[i];
match (a.space, a.extent) {
(MemorySpace::Shared | MemorySpace::Private, Extent::Static(n))
if a.elem == kernel.compute =>
{
Ok(Decl {
name: format!("a{i}"),
binding: 0,
role: BufferRole::ReadWrite,
elem: kernel.compute,
len: n,
private: a.space == MemorySpace::Private,
atomic: false,
spec_local: None,
len_spec: None,
})
}
(MemorySpace::Shared, Extent::Param(l)) if a.elem == kernel.compute => {
dynamic_shared(d, kernel, i, l)
}
(MemorySpace::Constant, _) => Err(refuse(
d.name(),
kernel,
format!(
"allocation `{}` is in the Constant space, which an allocation cannot initialise (ROADMAP 8k: a constant table needs an initialiser the IR does not carry)",
a.name
),
)),
_ => Err(refuse(
d.name(),
kernel,
format!(
"allocation `{}` is not a static shared or private array of the compute dtype (a shared array may have a dynamic extent from a scalar parameter)",
a.name
),
)),
}
}
pub(crate) fn place_ok(place: &Place) -> bool {
match place {
Place::Logical { coords, .. } => coords.len() == 1,
Place::Alloc { .. } => true,
_ => false,
}
}
pub(crate) fn place_name(place: &Place) -> Option<String> {
match place {
Place::Logical { buffer, .. } => Some(format!("b{buffer}")),
Place::Alloc { alloc, .. } => Some(format!("a{alloc}")),
_ => None,
}
}
fn atomic_places(block: &unpopped::kernel::Block, into: &mut BTreeSet<String>) {
for s in &block.0 {
match s {
Stmt::AtomicRmw { place, .. } | Stmt::AtomicCas { place, .. } => {
into.extend(place_name(place));
}
Stmt::If { then, els, .. } => {
atomic_places(then, into);
atomic_places(els, into);
}
Stmt::For { body, .. } | Stmt::While { body, .. } => atomic_places(body, into),
_ => {}
}
}
}
fn uses_subgroup(block: &unpopped::kernel::Block) -> bool {
use unpopped::exec::Scope as S;
block.0.iter().any(|s| match s {
Stmt::Barrier { scope, .. } | Stmt::Fence { scope, .. } => *scope == S::Subgroup,
Stmt::If { then, els, .. } => uses_subgroup(then) || uses_subgroup(els),
Stmt::For { body, .. } | Stmt::While { body, .. } => uses_subgroup(body),
_ => false,
})
}
fn launch<'k>(
d: &dyn Dialect,
kernel: &'k KernelFn,
) -> Result<&'k unpopped::exec::LaunchShape, LowerError> {
let Some(launch) = kernel.launch.as_ref() else {
return Err(refuse(
d.name(),
kernel,
"a kernel without a launch shape has no workgroup size",
));
};
if launch.grid_dims.is_some() {
return Err(refuse(
d.name(),
kernel,
"a parameter-dependent grid is not lowered",
));
}
Ok(launch)
}
fn locals(d: &dyn Dialect, kernel: &KernelFn) -> Result<(), LowerError> {
match kernel
.locals
.iter()
.position(|l| l.lanes != 1 || !buffer_elem_ok(d, kernel, l.elem))
{
Some(i) => Err(refuse(
d.name(),
kernel,
narrow_reason(d, kernel.locals[i].elem).map_or_else(
|| format!("local {i} is not a scalar of the compute dtype"),
|why| format!("local {i} {why}"),
),
)),
None => Ok(()),
}
}
fn mixed_widths(d: &dyn Dialect, kernel: &KernelFn) -> Result<(), LowerError> {
if !d.float() || d.mixed_float_widths() {
return Ok(());
}
let elems = kernel
.params
.iter()
.filter_map(|p| match &p.kind {
ParamKind::Buffer { elem, .. } => Some(*elem),
_ => None,
})
.chain(kernel.locals.iter().map(|l| l.elem))
.chain([kernel.compute]);
let has = |t: ElementKind| elems.clone().any(|e| e == t);
if has(ElementKind::F32) && has(ElementKind::F64) {
return Err(refuse(
d.name(),
kernel,
"a kernel using both F32 and F64 is not lowered: its float controls would be declared at two widths, which this dialect cannot name",
));
}
Ok(())
}
fn subgroup_range(
d: &dyn Dialect,
kernel: &KernelFn,
launch: &unpopped::exec::LaunchShape,
) -> Result<(), LowerError> {
let Some(sg) = launch.subgroup else {
return Ok(());
};
if sg.min >= 1 && (sg.min..=sg.max.min(128)).any(u32::is_power_of_two) {
return Ok(());
}
Err(refuse(
d.name(),
kernel,
format!(
"the required subgroup size range {}..={} holds no power of two up to 128, so no Vulkan device can run it",
sg.min, sg.max
),
))
}
pub(crate) fn check(d: &dyn Dialect, kernel: &KernelFn) -> Result<Scope, LowerError> {
check_kernel(kernel).map_err(|e| LowerError::UnsupportedKernel {
detail: format!("check_kernel refused kernel `{}`: {e:?}", kernel.name),
})?;
let (signed, float) = dtype(d, kernel)?;
let launch = launch(d, kernel)?;
locals(d, kernel)?;
subgroup_range(d, kernel, launch)?;
let mut buffers = (0..kernel.params.len())
.map(|i| buffer(d, kernel, i))
.collect::<Result<Vec<_>, _>>()?;
let mut shared = (0..kernel.allocs.len())
.map(|i| shared(d, kernel, i))
.collect::<Result<Vec<_>, _>>()?;
mixed_widths(d, kernel)?;
let mut atomic = BTreeSet::new();
if d.typed_atomics() {
atomic_places(&kernel.body, &mut atomic);
}
for decl in buffers.iter_mut().chain(shared.iter_mut()) {
decl.atomic = atomic.contains(&decl.name);
}
let is_narrow = |e: ElementKind| {
matches!(
e,
ElementKind::I8 | ElementKind::U8 | ElementKind::I16 | ElementKind::U16
)
};
let narrow = buffers.iter().any(|b| is_narrow(b.elem))
|| kernel.locals.iter().any(|l| is_narrow(l.elem));
Ok(Scope {
signed,
narrow,
float,
block: launch.block,
grid: launch.grid,
buffers,
shared,
subgroup: uses_subgroup(&kernel.body) || launch.subgroup.is_some(),
atomic,
rte: std::cell::RefCell::new(BTreeSet::new()),
backend: d.name(),
})
}