Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 192 additions & 12 deletions crates/victory-shader/src/emit/halves_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3881,27 +3881,207 @@ fn the_lanes_past_a_count_of_threads_do_not_run() {

/// A program whose only cross-half operations are first-lane reads goes to
/// halves for them (#522). Where halves cannot pass the read between them,
/// here in a loop counted to the host wave's first lane, it is translated as
/// before #522, each host wave reading its own first lane, rather than
/// refused (its draws dropped).
/// here in a loop counted to the console wave's first lane, it is refused,
/// with or without console lanes asked for as well: each 32-lane host wave
/// reading its own first lane would count the upper half's loop to thread
/// 32 and store 32 there, where the console stores 0
/// ([`a_first_lane_read_in_a_loop_matches_the_console_on_every_gpu`]).
#[test]
fn first_lane_reads_halves_refuse_fall_back_to_the_host_waves() {
fn first_lane_reads_halves_cannot_pass_are_refused() {
let words = first_lane_in_a_counted_loop();
let with_mbcnt = in_a_loop(&[0x7E18_0500], 12, Count::FirstLane);
for threads in [64, 128, 256] {
for program in [&words, &with_mbcnt] {
let why = refusal(program, threads, 0);
assert!(
why.contains("a first-lane read of a value that may differ between 32-lane halves"),
"{threads} threads: {why}"
);
}
}
}

/// [`in_a_loop`] of `v_readfirstlane_b32 s12, v0` counted to the first
/// lane, without its `v_mbcnt`: a program whose only cross-half operations
/// are first-lane reads, one of them in a loop.
fn first_lane_in_a_counted_loop() -> Vec<u32> {
let mbcnt = [0xD765_0001, 0x0001_00C1]; // v_mbcnt_lo_u32_b32 v1, -1, 0
let first = [0x7E18_0500]; // v_readfirstlane_b32 s12, v0
let with_mbcnt = in_a_loop(&first, 12, Count::FirstLane);
let at = with_mbcnt
.windows(2)
.position(|w| w == mbcnt)
.expect("in_a_loop's v_mbcnt");
let words = [&with_mbcnt[..at], &with_mbcnt[at + 2..]].concat();
for threads in [64, 128, 256] {
assert_eq!(refusal(&words, threads, 0), "translated", "{threads} threads");
// With console lanes asked for as well, the program stays in halves,
// and the read is still refused there.
let why = refusal(&with_mbcnt, threads, 0);
[&with_mbcnt[..at], &with_mbcnt[at + 2..]].concat()
}

/// A first-lane read the halves cannot pass between them is refused, never
/// made by each 32-lane host wave from its own first lane: on the console
/// every lane of a wave64 reads the wave's first lane, thread `l & !63`,
/// where the upper host wave's own first lane is thread `l & !31`.
#[cfg(windows)]
#[test]
fn a_first_lane_read_in_a_loop_matches_the_console_on_every_gpu() {
let words = first_lane_in_a_counted_loop();
let mut failed = Vec::new();
for threads in [64, 128] {
match lowered(&native(&words, true, threads, 0), true) {
Err(e) => {
let why = e.to_string();
let across = "a first-lane read of a value that may differ between 32-lane halves";
assert!(why.contains(across), "{threads} threads: {why}");
}
Ok(_) => {
let want: Vec<u32> = (0..threads).map(|l| l & !63).collect();
failed.extend(differences(&words, threads, 1, &want));
}
}
}
assert!(failed.is_empty(), "{}", failed.join("\n"));
}

/// `v_readfirstlane_b32 s12, v4` inside an `s_and_saveexec_b64` region
/// whose EXEC is `mask_words`'s, of `v4 = f32(s14) + 1.0` made there, with
/// `s14` the first dword of the SRV buffer at table dword 8 (a scalar load,
/// as in GTA VI's reciprocals of constant-buffer divisors): the same in
/// every lane EXEC has. `inside`: `v2 = s12` under the region's EXEC (only
/// lanes that ran the read see it), else after EXEC is restored (every
/// lane).
fn readfirstlane_of_a_scalar_in_branch(
mask_words: &[u32],
with_mbcnt: bool,
inside: bool,
) -> Vec<u32> {
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
]);
if with_mbcnt {
w.extend([
0xD765_0001,
0x0001_00C1, // v_mbcnt_lo_u32_b32 v1, -1, 0
]);
}
w.extend(mask_words);
w.push(0xBE80_246A); // s_and_saveexec_b64 s[0:1], vcc
let region = [
0x7E08_0C0E, // v_cvt_f32_u32 v4, s14
0x0608_08F2, // v_add_f32 v4, 1.0, v4
0x7E18_0504, // v_readfirstlane_b32 s12, v4
];
let skip = region.len() as u32 + u32::from(inside);
w.push(0xBF88_0000 | skip); // s_cbranch_execz (past the region)
w.extend(region);
if inside {
w.push(0x7E04_020C); // v_mov_b32 v2, s12
}
w.push(0xBEFE_0400); // s_mov_b64 exec, s[0:1]
if !inside {
w.push(0x7E04_020C); // v_mov_b32 v2, s12
}
w.extend(store(2, 0));
w.push(S_ENDPGM);
w
}

#[test]
fn the_scalar_readfirstlane_programs_are_what_their_comments_say() {
for inside in [false, true] {
let words = readfirstlane_of_a_scalar_in_branch(&MASK_LANES_40_41, true, inside);
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let all = gfx10::decode(&code)
.expect("decodes")
.iter()
.map(|i| i.text(true))
.collect::<Vec<_>>()
.join("\n");
for want in [
"s_load_dwordx4 s[44:47], s[4:5], 0x000020",
"s_buffer_load_dword s14, s[44:47]",
"v_cmp_eq_u32 vcc, 20, v3",
"s_and_saveexec_b64 s[0:1], vcc",
"s_cbranch_execz",
"v_cvt_f32_u32 v4, s14",
"v_add_f32 v4, 1.0, v4",
"v_readfirstlane_b32 s12, v4",
"v_mov_b32 v2, s12",
] {
assert!(all.contains(want), "{want:?} not in:\n{all}");
}
}
}

/// A first-lane read of a value every lane EXEC has computed alike, whose
/// result only those lanes see, is made by each host half by itself: its
/// own first active lane, with no value passed between the halves (none of
/// an exchange's group barriers), so it translates also where the halves
/// cannot meet (#522 refused it there). Seen by lanes outside the read's
/// EXEC, it still passes the halves' first lanes and ballots between them.
#[test]
fn a_first_lane_read_only_active_lanes_see_is_made_in_each_half() {
for mask in [&MASK_LANES_8_9, &MASK_LANES_40_41, &MASK_LANES_12_44] {
let inside = readfirstlane_of_a_scalar_in_branch(mask, true, true);
let alone = lowered(&native(&inside, true, 64, 0), true).expect("read alone in halves");
assert_eq!(alone.wave_size, Some(32));
let outside = readfirstlane_of_a_scalar_in_branch(mask, true, false);
let across = lowered(&native(&outside, true, 64, 0), true).expect("read across in halves");
// At least two exchanges (the value, the ballot) of two barriers.
assert!(
why.contains("a first-lane read of a value that may differ between 32-lane halves"),
"{threads} threads: {why}"
across.barriers >= alone.barriers + 4,
"{} {}",
across.barriers,
alone.barriers
);
}
}

/// [`readfirstlane_of_a_scalar_in_branch`] read inside its region gives the
/// console's lanes on every hardware adapter: `f32(41) + 1.0` in the lanes
/// EXEC has, 0 elsewhere, with the active lanes in the lower half only, in
/// the upper half only (the lower host wave has none), in both, or none (the
/// region is skipped); with console lanes needed elsewhere (halves) and not.
#[test]
#[cfg(windows)]
fn a_first_lane_read_only_active_lanes_see_matches_the_console_on_every_gpu() {
let value = (41.0f32 + 1.0).to_bits();
let lower: fn(u32) -> bool = |l| l >> 1 == 4;
let upper: fn(u32) -> bool = |l| l >> 1 == 20;
let both: fn(u32) -> bool = |l| l & 31 == 12;
let none: fn(u32) -> bool = |_| false;
let mut failed = Vec::new();
for (label, mask, active) in [
("lower_8_9", &MASK_LANES_8_9, lower),
("upper_40_41", &MASK_LANES_40_41, upper),
("both_12_44", &MASK_LANES_12_44, both),
("empty", &MASK_EMPTY, none),
] {
for with_mbcnt in [false, true] {
let words = readfirstlane_of_a_scalar_in_branch(mask, with_mbcnt, true);
let want: Vec<u32> = (0..64).map(|l| if active(l) { value } else { 0 }).collect();
let found = differences_with_buffer(
true,
&words,
64,
1,
&want,
0,
false,
false,
&[41, 0, 0, 0],
);
failed.extend(
found
.into_iter()
.map(|msg| format!("{label}(mbcnt={with_mbcnt}): {msg}")),
);
}
}
assert!(failed.is_empty(), "{}", failed.join("\n"));
}
20 changes: 17 additions & 3 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod atomics;
mod barriers;
mod bvh;
mod f64;
mod first_lane;
/// Format loads that move 16-bit halves without the d16 form, a child
/// module so it shares the emitter's state.
#[path = "half_views.rs"]
Expand Down Expand Up @@ -428,6 +429,9 @@ struct Lower<'a> {
/// In halves: VGPR values the same in every lane of the console wave
/// ([`halves::lane_uniform`]), whose first lane either half reads.
alike: HashSet<Value>,
/// The results of first-lane reads each host half makes by itself
/// ([`first_lane::read_alone`]).
first_alone: HashSet<Value>,
/// Pixel: an ordered section (POPS), whose UAVs the host makes
/// rasterizer-ordered views.
pops: bool,
Expand Down Expand Up @@ -672,7 +676,6 @@ impl<'a> Lower<'a> {
bad: &'a HashMap<Value, Why>,
keep: Option<&KeptOutputs>,
per_vertex_center: bool,
first_lane_halves: bool,
meet_plan: &'a Set<MeetSite>,
meet_found: &'a std::cell::RefCell<Set<MeetSite>>,
) -> Self {
Expand Down Expand Up @@ -704,6 +707,13 @@ impl<'a> Lower<'a> {
let mut console_lanes = false;
let (mut halves, mut halves_refused) = (false, None);
let mut alike_cache: Option<HashSet<Value>> = None;
// First-lane reads each 32-lane host half of a console wave makes
// by itself (`first_lane`).
let first_alone = if x.host_wave32 {
first_lane::read_alone(x)
} else {
HashSet::new()
};
let mut split_barriers = Set::default();
let mut pred = predicate::PredicatePlan::empty(Vec::new());
if let StageIo::Cs(cs) = x.native.io {
Expand Down Expand Up @@ -746,7 +756,7 @@ impl<'a> Lower<'a> {
.iter()
.any(|a| matches!(a, Atom::Opaque(_) | Atom::Constant(_)))
})
} || (first_lane_halves && halves::needs_first_lane(x, &mut alike_cache));
} || halves::needs_first_lane(x, &mut alike_cache, &first_alone);
let wave = if x.native.wave64 { 64 } else { 32 };
let group = cs.threads.iter().product::<u32>();
let one_wave = group <= wave;
Expand Down Expand Up @@ -941,6 +951,7 @@ impl<'a> Lower<'a> {
} else {
HashSet::new()
},
first_alone,
pops: matches!(x.native.io, StageIo::Ps { db_shader_control, .. }
if db_shader_control & 1 << 16 != 0),
depth_before_shader: matches!(x.native.io, StageIo::Ps { db_shader_control, .. }
Expand Down Expand Up @@ -7611,7 +7622,10 @@ impl<'a> Lower<'a> {
let is_vec = matches!(v.src.first(), Some(&Operand::Vector { .. }));
let is_alike = matches!(v.src.first(), Some(&Operand::Vector { reg, .. })
if inst.read(Reg::V(reg)).is_some_and(|x| self.alike.contains(&x)));
if self.halves && !self.one_host_wave() && is_vec && !is_alike {
// Each half's own first active lane is the console's
// (`first_lane`): read it, with no exchange.
let alone = inst.writes.first().is_some_and(|w| self.first_alone.contains(&w.1));
if self.halves && !self.one_host_wave() && is_vec && !is_alike && !alone {
if !self.across_here() {
return Err(halves::FIRST_ACROSS);
}
Expand Down
Loading