Skip to content

Recompiler: the pixel prelude's quad ballot has no loop for DXC to unroll - #2872

Draft
Royalflamejlh wants to merge 1 commit into
xcl-dev:mainfrom
Royalflamejlh:shader/quad-ballot-unrolled
Draft

Royalflamejlh wants to merge 1 commit into
xcl-dev:mainfrom
Royalflamejlh:shader/quad-ballot-unrolled

Conversation

@Royalflamejlh

@Royalflamejlh Royalflamejlh commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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 pixel shader with 84 calls, dxc -ftime-report put 22 % of its pass time in "Dxil Loop Unroll" and 9 % more in "Unroll loops" and LCSSA. The four members now come from ballot_px_member, so the pixel prelude has no loop left. In a retail pack, 10,196 of the 12,101 pixel shaders call it, 453,546 times in all. Since #2555 they are all compiled before a launch with a cold cache.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: ballot_px_member, and ballot_px_quad without the loop; the same computation.
  • emit.rs: quad_ballot_pixel_helper_has_no_loop_for_dxc_to_unroll, red without the change.

Tested

  • Windows 11, RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, rustc 1.100.0, DXC 1.9.2602.17 (Windows SDK). A probe test of ours (debug; DXC is its own DLL) compiled 282 pixel shaders of a retail pack (records 11000..11300) through dxc::compile_whole on 8 threads: 124.1 and 124.2 s on main 8b957be (2 runs), 74.9 s with this change (1 run).
  • The same PC, cargo test --workspace -- --test-threads=4 at 57f5622 (the same change, with an earlier form of the test that checked all of PRELUDE_PIXEL): 1,447 passed, 0 failed, 47 ignored; main 8b957be there: 1,446 / 0 / 47. The extra one is the new test. 29 SKIPPED lines, the same as main's: 21 "no wave64", 3 refused per-lane forms, 3 without the debug layer, 1 needing Developer Mode, 1 without heap windows. gpu_ps_masks_hold_helper_lanes_on_triangle_edges, which draws a translated pixel shader through ballot_px_quad in both wave sizes, passed.
  • Linux, Wine 11.21 Staging, Ryzen 9 9950X3D, no GPU, release x86_64-pc-windows-gnu, DXC 1.9.2609.5, XCL_DXC_THREADS=8. XCL's own pass, xcl shaders shaders.rpf --precompile --split 1 --only ps --limit 12000 (980 pixel shaders), empty DXIL cache, the two trees of each pair alternated:
tree pass 1 pass 2
main 8b957be 397.0 s 386.4 s
+ this 463.1 s 435.2 s
main + #688 107.8 s 105.5 s
main + #688 + this 69.0 s 60.0 s

Under Wine without #688 this change made XCL's pass slower, for a reason not yet found (the same 980 through compile_whole on 8 threads, also without #688, took 334.1 s against 387.0 s, 1 run each). With #688 (in nvidia-dev as 3842807a) it takes 36 to 43 % off. #688 conflicts with main in one import line of dxc.rs and in the README; we merged it by hand.

  • The output: the 980 programs' DXIL went from 76,096,620 to 76,081,568 B. Of 1,211 pixel shaders sampled across the pack and compiled with dxc.exe (this change's HLSL made by replacing the prelude in main's; the branch's translator gave the same text for the 354 compared), dxc -dumpbin was the same in 982 apart from the shader hash, and in 90 more once phi operands are put in one order. The other 139 differ; in the three I read, instructions come in another order (two selects swapped) or are numbered differently.
  • Linux: the same 980 pixel shaders from both trees through vkd3d-proton's dxil-spirv (34f0ff8, natively, no GPU), with spirv-val on every output: all 980 convert and pass on both, 31.1 s of conversion in all on both; 41 of the 980 SPIR-V outputs differ.
  • Linux under Wine, cargo test -p xcl --lib -- gpu::recomp: 402 passed, 0 failed, 20 ignored at 57f5622 (the card tests skip); at the head, -- emit::tests: 5 passed, and the new test fails with the loop put back.

Regression run

