From 2dad32e6303c6a90d738c87427023d9ad0cc287e Mon Sep 17 00:00:00 2001 From: Royalflamejlh <49204095+Royalflamejlh@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:02:28 -0700 Subject: [PATCH] Recompiler: the pixel prelude's quad ballot has no loop for DXC to unroll `ballot_px_quad` took its quad's four members in an `[unroll]` loop. It is called in most translated pixel shaders, often hundreds of times, and DXC unrolls every inlined copy: on a 395 KB one with 84 calls, `-ftime-report` put 22 % of its pass time in "Dxil Loop Unroll" and 9 % more in "Unroll loops" and LCSSA. The four members are now written out, in the form thehollowedone gave in review. On a retail pack, 282 pixel shaders compiled in-process on 8 threads took 72.1 s instead of 125.1 s on Windows (Ryzen 7 5700X3D). 980 of them through dxc.exe took 505 s instead of 854 s with DXC 1.9 and 575 s instead of 961 s with DXC 1.8. Under Wine, XCL's own pass over the 980 took 59 s instead of 116 s with #688's private DXC heaps; without them it was slower (428 s against 387 s). Co-authored-by: thehollowedone <36892342+thehollowedone@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 2f817a809f..35e99acb7f 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -6306,13 +6306,16 @@ const PRELUDE_PIXEL: &str = r#" // compare's vector operands, not of its result (`quad_bits`: AMD's compiler crashed on those, // #134); elsewhere ballot_px quad-reads the result. uint2 ballot_px_quad(uint q) { - uint2 m = uint2(0u, 0u); - [unroll] for (uint p = 0u; p < 4u; p++) { - uint2 w = WaveActiveBallot(((q >> p) & 1u) != 0u).xy; - w |= w >> 1; - w |= w >> 2; - m |= (w & 0x11111111u) << p; - } + uint2 w0 = WaveActiveBallot(((q >> 0u) & 1u) != 0u).xy; + w0 |= w0 >> 1; w0 |= w0 >> 2; + uint2 w1 = WaveActiveBallot(((q >> 1u) & 1u) != 0u).xy; + w1 |= w1 >> 1; w1 |= w1 >> 2; + uint2 w2 = WaveActiveBallot(((q >> 2u) & 1u) != 0u).xy; + w2 |= w2 >> 1; w2 |= w2 >> 2; + uint2 w3 = WaveActiveBallot(((q >> 3u) & 1u) != 0u).xy; + w3 |= w3 >> 1; w3 |= w3 >> 2; + uint2 m = ((w0 & 0x11111111u) << 0u) | ((w1 & 0x11111111u) << 1u) + | ((w2 & 0x11111111u) << 2u) | ((w3 & 0x11111111u) << 3u); return uint2(select(and(xcl_w32, xcl_half != 0u), m.y, m.x), select(xcl_w32, 0u, m.y)); } uint2 ballot_px(bool b) { @@ -6848,4 +6851,14 @@ mod tests { } } } + + #[test] + fn quad_ballot_pixel_helper_has_no_loop_for_dxc_to_unroll() { + let pixel = super::PRELUDE_PIXEL; + let start = pixel.find("uint2 ballot_px_quad(uint q) {").expect("ballot_px_quad in PRELUDE_PIXEL"); + let rest = &pixel[start..]; + let quad = &rest[..rest.find("\n}\n").expect("ballot_px_quad closing brace") + 2]; + let looped = quad.split(|c: char| !(c.is_alphanumeric() || c == '_')).any(|w| ["for", "while", "unroll"].contains(&w)); + assert!(!looped, "a loop remains in ballot_px_quad():\n{quad}"); + } }