use tree_sitter::{Parser, Tree};
use unpopped_convert::Frontend;
#[must_use]
pub fn parse_slang(src: &str) -> Option<Tree> {
let mut parser = Parser::new();
parser
.set_language(&tree_sitter_slang::LANGUAGE_SLANG.into())
.ok()?;
parser.parse(src, None)
}
pub const SLANG_RESIDUE: &[&str] = &[
"groupshared",
"GroupMemoryBarrier",
"DeviceMemoryBarrier",
"AllMemoryBarrier",
"InterlockedAdd",
"InterlockedCompareExchange",
"RWByteAddressBuffer",
];
pub const SLANG: Frontend = Frontend::new(
"slang",
"numthreads",
SLANG_RESIDUE,
"output",
"input",
parse_slang,
);
#[cfg(test)]
mod tests {
use super::*;
use unpopped::LiftError;
use unpopped::ir::{Access, ReduceOp, ScalarExpr};
use unpopped_convert::{lift, lift_elementwise, lift_reduction, lift_scan};
use unpopped_vocab::ElementKind;
const F32: &[ElementKind] = &[ElementKind::F32];
const SLANG_MUL: &str = "StructuredBuffer<float> input0;\n\
StructuredBuffer<float> input1;\n\
RWStructuredBuffer<float> output;\n\
[numthreads(256, 1, 1)]\n\
void mul(uint3 tid : SV_DispatchThreadID) {\n\
uint i = tid.x;\n\
output[i] = input0[i] * input1[i];\n\
}";
const SLANG_COPY: &str = "StructuredBuffer<float> input0;\n\
RWStructuredBuffer<float> output;\n\
[numthreads(256, 1, 1)]\n\
void k(uint3 tid : SV_DispatchThreadID) { uint i = tid.x; output[i] = input0[i]; }";
const SLANG_SUM: &str = "StructuredBuffer<float> input0;\n\
RWStructuredBuffer<float> output;\n\
[numthreads(256,1,1)]\n\
void rsum(uint3 tid : SV_DispatchThreadID){ float acc = 0.0f; for (uint i=0;i<256;i++) acc += input0[i]; output[0] = acc; }";
const SLANG_CSUM: &str = "StructuredBuffer<float> input0;\n\
RWStructuredBuffer<float> output;\n\
[numthreads(256,1,1)]\n\
void csum(uint3 tid : SV_DispatchThreadID){ float acc = 0.0f; for (uint i=0;i<256;i++){ acc += input0[i]; output[i] = acc; } }";
#[test]
fn grammar_loads_and_parses() {
let tree = parse_slang("float4 f(float x) { return x; }").expect("grammar loads");
assert!(!tree.root_node().has_error());
}
#[test]
fn syntax_errors_are_error_nodes_not_none() {
let tree = parse_slang("void f( {").expect("a parse tree even for broken source");
assert!(
tree.root_node().has_error(),
"broken source must show an ERROR node"
);
}
#[test]
fn slang_lifts_multiply() {
let lifted = lift_elementwise(&SLANG, SLANG_MUL, "slang_mul", F32).unwrap();
assert_eq!(lifted.n_inputs, 2);
assert_eq!(
lifted.op.body,
ScalarExpr::Mul(
Box::new(ScalarExpr::Input(0)),
Box::new(ScalarExpr::Input(1)),
)
);
}
#[test]
fn every_residue_marker_is_refused_as_inexpressible() {
for marker in SLANG_RESIDUE {
let src = format!(
"[numthreads(256,1,1)]\nvoid k(uint3 tid : SV_DispatchThreadID) {{ {marker}; output[tid.x] = input0[tid.x]; }}"
);
assert!(
matches!(
lift_elementwise(&SLANG, &src, "x", F32),
Err(LiftError::Inexpressible(ref w)) if w == marker
),
"{marker}: {:?}",
lift_elementwise(&SLANG, &src, "x", F32)
);
}
}
#[test]
fn slang_lifts_sum_reduction() {
let lifted = lift_reduction(&SLANG, SLANG_SUM, "rsum", F32).unwrap();
assert_eq!(lifted.op.body, ScalarExpr::Input(0));
assert!(matches!(
lifted.op.access,
Access::Reduction {
op: ReduceOp::Sum,
..
}
));
}
#[test]
fn slang_lifts_cumsum() {
let lifted = lift_scan(&SLANG, SLANG_CSUM, "csum", F32).unwrap();
assert!(matches!(
lifted.op.access,
Access::Scan {
op: ReduceOp::Sum,
..
}
));
}
#[test]
fn the_three_lifters_do_not_cross() {
assert!(lift_elementwise(&SLANG, SLANG_SUM, "x", F32).is_err());
assert!(lift_reduction(&SLANG, SLANG_COPY, "x", F32).is_err());
assert!(lift_scan(&SLANG, SLANG_CSUM, "x", F32).is_ok());
assert!(lift_reduction(&SLANG, SLANG_CSUM, "x", F32).is_err());
assert!(lift_elementwise(&SLANG, SLANG_CSUM, "x", F32).is_err());
assert!(lift(&SLANG, SLANG_SUM, "x", F32).is_ok());
assert!(lift(&SLANG, SLANG_COPY, "x", F32).is_ok());
}
#[test]
fn source_without_numthreads_is_not_a_kernel() {
assert!(matches!(
lift_elementwise(&SLANG, "float f(float x) { return x; }", "x", F32),
Err(LiftError::NotAKernel)
));
}
}