Skip to content

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

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

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, up to 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 are now written out in ballot_px_quad, in the form @thehollowedone gave in review (an earlier head called a ballot_px_member helper four times instead). 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_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

The three forms on the same retail pixel shaders: the loop (main 8b957be), the helper form of the earlier head 3893df0, and this head's four bodies.

loop (main) helper (3893df0) four bodies (this)
Windows, 282 PS, 8 threads, in-process, DXC 1.9.2602.17 125.1 s 74.2 s 72.1 s
Linux, 980 PS, dxc.exe 1.9.2609.5, 8 at a time: compile time in all 854 s 518 s 505 s
the same with dxc.exe 1.8.2502.8 961 s 588 s 575 s
Linux, XCL's own pass over the 980, with #688 116.2, 115.9 s 60.1, 60.6 s 58.8, 59.2 s
Linux, XCL's own pass over all 12,101 PS of the pack, with #688 (1 run each) 1,549.5 s 1,283.1 s
DXIL of the 980 (DXC 1.9.2609.5) 76,096,620 B 76,081,568 B 76,033,560 B
  • Windows: Windows 11, RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, rustc 1.100.0. A probe test of ours (debug; DXC is its own DLL) compiled the pixel shaders of records 11000..11300 through dxc::compile_whole; one run of each form, in one session (earlier runs: main 124.1 and 124.2 s, the helper form 74.9 s).
  • Linux: Wine 11.21 Staging, Ryzen 9 9950X3D, no GPU, release x86_64-pc-windows-gnu, XCL_DXC_THREADS=8. The 980 are the pixel shaders of xcl shaders shaders.rpf --precompile --split 1 --only ps --limit 12000. dxc.exe used XCL's flags (-O1 over 1 MB): sums of the per-program medians of 3 rounds for DXC 1.9, the mean of 2 round sums for 1.8, the forms in turn, in the same order each round. The pass: empty DXIL cache, 2 rounds, the forms in turn. The whole-pack pass ran this head first, from a load of 0.3 rising to 11 beside another job, then the loop at a load of 9–12, so its 17 % is one pair on a shared machine. The 2.7 MB shader with 510 calls took 23.1 / 7.6 / 7.2 s with DXC 1.9 and 25.1 / 8.4 / 8.3 s with 1.8.
  • Under Wine without Shaders: isolate DXC compiler heaps for parallel AOT #688, XCL's own pass was slower with the change on 8 threads: 427.7 s against 387.0 s over the 980 (1 run each, this head first, at a load of 6.8 against 4.2; the helper form: 463.1 and 435.2 s against 397.0 and 386.4 s). On the first 282 of them (--limit 11300, 1 run each), this head took 148.1 s on 1 DXC thread and 153.5 s on 8, main 276.0 s and 146.7 s: with one shared heap, more threads did not help this head at all. (Through compile_whole on 8 threads in a probe, without the rest of the pass, the helper form was faster: 334.1 against 387.0 s for the 980.) A reading, not shown: the loop passes this removes are CPU work with few allocations, so what is left waits more on the heap's lock. Shaders: isolate DXC compiler heaps for parallel AOT #688 (in nvidia-dev as 3842807a) conflicts with main in one import line of dxc.rs and in the README; we merged it by hand.
  • The output: with this head, all 788 of the 980 that call ballot_px_quad disassemble differently from the loop's (the 192 others are the same apart from the shader hash); with the helper form, 136 did. In the five read (two of this head's, three of the helper's), the same instructions come in another order or are numbered differently.
  • Linux: the 980 from the helper head b780ec78 and from main through vkd3d-proton's dxil-spirv (34f0ff8, natively, no GPU), with spirv-val on every output: all convert and pass, 31.1 s of conversion in all on both, 41 SPIR-V outputs differ. Not yet repeated for this head.
  • Windows suite, the same PC, cargo test --workspace -- --test-threads=4 at 57f5622 (the helper form, with an earlier form of the test): 1,447 passed, 0 failed, 47 ignored; main there 1,446 / 0 / 47; 29 SKIPPED lines on both. gpu_ps_masks_hold_helper_lanes_on_triangle_edges, which draws a translated pixel shader through ballot_px_quad in both wave sizes, passed. At 9388a7e (the same tree as the head 5216e5b): 1,447 passed, 0 failed, 47 ignored, 29 SKIPPED lines, that test among the passed.
  • Linux under Wine + vkd3d-proton on an RTX 4070 (driver 615.71.09), release, at this head on main 8b957be: gpu_ps_masks_hold_helper_lanes_on_triangle_edges passed in 3 of 3 runs, with no SKIPPED line, as on main; gpu::recomp::emit::tests:: 5 passed in 3 of 3 (main: 4).
  • @JJAutoCompare on an Intel Arc A770 (driver 32.0.101.9175), Windows 11, at 9388a7e (the head's tree), debug: the new test and gpu_ps_masks_hold_helper_lanes_on_triangle_edges passed, 0 wrong pixels of 1,024 in both console wave configurations.

Regression run

Not yet: since #2555 a new tree's first launch compiles the whole pack and builds every compute pipeline before the title starts, which took more than an hour on our test PC (main: 19,200 of 26,503 records after 69 minutes). The two blocks will be added here.

Not tested

  • The title (the regression run above); the pack's pixel shaders as pipelines on a Linux card; AMD; other thread counts.

AI use

  • Claude Code (Claude Opus 5.5) wrote the first form of the change, applied @thehollowedone's form, ran the measurements and wrote this description.

@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.

@Royalflamejlh
Royalflamejlh force-pushed the shader/quad-ballot-unrolled branch from 3893df0 to 9388a7e Compare November 26, 2026 23:17
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
@Royalflamejlh
Royalflamejlh marked this pull request as ready for review November 26, 2026 23:32
@JJAutoCompare

Copy link
Copy Markdown

Tested: Exact clean head 9388a7e7bfbae7b0e4714191198873d12c42a31e, Windows 11 26200.9974, Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175, Rust 1.100.0 MSVC, DXC/DXIL 1.9.2602.17, debug. Built with cargo test --offline --locked -p xcl --lib --no-run. Ran the built binary with --exact <test> --nocapture --test-threads=1 for gpu::recomp::emit::tests::quad_ballot_pixel_helper_has_no_loop_for_dxc_to_unroll and gpu::recomp::tests::gpu_ps_masks_hold_helper_lanes_on_triangle_edges. Each run passed 1 test, with 0 failed, 0 ignored, and 0 skips. On XCL_ADAPTER=A770, the triangle-edge draw reported 0 wrong pixels out of 1,024 for each of the two console wave configurations. The fixture checked derivatives, waterfall iteration bounds, and alpha.

Not tested: DXC timing, full workspace suite, retail smoke, or other adapters.

AI use: T3 Code / Codex (gpt-6.2-sol) ran and independently reran the tests and wrote this report.

…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 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 xcl-dev#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 <noreply@anthropic.com>
@Royalflamejlh
Royalflamejlh force-pushed the shader/quad-ballot-unrolled branch from 9388a7e to 5216e5b Compare November 26, 2026 23:43
@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

@thehollowedone, on the real shaders, your four bodies beat the loop with DXC 1.9 and 1.8, and they are 2–3 % faster than the helper form, so the head is now your form, 5216e5b (the same tree as 9388a7e, which the suite ran on), with you as co-author.

the same retail pixel shaders loop (main) helper (3893df0) four bodies (5216e5b)
282, Windows, 8 threads in one process, DXC 1.9.2602.17 125.1 s 74.2 s 72.1 s
980, dxc.exe 1.9.2609.5, 8 at a time, compile time in all 854 s 518 s 505 s
the same, dxc.exe 1.8.2502.8 961 s 588 s 575 s
  • The setups are in the description. Windows ran on an RTX 2070 SUPER PC (Ryzen 7 5700X3D), 1 run of each form in one session. Linux ran under Wine on a Ryzen 9 9950X3D: the Linux DXC 1.9 row sums the per-program medians of 3 rounds, the 1.8 row is the mean of 2 round sums.
  • Your synthetic chain did not show up in these shaders: no program of the 980 took more than 18 ms longer with the helper than with the loop, on either DXC. The one with the most calls under 1 MB, where XCL keeps -O3 (748 KB, 134 calls), took 10.5 s with the loop, 3.0 s with the helper and 3.1 s with your form on DXC 1.8.
  • With your form, all 788 programs that call ballot_px_quad disassemble differently from the loop's, against 136 of them with the helper; in the two we read, the same instructions come in another order. The DXIL is smaller: 76,033,560 B against 76,081,568 B with the helper and 76,096,620 B with the loop (DXC 1.9). The suite passed again at this head on the 8 GB PC: 1,447 passed, 0 failed, 47 ignored, as before; gpu_ps_masks_hold_helper_lanes_on_triangle_edges, which draws through ballot_px_quad, among them.

AI use: Claude Code (Claude Opus 5.5) ran the comparisons and wrote this reply.

Posted without review by a person: the account owner was away and reads replies later.

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.

3 participants