Files
metal2vulkan/crates/metal2vulkan-harness/tests/described.rs
T
Anees Iqbal 92ce7950fc Docs: "the runtime", not "the new runtime"
There is one runtime, so the prose of the harness, validation, devtools,
the translator's lockstep module, .config/nextest.toml and the refusal
surface and conformance coverage generators says "the runtime"
(scratch newrt.py: a word replacement, identifiers such as new_runtime
and NewRuntime untouched). docs/REFUSALS.md and
docs/CONFORMANCE-COVERAGE.md are regenerated from the edited scripts.

Four harness passages said the runtime makes no heap and no texture
over a buffer, and has no Managed mode. The runtime has all three
(Device::new_heap_buffer and heap textures, Buffer::new_texture,
Device::new_managed_buffer); the harness's own backend refuses the two
texture sources. They now say the harness makes neither on the runtime,
and the Managed note names Device::new_managed_buffer. The refusal
strings keep the words the harness's described test matches ("makes no
heap", "makes no texture over a buffer").

Left: three fixture comments (.metal under validation's public
fixtures, whose text keys the golden tables and the committed AIR) and
scripts/matrix, whose keys (harness-new) carry the same naming.

Verified locally: the refusal surface, Metal coverage, conformance
coverage and API signature checks pass.
2026-11-20 11:52:56 -08:00

385 lines
14 KiB
Rust

