From 628a9a1296899ad9a071cfeecd20b8471fa623cf Mon Sep 17 00:00:00 2001 From: Saurabh Suresh Date: Fri, 27 Nov 2026 22:01:33 +0400 Subject: [PATCH] fix(shader): let each half read a first lane a multiply-accumulate made first_lane::read_alone lets each 32-lane half of a console wave make a v_readfirstlane_b32 by itself where the value is the same in every lane of the read's EXEC: made from constants and scalars by plain VALU operations under that EXEC. It left out every multiply-accumulate, because one adds to its old destination, an operand the rule did not look at. ef154ebc9e03d1b1 reads a function of a constant-buffer value so made, in a region its lanes are picked into: v_mov_b32 v30, s43 v_mac_f32 v30, lit(0xc3960000), v29 v_fma_f32 v37, v30, s0, lit(0xbec00000) v_readfirstlane_b32 s15, v37 So the read sent the program to halves, which refused it: its dispatches have been dropped since #522. A multiply-accumulate (v_mac_f32, v_fmac_f32, v_mac_legacy_f32) now counts where its old destination is held to the same as its operands: then every lane of the read's EXEC adds the same product to the same value. ef154ebc9e03d1b1 and e1b8e701f0a02e50 translate again, with no group barrier. Over the 2,734 compute programs our runs passed, no other translates differently and none is newly refused: 68 refused, now 66. Tests: a first-lane read of v4 += 2.0 * v8 in a region, its result used there, takes no exchange where v8 is the same in the region's lanes (before: 5 group barriers, in halves), and still passes between the halves where v8 is the lane's thread id; both give the console's lanes on hardware, in groups of 64 and 128. Co-Authored-By: Claude Fable 5.2 --- .../victory-shader/src/emit/halves_tests.rs | 136 ++++++++++++++++++ .../src/emit/lower/first_lane.rs | 20 ++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/crates/victory-shader/src/emit/halves_tests.rs b/crates/victory-shader/src/emit/halves_tests.rs index dc0c9fda6..638e66de2 100644 --- a/crates/victory-shader/src/emit/halves_tests.rs +++ b/crates/victory-shader/src/emit/halves_tests.rs @@ -4085,3 +4085,139 @@ fn a_first_lane_read_only_active_lanes_see_matches_the_console_on_every_gpu() { } assert!(failed.is_empty(), "{}", failed.join("\n")); } + +/// A first-lane read of a value a multiply-accumulate made, in an +/// `s_and_saveexec_b64` region whose EXEC is `mask_words`'s, its result +/// used in the region only (as GTA VI's `ef154ebc9e03d1b1` reads a function +/// of a constant-buffer value): `v4 = f32(s14)`, `s14` the first dword of +/// the SRV buffer at table dword 8, then `v_mac_f32 v4, v7, v8` with +/// `v7 = 2.0`, so `v4 += 2.0 * v8`. `v8` is 2.0 (the same in every lane of +/// the region), or with `per_lane` the lane's thread id. The region's lanes +/// store the read; the others 0. +fn mac_read_in_a_region(mask_words: &[u32], per_lane: bool) -> Vec { + let mut w = PROLOGUE.to_vec(); + w.extend([ + 0xF408_0B02, + 0xFA00_0020, // s_load_dwordx4 s[44:47], s[4:5], 0x20 + S_WAITCNT_LGKM, + 0xF420_0396, + 0xFA00_0000, // s_buffer_load_dword s14, s[44:47], 0x0 + S_WAITCNT_LGKM, + 0x3420_0085, // v_lshlrev_b32 v16, 5, v0 + 0x7E04_0280, // v_mov_b32 v2, 0 + ]); + w.extend(mask_words); + w.extend([ + 0xBE80_246A, // s_and_saveexec_b64 s[0:1], vcc + 0xBF88_0006, // s_cbranch_execz (past the region) + 0x7E08_0C0E, // v_cvt_f32_u32 v4, s14 + 0x7E0E_02F4, // v_mov_b32 v7, 2.0 + if per_lane { + 0x7E10_0D00 // v_cvt_f32_u32 v8, v0 + } else { + 0x7E10_02F4 // v_mov_b32 v8, 2.0 + }, + 0x3E08_1107, // v_mac_f32 v4, v7, v8 + 0x7E18_0504, // v_readfirstlane_b32 s12, v4 + 0x7E04_020C, // v_mov_b32 v2, s12 + 0xBEFE_0400, // s_mov_b64 exec, s[0:1] + ]); + w.extend(store(2, 0)); + w.push(S_ENDPGM); + w +} + +#[test] +fn the_mac_read_programs_are_what_their_comments_say() { + for per_lane in [false, true] { + let words = mac_read_in_a_region(&MASK_LANES_12_44, per_lane); + let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let all = gfx10::decode(&code) + .expect("decodes") + .iter() + .map(|i| i.text(true)) + .collect::>() + .join("\n"); + for want in [ + "s_buffer_load_dword s14, s[44:47]", + "s_and_saveexec_b64 s[0:1], vcc", + "s_cbranch_execz", + "v_cvt_f32_u32 v4, s14", + "v_mov_b32 v7, 2.0", + if per_lane { + "v_cvt_f32_u32 v8, v0" + } else { + "v_mov_b32 v8, 2.0" + }, + "v_mac_f32 v4, v7, v8", + "v_readfirstlane_b32 s12, v4", + "v_mov_b32 v2, s12", + "s_mov_b64 exec, s[0:1]", + ] { + assert!(all.contains(want), "per_lane={per_lane}: {want:?} not in:\n{all}"); + } + } +} + +/// A multiply-accumulate adds to its old destination. Where that, like its +/// operands, is the same in every lane of the read's EXEC, so is its +/// result, and each half makes the read by itself: no halves for it, no +/// exchange. Before, any multiply-accumulate in the value's chain sent the +/// read to the halves. With a per-lane operand the read still takes them. +#[test] +fn a_first_lane_read_of_a_multiply_accumulate_is_made_in_each_half() { + for threads in [64, 128] { + let of = |per_lane: bool| { + let words = mac_read_in_a_region(&MASK_LANES_12_44, per_lane); + lowered(&native(&words, true, threads, 0), true) + .unwrap_or_else(|e| panic!("per_lane={per_lane} {threads} threads: {e}")) + }; + let alike = of(false); + assert_eq!(alike.barriers, 0, "{threads} threads: no exchange"); + let per_lane = of(true); + assert_eq!(per_lane.wave_size, Some(32)); + assert!(per_lane.barriers > 0, "{threads} threads: the halves pass the first lane"); + } +} + +/// Both give the console's lanes on every GPU: the region's lanes store +/// `41.0 + 2.0 * 2.0`, or with a per-lane operand `41.0 + 2.0 *` the thread +/// id of the console wave's first lane in the region. +#[cfg(windows)] +#[test] +fn a_first_lane_read_of_a_multiply_accumulate_matches_the_console_on_every_gpu() { + type InRegion = fn(u32) -> bool; + let masks: [(&str, &[u32; 2], InRegion, &[u32]); 3] = [ + ("lower_8_9", &MASK_LANES_8_9, |t| t >> 1 == 4, &[64]), + ("upper_40_41", &MASK_LANES_40_41, |t| t >> 1 == 20, &[64]), + ("both_12_44", &MASK_LANES_12_44, |t| t & 31 == 12, &[64, 128]), + ]; + let mut failed = Vec::new(); + for (label, mask, in_region, groups) in masks { + for &threads in groups { + for per_lane in [false, true] { + let words = mac_read_in_a_region(mask, per_lane); + let want: Vec = (0..threads) + .map(|t| { + if !in_region(t) { + return 0; + } + let base = t & !63; + let first = (base..base + 64).find(|&l| in_region(l)).expect("in it"); + let operand = if per_lane { first as f32 } else { 2.0 }; + (41.0 + 2.0 * operand).to_bits() + }) + .collect(); + let found = differences_with_buffer( + true, &words, threads, 1, &want, 0, false, false, &[41, 0, 0, 0], + ); + failed.extend( + found + .into_iter() + .map(|m| format!("{label}(per_lane={per_lane}) {threads} threads: {m}")), + ); + } + } + } + assert!(failed.is_empty(), "{}", failed.join("\n")); +} diff --git a/crates/victory-shader/src/emit/lower/first_lane.rs b/crates/victory-shader/src/emit/lower/first_lane.rs index 11d48a0a8..b0c2c40c9 100644 --- a/crates/victory-shader/src/emit/lower/first_lane.rs +++ b/crates/victory-shader/src/emit/lower/first_lane.rs @@ -12,7 +12,9 @@ //! lane holds the same function of the same scalars. The scalars are //! root parameters and what scalar loads and scalar ALU operations make //! of them: what each host half computes for itself in every halves -//! translation, none of them made from a lane mask or a lane's value. +//! translation, none of them made from a lane mask or a lane's value. A +//! multiply-accumulate (`v_mac_f32`, `v_fmac_f32`) adds to its old +//! destination, which is held to the same as its operands. //! 2. **Only lanes EXEC has at the read see the result.** A half with no //! active lane reads a lane of its own that does not hold the value; that //! is harmless only if no lane outside the read's EXEC ever sees what it @@ -185,8 +187,8 @@ impl Analysis<'_, '_> { && f.carry.is_none() && f.opsel == 0 && ![ - "lane", "cndmask", "mbcnt", "_co_", "interp", "mac", "dot", "cmp", "16", "mix", - "perm", "sad", "msad", "qsad", + "lane", "cndmask", "mbcnt", "_co_", "interp", "dot", "cmp", "16", "mix", "perm", + "sad", "msad", "qsad", ] .iter() .any(|n| name.contains(n)); @@ -194,6 +196,18 @@ impl Analysis<'_, '_> { { return false; } + // A multiply-accumulate adds to its old destination: an operand too. + if name.contains("mac") { + let Some(Operand::Vector { reg, .. }) = f.dst else { + return false; + }; + if !inst + .read(Reg::V(reg)) + .is_some_and(|old| self.active_alike(old, exec, memo)) + { + return false; + } + } f.src.iter().all(|op| match *op { Operand::Const(c) => c < 251, Operand::Literal(_) => true,