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
15 changes: 15 additions & 0 deletions tools/check_paired_wave64.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
'v_or_b32_dpp v11, v0, v1 quad_perm:[3,2,1,0] row_mask:0xf bank_mask:0x5'],
'permlane': ['v_mov_b32_e32 v10, 11', 'v_mov_b32_e32 v11, 22', 's_mov_b32 s30, 0x76543210', 's_mov_b32 s31, 0xfedcba98',
'v_permlanex16_b32 v10, v0, s30, s31', 's_mov_b32 s30, 0', 'v_permlanex16_b32 v11, v0, s30, s30'],
'swizzle': ['v_mov_b32_e32 v10, 11', 'v_mov_b32_e32 v11, 22', 'v_cmp_lt_u32_e32 vcc, 0x3e8, v0',
's_and_saveexec_b64 s[10:11], vcc', 'ds_swizzle_b32 v10, v0 offset:0x801b',
'ds_swizzle_b32 v11, v0 offset:0x041f', 's_mov_b64 exec, s[10:11]'],
'empty': ['v_cmpx_lt_u32_e32 0x100000, v0', ('branch', 's_cbranch_execz', 'skip'), 's_mov_b32 s20, 9',
('label', 'skip'), 'v_cmp_lt_u32_e32 vcc, 0, v1', ('branch', 's_cbranch_vccz', 'done'),
's_mov_b32 s21, 4', ('label', 'done'), 'v_readfirstlane_b32 s14, v1', 'v_mov_b32_e32 v10, 1'],
Expand Down Expand Up @@ -186,6 +189,18 @@ def step(w, text):
w.store(ops[0], old); w.exec = source & old if 'and' in base else source | old; w.scc = int(w.exec != 0)
else: raise ValueError('model lacks ' + name)
return
if base == 'ds_swizzle_b32':
off = int(re.search(r'offset:(\S+)', ops[1])[1], 0)
src = ops[1].split()[0]
old = [w.value(src, l) if w.active(l) else 0 for l in range(64)]
for l in range(64):
if not w.active(l): continue
if off & 0x8000:
partner = (l & ~3) | (((off & 0xff) >> (2 * (l & 3))) & 3)
else:
partner = (l & ~31) | (((l & (off & 31)) | ((off >> 5) & 31)) ^ ((off >> 10) & 31))
w.v[int(ops[0][1:])][l] = old[partner]
return
if base.startswith('ds_'):
# One wave per group: its lanes are all of the group.
for l in range(64):
Expand Down
35 changes: 35 additions & 0 deletions tools/shader-compiler/src/hlsl/paired_wave64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,38 @@ fn permlane(em: &mut Emitter<'_>, ins: &Insn) -> Result<(), String> {
Ok(())
}

/// Permute lanes within each quad (QDMode) or within each 32-lane half-wave.
fn ds_swizzle(em: &mut Emitter<'_>, ins: &Insn) -> Result<(), String> {
let mode = ins.aux.imm as u32;
let quad = mode & 0x8000 != 0;
if ins.raw[0] & 0x3_0000 != 0 || (quad && mode & 0xff00 != 0x8000) {
return Err("unsupported compute DS swizzle encoding".into());
}
let (Some(Opnd::Vgpr(to)), Some(Opnd::Vgpr(from))) = (ins.dst.first(), ins.src.first()) else {
return Err("paired wave64: ds_swizzle operands".into());
};
em.line("{");
for lane in 0..2u32 {
let me = format!("(tb_pair + {}u)", 32 * lane);
let partner = if quad {
format!("({me} & ~3u) | (({:#x}u >> (2u * ({me} & 3u))) & 3u)", mode & 255)
} else {
format!("({me} & ~31u) | ((({me} & {}u) | {}u) ^ {}u)", mode & 31, (mode >> 5) & 31, (mode >> 10) & 31)
};
for line in from_lane(u32::from(*from), "tbs_", lane, &partner) {
em.line(&line);
}
em.line(&format!("if (!({})) tbs_{lane} = 0u;", source_active(lane)));
}
// Both lanes are read before either is written.
for lane in 0..2u32 {
let active = lane_bit("exec_lo", "exec_hi", lane);
em.line(&format!("if ({active} != 0u) v{to}__{lane} = tbs_{lane};"));
}
em.line("}");
Ok(())
}

pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br: Option<usize>) -> Result<(), String> {
let m = ins.mnemonic.as_str();
if !per_lane(ins.enc) {
Expand Down Expand Up @@ -340,6 +372,9 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br
if matches!(m, "v_permlane16_b32" | "v_permlanex16_b32") {
return permlane(em, ins);
}
if m == "ds_swizzle_b32" {
return ds_swizzle(em, ins);
}
// A DPP8 first source reads a neighbour within the lane's group of eight.
let (mut moved, mut gated) = (None, false);
match ins.src.first() {
Expand Down
20 changes: 20 additions & 0 deletions tools/shader-compiler/src/tests/hlsl/compute_swizzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ fn other_high_offset_bits_and_gds_stay_refused() {
assert!(error.contains("unsupported compute DS swizzle encoding"), "{offset:#x}/{extra:#x}: {error}");
}
}

#[test]
fn paired_wave64_lowers_quad_and_bitmask_swizzles_without_shared_memory() {
use crate::hlsl::translate_paired_wave64;

let quad = translate_paired_wave64(&fixture::shader(0x801b, 0)).unwrap().hlsl;
assert!(quad.contains("(tb_pair + 0u) & ~3u"), "{quad}");
assert!(quad.contains("(tb_pair + 32u) & ~3u"), "{quad}");
assert!(!quad.contains("tb_swizzle"), "{quad}");
assert!(!quad.contains("GroupMemoryBarrierWithGroupSync"), "{quad}");

let bitmask = translate_paired_wave64(&fixture::shader(0x041f, 0)).unwrap().hlsl;
assert!(bitmask.contains("(((tb_pair + 0u) & 31u) | 0u) ^ 1u"), "{bitmask}");
assert!(bitmask.contains("(((tb_pair + 32u) & 31u) | 0u) ^ 1u"), "{bitmask}");

for (offset, extra) in [(0x8100, 0), (0xe000, 0), (0x801b, 1 << 16), (0x041f, 1 << 17)] {
let error = translate_paired_wave64(&fixture::shader(offset, extra)).map(|t| t.hlsl).unwrap_err();
assert!(error.contains("unsupported compute DS swizzle encoding"), "{offset:#x}/{extra:#x}: {error}");
}
}
Loading