From 9da0f0a63b6efccb9a7eb6897abb4af02c04ef1f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 10:35:12 -0800 Subject: [PATCH] Matrix: a subgroup multiply-add's loop of step pairs asks the driver nothing about unrolling The loop a1ca99880a wrote for a subgroup tier's multiply-add (tile_mad's paired_steps) carried DontUnroll. On an RX 7900 XTX (RADV, Mesa 26.2.6) that made one real kernel 12.6 to 12.9% slower than development's form before it. Without the hint (LoopSpec's default, the driver's own choice) the same kernel is back within 0.3% of that form, and NVIDIA's driver compiles it in the memory it took for the DontUnroll loop. SEMANTICS_VERSION 2274 -> 2275. Verified, on a probe branch that never lands (the same change on the probe runners; the kernel compiled in the harness process unless the helper is named), the kernel's two workloads each: - RX 7900 XTX, four arms alternating in one job, 3 rounds of 100 runs (m3-max-68270), steady and GPU p50 means in us: before 653.7 / 671.6 and 750.4 / 766.6, compile 2.64 s, peak 271 MiB; a1ca99880a 737.9 / 751.7 and 845.1 / 864.8, 0.46 s, 199 MiB; this change 655.9 / 672.1 and 751.1 / 765.8, 2.43 s, 275 MiB. Every arm's outputs bit-equal (the same digests in every round). - RTX 5080 Laptop (NVIDIA 615.71.09; m3-max-68271), one round each, the arms in turn: a1ca99880a reached first use in 92.4 s at a peak of 2.85 GiB, GPU p50 10,845 and 4,305 us, steady 10,197 and 3,953 us; this change in 79.6 s at 2.85 GiB, GPU 10,888 and 4,284 us, steady 10,626 and 4,163 us; outputs bit-equal. - RTX 3090 (NVIDIA 620.70.06; m3-max-68288), the kernel's module (bound weight 62,810, 644 subgroup instructions, spirv-val valid) through the helper under the consumer's 3,072 MiB cap: filled in 59.2 s at a peak of 2,701 MiB, against a1ca99880a's form at 2,699 MiB (m3-max-67936). - The paired steps written out instead of looped were as fast on the RX 7900 XTX (654.6 and 748.5 us) but their module passed 6,144 MiB in the RTX 3090's helper (m3-max-67936). - fmt, clippy --workspace --all-targets --all-features and Rustdoc clean; the stage-bound, widest-pin, multiply-add and API-surface tests pass, 52 of 52 (m3-max-68301). --- crates/metal2vulkan/src/cache/mod.rs | 2 +- crates/metal2vulkan/src/lower/simd/matrix.rs | 21 +++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 751b076ad..c0791f5c6 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2274; +pub const SEMANTICS_VERSION: u32 = 2275; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/simd/matrix.rs b/crates/metal2vulkan/src/lower/simd/matrix.rs index 22625e96f..2eec18ce4 100644 --- a/crates/metal2vulkan/src/lower/simd/matrix.rs +++ b/crates/metal2vulkan/src/lower/simd/matrix.rs @@ -39,8 +39,9 @@ //! in increasing `k` (the `agx2` schedule, shared with [`super::agx`]). On a subgroup the reads //! are paired and the steps are a loop: one shuffle of a `vec2` carries a step's two `b` cells, //! one carries the `a` cells of steps `2m` and `2m + 1` (one lane owns both), and the `dim / 2` -//! step pairs are the trips of a loop, so a driver compiles each shuffle once per multiply-add -//! (its compile's memory grows with every subgroup instruction). Every operand format is +//! step pairs are the trips of a loop with no unrolling hint, which each driver unrolls or not +//! as it compiles (its compile's memory grows with every subgroup instruction it compiles; with +//! `DontUnroll` RADV ran one real kernel's loop 12% slower). Every operand format is //! converted exactly to `f32` first (`half` and `bfloat` are held widened; 8-bit floats decode //! exactly). When every format is `half` each step rounds to `half` (the `f16matmad` form); //! otherwise the sum accumulates in `f32` and rounds once to the result format. @@ -80,7 +81,7 @@ use crate::spirv::builder::constructs::{ArmEnd, ArmFn, LoopFn, LoopSpec}; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::BResult; use crate::spirv::ids::Value as SValue; -use crate::spirv::ir::{BinOp, GlslOp, MemAccess, Unrolling}; +use crate::spirv::ir::{BinOp, GlslOp, MemAccess}; use super::check_operands; use super::group::{bin_k, lane, shuffle, Lane, Live}; @@ -731,10 +732,10 @@ fn owner_bits_of(fb: &mut FunctionBuilder<'_>, dim: u32, k: SValue) -> BResult<( } } -/// The steps of a multiply-add on a subgroup: a loop (`DontUnroll`, as `spirv::roll` writes one) -/// of `dim / 2` trips, trip `m` taking steps `2m` and `2m + 1`, the sums carried in `Function` -/// variables. Each step reads the cells the written-out steps read and accumulates them in the -/// same order. +/// The steps of a multiply-add on a subgroup: a loop of `dim / 2` trips with no unrolling hint +/// (the driver's own choice, the module docs), trip `m` taking steps `2m` and `2m + 1`, the sums +/// carried in `Function` variables. Each step reads the cells the written-out steps read and +/// accumulates them in the same order. fn paired_steps( fb: &mut FunctionBuilder<'_>, ln: &Lane, @@ -782,11 +783,7 @@ fn paired_steps( fb.store(sums[0], acc[0], MemAccess::default())?; fb.store(sums[1], acc[1], MemAccess::default()) }); - let spec = LoopSpec { - exit_sel: None, - unrolling: Unrolling::DontUnroll, - }; - fb.loop_(&mut (), spec, body)?; + fb.loop_(&mut (), LoopSpec::default(), body)?; Ok([ fb.load(sums[0], MemAccess::default())?, fb.load(sums[1], MemAccess::default())?,