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
77 changes: 77 additions & 0 deletions crates/victory-shader/src/emit/halves_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,49 @@ fn reduction(op: u32, ident: u32, input: u32) -> Vec<u32> {
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
/// compiler's `v_add_f16` lowering does.
fn half_reduction() -> Vec<u32> {
let vop2 = |src0: u16| (0x32u32 << 25) | (1 << 17) | (1 << 9) | u32::from(src0);
let dpp16 = |ctrl: u16| (0xFu32 << 28) | (0xFu32 << 24) | (u32::from(ctrl) << 8) | 1;
let vop3 = |dst: u8, op: u16, src0: u16, src1: u16, src2: u16| {
[
0xD400_0000 | u32::from(op) << 16 | u32::from(dst),
u32::from(src0) | u32::from(src1) << 9 | u32::from(src2) << 18,
]
};
let mut w = PROLOGUE.to_vec();
w.extend([
0x3420_0085, // v_lshlrev_b32 v16, 5, v0
0x7E06_0300, // v_mov_b32 v3, v0
0xBE8C_25C1, // s_or_saveexec_b64 s[12:13], -1
0xD501_0001,
0x0032_0680, // v_cndmask_b32 v1, 0, v3, s[12:13]
vop2(128), // v_add_f16 v1, 0, v1
]);
for ctrl in [0x00B1, 0x004E, 0x0164, 0x0168] {
w.push(vop2(0x00FA));
w.push(dpp16(ctrl));
}
w.extend(vop3(2, 0x378, 257, 128, 128)); // v_permlanex16_b32 v2, v1, 0, 0
w.push(vop2(258)); // v_add_f16 v1, v2, v1
// Lane 31 holds the sum of lanes 0-31, lane 63 that of lanes 32-63.
w.extend(vop3(1, 0x360, 257, 159, 0)); // v_readlane_b32 s1, v1, 31
w.extend(vop3(2, 0x360, 257, 191, 0)); // v_readlane_b32 s2, v1, 63
w.push(0x7E06_0202); // v_mov_b32 v3, s2
w.push((0x32 << 25) | (1 << 17) | (3 << 9) | 1); // v_add_f16 v1, s1, v3
w.extend(vop3(1, 0x360, 257, 159, 0)); // v_readlane_b32 s1, v1, 31
w.extend([
0xBEFE_040C, // s_mov_b64 exec, s[12:13]
0x7E02_0201, // v_mov_b32 v1, s1
]);
w.extend(store(1, 0));
w.push(S_ENDPGM);
w
}

/// What the console gives [`reduction`] of `f` (folding the inputs of a
/// console wave's lanes) in lane `l` of a group of `threads`.
fn console_reduction(
Expand Down Expand Up @@ -720,6 +763,23 @@ fn reductions_and_prefixes_pass_between_halves() {
.expect("DXIL validation failed");
}
}

let half_sum = half_reduction();
let p = ir::build(&native(&half_sum, true, 96, 0).code, true, 32).expect("f16 IR");
assert!(
super::wave::sections(&p, 0).values().any(|section| {
section
.shapes
.values()
.any(|shape| matches!(shape, super::wave::Shape::Half(..)))
}),
"f16 reduction fixture was not recognized"
);
for threads in [32, 64, 96, 128] {
assert_eq!(refusal(&half_sum, threads, 0), "translated");
}
let halves = lowered(&native(&half_sum, true, 96, 0), true).expect("f16 halves");
assert_eq!(halves.wave_size, Some(32));
}

/// [`reduction`]s and [`exclusive_prefix`] on every hardware adapter give
Expand Down Expand Up @@ -778,6 +838,23 @@ fn reductions_and_prefixes_across_halves_match_the_console_on_every_gpu() {
.collect();
failed.extend(differences(&looped, threads, 1, &want));
}
// Each input lane is a positive f16 subnormal whose bit pattern is its
// lane number. These sums are exactly representable, so per-step f16
// arithmetic and the translator's f32 accumulation round to the same
// result. The 96-thread case also checks a missing upper half.
let half_sum = half_reduction();
for threads in [32, 64, 96, 128] {
let want: Vec<u32> = (0..threads)
.map(|l| match (threads, l & !63) {
(32, _) => 0x01F0,
(_, 0) => 0x07E0,
(96, 64) => 0x08F8,
(128, 64) => 0x0DF8,
_ => unreachable!(),
})
.collect();
failed.extend(differences(&half_sum, threads, 1, &want));
}
assert!(failed.is_empty(), "{}", failed.join("\n"));
}

