Console waves' meetings: cheaper, fewer, and none where LDS order does not count
CS_DefragHeap_ComputeRelocations took 11.9 ms of GPU time a frame with the pair meetings of ce383f8 (run 184458, the card's time by command). Three changes, all for console waves meeting on their own (`with_pair_meetings`: the digest list, VICTORY_WAVE_WIDE=1): - LDS order (`LdsOrder`): the program cut at its s_barriers; an access needs the host waves of its console wave to meet only when a plain ds_write stands in a piece it can reach or be reached from. Atomics among themselves and reads of what an earlier piece wrote need none. In ComputeRelocations the relocation pass (reads, ds_cmpst loops, ds_add_rtn) and the copy pass after it lose every meeting, and their per-lane loops are no longer taken wave-wide: 8 branches widened instead of 18, 50 meetings in the code instead of 138. Reads near an LDS atomic are not taken as alike in every lane. - A meeting: the first lane of each host wave counts it in (one atomic per host wave, two a meeting), the wave polls the counter with one shared load behind a fence. Before: an atomic per lane to arrive and per lane per poll. - An exchange is one meeting: two buffers per console wave, taken in turn by the count of meetings (a half writes one only after the meeting its partner's reads of it came before). Before: two meetings. A first-lane read exchanges "has a lane" and the value at once. Other programs translate as before (900 compute programs, the same DXIL with VICTORY_HOST_MAX_WAVE=32); ComputeRelocations validates. Tests: the two console waves' loops still bit-exact on the RTX 4070 SUPER; a new test for the LDS order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
02b255d25b
commit
89cfae291b
2 files changed
+329
-58
No files matched your search
@@ -365,6 +365,12 @@ struct Lower<'a> {
|
||||
/// thread's console wave (made in the entry block).
|
||||
pairs: Option<u32>,
|
||||
meeting: Option<V>,
|
||||
/// With `pairs`: the LDS accesses that need no meeting ([`LdsOrder`]).
|
||||
lds_unordered: HashSet<(usize, usize)>,
|
||||
/// With `pairs`: the console waves of the group, and the first of this console wave's
|
||||
/// exchange dwords ([`Self::exchange_dword`], made in the entry block).
|
||||
pair_waves: u32,
|
||||
pair_base: Option<V>,
|
||||
/// Vertex and mesh outputs holding 16-bit values: row, whether two per
|
||||
/// dword, and the row packed after it.
|
||||
half_outputs: HashMap<u8, (bool, Option<u8>)>,
|
||||
@@ -568,7 +574,13 @@ fn need(block: usize) {
|
||||
/// wave-uniform data in VGPRs and fetches it with `v_readfirstlane`.
|
||||
/// shortcut: a value joined by a phi counts only where every predecessor is a block all lanes
|
||||
/// run (no lane branch can have split the arriving lanes); loop-carried values never do.
|
||||
fn lane_uniform(p: &Program, st: &Structured, all: &[bool], ordered_lds: bool) -> HashSet<Value> {
|
||||
fn lane_uniform(
|
||||
p: &Program,
|
||||
st: &Structured,
|
||||
all: &[bool],
|
||||
ordered_lds: bool,
|
||||
unstable: &HashSet<(usize, usize)>,
|
||||
) -> HashSet<Value> {
|
||||
let is_mask = |v: Value| st.masks.contains_key(&v) || st.highs.contains_key(&v);
|
||||
let mut same: HashSet<Value> = HashSet::new();
|
||||
for b in crate::ir::reverse_postorder(&p.blocks) {
|
||||
@@ -598,7 +610,7 @@ fn lane_uniform(p: &Program, st: &Structured, all: &[bool], ordered_lds: bool) -
|
||||
addr: Some(Operand::Vector { reg, dwords: 1 }),
|
||||
gds: false,
|
||||
..
|
||||
} if ordered_lds && name == "ds_read_b32" => {
|
||||
} if ordered_lds && name == "ds_read_b32" && !unstable.contains(&(b, i)) => {
|
||||
if inst.read(Reg::V(*reg)).is_some_and(|a| same.contains(&a)) {
|
||||
same.extend(inst.writes.iter().map(|w| w.1));
|
||||
}
|
||||
@@ -957,12 +969,129 @@ fn meeting_mode() -> u32 {
|
||||
/// host waves keep that order if they meet between any two accesses of which one writes. A
|
||||
/// barrier must be run by every thread, so it cannot stand in a lane branch's region; but
|
||||
/// every way into and out of such a region passes a block all lanes run, and the waves meet
|
||||
/// LDS accesses (block, instruction) whose order against the other lanes of their console wave
|
||||
/// does not count, and of those the reads that may see an LDS atomic of another lane
|
||||
/// ([`with_pair_meetings`] only).
|
||||
#[derive(Default)]
|
||||
pub(super) struct LdsOrder {
|
||||
/// No plain write (`ds_write*`) can stand between them and another access without a group
|
||||
/// barrier (`s_barrier`) between: what they read was written before the last barrier, or
|
||||
/// is changed by atomics only, whose order among the lanes is any the console may take
|
||||
/// between its waves too (the triangle clip's relocation pass: a compare-exchange loop
|
||||
/// over the list a first pass wrote). They need no meeting of the host waves.
|
||||
pub(super) unordered: HashSet<(usize, usize)>,
|
||||
/// Of `unordered`, the reads near an LDS atomic: the two halves of a console wave may see
|
||||
/// different values there ([`lane_uniform`] does not take them as alike).
|
||||
pub(super) unstable: HashSet<(usize, usize)>,
|
||||
}
|
||||
|
||||
/// [`LdsOrder`] of `p`: the program cut at its `s_barrier`s into pieces joined by the edges
|
||||
/// between blocks; an access is ordered when a plain write stands in a piece it can reach or
|
||||
/// be reached from (or it is a plain write that can reach itself or another access).
|
||||
pub(super) fn lds_order(p: &crate::ir::Program, st: &Structured) -> LdsOrder {
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Kind {
|
||||
Read,
|
||||
Write,
|
||||
Atomic,
|
||||
}
|
||||
let n = st.exits.len().min(p.blocks.len());
|
||||
// pieces: per block, the barriers' places; piece k of block b lies after its k-th barrier
|
||||
let mut first = vec![0usize; n + 1];
|
||||
for b in 0..n {
|
||||
let barriers = p.blocks[b].insts.iter().filter(|i| i.name() == "s_barrier").count();
|
||||
first[b + 1] = first[b] + barriers + 1;
|
||||
}
|
||||
let pieces = first[n];
|
||||
let mut succ: Vec<Vec<usize>> = vec![Vec::new(); pieces];
|
||||
let mut accesses: Vec<Vec<(usize, usize, Kind)>> = vec![Vec::new(); pieces];
|
||||
for b in 0..n {
|
||||
let mut k = first[b];
|
||||
for (i, inst) in p.blocks[b].insts.iter().enumerate() {
|
||||
let name = inst.name();
|
||||
if name == "s_barrier" {
|
||||
k += 1;
|
||||
continue;
|
||||
}
|
||||
if !matches!(inst.fields, Fields::Ds { gds: false, .. }) || name.starts_with("ds_swizzle") || name.contains("permute") {
|
||||
continue;
|
||||
}
|
||||
let kind = if name.starts_with("ds_read") {
|
||||
Kind::Read
|
||||
} else if name.starts_with("ds_write") {
|
||||
Kind::Write
|
||||
} else {
|
||||
Kind::Atomic
|
||||
};
|
||||
accesses[k].push((b, i, kind));
|
||||
}
|
||||
let last = first[b + 1] - 1;
|
||||
let to: Vec<usize> = match st.exits[b] {
|
||||
LaneExit::Jump(t) => vec![t],
|
||||
LaneExit::Branch { taken, not_taken, .. } => vec![taken, not_taken],
|
||||
LaneExit::End => Vec::new(),
|
||||
// (anywhere: every access stays ordered)
|
||||
LaneExit::Indirect => return LdsOrder::default(),
|
||||
};
|
||||
succ[last].extend(to.into_iter().filter(|&t| t < n).map(|t| first[t]));
|
||||
}
|
||||
let mut pred: Vec<Vec<usize>> = vec![Vec::new(); pieces];
|
||||
for (a, s) in succ.iter().enumerate() {
|
||||
for &t in s {
|
||||
pred[t].push(a);
|
||||
}
|
||||
}
|
||||
// the pieces reachable from each (`next`) and reaching it, itself only on a cycle
|
||||
let reach = |edges: &Vec<Vec<usize>>, from: usize| -> Vec<bool> {
|
||||
let mut seen = vec![false; pieces];
|
||||
let mut stack = edges[from].clone();
|
||||
while let Some(x) = stack.pop() {
|
||||
if !seen[x] {
|
||||
seen[x] = true;
|
||||
stack.extend(edges[x].iter().copied());
|
||||
}
|
||||
}
|
||||
seen
|
||||
};
|
||||
let mut out = LdsOrder::default();
|
||||
for k in 0..pieces {
|
||||
if accesses[k].is_empty() {
|
||||
continue;
|
||||
}
|
||||
let (fwd, bwd) = (reach(&succ, k), reach(&pred, k));
|
||||
let near: Vec<&(usize, usize, Kind)> = (0..pieces)
|
||||
.filter(|&x| x == k || fwd[x] || bwd[x])
|
||||
.flat_map(|x| accesses[x].iter())
|
||||
.collect();
|
||||
let writes = near.iter().any(|a| a.2 == Kind::Write);
|
||||
let atomics = near.iter().any(|a| a.2 == Kind::Atomic);
|
||||
for &(b, i, kind) in &accesses[k] {
|
||||
let ordered = match kind {
|
||||
Kind::Read | Kind::Atomic => writes,
|
||||
Kind::Write => fwd[k] || near.iter().any(|a| (a.0, a.1) != (b, i)),
|
||||
};
|
||||
if !ordered {
|
||||
out.unordered.insert((b, i));
|
||||
if kind == Kind::Read && atomics {
|
||||
out.unstable.insert((b, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// there: before the exit of an all-lane block into a region with LDS accesses, and at the
|
||||
/// start of an all-lane block entered from one. Accesses in all-lane blocks get their own
|
||||
/// barrier as before. What that cannot order is a write and a read inside one region (blocks
|
||||
/// joined without an all-lane block between them): refused.
|
||||
/// shortcut: two writes in one region are taken not to hit the same dword from both halves.
|
||||
fn lds_meetings(p: &crate::ir::Program, st: &Structured, all: &[bool]) -> Option<Vec<(bool, bool)>> {
|
||||
fn lds_meetings(
|
||||
p: &crate::ir::Program,
|
||||
st: &Structured,
|
||||
all: &[bool],
|
||||
unordered: &HashSet<(usize, usize)>,
|
||||
) -> Option<Vec<(bool, bool)>> {
|
||||
let n = st.exits.len();
|
||||
let succ = |b: usize| -> Vec<usize> {
|
||||
match st.exits[b] {
|
||||
@@ -996,11 +1125,14 @@ fn lds_meetings(p: &crate::ir::Program, st: &Structured, all: &[bool]) -> Option
|
||||
continue;
|
||||
}
|
||||
let r = find(&mut region, b);
|
||||
for i in &block.insts {
|
||||
for (k, i) in block.insts.iter().enumerate() {
|
||||
let name = i.name();
|
||||
if !matches!(i.fields, Fields::Ds { .. }) || name.starts_with("ds_swizzle") || name.contains("permute") {
|
||||
continue;
|
||||
}
|
||||
if unordered.contains(&(b, k)) {
|
||||
continue; // (no meeting: [`LdsOrder`])
|
||||
}
|
||||
if name.starts_with("ds_read") {
|
||||
reads[r] = true;
|
||||
} else {
|
||||
@@ -1498,6 +1630,9 @@ impl<'a> Lower<'a> {
|
||||
let mut halves: Option<u32> = None;
|
||||
// its host waves meet per console wave (`with_pair_meetings`): the first counter's dword
|
||||
let mut pairs: Option<u32> = None;
|
||||
let mut pair_waves = 0;
|
||||
// (and only around the LDS accesses whose order counts)
|
||||
let lds_order = if PAIR_MEETINGS.get() { lds_order(p, x.st) } else { LdsOrder::default() };
|
||||
if let StageIo::Cs(cs) = x.native.io {
|
||||
s.threads = Some(cs.threads);
|
||||
s.lds_dwords = cs.lds_bytes() / 4;
|
||||
@@ -1546,18 +1681,21 @@ impl<'a> Lower<'a> {
|
||||
// two waves of 32, a group barrier before every LDS access keeps their order
|
||||
// – where every thread of the group gets there (no lane branch around it,
|
||||
// no guard), else a barrier would hang the group
|
||||
let lds = |i: &Inst| {
|
||||
// (with console waves meeting on their own, only the accesses whose order
|
||||
// counts: [`LdsOrder`])
|
||||
let lds = |b: usize, k: usize, i: &Inst| {
|
||||
matches!(i.fields, Fields::Ds { .. })
|
||||
&& !(i.name().starts_with("ds_swizzle") || i.name().contains("permute"))
|
||||
&& !lds_order.unordered.contains(&(b, k))
|
||||
};
|
||||
if let Some(all) = all_lane_blocks(x.st) {
|
||||
let ok = p.blocks.iter().enumerate().all(|(b, blk)| {
|
||||
blk.insts.iter().all(|inst| {
|
||||
!lds(inst) || all.get(b).copied().unwrap_or(false)
|
||||
blk.insts.iter().enumerate().all(|(k, inst)| {
|
||||
!lds(b, k, inst) || all.get(b).copied().unwrap_or(false)
|
||||
})
|
||||
});
|
||||
let mut lds_ok = !(s.lds_dwords > 0 && group > 32) || ok;
|
||||
if !lds_ok && let Some(meet) = lds_meetings(p, x.st, &all) {
|
||||
if !lds_ok && let Some(meet) = lds_meetings(p, x.st, &all, &lds_order.unordered) {
|
||||
lds_meet = Some((all.clone(), meet));
|
||||
lds_ok = true;
|
||||
}
|
||||
@@ -1565,7 +1703,9 @@ impl<'a> Lower<'a> {
|
||||
// (the blocks whose LDS accesses not every lane runs: with the
|
||||
// branches around them taken wave-wide they get their barriers)
|
||||
for (b, blk) in p.blocks.iter().enumerate() {
|
||||
if !all.get(b).copied().unwrap_or(false) && blk.insts.iter().any(lds) {
|
||||
if !all.get(b).copied().unwrap_or(false)
|
||||
&& blk.insts.iter().enumerate().any(|(k, inst)| lds(b, k, inst))
|
||||
{
|
||||
need(b);
|
||||
}
|
||||
}
|
||||
@@ -1611,7 +1751,7 @@ impl<'a> Lower<'a> {
|
||||
why.push("LDS in a group over 32");
|
||||
match all_lane_blocks(x.st) {
|
||||
None => why.push("an indirect exit"),
|
||||
Some(all) if lds_meetings(p, x.st, &all).is_none() => {
|
||||
Some(all) if lds_meetings(p, x.st, &all, &lds_order.unordered).is_none() => {
|
||||
why.push("an LDS read and write in one lane-branch region")
|
||||
}
|
||||
Some(_) => {}
|
||||
@@ -1626,8 +1766,10 @@ impl<'a> Lower<'a> {
|
||||
halves = Some(s.lds_dwords);
|
||||
s.lds_dwords += 1;
|
||||
if PAIR_MEETINGS.get() && group > 64 && group % 64 == 0 && (split_lds || exchange.is_some()) {
|
||||
// a counter per console wave, then two exchange buffers of four dwords each
|
||||
pairs = Some(s.lds_dwords);
|
||||
s.lds_dwords += group / 64;
|
||||
pair_waves = group / 64;
|
||||
s.lds_dwords += 9 * pair_waves;
|
||||
}
|
||||
}
|
||||
if console_lanes || one_wave && s.lds_dwords > 0 {
|
||||
@@ -1787,7 +1929,7 @@ impl<'a> Lower<'a> {
|
||||
local_any: LOCAL_ANY.get(),
|
||||
uniform: match &exchange {
|
||||
Some((_, false, all)) if !LOCAL_WAVES.get() => {
|
||||
lane_uniform(p, x.st, all, split_lds && super::group_sync() != 0)
|
||||
lane_uniform(p, x.st, all, split_lds && super::group_sync() != 0, &lds_order.unstable)
|
||||
}
|
||||
_ => HashSet::new(),
|
||||
},
|
||||
@@ -1804,6 +1946,9 @@ impl<'a> Lower<'a> {
|
||||
wave_base: None,
|
||||
pairs,
|
||||
meeting: None,
|
||||
lds_unordered: lds_order.unordered.clone(),
|
||||
pair_waves,
|
||||
pair_base: None,
|
||||
flat: None,
|
||||
no_barrier,
|
||||
fence_only,
|
||||
@@ -4302,47 +4447,48 @@ impl<'a> Lower<'a> {
|
||||
|
||||
/// The two host waves of this thread's console wave meet: what either wrote to LDS
|
||||
/// before is seen by both after. A group barrier with an LDS fence; with
|
||||
/// [`with_pair_meetings`], the console wave's own meeting: each of its 64 lanes counts
|
||||
/// itself in on the console wave's LDS counter and waits (polling, at most
|
||||
/// [`MEETING_POLLS`] times) until the count reaches the next multiple of 64. The code
|
||||
/// [`with_pair_meetings`], the console wave's own meeting: the first lane of each host
|
||||
/// wave counts it in on the console wave's LDS counter (two a meeting), and the wave reads
|
||||
/// the counter (one load every lane shares, behind a fence so that it is read again)
|
||||
/// until it reaches the next even number, at most [`MEETING_POLLS`] times. (The first
|
||||
/// form counted each of the 64 lanes in with an atomic and polled with an atomic per lane:
|
||||
/// 11.9 ms a frame for `CS_DefragHeap_ComputeRelocations` alone, run 184458.) The code
|
||||
/// after it is in a new block ([`Self::cur`]).
|
||||
fn meet(&mut self) {
|
||||
let Some(first) = self.pairs else {
|
||||
if self.pairs.is_none() {
|
||||
let c = self.cur;
|
||||
self.group_sync(c);
|
||||
return;
|
||||
};
|
||||
let (i32t, void) = (self.s.t.i32, self.s.t.void);
|
||||
}
|
||||
let (i1t, i32t, void) = (self.s.t.i1, self.s.t.i32, self.s.t.void);
|
||||
let fence = self.s.m.i32(8);
|
||||
let head = self.cur;
|
||||
self.s.op(head, "barrier", Attr::NoDuplicate, void, 80, &[fence]);
|
||||
if super::group_sync() == 0 {
|
||||
return;
|
||||
}
|
||||
let index = match self.meeting {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let e = self.s.entry;
|
||||
let thread = self.console_thread();
|
||||
let six = self.s.m.i32(6);
|
||||
let wave = self.s.m.bin(e, Bin::LShr, thread, six);
|
||||
let base = self.s.m.i32(first);
|
||||
let v = self.s.m.bin(e, Bin::Add, base, wave);
|
||||
self.meeting = Some(v);
|
||||
v
|
||||
}
|
||||
};
|
||||
let index = self.meeting_index();
|
||||
let lds = self.s.lds();
|
||||
let ptr = self.s.m.element(head, lds, index);
|
||||
let (zero, one) = (self.s.m.i32(0), self.s.m.i32(1));
|
||||
let old = self.s.m.atomic(head, Rmw::Add, ptr, one);
|
||||
let (low, k64) = (self.s.m.i32(!63u32), self.s.m.i32(64));
|
||||
let base = self.s.m.bin(head, Bin::And, old, low);
|
||||
let target = self.s.m.bin(head, Bin::Add, base, k64);
|
||||
// (one lane of the host wave counts it in; every lane learns what it found)
|
||||
let first = self.s.op(head, "waveIsFirstLane", Attr::NoUnwind, i1t, 110, &[]);
|
||||
self.s.flags |= flags::WAVE_OPS;
|
||||
let at = self.begin_guarded(Some(first));
|
||||
let old = self.s.m.atomic(self.cur, Rmw::Add, ptr, one);
|
||||
let none = self.s.m.undef(i32t);
|
||||
let old = self.end_guarded(at, &[(old, none)])[0];
|
||||
let c = self.cur;
|
||||
let old = self.s.op(c, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[old]);
|
||||
let (even, two) = (self.s.m.i32(!1u32), self.s.m.i32(2));
|
||||
let base = self.s.m.bin(c, Bin::And, old, even);
|
||||
let target = self.s.m.bin(c, Bin::Add, base, two);
|
||||
let (wait, done) = (self.s.m.new_block(), self.s.m.new_block());
|
||||
self.s.m.terminate(head, Term::Br(wait));
|
||||
self.s.m.terminate(c, Term::Br(wait));
|
||||
let n = self.s.m.phi(wait, i32t);
|
||||
let now = self.s.m.atomic(wait, Rmw::Add, ptr, zero);
|
||||
self.s.op(wait, "barrier", Attr::NoDuplicate, void, 80, &[fence]);
|
||||
let now = self.s.m.load(wait, ptr);
|
||||
let now = self.s.op(wait, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[now]);
|
||||
let short = self.s.m.bin(wait, Bin::Sub, now, target);
|
||||
let early = self.s.m.cmp(wait, pred::SLT, short, zero);
|
||||
let n1 = self.s.m.bin(wait, Bin::Add, n, one);
|
||||
@@ -4350,11 +4496,97 @@ impl<'a> Lower<'a> {
|
||||
let more = self.s.m.cmp(wait, pred::IULT, n1, limit);
|
||||
let again = self.s.m.bin(wait, Bin::And, early, more);
|
||||
self.s.m.terminate(wait, Term::CondBr(again, wait, done));
|
||||
self.s.m.set_phi(n, vec![(zero, head), (n1, wait)]);
|
||||
self.s.m.set_phi(n, vec![(zero, c), (n1, wait)]);
|
||||
self.cur = done;
|
||||
self.s.op(done, "barrier", Attr::NoDuplicate, void, 80, &[fence]);
|
||||
}
|
||||
|
||||
/// With `pairs`: the LDS dword of this console wave's meeting counter (made in the entry
|
||||
/// block).
|
||||
fn meeting_index(&mut self) -> V {
|
||||
if let Some(v) = self.meeting {
|
||||
return v;
|
||||
}
|
||||
let first = self.pairs.expect("console waves meeting on their own");
|
||||
let e = self.s.entry;
|
||||
let thread = self.console_thread();
|
||||
let six = self.s.m.i32(6);
|
||||
let wave = self.s.m.bin(e, Bin::LShr, thread, six);
|
||||
let base = self.s.m.i32(first);
|
||||
let v = self.s.m.bin(e, Bin::Add, base, wave);
|
||||
self.meeting = Some(v);
|
||||
v
|
||||
}
|
||||
|
||||
/// With `pairs`: the first dword of this console wave's exchange buffer for the next
|
||||
/// meeting. Each console wave has two buffers of four dwords past the counters (the lower
|
||||
/// half's two, then the upper half's), taken in turn by the count of meetings: a half
|
||||
/// writes one only after the meeting that the other half's reads of it came before, so an
|
||||
/// exchange is one meeting. (Both halves meet as often; between two of its meetings a half
|
||||
/// finds the counter at twice its meetings, or one more.)
|
||||
fn exchange_dword(&mut self) -> V {
|
||||
let c = self.cur;
|
||||
let counter = self.meeting_index();
|
||||
let lds = self.s.lds();
|
||||
let ptr = self.s.m.element(c, lds, counter);
|
||||
let now = self.s.m.load(c, ptr);
|
||||
let i32t = self.s.t.i32;
|
||||
let now = self.s.op(c, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[now]);
|
||||
self.s.flags |= flags::WAVE_OPS;
|
||||
let (one, two) = (self.s.m.i32(1), self.s.m.i32(2));
|
||||
let met = self.s.m.bin(c, Bin::LShr, now, one);
|
||||
let buffer = self.s.m.bin(c, Bin::And, met, one);
|
||||
let at = self.s.m.bin(c, Bin::Shl, buffer, two);
|
||||
let base = match self.pair_base {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let (e, waves) = (self.s.entry, self.pair_waves);
|
||||
let thread = self.console_thread();
|
||||
let (six, three) = (self.s.m.i32(6), self.s.m.i32(3));
|
||||
let wave = self.s.m.bin(e, Bin::LShr, thread, six);
|
||||
let eight = self.s.m.bin(e, Bin::Shl, wave, three);
|
||||
let first = self.s.m.i32(self.pairs.expect("pairs") + waves);
|
||||
let v = self.s.m.bin(e, Bin::Add, first, eight);
|
||||
self.pair_base = Some(v);
|
||||
v
|
||||
}
|
||||
};
|
||||
self.s.m.bin(c, Bin::Add, base, at)
|
||||
}
|
||||
|
||||
/// With `pairs`: each host wave gives the values `mine` (each the same in all its lanes,
|
||||
/// two at most) for its half; all get (lower half's, upper half's) of each, after one
|
||||
/// meeting.
|
||||
fn exchange_pairs(&mut self, mine: &[V]) -> Vec<(V, V)> {
|
||||
debug_assert!(mine.len() <= 2);
|
||||
let at = self.exchange_dword();
|
||||
let c = self.cur;
|
||||
let cl = self.console_lane();
|
||||
let (five, one) = (self.s.m.i32(5), self.s.m.i32(1));
|
||||
let half = self.s.m.bin(c, Bin::LShr, cl, five);
|
||||
let half = self.s.m.bin(c, Bin::Shl, half, one);
|
||||
let own = self.s.m.bin(c, Bin::Add, at, half);
|
||||
let lds = self.s.lds();
|
||||
for (w, &x) in mine.iter().enumerate() {
|
||||
let k = self.s.m.i32(w as u32);
|
||||
let dword = self.s.m.bin(c, Bin::Add, own, k);
|
||||
let ptr = self.s.m.element(c, lds, dword);
|
||||
self.s.m.store(c, ptr, x);
|
||||
}
|
||||
self.meet();
|
||||
let c = self.cur;
|
||||
let two = self.s.m.i32(2);
|
||||
(0..mine.len())
|
||||
.map(|w| {
|
||||
let k = self.s.m.i32(w as u32);
|
||||
let lo = self.s.m.bin(c, Bin::Add, at, k);
|
||||
let hi = self.s.m.bin(c, Bin::Add, lo, two);
|
||||
let (p0, p1) = (self.s.m.element(c, lds, lo), self.s.m.element(c, lds, hi));
|
||||
(self.s.m.load(c, p0), self.s.m.load(c, p1))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A group barrier with an LDS fence in `block`.
|
||||
fn group_sync(&mut self, block: usize) {
|
||||
let mode = self.s.m.i32(meeting_mode());
|
||||
@@ -4385,9 +4617,12 @@ impl<'a> Lower<'a> {
|
||||
let hi = self.s.m.select(at, upper, mine, z);
|
||||
return Ok((lo, hi));
|
||||
}
|
||||
if self.pairs.is_some() && at != self.cur {
|
||||
// (the meeting would split a block lowered already)
|
||||
return Err("a lane mask exchanged after its block, in a group of several console waves");
|
||||
if self.pairs.is_some() {
|
||||
if at != self.cur {
|
||||
// (the meeting would split a block lowered already)
|
||||
return Err("a lane mask exchanged after its block, in a group of several console waves");
|
||||
}
|
||||
return Ok(self.exchange_pairs(&[mine])[0]);
|
||||
}
|
||||
// the pair of dwords of this thread's console wave: its half's, then both
|
||||
let pair = self.exchange_pair(slot);
|
||||
@@ -4398,13 +4633,7 @@ impl<'a> Lower<'a> {
|
||||
let lds = self.s.lds();
|
||||
let ptr = self.s.m.element(at, lds, index);
|
||||
self.s.m.store(at, ptr, mine);
|
||||
let at = if self.pairs.is_some() {
|
||||
self.meet();
|
||||
self.cur
|
||||
} else {
|
||||
self.group_sync(at);
|
||||
at
|
||||
};
|
||||
self.group_sync(at);
|
||||
let one = self.s.m.i32(1);
|
||||
let k1 = self.s.m.bin(at, Bin::Add, pair, one);
|
||||
let p0 = self.s.m.element(at, lds, pair);
|
||||
@@ -4412,11 +4641,7 @@ impl<'a> Lower<'a> {
|
||||
let lo = self.s.m.load(at, p0);
|
||||
let hi = self.s.m.load(at, p1);
|
||||
// the slots are written again only after every thread read them
|
||||
if self.pairs.is_some() {
|
||||
self.meet();
|
||||
} else {
|
||||
self.group_sync(at);
|
||||
}
|
||||
self.group_sync(at);
|
||||
Ok((lo, hi))
|
||||
}
|
||||
|
||||
@@ -5554,6 +5779,7 @@ impl<'a> Lower<'a> {
|
||||
&& matches!(inst.fields, Fields::Ds { .. })
|
||||
&& !(name.starts_with("ds_swizzle") || name.contains("permute"))
|
||||
&& self.lds_meet.as_ref().is_none_or(|(all, _)| all[b])
|
||||
&& !self.lds_unordered.contains(&(b, i))
|
||||
{
|
||||
// the two host waves of a console wave meet before each LDS access: what the other
|
||||
// half wrote is visible, and what it still reads is not overwritten
|
||||
@@ -8989,7 +9215,9 @@ impl<'a> Lower<'a> {
|
||||
}
|
||||
let cl = self.console_lane();
|
||||
let is = self.s.m.cmp(self.cur, pred::EQ, cl, lane);
|
||||
let k = self.exchange_pair(slot);
|
||||
// (console waves meeting on their own: a buffer of the next meeting, which
|
||||
// needs no second meeting before it is written again)
|
||||
let k = if self.pairs.is_some() { self.exchange_dword() } else { self.exchange_pair(slot) };
|
||||
let at = self.begin_guarded(Some(is));
|
||||
let lds = self.s.lds();
|
||||
let ptr = self.s.m.element(self.cur, lds, k);
|
||||
@@ -8999,7 +9227,9 @@ impl<'a> Lower<'a> {
|
||||
let c2 = self.cur;
|
||||
let ptr = self.s.m.element(c2, lds, k);
|
||||
let r = self.s.m.load(c2, ptr);
|
||||
self.meet();
|
||||
if self.pairs.is_none() {
|
||||
self.meet();
|
||||
}
|
||||
return int(self, r);
|
||||
}
|
||||
let r = self.read_lane(a, lane);
|
||||
@@ -9080,9 +9310,15 @@ impl<'a> Lower<'a> {
|
||||
} else {
|
||||
let i32t = self.s.t.i32;
|
||||
let has = self.s.m.cast(c, Cast::ZExt, any, i32t);
|
||||
let (lo_has, hi_has) = self.exchange_halves(c, has)?;
|
||||
let c = self.cur;
|
||||
let (lo, hi) = self.exchange_halves(c, mine)?;
|
||||
// (console waves meeting on their own: both in one meeting)
|
||||
let ((lo_has, hi_has), (lo, hi)) = if self.pairs.is_some() {
|
||||
let both = self.exchange_pairs(&[has, mine]);
|
||||
(both[0], both[1])
|
||||
} else {
|
||||
let has = self.exchange_halves(c, has)?;
|
||||
let c = self.cur;
|
||||
(has, self.exchange_halves(c, mine)?)
|
||||
};
|
||||
let c = self.cur;
|
||||
let z = self.s.m.i32(0);
|
||||
let in_lo = self.s.m.cmp(c, pred::NE, lo_has, z);
|
||||
|
||||
@@ -898,3 +898,38 @@ fn console_waves_of_one_group_meet_on_their_own_in_loops_of_their_own() {
|
||||
}
|
||||
eprintln!("ran on the hardware device: both console waves' loops, as the console runs them");
|
||||
}
|
||||
|
||||
/// Which LDS accesses need the host waves of their console wave to meet
|
||||
/// ([`lower::LdsOrder`]): before a barrier a lane writes its dword and reads it back (the
|
||||
/// order counts); after it the lanes read and add with atomics only (it does not: a read
|
||||
/// there may see another lane's atomic, so it is not taken as alike in every lane).
|
||||
#[test]
|
||||
fn lds_accesses_after_the_last_plain_write_need_no_meeting() {
|
||||
let words = [
|
||||
0x3402_0082, // v_lshlrev_b32 v1, 2, v0
|
||||
0xD834_0000,
|
||||
0x0000_0001, // ds_write_b32 v1, v0
|
||||
0xD8D8_0000,
|
||||
0x0400_0001, // ds_read_b32 v4, v1
|
||||
0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
|
||||
0xBF8A_0000, // s_barrier
|
||||
0xD8D8_0000,
|
||||
0x0500_0001, // ds_read_b32 v5, v1
|
||||
0xD800_0000,
|
||||
0x0000_0001, // ds_add_u32 v1, v0
|
||||
];
|
||||
let mut native = native_of(&words, true, 128);
|
||||
if let StageIo::Cs(cs) = &mut native.io {
|
||||
cs.rsrc2 |= 1 << 15;
|
||||
}
|
||||
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
|
||||
let st = structure::structure(&p).expect("structure");
|
||||
let order = lower::lds_order(&p, &st);
|
||||
let name = |&(b, i): &(usize, usize)| (p.blocks[b].insts[i].name(), p.blocks[b].insts[i].machine.offset);
|
||||
let mut unordered: Vec<_> = order.unordered.iter().map(name).collect();
|
||||
unordered.sort_unstable_by_key(|x| x.1);
|
||||
let unstable: Vec<_> = order.unstable.iter().map(name).collect();
|
||||
assert_eq!(unordered.iter().map(|x| x.0).collect::<Vec<_>>(), ["ds_read_b32", "ds_add_u32"]);
|
||||
assert_eq!(unstable.iter().map(|x| x.0).collect::<Vec<_>>(), ["ds_read_b32"]);
|
||||
assert!(unordered[0].1 > 0x10, "the read after the barrier");
|
||||
}
|
||||
Reference in new issue
Block a user