Harness: row_tail workloads, rows of floats written four at a time with the tail guarded

row_tail writes a row of `shape.x` floats four at a time from a float4: the first float, then
the next three where the row holds them, else the next two, else the next one. Six workloads
run it: rows of 224 floats (every row 16-byte aligned), 27,730 (every other row) and 49 (one
row in four), each into a Shared buffer over about 381,000 threads and into a Private one over
16 times as many. Each checks the whole output against its CPU reference. They time the store
shape the translator's next change merges.

Companions regenerated by scripts/regen-air-fixtures --dir harness on the M3 (m3-max-80294).
This commit is contained in:
Anees Iqbal committed 2026-12-02 16:24:49 -08:00
1 parent 2005dae350
commit cfc69949a4
4 files changed
+1222 -886

No files matched your search

+125 -1
View File
@@ -21,7 +21,7 @@ pub struct Workload {
}
/// The authored workloads.
pub const WORKLOADS: [Workload; 40] = [
pub const WORKLOADS: [Workload; 46] = [
Workload {
name: "bandwidth",
plan: bandwidth,
@@ -150,6 +150,30 @@ pub const WORKLOADS: [Workload; 40] = [
name: "vote_loop",
plan: vote_loop,
},
Workload {
name: "row_tail_224",
plan: row_tail_224,
},
Workload {
name: "row_tail_27730",
plan: row_tail_27730,
},
Workload {
name: "row_tail_49",
plan: row_tail_49,
},
Workload {
name: "row_tail_224_private",
plan: row_tail_224_private,
},
Workload {
name: "row_tail_27730_private",
plan: row_tail_27730_private,
},
Workload {
name: "row_tail_49_private",
plan: row_tail_49_private,
},
Workload {
name: "constant_managed",
plan: constant_managed,
@@ -1222,6 +1246,106 @@ fn vote_loop() -> Plan {
}
}
/// Rows of 224 `float`s into a `Shared` buffer, four a thread (`row_tail`): every row starts at a
/// multiple of 16 bytes, and every thread writes four.
fn row_tail_224() -> Plan {
row_tail("row_tail_224", 224, Storage::Shared, 1)
}
/// Rows of 27,730 `float`s (2 mod 4) into a `Shared` buffer, four a thread (`row_tail`): every
/// other row starts at a multiple of 16 bytes, the others 8 past one, and a row's last thread
/// writes two.
fn row_tail_27730() -> Plan {
row_tail("row_tail_27730", 27_730, Storage::Shared, 1)
}
/// Rows of 49 `float`s into a `Shared` buffer, four a thread (`row_tail`): one row in four starts at
/// a multiple of 16 bytes, and a row's last thread writes one.
fn row_tail_49() -> Plan {
row_tail("row_tail_49", 49, Storage::Shared, 1)
}
/// [`row_tail_224`] into a `Private` buffer, 16 times the threads.
fn row_tail_224_private() -> Plan {
row_tail("row_tail_224_private", 224, Storage::Private, 16)
}
/// [`row_tail_27730`] into a `Private` buffer, 16 times the threads.
fn row_tail_27730_private() -> Plan {
row_tail("row_tail_27730_private", 27_730, Storage::Private, 16)
}
/// [`row_tail_49`] into a `Private` buffer, 16 times the threads.
fn row_tail_49_private() -> Plan {
row_tail("row_tail_49_private", 49, Storage::Private, 16)
}
/// `row_tail` over rows of `w` `float`s: about 381,000 threads times `scale` in threadgroups of
/// 1,024, as many rows as `ceil(w / 4)` threads a row make of them, each thread reading a `float4`
/// of a `Private` buffer and writing its lanes from column `4 (i mod threads a row)` of its row of
/// a buffer of `storage` and about 6.1 MB times `scale` that nothing else writes: the first lane,
/// then the three, two or one more the row holds.
fn row_tail(name: &str, w: u32, storage: Storage, scale: u32) -> Plan {
const THREADS: u32 = 381_952;
let per_row = w.div_ceil(4);
let rows = scale * THREADS / per_row;
let threads = rows * per_row;
let input: Vec<f32> = (words(15, 4 * threads as usize).into_iter())
.map(|word| (word & 0xFFFF) as f32)
.collect();
let mut output = vec![0u32; (rows * w) as usize];
for index in 0..threads {
let (row, col) = (index / per_row, (index % per_row) << 2);
let lanes = (w - col).min(4) as usize;
let at = (row * w + col) as usize;
let from = 4 * index as usize;
for lane in 0..lanes {
output[at + lane] = input[from + lane].to_bits();
}
}
let length = (4 * u64::from(rows * w)).next_multiple_of(16);
Plan {
name: name.to_owned(),
library: Source::Authored,
buffers: vec![
private(float_bytes(&input)),
BufferPlan {
length,
storage,
contents: None,
modified: None,
},
],
dispatches: vec![Dispatch {
blits: Vec::new(),
function: "row_tail".to_owned(),
linked: Vec::new(),
constants: Vec::new(),
bindings: vec![
buffer(0, 0),
buffer(1, 1),
bytes(2, word_bytes(&[w, per_row])),
],
grid: Grid::Threads {
threads: [u64::from(threads), 1, 1],
per_group: [1024, 1, 1],
},
}],
expect: vec![Expect {
buffer: 1,
values: Values::Words(output),
}],
textures: Vec::new(),
expect_textures: Vec::new(),
outputs: Vec::new(),
per_command_buffer: None,
per_encoder: None,
commit_to_complete: false,
frames: false,
scaled: None,
}
}
/// Reads of a 64 KiB table as constants (`constant_reads`): 2^20 threads in threadgroups of 256,
/// each folding 64 of the table's 4,096 entries into a word, the table `Managed`, and the first
/// 256 bytes of it written again by the CPU before each run and marked with `didModifyRange:`, as
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -4,11 +4,12 @@
// integer arithmetic, sampling through a runtime sampler, through a constexpr one and over a grid
// more than one deep, products by a constant zero of either sign, many buffers bound for each of
// many dispatches, many one-wave threadgroups that each do almost nothing, reads of a table of
// constants, and a grid's cells indexed by row, with the control the harness's tests hold beside
// it; and one kernel the new runtime's device tests run against Metal's reading of it
// (row_barrier). Written from scratch for it;
// `scripts/regen-air-fixtures/regen-air-fixtures.sh --dir harness` compiles it. The harness holds a
// CPU reference of every workload's result. No translator sweep reads this directory.
// constants, rows of floats written four at a time into a Shared buffer with their tail guarded,
// and a grid's cells indexed by row, with the control the harness's tests hold beside it; and one
// kernel the new runtime's device tests run against Metal's reading of it (row_barrier). Written
// from scratch for it; `scripts/regen-air-fixtures/regen-air-fixtures.sh --dir harness` compiles
// it. The harness holds a CPU reference of every workload's result. No translator sweep reads this
// directory.
#include <metal_stdlib>
using namespace metal;
@@ -413,3 +414,32 @@ kernel void constant_reads(constant uint4 *table [[buffer(0)]],
}
destination[index] = folded;
}
// Rows of `shape.x` floats written into a Shared buffer four at a time from a float4, the row's
// last thread writing what fits: the first float, then the next three where the row holds them,
// else the next two, else the next one. `shape.y` threads a row. A row starts at a multiple of 16
// bytes only where its index times `shape.x` is a multiple of 4.
kernel void row_tail(device const float4 *source [[buffer(0)]],
device float *destination [[buffer(1)]],
constant uint2 &shape [[buffer(2)]],
uint index [[thread_position_in_grid]]) {
uint w = shape.x;
uint row = index / shape.y;
uint col = (index % shape.y) << 2;
if (col >= w) {
return;
}
float4 v = source[index];
uint i = row * w + col;
destination[i] = v.x;
if ((col | 3) < w) {
destination[i + 1] = v.y;
destination[i + 2] = v.z;
destination[i + 3] = v.w;
} else if ((col | 2) < w) {
destination[i + 1] = v.y;
destination[i + 2] = v.z;
} else if ((col | 1) < w) {
destination[i + 1] = v.y;
}
}