Skip to content
Closed
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
41 changes: 38 additions & 3 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,12 @@ impl<'a> Lower<'a> {
// of 8, 32 or 4): the rest may read the other half of a wave64.
let in_half = |i: &Inst| {
i.name().starts_with("ds_swizzle")
|| i.name() == "ds_bpermute_b32"
|| matches!(&i.fields, Fields::Valu(v) if v.dpp.is_some())
};
let whole = |i: &Inst| {
let n = i.name();
n.contains("permute")
(n.contains("permute") && n != "ds_bpermute_b32")
|| n.starts_with("v_mbcnt")
|| matches!(n, "v_readlane_b32" | "v_writelane_b32")
|| reads_mask_as_data(x.st, i)
Expand Down Expand Up @@ -465,7 +466,7 @@ impl<'a> Lower<'a> {
Some(
"console wave64 lanes in a group of more than 64 threads on a host without 64-lane waves",
)
} else if insts().any(|i| i.name().contains("permute")) {
} else if insts().any(|i| i.name().contains("permute") && i.name() != "ds_bpermute_b32") {
Some("ds_permute across the halves of a wave64 on a host without 64-lane waves")
} else if !(0..p.blocks.len()).all(|b| super::wave::sections(p, b).is_empty()) {
Some("a whole-wave section of a wave64 on a host without 64-lane waves")
Expand All @@ -489,7 +490,7 @@ impl<'a> Lower<'a> {
.exits
.iter()
.any(|e| matches!(*e, LaneExit::Branch { when, .. } if !uniform(when)))
&& !insts().any(|i| i.name().contains("permute"))
&& !insts().any(|i| i.name().contains("permute") && i.name() != "ds_bpermute_b32")
&& (0..p.blocks.len()).all(|b| super::wave::sections(p, b).is_empty());
if x.console_cf {
console_lanes = true;
Expand Down Expand Up @@ -2225,6 +2226,40 @@ impl<'a> Lower<'a> {
Some(Operand::Vector { reg, .. }) => Some(*reg),
_ => None,
};
if name == "ds_bpermute_b32" {
// RDNA2 gathers separately within each 32-lane half. Disabled
// source lanes supply zero and disabled destinations are preserved.
self.cross_lane(false)?;
let address = self.vgprs(inst, addr.as_ref().ok_or("a backward permute address")?, 1)?[0];
let source = self.vgprs(inst, data.first().ok_or("a backward permute source")?, 1)?[0];
let offset = self.s.m.i32(u32::from(*offset0));
let byte_address = self.s.m.bin(c, Bin::Add, address, offset);
let two = self.s.m.i32(2);
let word = self.s.m.bin(c, Bin::LShr, byte_address, two);
let mask = self.s.m.i32(31);
let within_half = self.s.m.bin(c, Bin::And, word, mask);
let lane = self.s.op(c, "waveGetLaneIndex", Attr::ReadOnly, self.s.t.i32, 111, &[]);
let high_mask = self.s.m.i32(!31);
let base = self.s.m.bin(c, Bin::And, lane, high_mask);
let index = self.s.m.bin(c, Bin::Or, base, within_half);

let active = guard.unwrap_or_else(|| self.s.m.bool(true));
let ty = self.s.m.named_struct("dx.types.fouri32", vec![self.s.t.i32; 4]);
let ballot = self.s.op(c, "waveActiveBallot", Attr::NoUnwind, ty, 116, &[active]);
let lo = self.s.m.extract(c, ballot, 0);
let hi = self.s.m.extract(c, ballot, 1);
let zero = self.s.m.i32(0);
let lower_half = self.s.m.cmp(c, pred::EQ, base, zero);
let bits = self.s.m.select(c, lower_half, lo, hi);
let shifted = self.s.m.bin(c, Bin::LShr, bits, within_half);
let one = self.s.m.i32(1);
let bit = self.s.m.bin(c, Bin::And, shifted, one);
let enabled = self.s.m.cmp(c, pred::NE, bit, zero);
let gathered = self.read_lane(source, index);
let value = self.s.m.select(c, enabled, gathered, zero);
self.write(inst, Reg::V(dst_reg.ok_or("a backward permute result")?), value, guard);
return Ok(());
}
if name == "ds_swizzle_b32" {
// Bit 15 set: each lane of a quad picks a lane of it; else lanes
// of each group of 32 combine their index with and/or/xor masks.
Expand Down
23 changes: 23 additions & 0 deletions crates/victory-shader/src/emit/split_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,26 @@ fn the_last_modules_summary_names_its_group_wave_size_and_operations() {
assert!(summary.contains(op), "{op} in {summary}");
}
}

#[test]
fn backward_permute_wraps_within_each_half() {
let body = [0x3406_0082, 0xDACC_0004, 0x0200_0003];
console_flow_case("backward permute", &body, 64, |lane| (lane & !31) | ((lane + 1) & 31));
}

#[test]
fn backward_permute_respects_both_exec_masks() {
let body = [
0x7E04_0281, // v_mov_b32 v2, 1
0x3406_0082, // v_lshlrev_b32 v3, 2, v0
0x7D88_0094, // v_cmp_gt_u32 vcc, 20, v0
0xBE8A_246A, // s_and_saveexec_b64 s[10:11], vcc
0xDACC_0004, 0x0200_0003, // ds_bpermute_b32 v2, v3, v0 offset:4
0x88FE_0A7E, // s_or_b64 exec, exec, s[10:11]
];
console_flow_case("masked backward permute", &body, 64, |lane| match lane {
0..19 => lane + 1,
19 => 0,
_ => 1,
});
}