Not yet: since #2555 a new tree's first launch compiles the whole pack before the title starts, longer than our smoke runs wait (smoke.ps1's 120 s -StartupTimeoutSeconds). The two blocks will be added here.

Not tested

  • The title (the regression run above); a Linux GPU (the pipelines on a card through vkd3d-proton); AMD; the pass on the whole pack (980 pixel shaders in XCL's pass, 282 on Windows, 1,211 more through dxc.exe); other thread counts.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change, ran the measurements and wrote this description.

…roll

`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 the compile in "Dxil Loop Unroll" and 9 %
more in "Unroll loops" and LCSSA. The four members now come from
`ballot_px_member`, so there is no loop left in the pixel prelude.

On a retail pack, 282 pixel shaders compiled in-process on 8 threads
took 74.9 s instead of 124.1 s on Windows (Ryzen 7 5700X3D). Under Wine,
XCL's own pass over 980 of them took 60-69 s instead of 105-108 s with
xcl-dev#688's private DXC heaps; without them it was slower, 435-463 s against
386-397 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Royalflamejlh
Royalflamejlh force-pushed the shader/quad-ballot-unrolled branch from b780ec7 to 3893df0 Compare November 26, 2026 20:47
@thehollowedone

Copy link
Copy Markdown

Independent Windows/NVIDIA check of draft head 3893df0 against its base 8b957be. The stock head's translated pixel GPU check gpu_ps_masks_hold_helper_lanes_on_triangle_edges passed 1/0/0 on the RTX 3080 Ti, with no skip; gpu::recomp::emit::tests:: passed 5/0/0. Commands: cargo test --offline --locked -p xcl --lib <filter> -- --test-threads=1 for each filter.

I also checked DXC sensitivity with synthetic HLSL only (no retail data). A pixel shader makes 84 successive ballot_px_quad(q) calls, updating q before each call and XORing the uint2 results. The two helper forms use this PR's old loop and current ballot_px_member implementation, except both return m directly to isolate the helper. Windows SDK DXC 1.8.2502.11, -E main -T ps_6_6 -HV 2021 -O3, separate compiler process per run, alternating order, six runs:

Calls Old [unroll] loop PR helper calls Four ballot bodies written directly
84 0.145, 0.143 s 0.249, 0.246 s 0.133, 0.125 s
840 3.733, 3.597 s 15.429, 15.035 s 1.181, 1.147 s

The current PR form removes loop-unroll time but can move the cost into another DXC pass: on the 840-call synthetic shader, -ftime-report attributed 92.8% of its 15.2 s total to Simplify Instructions; the old form's total was 3.5 s, with Unroll loops at 45.7%. The explicit four-body form has no loop or helper call. Its DXIL was 64 B larger than the PR form at 84 calls (28,296 versus 28,232 B) and 628 B larger at 840 calls (273,548 versus 272,920 B).

I applied the explicit-body form locally to the real PRELUDE_PIXEL; the translated pixel GPU test passed again 1/0/0, and the emitter tests passed 5/0/0. The candidate is below for consideration. Please compare it on the same real shader sample and DXC build used for the PR before choosing a form. This synthetic result does not establish a retail compilation regression or an in-game speedup, and I did not run the workspace suite or paired smoke. The PR is still draft.

Local candidate against this PR's `emit.rs`
-uint2 ballot_px_member(uint q, uint p) {
-    uint2 w = WaveActiveBallot(((q >> p) & 1u) != 0u).xy;
-    w |= w >> 1;
-    w |= w >> 2;
-    return (w & 0x11111111u) << p;
-}
 uint2 ballot_px_quad(uint q) {
-    uint2 m = ballot_px_member(q, 0u) | ballot_px_member(q, 1u) | ballot_px_member(q, 2u) | ballot_px_member(q, 3u);
+    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);

Hardware: Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti / driver 621.35, rustc 1.99.1 MSVC. Not tested: AMD, Intel, Wine, a real shader pack or retail title under this candidate. AI use: Codex (GPT-6.2 Sol) inspected the exact diff, generated and ran the synthetic DXC comparison, ran the GPU tests, and drafted this report. I checked the code and measurements before posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants