From b3d97e35a9c3969ae4d56aa652a84e42b2bd719e Mon Sep 17 00:00:00 2001 From: Valdi320 <61891540+Valdi320@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:58:44 +0100 Subject: [PATCH] xshader: trim paired Wave64 arithmetic register and mask shadows --- tools/check_paired_wave64.py | 32 +++++++++++- .../shader-compiler/src/hlsl/paired_wave64.rs | 50 ++++++++++++++++--- .../src/tests/hlsl/host_profiles.rs | 15 ++++++ .../src/tests/hlsl/host_profiles.txt | 34 ++++++------- 4 files changed, 107 insertions(+), 24 deletions(-) diff --git a/tools/check_paired_wave64.py b/tools/check_paired_wave64.py index c18d674f..8de429b3 100644 --- a/tools/check_paired_wave64.py +++ b/tools/check_paired_wave64.py @@ -84,6 +84,25 @@ } +PROGRAMS['carry_chain'] = [ + 'v_mov_b32_e32 v10, v0', + 'v_mov_b32_e32 v11, 0xffffffff', + 'v_add_co_u32 v10, s[30:31], v10, 0xfffffc00', +] + [ + 'v_add_co_ci_u32_e64 v10, s[30:31], v10, v11, s[30:31]', + 'v_add_co_ci_u32_e64 v11, vcc, v11, v10, s[30:31]', + 'v_add_co_ci_u32_e64 v10, s[30:31], v0, v10, vcc', +] * 16 + ['s_bcnt1_i32_b64 s20, s[30:31]', 's_bcnt1_i32_b64 s21, vcc'] + +PROGRAMS['mad64_chain'] = [ + 'v_mov_b32_e32 v10, v0', + 'v_mov_b32_e32 v11, 0xffffffff', +] + [ + 'v_mad_u64_u32 v[10:11], s[30:31], v0, v1, v[10:11]', + 'v_mad_u64_u32 v[10:11], vcc, v10, v11, v[10:11]', +] * 16 + ['s_bcnt1_i32_b64 s20, s[30:31]', 's_bcnt1_i32_b64 s21, vcc'] + + def assemble(llvm_mc, lines): result = subprocess.run([str(llvm_mc), "-triple=amdgcn-amd-amdhsa", "-mcpu=gfx1030", "-mattr=-wavefrontsize32,+wavefrontsize64", "-show-encoding"], @@ -232,6 +251,17 @@ def step(w, text): source = ((l & ~15) ^ 16) | sel if w.active(l) and w.active(source): w.v[int(ops[0][1:])][l] = old[source] return + if base == 'v_mad_u64_u32': + dst, high = map(int, re.fullmatch(r'v\[(\d+):(\d+)\]', ops[0]).groups()) + a, b = map(int, re.fullmatch(r'v\[(\d+):(\d+)\]', ops[4]).groups()) + mask = 0 + for lane in range(64): + if w.active(lane): + total = w.value(ops[2], lane) * w.value(ops[3], lane) + (w.v[a][lane] | w.v[b][lane] << 32) + w.v[dst][lane], w.v[high][lane] = total & M32, (total >> 32) & M32 + mask |= int(total > M64) << lane + w.store(ops[1], mask) + return dst = int(ops[0][1:]) if base in ('v_add_co_u32', 'v_add_co_ci_u32'): carry_out, a, b = ops[1], ops[2], ops[3] @@ -324,7 +354,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", "carry_chain", "mad64_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..1c2d0353 100644 --- a/tools/shader-compiler/src/hlsl/paired_wave64.rs +++ b/tools/shader-compiler/src/hlsl/paired_wave64.rs @@ -132,6 +132,27 @@ fn identifiers(code: &str) -> impl Iterator { }) } +/// Rename VGPR identifiers in a single lane's emitted arithmetic body. +/// The two lanes have disjoint VGPR storage, so reads, assignments and +/// in/out arguments can address the paired register directly. Keep the +/// existing lexical boundaries: do not touch comments, members or literals. +fn paired_vector_code(code: &str, lane: u32) -> String { + let mut result = String::with_capacity(code.len()); + for line in code.split_inclusive('\n') { + let body = line.trim_end_matches(['\r', '\n']); + let mut copied = 0; + for (word, after) in identifiers(body).filter(|(word, _)| is_vector_register(word)) { + let end = body.len() - after.len(); + let start = end - word.len(); + result.push_str(&line[copied..start]); + let _ = write!(result, "{word}__{lane}"); + copied = end; + } + result.push_str(&line[copied..]); + } + result +} + /// The two words of the lane mask a vector instruction writes through `operand`. fn mask_words(operand: &Opnd) -> Option<[String; 2]> { match operand { @@ -441,7 +462,16 @@ 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")); } } - let vectors: BTreeSet<&str> = identifiers(&code).map(|(word, _)| word).filter(|w| is_vector_register(w)).collect(); + let names: BTreeSet<&str> = identifiers(&code).map(|(word, _)| word).collect(); + let vectors: BTreeSet<&str> = names.iter().copied().filter(|w| is_vector_register(w)).collect(); + // EXEC is needed for ballot reconstruction even when the body does + // not mention it. Other masks need a local only if used or written. + read.retain(|pair| pair[0] == "exec_lo" || written.contains(pair) + || pair.iter().any(|word| names.contains(word.as_str()))); + // Arithmetic has no cross-lane VGPR access here: DPP sources were + // captured before either lane writes, and wave intrinsics were rejected. + // Keep the existing shadow path for memory instructions. + let direct = matches!(ins.enc, Enc::Vop1 | Enc::Vop2 | Enc::Vop3 | Enc::Vopc); let mut shadowed = read.clone(); for words in &written { if !shadowed.contains(words) { @@ -454,7 +484,9 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br em.line(line); } } - for [low, high] in &shadowed { + // A write-only mask starts at zero in each lane. Its full original + // value is never consumed; avoid snapshotting it. + for [low, high] in &read { em.line(&format!("uint tbx_{low} = {low}, tbx_{high} = {high};")); } for [low, _] in &written { @@ -475,14 +507,20 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br } for vector in &vectors { em.regs.insert((*vector).to_string()); - em.line(&format!("uint {vector} = {vector}__{lane};")); + if !direct { + em.line(&format!("uint {vector} = {vector}__{lane};")); + } } if moved.is_some() { em.line(&format!("uint {} = tbd_{lane};", name(&Opnd::Temp(MOVED))?)); } - em.body.push_str(&code); - for vector in &vectors { - em.line(&format!("{vector}__{lane} = {vector};")); + if direct { + em.body.push_str(&paired_vector_code(&code, lane)); + } else { + em.body.push_str(&code); + for vector in &vectors { + em.line(&format!("{vector}__{lane} = {vector};")); + } } for [low, _] in &written { em.line(&format!("tbm_{low}_{lane} = ({low} & 1u) != 0u;")); diff --git a/tools/shader-compiler/src/tests/hlsl/host_profiles.rs b/tools/shader-compiler/src/tests/hlsl/host_profiles.rs index 9b28bb53..2b1f50ad 100644 --- a/tools/shader-compiler/src/tests/hlsl/host_profiles.rs +++ b/tools/shader-compiler/src/tests/hlsl/host_profiles.rs @@ -90,3 +90,18 @@ fn group_shared_accesses_are_checked_against_the_declared_size() { } assert!(checked > 0, "no host profile translated the shared-memory program"); } + +#[test] +fn paired_carry_uses_paired_registers_and_only_needed_mask_locals() { + let &(_, threads, words) = programs::PROGRAMS.iter().find(|p| p.0 == "cmpx_carry").unwrap(); + let hlsl = translate_paired_wave64(&program(threads, words)).unwrap().hlsl; + // The carry instructions write a scalar mask, but must not copy all + // vector operands back or snapshot a write-only mask. + let carry = hlsl.split_once("// 0x0018 v_add_co_u32").expect("carry instruction").1 + .split("// 0x0024 v_add_co_ci_u32").next().unwrap(); + assert!(!carry.contains("uint tbx_s30"), "{carry}"); + assert!(!carry.contains("v0__0 = v0;"), "{carry}"); + assert!(!carry.contains("uint tbx_vcc_lo"), "{carry}"); + assert!(carry.contains("v10__0 = t0;"), "{carry}"); + assert!(carry.contains("v0__0"), "{carry}"); +} diff --git a/tools/shader-compiler/src/tests/hlsl/host_profiles.txt b/tools/shader-compiler/src/tests/hlsl/host_profiles.txt index b63a2056..c5e1ef7b 100644 --- a/tools/shader-compiler/src/tests/hlsl/host_profiles.txt +++ b/tools/shader-compiler/src/tests/hlsl/host_profiles.txt @@ -1,48 +1,48 @@ 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 522329eeed1d8422 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 b46581cb831f8b45 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 +mbcnt two-lanes cs_6_6 waves=32 4786d3c90013b4fa 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 b3b5666e64e1a635 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 +cmpx_carry two-lanes cs_6_6 waves=32 2cd2ece60df77a4c lanes any-host cs_5_0 waves=any 4dfb64f0c32887f2 lanes wave32-host cs_5_0 waves=any 4dfb64f0c32887f2 -lanes two-lanes cs_6_6 waves=32 ba7897692f781b7b +lanes two-lanes cs_6_6 waves=32 722f07843a017edd dpp8 any-host cs_5_0 waves=any 1bfe3fd91e4804f0 dpp8 wave32-host cs_5_0 waves=any 1bfe3fd91e4804f0 -dpp8 two-lanes cs_6_6 waves=32 c54178f1098ec8f2 +dpp8 two-lanes cs_6_6 waves=32 c6e23680ad6736db row_shift any-host cs_5_0 waves=32 cc1e201b4a9ff628 row_shift wave32-host cs_5_0 waves=32 cc1e201b4a9ff628 -row_shift two-lanes cs_6_6 waves=32 db5b85908612433b +row_shift two-lanes cs_6_6 waves=32 4672f2592b92782c row_masks any-host refused row_masks wave32-host refused -row_masks two-lanes cs_6_6 waves=32 ac3a73b8bc6bce4c +row_masks two-lanes cs_6_6 waves=32 9bc3710c4fe94c65 permlane any-host cs_5_0 waves=any 189cd822efaab806 permlane wave32-host cs_5_0 waves=any 189cd822efaab806 -permlane two-lanes cs_6_6 waves=32 ca0116ae4cce249d +permlane two-lanes cs_6_6 waves=32 ff9592255acd81cc 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 c52e6773d2c310be 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 bb3d77f1765f19cf 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 +shared two-lanes cs_6_6 waves=32 17da7b2d027ca8d5 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 ab8e49df9d408bf1 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 +lane-sum two-lanes cs_6_6 waves=32 55dc084ec7de5c88 lane-sum-two-waves any-host cs_5_0 waves=64 0cfd9e050c2caba1 -lane-sum-two-waves wave32-host cs_6_6 waves=32 b62a8aa3cc1c1ad1 -lane-sum-two-waves two-lanes cs_6_6 waves=32 b62a8aa3cc1c1ad1 +lane-sum-two-waves wave32-host cs_6_6 waves=32 ffc128709f769cd5 +lane-sum-two-waves two-lanes cs_6_6 waves=32 ffc128709f769cd5