use unpopped::exec::{ExecId, Scope};
use unpopped::ir::{ArithOp, BinaryOp};
use unpopped::kernel::{AtomicOp, BufferRole, MemoryOrder};
use unpopped_vocab::ElementKind;
pub const COMPARISONS: &[(BinaryOp, &str)] = &[
(BinaryOp::CmpEq, "=="),
(BinaryOp::CmpNe, "!="),
(BinaryOp::CmpLt, "<"),
(BinaryOp::CmpLe, "<="),
(BinaryOp::CmpGt, ">"),
(BinaryOp::CmpGe, ">="),
];
pub const BIT_OPS: &[(BinaryOp, &str)] = &[
(BinaryOp::BitAnd, "&"),
(BinaryOp::BitOr, "|"),
(BinaryOp::BitXor, "^"),
];
#[must_use]
pub fn operator(table: &[(BinaryOp, &'static str)], op: BinaryOp) -> Option<&'static str> {
table.iter().find(|(o, _)| *o == op).map(|(_, s)| *s)
}
#[derive(Clone, Debug)]
pub struct Decl {
pub name: String,
pub binding: u32,
pub role: BufferRole,
pub len: u32,
pub private: bool,
pub atomic: bool,
pub spec_local: Option<u32>,
pub len_spec: Option<String>,
}
#[derive(Clone, Debug)]
pub struct Layout {
pub name: String,
pub signed: bool,
pub block: [u32; 3],
pub buffers: Vec<Decl>,
pub shared: Vec<Decl>,
pub spec_locals: Vec<(u32, String)>,
pub locals: usize,
pub subgroup: bool,
}
impl Layout {
#[must_use]
pub fn spec_init(&self, local: usize) -> Option<&str> {
self.spec_locals
.iter()
.find(|(l, _)| usize::try_from(*l).is_ok_and(|l| l == local))
.map(|(_, n)| n.as_str())
}
}
pub trait Dialect {
fn name(&self) -> &'static str;
fn wide(&self) -> bool {
false
}
fn cast_to_compute(&self, _x: &str, _from: ElementKind) -> Option<String> {
None
}
fn prologue(&self, layout: &Layout) -> String;
fn epilogue(&self) -> &'static str;
fn lit(&self, v: i64, signed: bool) -> String;
fn index(&self, name: &str, idx: &str, is_buffer: bool) -> String;
fn exec(&self, id: ExecId, signed: bool) -> Option<String>;
fn binary(&self, op: BinaryOp, a: &str, b: &str, signed: bool) -> Option<String>;
fn arith(&self, op: ArithOp, a: &str, b: &str) -> Option<String> {
let sym = match op {
ArithOp::Add => "+",
ArithOp::Sub => "-",
ArithOp::Mul => "*",
_ => return None,
};
Some(format!("({a} {sym} {b})"))
}
fn select(&self, cond: &str, a: &str, b: &str, signed: bool) -> String;
fn truthy(&self, value: &str, signed: bool) -> String {
format!("({value} != {})", self.lit(0, signed))
}
fn temp(&self, name: &str, rhs: &str, signed: bool) -> String;
fn bound(&self, name: &str, rhs: &str, signed: bool) -> String;
fn barrier(&self, scope: Scope) -> Option<String>;
fn atomic_rmw(&self, _op: AtomicOp, _elem: &str, _value: &str) -> Option<String> {
None
}
fn atomic_cas(&self, _elem: &str, _expected: &str, _desired: &str) -> Option<String> {
None
}
fn typed_atomics(&self) -> bool {
false
}
fn atomic_scope_ok(&self, _scope: Scope) -> bool {
true
}
fn atomic_load(&self, _elem: &str) -> Option<String> {
None
}
fn atomic_store(&self, _elem: &str, _value: &str) -> Option<String> {
None
}
fn atomic_cas_lines(
&self,
elem: &str,
expected: &str,
desired: &str,
dest: Option<&str>,
) -> Option<Vec<String>> {
let call = self.atomic_cas(elem, expected, desired)?;
Some(vec![match dest {
Some(l) => format!("{l} = {call};"),
None => format!("{call};"),
}])
}
fn fence(&self, scope: Scope, order: MemoryOrder) -> Option<String>;
}