From c1d81790a3a27775a16685184c074fe2f6caff1c Mon Sep 17 00:00:00 2001 From: Diego Casares Date: Fri, 27 Nov 2026 19:14:50 +0100 Subject: [PATCH] Shaders: take a mask's lane bit by console lane in halves mode A console wave64 compute program that runs as two host waves of 32 ("halves") numbers its lanes through console_thread (thread_id does). console_lane and lane_bit still read the host lane index unless exchange is set. A 64-bit mask read as a number, such as a select on an SGPR pair, then gave lanes 32-63 the bits of the low dword: their host lane is 0-31, so the test `lane < 32` always picked the low dword. Use the console lane in both places when halves is set, as exchange already does. (cherry picked from commit 245cb85145bfd083e3df6769adf68dbdbb670340) --- crates/victory-shader/src/emit/lower.rs | 6 +-- .../victory-shader/src/emit/wave64_tests.rs | 47 ++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index b58845c..2e3858c 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -4560,9 +4560,9 @@ impl<'a> Lower<'a> { } /// This thread's console lane (0..63): its relabelled thread index (`thread_id`), whose host - /// wave holds a 32-aligned run of console lanes. Without `exchange`: the host lane. + /// wave holds a 32-aligned run of console lanes. Without `exchange` or `halves`: the host lane. fn console_lane(&mut self) -> V { - if self.exchange.is_none() { + if self.exchange.is_none() && self.halves.is_none() { return self.lane_index(); } if let Some(v) = self.clane { @@ -4941,7 +4941,7 @@ impl<'a> Lower<'a> { /// This lane's bit of the mask `lo`, `hi` (wave64). fn lane_bit(&mut self, at: usize, lo: V, hi: Option) -> V { self.s.flags |= flags::WAVE_OPS; - let lane = if self.exchange.is_some() { + let lane = if self.exchange.is_some() || self.halves.is_some() { self.console_lane() } else { self.s.op(at, "waveGetLaneIndex", Attr::ReadOnly, self.s.t.i32, 111, &[]) diff --git a/crates/victory-shader/src/emit/wave64_tests.rs b/crates/victory-shader/src/emit/wave64_tests.rs index 1900922..7e529de 100644 --- a/crates/victory-shader/src/emit/wave64_tests.rs +++ b/crates/victory-shader/src/emit/wave64_tests.rs @@ -292,6 +292,12 @@ fn on_64_lanes(native: &NativeProgram, p: &ir::Program) -> HostShader { /// Dword 0 of the UAV after `shader` ran with root constants `constants`; none without a /// device that takes it. fn dispatched(shader: &HostShader, constants: &[u32]) -> Option { + dispatched_words(shader, constants, 1).map(|words| words[0]) +} + +/// The first `dwords` dwords of the UAV after `shader` ran with root constants `constants`; +/// none without a device that takes it. +fn dispatched_words(shader: &HostShader, constants: &[u32], dwords: u32) -> Option> { let _one = super::valu_tests::ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner()); let Ok(host) = Host::new(false) else { eprintln!("no SM 6.6 hardware device; not run"); @@ -316,13 +322,13 @@ fn dispatched(shader: &HostShader, constants: &[u32]) -> Option { &pso, &Dispatch { roots: vec![(0, Root::Constants(ud))], - views: &[View::RawUav(1)], + views: &[View::RawUav(dwords)], samplers: &[], groups: [1, 1, 1], }, ) .unwrap_or_else(|e| panic!("dispatch failed: {e}")); - Some(out[0][0]) + Some(out[0][..dwords as usize].to_vec()) } /// The same sum inside `if (t >= s0) { ... }` on a host of 64-lane waves: the lanes below s0 @@ -615,6 +621,43 @@ fn on_the_card(shader: &HostShader, constants: [u32; 2]) -> Option { Some(out[0][0]) } +/// A 64-bit mask the program does not track, read as a number by a select, in a console wave of +/// 64 that runs as two host waves of 32 (the DPP move makes it take the console's lanes, so the +/// halves form): `v_cndmask_b32` reads s[2:3] (root constants), so each lane takes its own bit of +/// it, lanes 32-63 the high dword's. Their host lane numbers are 0-31 in the second host wave, so +/// they must be numbered by their console lane, not by the host lane. +#[test] +fn lanes_past_32_take_the_high_dword_of_a_mask_held_as_a_number() { + let words = [ + 0x7E0E_02FA, + 0xFFFF_B100, // v_mov_b32_dpp v7, v0 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf + 0xD501_0005, + 0x0009_0280, // v_cndmask_b32 v5, 0, 1, s[2:3] + 0x340C_0082, // v_lshlrev_b32 v6, 2, v0 + 0xE070_1000, + 0x8002_0506, // buffer_store_dword v5, v6, s[8:11], 0 offen + ]; + let native = native(&words); + let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build"); + let shader = lowered_on(&native, &p, &HashSet::new(), 32).expect("lower"); + for (lo, hi) in [(0x5555_0F0F, 0xA0F0_1234), (u32::MAX, 0), (0, u32::MAX)] { + let Some(out) = dispatched_words(&shader, &[0, 0, lo, hi], 64) else { + return; + }; + for lane in 0..64u32 { + let bit = if lane < 32 { + (lo >> lane) & 1 + } else { + (hi >> (lane - 32)) & 1 + }; + assert_eq!( + out[lane as usize], bit, + "lane {lane} of the mask {hi:08x}{lo:08x}" + ); + } + } +} + /// A 32-lane console wave. A register holds a lane mask on one path (the lanes below s0) /// and a number on the other, and is read as a number after the join, as the trace programs /// keep their lanes' masks and their node numbers on one stack