Expand Down
35 changes: 28 additions & 7 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,12 +1344,15 @@ impl<'a> Lower<'a> {
Shape::Prefix(op, src, mask, ident, inclusive) => {
(op, src, mask, ident, Some(inclusive))
}
Shape::Half(..) if self.halves => return Err(ACROSS),
Shape::Half(_, src, mask, ident, hi) => {
// The high half is a console lane's: compute only.
if self.mask_data != MaskData::Console {
return Err("a 16-bit wave reduction outside compute");
}
let across = self.halves && !self.one_host_wave();
if across && !self.across_here() {
return Err(ACROSS);
}
let c = self.cur;
let &m = self
.x
Expand All @@ -1376,22 +1379,40 @@ impl<'a> Lower<'a> {
119,
&[f, sum, signed],
);
// In halves, each host wave reduced its own 32 lanes. Pass
// those f32 totals between the halves before rounding once.
let r = if across {
self.reduce_across(Op::AddH, r, r, false, ident)?
} else {
r
};
let r = self.f32_to_f16(c, r);
let r = self.s.m.bin(c, Bin::And, r, low);
// Lane `hi` runs the section on the console even where it is
// elsewhere on the host; there it holds the identity.
let i32t = self.s.t.i32;
let four = self.s.m.named_struct("dx.types.fouri32", vec![i32t; 4]);
let b = self
.s
.op(c, "waveActiveBallot", Attr::NoUnwind, four, 116, &[on]);
let word = self.s.m.extract(c, b, hi / 32);
let word = if across {
// Lane `hi` may be in the other host wave: both halves'
// ballots, as its value is read across them below.
let (lo, up) = self.ballot_across_halves(on, c)?;
if hi < 32 { lo } else { up }
} else {
let four = self.s.m.named_struct("dx.types.fouri32", vec![i32t; 4]);
let b = self
.s
.op(c, "waveActiveBallot", Attr::NoUnwind, four, 116, &[on]);
self.s.m.extract(c, b, hi / 32)
};
let (sh, one, z) = (self.s.m.i32(hi % 32), self.s.m.i32(1), self.s.m.i32(0));
let bit = self.s.m.bin(c, Bin::LShr, word, sh);
let bit = self.s.m.bin(c, Bin::And, bit, one);
let there = self.s.m.cmp(c, pred::NE, bit, z);
let lane = self.s.m.i32(hi);
let at = self.read_lane(x, lane);
let at = if across {
self.read_lane_across(x, lane)?
} else {
self.read_lane(x, lane)
};
let top = self.s.m.select(c, there, at, id);
let high = self.s.m.i32(0xFFFF_0000);
let top = self.s.m.bin(c, Bin::And, top, high);
Expand Down
9 changes: 5 additions & 4 deletions crates/victory-shader/src/emit/lower/halves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ impl Lower<'_> {
) -> Result<V, Why> {
use super::super::wave::Op;
let c = self.cur;
let float = matches!(op, Op::AddF | Op::MinF | Op::MaxF);
// AddH's half results are accumulated in f32, then rounded to f16 by
// the caller, just like the single-wave lowering.
let float = matches!(op, Op::AddF | Op::AddH | Op::MinF | Op::MaxF);
let bits = if float { self.as_int(total) } else { total };
let missing = self.s.m.i32(ident);
let missing = self.s.m.i32(if op == Op::AddH { 0 } else { ident });
let (lo, hi) = self.exchange_halves(bits, missing, c)?;
let (lo, hi) = if float {
(self.as_float(lo), self.as_float(hi))
Expand All @@ -311,7 +313,7 @@ impl Lower<'_> {
l.s.m.select(c, less, lo, hi)
};
Ok(match op {
Op::AddU | Op::AddF => self.s.m.bin(c, Bin::Add, lo, hi),
Op::AddU | Op::AddF | Op::AddH => self.s.m.bin(c, Bin::Add, lo, hi),
Op::Or => self.s.m.bin(c, Bin::Or, lo, hi),
Op::And => self.s.m.bin(c, Bin::And, lo, hi),
Op::Xor => self.s.m.bin(c, Bin::Xor, lo, hi),
Expand All @@ -322,7 +324,6 @@ impl Lower<'_> {
// FMin, FMax: as the host's own wave minimum and maximum.
Op::MinF => self.binary(36, lo, hi),
Op::MaxF => self.binary(35, lo, hi),
Op::AddH => return Err("a 16-bit wave reduction over both 32-lane halves"),
})
}
}
Expand Down