//! A description's workloads over a metallib run on the runtime from their starting contents, and
//! where the runtime does not refuse one with a typed refusal, which the test prints, the outputs
//! of those the test knows match its CPU reference. The metallib
//! is the authored fixture's kernels in a synthetic container, so the path a described workload
//! takes runs without any kernel the repository does not own. Each function's bitcode is a module
//! holding that kernel alone, as a metallib's is: the translator checks that a metallib function's
//! module declares one entry point.
#![cfg(any(m2v_gpu, m2v_cpu_icd))]
use metal2vulkan_devtools::metallib::{metallib, wrap, MtlbFunction};
use metal2vulkan_devtools::scratch::Owned;
use metal2vulkan_devtools::{fixtures_dir, llvm_assemble_text};
use metal2vulkan_harness::plan::seeded_words;
use metal2vulkan_harness::run::{run_each, Harness, Runs, Session};
use metal2vulkan_harness::{authored, description, rt};
/// The description: each fill, both storage modes, a binding at an offset, bindings that stay
/// set for the dispatches after, both dispatch calls, `repeat`, both comparisons, a threadgroup
/// memory length, textures of each source with their bytes written by the CPU, a sampler
/// state, a texture copy from a slice of an array into a 2D texture, a Private texture
/// written from a buffer and read into another around a fill, a Managed buffer filled and
/// synchronized, and a 2D view of one slice of a 2D array read by a kernel.
const DESCRIPTION: &str = "\
# Synthetic workloads over the authored kernels.
workload scale kernels.metallib
buffer 65536 private unit 7
buffer 65536 shared zero
set-buffer 0 0
set-buffer 1 1
set-bytes 2 8 hex 000000400000803f # float2(2, 1)
dispatch scale_offset threadgroups 16 1 1 256 1 1
check 1 float 0
workload chain kernels.metallib
buffer 1024 private words 9
set-buffer 0 0
set-bytes 1 4 words 3
dispatch lcg_step threadgroups 1 1 1 256 1 1 repeat 64
check 0 exact
workload filter kernels.metallib
buffer 65536 private unit 11
buffer 65536 private zero
set-buffer 0 0
set-buffer 1 1
set-bytes 2 8 hex 2800000066000000 # uint2(40, 102): 4080 texels of 4096
dispatch binomial3x3 threads 40 102 1 16 16 1
check 1 float 0.0001
workload reduce kernels.metallib
buffer 16400 private words 5
buffer 16 shared zero
set-buffer 0 0 16
set-buffer 1 1
set-bytes 2 8 hex 0010000000100000 # uint2(4096, 4096)
dispatch reduce_sum threadgroups 1 1 1 256 1 1
check 1 exact
workload groups kernels.metallib
buffer 16384 private words 13
buffer 64 shared zero
set-buffer 0 0
set-buffer 1 1
threadgroup-length 0 1024 # a word for each of 256 threads
dispatch group_sums threadgroups 16 1 1 256 1 1
check 1 exact
workload texels kernels.metallib
texture 2 70 32 16 1 1 shared plain words 17 # RGBA8Unorm, ShaderRead
buffer 8192 shared zero
set-texture 0 0
set-buffer 0 0
set-bytes 1 4 hex 20000000 # its width, 32
dispatch texels_out threadgroups 2 1 1 16 16 1
check 0 exact
workload heaped kernels.metallib
texture 2 70 32 16 1 1 shared heap words 17
buffer 8192 shared zero
set-texture 0 0
set-buffer 0 0
set-bytes 1 4 hex 20000000
dispatch texels_out threadgroups 2 1 1 16 16 1
check 0 exact
workload over kernels.metallib
texture 2 70 32 16 1 1 shared over-buffer words 17
buffer 8192 shared zero
set-texture 0 0
set-buffer 0 0
set-bytes 1 4 hex 20000000
dispatch texels_out threadgroups 2 1 1 16 16 1
check 0 exact
workload sampled kernels.metallib
texture 2 70 32 32 1 1 managed plain words 19
texture 2 115 16 16 1 3 private plain undefined # RGBA16Float, ShaderRead | ShaderWrite
buffer 4096 shared zero
set-texture 0 0
set-texture 1 1
set-sampler 0 nearest clamp-to-zero normalized
dispatch sample_down threadgroups 1 1 1 16 16 1
set-texture 0 1
set-buffer 0 0
set-bytes 1 4 hex 10000000 # its width, 16
dispatch texels_out threadgroups 1 1 1 16 16 1
check 0 exact
workload copied kernels.metallib
texture 3 70 32 16 2 1 shared plain words 23 # RGBA8Unorm, a 2D array of 2
texture 2 70 32 16 1 1 shared plain words 17
buffer 8192 shared zero
copy-texture 0 1 4 2 8 6 1 0 20 9 # 8x6 texels at (4, 2) of slice 1 to (20, 9)
set-texture 0 1
set-buffer 0 0
set-bytes 1 4 hex 20000000
dispatch texels_out threadgroups 2 1 1 16 16 1
check 0 exact
workload staged kernels.metallib
texture 2 70 32 16 1 1 private plain undefined # RGBA8Unorm, written by a copy alone
buffer 4096 shared words 29
buffer 8192 shared zero
buffer 1024 shared words 31
copy-buffer-to-texture 0 0 128 0 32 16 0 0 0 0
fill-buffer 2 4 20 171
copy-texture-to-buffer 0 0 4 2 8 6 2 256 64 0 # 8x6 texels at (4, 2), rows 64 apart
set-texture 0 0
set-buffer 0 1
set-bytes 1 4 hex 20000000
dispatch texels_out threadgroups 2 1 1 16 16 1
check 1 exact
check 2 exact
workload synced kernels.metallib
buffer 1024 managed words 41
fill-buffer 0 16 64 90
synchronize 0
set-buffer 0 0
set-bytes 1 4 words 3
dispatch lcg_step threadgroups 1 1 1 256 1 1
check 0 exact
workload viewed kernels.metallib
texture 3 70 32 16 2 1 shared plain words 43 # RGBA8Unorm, a 2D array of 2 slices, ShaderRead
texture-view 0 2 1 1 # a 2D view of slice 1
buffer 8192 shared zero
set-texture 0 1
set-buffer 0 0
set-bytes 1 4 hex 20000000 # its width, 32
dispatch texels_out threadgroups 2 1 1 16 16 1
check 0 exact
";
#[test]
fn described_workloads_match_their_references() {
let air = authored::library_air().unwrap();
let ll = std::fs::read_to_string(fixtures_dir().join("harness/workloads.ll")).unwrap();
let functions: Vec<MtlbFunction> = [
"scale_offset",
"lcg_step",
"binomial3x3",
"reduce_sum",
"group_sums",
"texels_out",
"sample_down",
]
.map(|name| MtlbFunction {
name,
type_code: 2,
bitcode: wrap(&llvm_assemble_text(&single_entry(&ll, name)).unwrap()),
constants: Vec::new(),
public: Vec::new(),
tags: Vec::new(),
})
.into();
let scratch = Owned::made("harness-described").unwrap();
std::fs::write(
scratch.path().join("kernels.metallib"),
metallib(&functions),
)
.unwrap();
let described = description::parse(DESCRIPTION, scratch.path()).unwrap();
let mut sessions: [(&str, Box<dyn Session>); 1] = [(
"new",
Box::new(Harness::new(rt::system_default(None).unwrap(), &air).unwrap()),
)];
let runs = Runs {
warmup: 1,
settle: false,
timed: 2,
};
for workload in &described {
let plan = workload.plan().unwrap();
let each = (run_each(&plan, &mut sessions, runs, None))
.unwrap_or_else(|why| panic!("{}: {why}", plan.name));
// The harness makes no heap and no texture over a buffer on the runtime.
let missing = match plan.name.as_str() {
"heaped" => Some("makes no heap"),
"over" => Some("makes no texture over a buffer"),
_ => None,
};
if let Some(missing) = missing {
let refused = (each[0].as_ref().err()).map(ToString::to_string);
assert!(
refused.as_deref().is_some_and(|why| why.contains(missing)),
"{}: {refused:?}",
plan.name
);
}
let first = match &each[0] {
Ok(first) => first,
Err(refused) => {
println!("{}: REFUSED: {refused}", plan.name);
continue;
}
};
assert_eq!(first.gpu_us.len(), 2, "{}", plan.name);
if plan.name == "chain" {
// 64 steps of the generator, each with the one increment set before the first.
let increment = seeded_words(3, 1)[0];
let want: Vec<u32> = (seeded_words(9, 256).into_iter())
.map(|word| {
(0..64).fold(word, |x, _| {
x.wrapping_mul(1_664_525).wrapping_add(increment)
})
})
.collect();
let got: Vec<u32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| u32::from_le_bytes(*word))
.collect();
assert_eq!(got, want);
}
if plan.name == "texels" {
// Each texel's four bytes as `float`s of [0, 1], read back where the CPU wrote them.
let bytes: Vec<u8> = (seeded_words(17, 512).iter())
.flat_map(|word| word.to_le_bytes())
.collect();
let got: Vec<f32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| f32::from_le_bytes(*word))
.collect();
assert_eq!(got.len(), bytes.len());
for (at, (&got, &byte)) in got.iter().zip(&bytes).enumerate() {
let want = f32::from(byte) / 255.0;
assert!((got - want).abs() <= 1e-6, "channel {at}: {got} for {want}");
}
}
if plan.name == "viewed" {
// Slice 1 of the array: its second 2048 bytes.
let slice = &seeded_bytes(43, 1024)[2048..];
let got: Vec<f32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| f32::from_le_bytes(*word))
.collect();
assert_eq!(got.len(), slice.len());
for (at, (&got, &byte)) in got.iter().zip(slice).enumerate() {
let want = f32::from(byte) / 255.0;
assert!(
(got - want).abs() <= 1e-6,
"viewed: channel {at}: {got} for {want}"
);
}
}
if plan.name == "staged" {
// The texture is buffer 0's first 2048 bytes; buffer 2 its words with 4..24 filled
// and the region's rows at 256, 64 bytes apart.
let texture = &seeded_bytes(29, 1024)[..2048];
let got: Vec<f32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| f32::from_le_bytes(*word))
.collect();
assert_eq!(got.len(), texture.len());
for (at, (&got, &byte)) in got.iter().zip(texture).enumerate() {
let want = f32::from(byte) / 255.0;
assert!(
(got - want).abs() <= 1e-6,
"staged: channel {at}: {got} for {want}"
);
}
let mut want = seeded_bytes(31, 256);
want[4..24].fill(171);
for y in 0..6 {
let (from, to) = (((2 + y) * 32 + 4) * 4, 256 + y * 64);
want[to..to + 32].copy_from_slice(&texture[from..from + 32]);
}
assert_eq!(first.outputs[1], want, "staged: buffer 2");
}
if plan.name == "copied" {
// The 2D texture's bytes, with the region of the array's slice 1 copied over them.
let words = |seed, count| -> Vec<u8> {
(seeded_words(seed, count).iter())
.flat_map(|word| word.to_le_bytes())
.collect()
};
let (array, mut bytes) = (words(23, 1024), words(17, 512));
let row = 32 * 4;
for y in 0..6 {
let from = 2048 + (2 + y) * row + 4 * 4;
let to = (9 + y) * row + 20 * 4;
bytes[to..to + 8 * 4].copy_from_slice(&array[from..from + 8 * 4]);
}
let got: Vec<f32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| f32::from_le_bytes(*word))
.collect();
assert_eq!(got.len(), bytes.len());
for (at, (&got, &byte)) in got.iter().zip(&bytes).enumerate() {
let want = f32::from(byte) / 255.0;
assert!(
(got - want).abs() <= 1e-6,
"copied: channel {at}: {got} for {want}"
);
}
}
if plan.name == "groups" {
// Each threadgroup of 256 threads sums its words through the length the host set.
let want: Vec<u32> = (seeded_words(13, 4096).chunks(256))
.map(|words| words.iter().fold(0u32, |sum, &word| sum.wrapping_add(word)))
.collect();
let got: Vec<u32> = (first.outputs[0].as_chunks::<4>().0.iter())
.map(|word| u32::from_le_bytes(*word))
.collect();
assert_eq!(got, want);
}
}
assert_eq!(described.len(), 13);
}
/// The bytes of `count` of SplitMix64's words from `seed`, as a description's `words` fill.
fn seeded_bytes(seed: u64, count: usize) -> Vec<u8> {
(seeded_words(seed, count).iter())
.flat_map(|word| word.to_le_bytes())
.collect()
}
/// The fixture's LLVM text `ll` with `kernel` its only entry point: every other kernel's
/// definition, its `!air.kernel` node and the threadgroup globals named after it left out.
fn single_entry(ll: &str, kernel: &str) -> String {
/// The name of the function `line` defines, where it starts a definition.
fn defined(line: &str) -> Option<&str> {
let rest = line.strip_prefix("define ")?;
Some(rest.split_once('@')?.1.split_once('(')?.0)
}
let others: Vec<&str> = (ll.lines().filter_map(defined))
.filter(|&name| name != kernel)
.collect();
let names_other_entry =
|line: &str| (others.iter()).any(|other| line.contains(&format!("ptr @{other},")));
let dropped: Vec<&str> = (ll.lines())
.filter(|line| line.starts_with('!') && names_other_entry(line))
.filter_map(|line| Some(line.split_once(" = ")?.0))
.collect();
let mut out = String::new();
let mut in_other = false;
for line in ll.lines() {
if in_other {
in_other = line != "}";
continue;
}
if defined(line).is_some_and(|name| name != kernel) {
in_other = true;
continue;
}
let other_global = line.starts_with('@') && others.iter().any(|other| line.contains(other));
if other_global || (line.starts_with('!') && names_other_entry(line)) {
continue;
}
match (line.strip_prefix("!air.kernel = !{")).and_then(|list| list.strip_suffix('}')) {
Some(list) => {
let kept: Vec<&str> = (list.split(", "))
.filter(|node| !dropped.contains(node))
.collect();
out.push_str(&format!("!air.kernel = !{{{}}}\n", kept.join(", ")));
}
None => {
out.push_str(line);
out.push('\n');
}
}
}
assert_eq!(
out.lines().filter(|line| defined(line).is_some()).count(),
1,
"{kernel} alone"
);
out
}