Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ struct Lower<'a> {
wave_ids: Option<(V, V, V)>,
/// Active `begin_guarded` nesting depth (never emit barriers while > 0).
guard_depth: u32,
/// First-lane reads elected by a uniform predecessor before it splits on EXEC.
firstlane_at_entry: HashMap<usize, V>,
/// Masks read as data in a block other than their defining block, keyed by `def_block`.
cross_block_masks: HashMap<Option<usize>, Vec<Value>>,
/// Errors deferred from pre-emitted cross-block masks.
Expand Down Expand Up @@ -899,6 +901,7 @@ impl<'a> Lower<'a> {
val_uniform,
wave_ids: None,
guard_depth: 0,
firstlane_at_entry: HashMap::new(),
cross_block_masks,
ballot_fail: HashMap::new(),
flat: None,
Expand Down Expand Up @@ -1372,6 +1375,7 @@ impl<'a> Lower<'a> {
self.mask_ok = self.mask_data == MaskData::Console;
if self.emulating_wave64() {
self.preemit_masks(Some(b));
self.elect_before_exec_branch(b)?;
}
if lds_fences.leave.contains(&b) {
self.lds_barrier(self.cur);
Expand Down Expand Up @@ -6670,6 +6674,11 @@ impl<'a> Lower<'a> {
_ => false,
});
if self.emulating_wave64() {
if i == 0
&& let Some(v) = self.firstlane_at_entry.remove(&b)
{
return int(self, v);
}
if !non_uniform {
return int(self, a);
}
Expand Down
71 changes: 71 additions & 0 deletions crates/victory-shader/src/emit/lower/wave64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,77 @@ impl<'a> Lower<'a> {
Ok(self.s.m.load(self.cur, rptr))
}

/// A read at the start of the active EXEC edge can elect across the whole
/// guest wave while all host waves are still together in its predecessor.
pub(super) fn elect_before_exec_branch(&mut self, b: usize) -> Result<(), Stop> {
let block = &self.x.p.blocks[b];
let LaneExit::Branch {
when,
taken,
not_taken,
} = self.x.st.exits[b]
else {
return Ok(());
};
let branch = block.insts.last().expect("branch instruction");
let (active, on_taken) = match branch.name() {
"s_cbranch_execz" => (not_taken, false),
"s_cbranch_execnz" => (taken, true),
_ => return Ok(()),
};
let next = &self.x.p.blocks[active];
if !self.uniform[b]
|| self.uniform[active]
|| next.preds.as_slice() != [b]
|| self.rpo[active] <= self.rpo[b]
{
return Ok(());
}
let Some(read) = next.insts.first() else {
return Ok(());
};
if read.name() != "v_readfirstlane_b32"
|| read
.writes
.first()
.is_some_and(|w| self.x.res.waterfall.reads.contains(&w.1))
{
return Ok(());
}
let Fields::Valu(v) = &read.fields else {
unreachable!()
};
let [Operand::Vector { reg, .. }] = v.src[..] else {
return Ok(());
};
let Some(src) = read.read(Reg::V(reg)) else {
return Ok(());
};
// A successor phi is unavailable here. A DPP source might itself
// read another lane, so leave that form to the ordinary lowering.
if !self.vals.contains_key(&src) || self.val_uniform[src.0 as usize] || v.dpp.is_some() {
return Ok(());
}
let at = read.machine.offset;
let Ok(value) = self.src(read, 0) else {
return Ok(());
};
let condition = self
.boolean(when, self.cur)
.map_err(|w| unsupported(at, w))?;
let enabled = if on_taken {
condition
} else {
let yes = self.s.m.bool(true);
self.s.m.bin(self.cur, Bin::Xor, condition, yes)
};
let picked = self
.emulated_readfirstlane(value, Some(enabled))
.map_err(|w| unsupported(at, w))?;
self.firstlane_at_entry.insert(active, picked);
Ok(())
}

/// Reads `val` from the first active lane of the host hardware wave.
pub(super) fn host_wave_readfirstlane(&mut self, val: V, act: V) -> V {
let c = self.cur;
Expand Down
53 changes: 49 additions & 4 deletions crates/victory-shader/src/emit/wave64_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,10 @@ fn divergent_cross_half_ballot_is_rejected_instead_of_hanging() {
}

#[test]
fn divergent_readfirstlane_reads_its_halfs_first_lane_on_wave32() {
fn divergent_readfirstlane_preserves_the_requested_wave_range() {
// if (v0 < 32) { s4 = readfirstlane(v0); v1 = s4; } else { v1 = 0; } store(v1)
// Inside divergent flow the halves cannot meet at a barrier: Wave32 reads
// each half's own first active lane, and Emulate reads the host wave's
// first active lane without a barrier.
// Both emulated forms can elect before the split, including on a host
// that cannot request Wave32.
let mut code = Vec::new();
code.push(v_mov_b32(1, inline_u(0)));
code.push(v_cmp_gt_u32(inline_u(32), 0)); // 32 > v0
Expand Down Expand Up @@ -1639,6 +1638,52 @@ mod gpu {
);
}

#[test]
fn gpu_divergent_readfirstlane_spans_host_waves() {
// The source is the group-thread ID. Cover a mask spanning halves,
// a first active lane in the high half, and a wholly inactive wave.
let cases = [
("first_40", v_cmp_gt_u32(inline_u(40), 3), [0..40, 64..104]),
("last_25", v_cmp_lt_u32(inline_u(38), 3), [39..64, 103..128]),
(
"empty_second",
v_cmp_gt_u32(inline_u(40), 0),
[0..40, 64..64],
),
];
for (name, compare, ranges) in cases {
let expected: Vec<u32> = (0..128)
.map(|id| {
ranges
.iter()
.find(|r| r.contains(&id))
.map_or(42, |r| r.start)
})
.collect();
for taken in [false, true] {
let mut code = vec![v_mov_b32(1, inline_u(42))];
code.extend(v_mbcnt_lo_u32_b32(3, NEG_ONE, inline_u(0)));
code.extend(v_mbcnt_hi_u32_b32(3, NEG_ONE, vgpr(3)));
code.push(compare);
code.push(s_and_saveexec_b64(6, VCC));
if taken {
code.push(sopp(0x09, 1)); // s_cbranch_execnz -> read
code.push(sopp(0x02, 2)); // s_branch -> restore
} else {
code.push(s_cbranch_execz(2)); // -> restore
}
code.push(v_readfirstlane_b32(4, 0));
code.push(v_mov_b32(1, 4));
code.push(s_or_b64(EXEC_LO, EXEC_LO, 6));
emit_uav_store(&mut code, 1, 0);
code.push(S_ENDPGM);
let prog = make_cs_program(&code, [128, 1, 1], 0, false);
let case = format!("divergent_readfirstlane_{name}_taken_{taken}");
run_on_hosts_modes(&case, &prog, [1, 1, 1], 128, &expected, true, true);
}
}
}

#[test]
fn gpu_64_threads_empty_exec_readfirstlane_returns_lane0() {
// GFX10 v_readfirstlane_b32 with EXEC == 0 returns lane 0's value (`v2[0] = 7`).
Expand Down