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
74 changes: 74 additions & 0 deletions crates/victory-shader/src/emit/halves_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,36 @@ fn reduction(op: u32, ident: u32, input: u32) -> Vec<u32> {
w
}

/// A console wave32 waterfall loop over the low two bits of the thread id
/// (`s_andn2_wrexec_b32` takes each value's lanes out of the pending mask
/// and sets SCC to whether lanes remain), then the compiler's select on
/// that SCC after the loop: `s_cselect_b32` of EXEC or 0, OR-ed with the
/// lanes below 20. The loop leaves when no lanes remain, so SCC is 0 there
/// and the mask is the compare's. Per thread, one dword at `l * 32`: 9
/// below 20, else 7.
fn mask_selected_after_a_waterfall() -> Vec<u32> {
let mut w = PROLOGUE.to_vec();
w.extend([
0x3420_0085, // v_lshlrev_b32 v16, 5, v0
0x360A_0083, // v_and_b32 v5, 3, v0
0xBE83_037E, // s_mov_b32 s3, exec_lo
0xBE86_037E, // s_mov_b32 s6, exec_lo
0x7E0E_0505, // v_readfirstlane_b32 s7, v5
0x7DA4_0A07, // v_cmpx_eq_u32 s7, v5
0xBE86_4706, // s_andn2_wrexec_b32 s6, s6
0xBF89_FFFC, // s_cbranch_execnz (back to the v_readfirstlane)
0xBEFE_0303, // s_mov_b32 exec_lo, s3
0x7D88_0094, // v_cmp_gt_u32 vcc_lo, 20, v0
0x8504_807E, // s_cselect_b32 s4, exec_lo, 0
0x8804_046A, // s_or_b32 s4, vcc_lo, s4
0xD501_0001,
0x0011_1287, // v_cndmask_b32 v1, 7, 9, s4
]);
w.extend(store(1, 0));
w.push(S_ENDPGM);
w
}

/// The console's low-half f16 whole-wave64 sum of `v0`: DPP within rows,
/// `v_permlanex16` across rows, then lanes 31 and 63 (each 32-lane half's
/// sum) read and added. Its high half is kept from lane 31, as the
Expand Down Expand Up @@ -907,6 +937,50 @@ fn reductions_and_prefixes_across_halves_match_the_console_on_every_gpu() {
assert!(failed.is_empty(), "{}", failed.join("\n"));
}

/// [`mask_selected_after_a_waterfall`]: the SCC the waterfall loop's exit
/// shows is 0, so the select is the compare's mask and no lane mask is
/// read as data.
#[test]
fn a_mask_selected_on_a_waterfall_exits_scc_translates() {
let words = mask_selected_after_a_waterfall();
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(false))
.collect::<Vec<_>>()
.join("\n");
for want in [
"v_and_b32 v5, 3, v0",
"s_mov_b32 s3, exec_lo",
"s_mov_b32 s6, exec_lo",
"v_readfirstlane_b32 s7, v5",
"v_cmpx_eq_u32 exec_lo, s7, v5",
"s_andn2_wrexec_b32 s6, s6",
"s_cbranch_execnz",
"s_mov_b32 exec_lo, s3",
"v_cmp_gt_u32 vcc_lo, 20, v0",
"s_cselect_b32 s4, exec_lo, 0",
"s_or_b32 s4, vcc_lo, s4",
"v_cndmask_b32 v1, 7, 9, s4",
] {
assert!(all.contains(want), "{want:?} not in:\n{all}");
}
let n = native(&words, false, 32, 0);
lowered(&n, false).expect("wave32");
}

/// [`mask_selected_after_a_waterfall`] on every hardware adapter gives the
/// console's values.
#[test]
#[cfg(windows)]
fn a_mask_selected_on_a_waterfall_exits_scc_matches_the_console_on_every_gpu() {
let want: Vec<u32> = (0..32).map(|l| if l < 20 { 9 } else { 7 }).collect();
let words = mask_selected_after_a_waterfall();
let failed = differences_in(false, &words, 32, 1, &want, 0, false, false);
assert!(failed.is_empty(), "{}", failed.join("\n"));
}

/// Lane reads and first-lane reads that may be in the other half pass
/// between the halves where every lane of the group runs them together
/// (once, or in a loop on a count every host wave computes alike), and are
Expand Down
65 changes: 60 additions & 5 deletions crates/victory-shader/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,55 @@ struct Lanes<'a> {
kills: HashSet<(usize, usize)>,
}

/// SCC value `scc` in block `at`, where the program shows it: SCC that a
/// lane-mask operation writing EXEC set to whether its result (the new
/// EXEC) has a lane, read in a block reached only through the edge of an
/// `s_cbranch_execz` or `s_cbranch_execnz` on that same EXEC (a chain of
/// blocks of one predecessor each). That edge says whether the console
/// wave's EXEC had a lane, so it says SCC too. The console compiler leaves
/// such reads after a waterfall loop, whose last `s_andn2_wrexec` sets SCC
/// to whether lanes remain and whose exit is taken when none do.
fn exec_scc(p: &Program, defs: &Map<Value, (usize, usize)>, scc: Value, at: usize) -> Option<bool> {
let &(b, i) = defs.get(&scc)?;
let def = &p.blocks[b].insts[i];
let width = if p.wave64 { "_b64" } else { "_b32" };
let op = def.name().strip_prefix("s_")?.strip_suffix(width)?;
let op = op
.strip_suffix("_saveexec")
.or_else(|| op.strip_suffix("_wrexec"))
.unwrap_or(op);
let logical = [
"and", "or", "xor", "andn2", "orn2", "andn1", "orn1", "nand", "nor", "xnor", "not",
];
if !logical.contains(&op) || def.write(Reg::Scc) != Some(scc) {
return None;
}
let exec = (def.write(Reg::S(EXEC))?, def.write(Reg::S(EXEC + 1)));
let mut block = at;
for _ in 0..p.blocks.len() {
let &[q] = &p.blocks[block].preds[..] else {
return None;
};
if let Exit::Branch { taken, not_taken } = p.blocks[q].exit
&& taken != not_taken
&& let Some(branch) = p.blocks[q].insts.last()
&& (
branch.read(Reg::S(EXEC)),
branch.read(Reg::S(EXEC + 1)).filter(|_| p.wave64),
) == (Some(exec.0), exec.1.filter(|_| p.wave64))
{
let none = match branch.name() {
"s_cbranch_execz" => taken,
"s_cbranch_execnz" => not_taken,
_ => return None,
};
return Some(block != none);
}
block = q;
}
None
}

/// A mask join taken as `value` in every lane that reaches `block`: each
/// incoming mask held (or failed) on its edge.
struct Join {
Expand Down Expand Up @@ -1209,11 +1258,17 @@ impl<'a> Lanes<'a> {
let (a, c) = (arg(0, self)?, arg(1, self)?);
let scc = inst.read(Reg::Scc)?;
let born = self.born(scc);
let u = self
.scc_votes
.get(&scc)
.copied()
.unwrap_or_else(|| self.b.atom(Atom::Uniform(scc), born));
let at = inst.writes.first().and_then(|w| self.defs.get(&w.1));
let known = at.and_then(|&(b, _)| exec_scc(self.p, &self.defs, scc, b));
let u = match known {
Some(true) => Bool::TRUE,
Some(false) => Bool::FALSE,
None => self
.scc_votes
.get(&scc)
.copied()
.unwrap_or_else(|| self.b.atom(Atom::Uniform(scc), born)),
};
(self.b.ite(u, a, c), None)
}
_ => {
Expand Down