diff --git a/tools/check_paired_wave64.py b/tools/check_paired_wave64.py index c18d674f..1b90f648 100644 --- a/tools/check_paired_wave64.py +++ b/tools/check_paired_wave64.py @@ -84,6 +84,27 @@ } +# Each comparison feeds a conditional select and a dependent result, with +# alternating VCC and explicit SGPR destinations. Inputs cover sparse and +# empty EXEC halves, scalar constants, and dependent register values. +PROGRAMS['unsigned_compare_chain'] = [ + 'v_mov_b32_e32 v10, v0', 'v_mov_b32_e32 v11, v1', + 's_mov_b32 s40, 0x12345678', +] + [ + item + for op in ('eq', 'ne', 'lt', 'le', 'gt', 'ge') + for item in ( + f'v_cmp_{op}_u32_e32 vcc, s40, v10', + 'v_cndmask_b32_e32 v10, v11, v10, vcc', + f'v_cmp_{op}_u32_e64 s[30:31], v10, v11', + 'v_cndmask_b32_e64 v11, v10, v11, s[30:31]', + 'v_add_nc_u32_e32 v10, v1, v10', + 's_bcnt1_i32_b64 s20, vcc', + 's_bcnt1_i32_b64 s21, s[30:31]', + ) +] * 4 + + def assemble(llvm_mc, lines): result = subprocess.run([str(llvm_mc), "-triple=amdgcn-amd-amdhsa", "-mcpu=gfx1030", "-mattr=-wavefrontsize32,+wavefrontsize64", "-show-encoding"], @@ -222,7 +243,8 @@ def step(w, text): cmpx = base.startswith('v_cmpx') if cmpx: dst, a, b = 'exec', ops[-2], ops[-1] else: dst, a, b = ops - test = {'lt': lambda x, y: x < y, 'eq': lambda x, y: x == y, 'ne': lambda x, y: x != y}[base.split('_')[2]] + test = {'lt': lambda x, y: x < y, 'le': lambda x, y: x <= y, 'gt': lambda x, y: x > y, + 'ge': lambda x, y: x >= y, 'eq': lambda x, y: x == y, 'ne': lambda x, y: x != y}[base.split('_')[2]] mask = sum(1 << l for l in range(64) if w.active(l) and test(w.value(a, l), w.value(b, l))) w.store(dst, mask); return if base == 'v_permlanex16_b32': @@ -324,7 +346,7 @@ def inputs(rng): def selected(): """Each program in one console wave per group, and some in four.""" single = [(name, body, 64) for name, body in PROGRAMS.items()] - return single + [(f"{name}_x4", PROGRAMS[name], 256) for name in ("saveexec", "waterfall", "permlane", "dispatch")] + return single + [(f"{name}_x4", PROGRAMS[name], 256) for name in ("saveexec", "waterfall", "permlane", "dispatch", "unsigned_compare_chain")] def main(argv=None): diff --git a/tools/shader-compiler/src/hlsl/paired_wave64.rs b/tools/shader-compiler/src/hlsl/paired_wave64.rs index 02f22638..97ca8258 100644 --- a/tools/shader-compiler/src/hlsl/paired_wave64.rs +++ b/tools/shader-compiler/src/hlsl/paired_wave64.rs @@ -314,6 +314,58 @@ fn ds_swizzle(em: &mut Emitter<'_>, ins: &Insn) -> Result<(), String> { Ok(()) } +/// A simple unsigned comparison produces two ballot words directly. Keep +/// the generic lane-local implementation for any other emitted shape, +/// including CMPX, DPP, mask operands and aliased scalar destinations. +fn direct_compare(em: &mut Emitter<'_>, ins: &Insn, code: &str, written: &[[String; 2]]) -> bool { + let op = match ins.mnemonic.as_str() { + "v_cmp_eq_u32" => "==", + "v_cmp_ne_u32" => "!=", + "v_cmp_lt_u32" => "<", + "v_cmp_le_u32" => "<=", + "v_cmp_gt_u32" => ">", + "v_cmp_ge_u32" => ">=", + _ => return false, + }; + let [[low, high]] = written else { return false }; + let [a, b] = ins.src.as_slice() else { return false }; + let operand = |source: &Opnd, lane: Option| -> Option { + match source { + Opnd::Vgpr(n) => Some(match lane { + Some(lane) => format!("v{n}__{lane}"), + None => format!("v{n}"), + }), + Opnd::Sgpr(_) => { + let value = r(source).ok()?; + if &value == low || &value == high { None } else { Some(value) } + } + Opnd::ConstInt(_) | Opnd::Literal(_) => r(source).ok(), + _ => None, + } + }; + let (Some(a0), Some(b0)) = (operand(a, None), operand(b, None)) else { return false }; + let expected = format!("{low} = (((exec_lo & 1u) != 0u) && ({a0} {op} {b0})) ? 1u : 0u; {high} = 0u;"); + let actual = code.lines().map(str::trim).filter(|s| !s.is_empty() && !s.starts_with("//")) + .collect::>().join(" "); + if actual != expected { return false; } + for (word, _) in identifiers(code) { + if is_vector_register(word) { + em.regs.insert(word.to_string()); + } + } + em.line("{"); + for lane in 0..2u32 { + // Ballot across the host wave, then intersect with guest EXEC. + // Both words are computed before either destination is modified. + let (a, b) = (operand(a, Some(lane)).unwrap(), operand(b, Some(lane)).unwrap()); + let exec = if lane == 0 { "exec_lo" } else { "exec_hi" }; + em.line(&format!("uint tb_cmp{lane} = WaveActiveBallot({a} {op} {b}).x & {exec};")); + } + em.line(&format!("{low} = tb_cmp0; {high} = tb_cmp1;")); + em.line("}"); + true +} + pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br: Option) -> Result<(), String> { let m = ins.mnemonic.as_str(); if !per_lane(ins.enc) { @@ -441,6 +493,9 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br return Err(format!("paired wave64: {m} writes {word} outside a known mask")); } } + if moved.is_none() && direct_compare(em, ins, &code, &written) { + return Ok(()); + } let vectors: BTreeSet<&str> = identifiers(&code).map(|(word, _)| word).filter(|w| is_vector_register(w)).collect(); let mut shadowed = read.clone(); for words in &written { diff --git a/tools/shader-compiler/src/tests/hlsl/host_profiles.rs b/tools/shader-compiler/src/tests/hlsl/host_profiles.rs index 9b28bb53..01173763 100644 --- a/tools/shader-compiler/src/tests/hlsl/host_profiles.rs +++ b/tools/shader-compiler/src/tests/hlsl/host_profiles.rs @@ -90,3 +90,14 @@ fn group_shared_accesses_are_checked_against_the_declared_size() { } assert!(checked > 0, "no host profile translated the shared-memory program"); } + +#[test] +fn paired_integer_compare_emits_ballots_without_lane_shadows() { + // v_cmp_lt_u32_e32 vcc, 0x3e8, v0; s_endpgm. + let words = [0x7d8200ff, 0x000003e8, 0xbf810000]; + let hlsl = translate_paired_wave64(&program(64, &words)).unwrap().hlsl; + assert!(hlsl.contains("WaveActiveBallot(0x3e8u < v0__0)")); + assert!(hlsl.contains("WaveActiveBallot(0x3e8u < v0__1)")); + assert!(!hlsl.contains("tbm_vcc_lo")); + assert!(!hlsl.contains("uint v0 =")); +} diff --git a/tools/shader-compiler/src/tests/hlsl/host_profiles.txt b/tools/shader-compiler/src/tests/hlsl/host_profiles.txt index b63a2056..009abced 100644 --- a/tools/shader-compiler/src/tests/hlsl/host_profiles.txt +++ b/tools/shader-compiler/src/tests/hlsl/host_profiles.txt @@ -1,15 +1,15 @@ compare any-host cs_5_0 waves=any 00ed2a2e5e807d85 compare wave32-host cs_5_0 waves=any 00ed2a2e5e807d85 -compare two-lanes cs_6_6 waves=32 9e68c336f8d6010e +compare two-lanes cs_6_6 waves=32 32479e90b26263c1 saveexec any-host cs_5_0 waves=any d39d73f14c0262a8 saveexec wave32-host cs_5_0 waves=any d39d73f14c0262a8 -saveexec two-lanes cs_6_6 waves=32 7e752b8501d90a84 +saveexec two-lanes cs_6_6 waves=32 e3dace19e742b39f mbcnt any-host cs_5_0 waves=any f9b64857bd2ae39b mbcnt wave32-host cs_5_0 waves=any f9b64857bd2ae39b mbcnt two-lanes cs_6_6 waves=32 d7be408280f250a4 waterfall any-host cs_5_0 waves=any cf4390217f7ee376 waterfall wave32-host cs_5_0 waves=any cf4390217f7ee376 -waterfall two-lanes cs_6_6 waves=32 1072bf42d6cc3002 +waterfall two-lanes cs_6_6 waves=32 a043dddeb285c453 cmpx_carry any-host cs_5_0 waves=any bc5f5813277397b3 cmpx_carry wave32-host cs_5_0 waves=any bc5f5813277397b3 cmpx_carry two-lanes cs_6_6 waves=32 b156ed63e584b218 @@ -30,16 +30,16 @@ permlane wave32-host cs_5_0 waves=any 189cd822efaab806 permlane two-lanes cs_6_6 waves=32 ca0116ae4cce249d empty any-host cs_5_0 waves=any d308d92ea83f4e32 empty wave32-host cs_5_0 waves=any d308d92ea83f4e32 -empty two-lanes cs_6_6 waves=32 49940f6d68fca5ed +empty two-lanes cs_6_6 waves=32 ee3dddb3712587ec dispatch any-host cs_5_0 waves=any a09feae2667e9d78 dispatch wave32-host cs_5_0 waves=any a09feae2667e9d78 -dispatch two-lanes cs_6_6 waves=32 48b23403731e9be4 +dispatch two-lanes cs_6_6 waves=32 234d2694c3a0bf53 shared any-host cs_5_0 waves=any d80843365be5c48d shared wave32-host cs_5_0 waves=any d80843365be5c48d shared two-lanes cs_6_6 waves=32 39003b0cc1e22abe masks any-host cs_5_0 waves=any 103f015433e974a1 masks wave32-host cs_5_0 waves=any 103f015433e974a1 -masks two-lanes cs_6_6 waves=32 ca68eb3457bb35b3 +masks two-lanes cs_6_6 waves=32 94699e105890baee lane-sum any-host cs_5_0 waves=any 621ed1c48da4ec86 lane-sum wave32-host cs_5_0 waves=any 621ed1c48da4ec86 lane-sum two-lanes cs_6_6 waves=32 d3db670e6ab5c712