Recompiler: restore four-ballot lowering for pixel quad masks #3797

Open
CharlyRien wants to merge 1 commit from upstream-pr/3797 into main
CharlyRien commented 2026-12-03 16:39:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by CharlyRien on 2026-12-03T16:39:14Z.


Restores the direct DXIL pixel-shader quad ballot (Lw::ballot_ps_quad) to the form it had before #3501. Follow-up to #3501.

#3501 (a50fa7ab) changed ballot_ps_quad from four WaveActiveBallot (one per quad member) to each lane shifting its quad's four predicate bits into place and OR-ing them across the wave with WaveActiveBit(Or). Two public reports on #3501 point at that function:

  • RTX 4090 (driver 621.40), minimalhipster990's report: in a replay of one pixel-shader draw of the retail title (a wave64 program on wave32 hardware), the draw's median GPU time goes from 0.0993 ms at e18e7b2 (the commit before a50fa7a) to 16.0256 ms at a50fa7a (13 pairs). On the complete #3501, restoring only the old ballot_ps_quad body takes it from 17.9420 ms to 0.0814 ms (12 pairs; B slower in every pair, in both orders). The output changed too: between the two forms, 60/63/40 and 56/60/35 texels on targets 0/1/2 of two draws; the report notes that a console reference is needed to decide which is right. Host a07ad1b1 + #3318, not this commit.
  • RTX 3090 (driver 616.56), my earlier report, on a local stack (main 493dbd3d + #3528 + #3517 + 42 local commits, local switches listed there): with #2571's mask forms switched off, the GPU hung (DRED device state 3) in the same mesh + pixel-shader draw in 2 runs of 2; with the old ballot_ps_quad as well, no hang in 2 runs of 2 (0.66 and 1.41 presents/s in the world). With #2571's forms on and the #3501 body: 0.23 presents/s, the intro card never left in 358 s (1 run).

We have not found why the WaveActiveBit form is slow on the 4090 or hangs that draw on the 3090. There is one reason in the tree to prefer ballots in pixel shaders: the HLSL prelude's ballot_px_quad (recomp/emit.rs) uses only ballots and quad reads on purpose, because a helper lane gets the wave's result of a ballot but WaveActiveBitOr gave helper lanes another value on an RTX 5070 Ti (comment there, and gpu_ps_helper_lanes_see_ballots_and_quads_are_lanes_4k, which prints it). On this 3090 that test prints: 32 pixels next to a helper lane; its ballot right in 32, its bit-or in 0. #3501 did not change the HLSL path, so this also puts the two backends back on the same lowering.

The restored body is the one of a50fa7ab's parent, character for character; only its doc comment is new. The rest of #3501 (direct quad shuffles, predicate-derived lane bits) stays: the 4090 function-only control kept it too.

What is in it

  • crates/xcl/src/gpu/dxil/lanes.rs, Lw::ballot_ps_quad: one WaveActiveBallot per quad member p; each quad's nibble of that ballot is OR-ed into bit 4k and moved to 4k + p. A wave64 program uses both words of the same ballots; a wave32 program in 64-lane hardware takes its half's word (as before #3501 and as HLSL ballot_px_quad).
  • Same file, test direct_ps_non_vopc_carry_ballot_uses_quad_op: the fixture also reads VCC_LO as a word (so the test does not depend on v_cndmask taking only its own bit), runs in wave32 and wave64, and checks four non-constant waveActiveBallot and no waveActiveBit in the DXIL. On main these checks fail: its ballot_ps_quad emits waveActiveBit and no predicate ballot (read in the code; the test was not run on main).

No cache bump: the change is under gpu/dxil, so XCL_DIRECT_DXIL_SOURCE_ID changes on its own (every direct translation gets a new identity, all stages); the HLSL path's XCL_TRANSLATOR_SOURCE_ID is unchanged.

Tested

  • RTX 3090 (driver 621.47), Ryzen 9 5950X, Windows 11 22631, Rust 1.100.0, debug, DXC 1.9.2602.17.
  • cargo build --workspace and cargo test --workspace --no-run: ok
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 3,016 passed, 2 failed (test_title_draws_with_a_root_signature_under_the_debug_layer, test_title_runs_under_the_debug_layer_with_dred), 56 ignored, over 45 test binaries. The same two debug-layer tests fail on this machine without this commit (main a815de93 + another one-commit recompiler branch: 3,020 passed, the same 2 failed); main a815de93 itself was not run through the whole suite.
  • SKIPPED on this adapter: 25 lines: 22 "no wave64", 3 "no heap windows" or "no GPU_UPLOAD heaps", the reasons this card always gives.
  • The pixel ballot and mask tests by name with XCL_REQUIRE_GPU=mesh: 17 of 17 passed. Direct path: the 8 tests of gpu::dxil::lanes::ps_ballot_tests that cover pixel masks, the changed one included; gpu_direct_ps_masks_hold_helper_lanes_on_triangle_edges needs wave64 programs for all its cases and prints SKIPPED … no wave64 on this card. HLSL path (unchanged, as controls): the recomp ps_masks, ballot_px_quad, rla, helper-lane and primitive-edge tests.
  • Release build of this head: 5 tests of the ps_ballot_tests module by name (the changed test, 3 other CPU DXIL checks, the wave32 direct GPU edge test): passed, twice.

Regression run
scripts/smoke.ps1, 60 s each, the same title, the same machine and driver, a fresh shader cache on both sides; main first, then this head:

xcl-smoke v1
commit: a815de932c4dc8da3b383ca44f4b094aa406fec9
gpu: NVIDIA GeForce RTX 3090
title: 16b5e5d4
seconds: 60
exit: 0x0
frames shown: 495 (PresentX 495)
presents per 2 s (last 3): 61 61 60
device removals: 0
crashes: 0
peak commit: 15974 MB
xcl-smoke v1
commit: f95100fc16c7629fe42b7a2144de54c7672d339c
gpu: NVIDIA GeForce RTX 3090
title: 16b5e5d4
seconds: 60
exit: 0x0
frames shown: 523 (PresentX 523)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15764 MB

Frames 523 / 495 (106 %), peak commit 15,764 / 15,974 MB (99 %), no device removal, no crash.

Not tested

  • AMD, Intel, and NVIDIA cards and drivers other than this 3090 with this commit. The 4090 numbers above come from a function-only restore on another tree, not from this commit.
  • The world. The 60 s smoke run does not reach it, nor the draw that hung on the 3090 (110-121 s into those runs). On main (with #2571's forms on), we have no run of this commit or of the old body alone in the world. One world run each on this machine (same driver, each side with its own prepared shader cache, 03/12): main a815de93 reached the intro cinematic and lost the device at 233 s; this head reached the first scene after it and lost the device at 313 s. Both removals look the same (0x887a0005, DRED: device state 0, no breadcrumbs, no list in flight), so this commit does not remove it; neither side reached a stable world. One run per side shows no difference that can be credited to the commit.
  • The GPU result of the restored function on this card: the one direct GPU test of helper-lane masks skips here (no wave64), and the direct GPU tests that do run here were not checked for whether they reach ballot_ps_quad. The function is covered here by the CPU DXIL test; the HLSL ballot_px_quad, the same algorithm, has GPU tests that run on this card.
  • Which output is right for the texels that changed between the two forms in the 4090 report: no console reference.
  • Frame time beyond the smoke run: four ballots plus the spreading per pixel mask, instead of two WaveActiveBit(Or) and their selects.

Overlap with #3455
#3455 (open) changes the caller Lw::ballot_ps (quad reads lane by lane instead of quadOp) and rewrites and renames the same test (direct_ps_non_vopc_carry_ballot_uses_quad_op); it does not change the body of ballot_ps_quad. A test merge of the two lanes.rs (#3455 at 1895d9e8) conflicts only in that test; the ballot_ps and ballot_ps_quad hunks merge cleanly. Whichever lands second rebases: if #3455 is first, the no-waveActiveBit check and the predicate-ballot count move into its test (with v_readfirstlane its variants may have one more non-constant ballot, so the count would be checked against the form without it).

AI use

  • AI use: OpenAI Codex (GPT-6) and Claude Code (Claude Opus 5.5): Codex wrote the change and its test; Claude Code reviewed it (an independent read-only review as well), extended the test to wave64, ran the builds, the tests and the smoke run, and wrote this description; the 3090 measurements above were also run by Claude Code. Checked and posted by me.

Base: main a815de93, one commit, no local switches.

<!-- greyfinch-sync:ivall/xcl:issue:5630760423 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3797). Originally posted by [CharlyRien](https://github.com/CharlyRien) on 2026-12-03T16:39:14Z. --- Restores the direct DXIL pixel-shader quad ballot (`Lw::ballot_ps_quad`) to the form it had before #3501. Follow-up to #3501. #3501 (`a50fa7ab`) changed `ballot_ps_quad` from four `WaveActiveBallot` (one per quad member) to each lane shifting its quad's four predicate bits into place and OR-ing them across the wave with `WaveActiveBit(Or)`. Two public reports on #3501 point at that function: - **RTX 4090** (driver 621.40), [minimalhipster990's report](https://github.com/xcl-dev/xcl/pull/3501#issuecomment-7319336125): in a replay of one pixel-shader draw of the retail title (a wave64 program on wave32 hardware), the draw's median GPU time goes from 0.0993 ms at `e18e7b2` (the commit before `a50fa7a`) to 16.0256 ms at `a50fa7a` (13 pairs). On the complete #3501, restoring only the old `ballot_ps_quad` body takes it from 17.9420 ms to 0.0814 ms (12 pairs; B slower in every pair, in both orders). The output changed too: between the two forms, 60/63/40 and 56/60/35 texels on targets 0/1/2 of two draws; the report notes that a console reference is needed to decide which is right. Host `a07ad1b1` + #3318, not this commit. - **RTX 3090** (driver 616.56), [my earlier report](https://github.com/xcl-dev/xcl/pull/3501#issuecomment-7311293228), on a local stack (main `493dbd3d` + #3528 + #3517 + 42 local commits, local switches listed there): with #2571's mask forms switched off, the GPU hung (DRED device state 3) in the same mesh + pixel-shader draw in 2 runs of 2; with the old `ballot_ps_quad` as well, no hang in 2 runs of 2 (0.66 and 1.41 presents/s in the world). With #2571's forms on and the #3501 body: 0.23 presents/s, the intro card never left in 358 s (1 run). We have not found why the `WaveActiveBit` form is slow on the 4090 or hangs that draw on the 3090. There is one reason in the tree to prefer ballots in pixel shaders: the HLSL prelude's `ballot_px_quad` (`recomp/emit.rs`) uses only ballots and quad reads on purpose, because a helper lane gets the wave's result of a ballot but `WaveActiveBitOr` gave helper lanes another value on an RTX 5070 Ti (comment there, and `gpu_ps_helper_lanes_see_ballots_and_quads_are_lanes_4k`, which prints it). On this 3090 that test prints: `32 pixels next to a helper lane; its ballot right in 32, its bit-or in 0`. #3501 did not change the HLSL path, so this also puts the two backends back on the same lowering. The restored body is the one of `a50fa7ab`'s parent, character for character; only its doc comment is new. The rest of #3501 (direct quad shuffles, predicate-derived lane bits) stays: the 4090 function-only control kept it too. **What is in it** - `crates/xcl/src/gpu/dxil/lanes.rs`, `Lw::ballot_ps_quad`: one `WaveActiveBallot` per quad member `p`; each quad's nibble of that ballot is OR-ed into bit `4k` and moved to `4k + p`. A wave64 program uses both words of the same ballots; a wave32 program in 64-lane hardware takes its half's word (as before #3501 and as HLSL `ballot_px_quad`). - Same file, test `direct_ps_non_vopc_carry_ballot_uses_quad_op`: the fixture also reads `VCC_LO` as a word (so the test does not depend on `v_cndmask` taking only its own bit), runs in wave32 and wave64, and checks four non-constant `waveActiveBallot` and no `waveActiveBit` in the DXIL. On `main` these checks fail: its `ballot_ps_quad` emits `waveActiveBit` and no predicate ballot (read in the code; the test was not run on `main`). No cache bump: the change is under `gpu/dxil`, so `XCL_DIRECT_DXIL_SOURCE_ID` changes on its own (every direct translation gets a new identity, all stages); the HLSL path's `XCL_TRANSLATOR_SOURCE_ID` is unchanged. **Tested** - RTX 3090 (driver 621.47), Ryzen 9 5950X, Windows 11 22631, Rust 1.100.0, debug, DXC 1.9.2602.17. - `cargo build --workspace` and `cargo test --workspace --no-run`: ok - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 3,016 passed, 2 failed (`test_title_draws_with_a_root_signature_under_the_debug_layer`, `test_title_runs_under_the_debug_layer_with_dred`), 56 ignored, over 45 test binaries. The same two debug-layer tests fail on this machine without this commit (`main` `a815de93` + another one-commit recompiler branch: 3,020 passed, the same 2 failed); `main` `a815de93` itself was not run through the whole suite. - `SKIPPED` on this adapter: 25 lines: 22 "no wave64", 3 "no heap windows" or "no `GPU_UPLOAD` heaps", the reasons this card always gives. - The pixel ballot and mask tests by name with `XCL_REQUIRE_GPU=mesh`: 17 of 17 passed. Direct path: the 8 tests of `gpu::dxil::lanes::ps_ballot_tests` that cover pixel masks, the changed one included; `gpu_direct_ps_masks_hold_helper_lanes_on_triangle_edges` needs wave64 programs for all its cases and prints `SKIPPED … no wave64` on this card. HLSL path (unchanged, as controls): the `recomp` `ps_masks`, `ballot_px_quad`, `rla`, helper-lane and primitive-edge tests. - Release build of this head: 5 tests of the `ps_ballot_tests` module by name (the changed test, 3 other CPU DXIL checks, the wave32 direct GPU edge test): passed, twice. **Regression run** `scripts/smoke.ps1`, 60 s each, the same title, the same machine and driver, a fresh shader cache on both sides; `main` first, then this head: ``` xcl-smoke v1 commit: a815de932c4dc8da3b383ca44f4b094aa406fec9 gpu: NVIDIA GeForce RTX 3090 title: 16b5e5d4 seconds: 60 exit: 0x0 frames shown: 495 (PresentX 495) presents per 2 s (last 3): 61 61 60 device removals: 0 crashes: 0 peak commit: 15974 MB ``` ``` xcl-smoke v1 commit: f95100fc16c7629fe42b7a2144de54c7672d339c gpu: NVIDIA GeForce RTX 3090 title: 16b5e5d4 seconds: 60 exit: 0x0 frames shown: 523 (PresentX 523) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 15764 MB ``` Frames 523 / 495 (106 %), peak commit 15,764 / 15,974 MB (99 %), no device removal, no crash. **Not tested** - AMD, Intel, and NVIDIA cards and drivers other than this 3090 with this commit. The 4090 numbers above come from a function-only restore on another tree, not from this commit. - The world. The 60 s smoke run does not reach it, nor the draw that hung on the 3090 (110-121 s into those runs). On `main` (with #2571's forms on), we have no run of this commit or of the old body alone in the world. One world run each on this machine (same driver, each side with its own prepared shader cache, 03/12): `main` `a815de93` reached the intro cinematic and lost the device at 233 s; this head reached the first scene after it and lost the device at 313 s. Both removals look the same (`0x887a0005`, DRED: device state 0, no breadcrumbs, no list in flight), so this commit does not remove it; neither side reached a stable world. One run per side shows no difference that can be credited to the commit. - The GPU result of the restored function on this card: the one direct GPU test of helper-lane masks skips here (no wave64), and the direct GPU tests that do run here were not checked for whether they reach `ballot_ps_quad`. The function is covered here by the CPU DXIL test; the HLSL `ballot_px_quad`, the same algorithm, has GPU tests that run on this card. - Which output is right for the texels that changed between the two forms in the 4090 report: no console reference. - Frame time beyond the smoke run: four ballots plus the spreading per pixel mask, instead of two `WaveActiveBit(Or)` and their selects. **Overlap with #3455** #3455 (open) changes the caller `Lw::ballot_ps` (quad reads lane by lane instead of `quadOp`) and rewrites and renames the same test (`direct_ps_non_vopc_carry_ballot_uses_quad_op`); it does not change the body of `ballot_ps_quad`. A test merge of the two `lanes.rs` (#3455 at `1895d9e8`) conflicts only in that test; the `ballot_ps` and `ballot_ps_quad` hunks merge cleanly. Whichever lands second rebases: if #3455 is first, the no-`waveActiveBit` check and the predicate-ballot count move into its test (with `v_readfirstlane` its variants may have one more non-constant ballot, so the count would be checked against the form without it). **AI use** - AI use: OpenAI Codex (GPT-6) and Claude Code (Claude Opus 5.5): Codex wrote the change and its test; Claude Code reviewed it (an independent read-only review as well), extended the test to wave64, ran the builds, the tests and the smoke run, and wrote this description; the 3090 measurements above were also run by Claude Code. Checked and posted by me. Base: `main` `a815de93`, one commit, no local switches.
Foniass commented 2026-12-03 16:40:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T16:40:40Z.


Review of f95100fc (code reading only, not a merge-check; draft) · no findings

In short: The PR restores the direct-DXIL Lw::ballot_ps_quad to its body from before #3501 (a50fa7ab): four WaveActiveBallot calls, one per quad member, instead of a WaveActiveBit(Or) of shifted bits. The reasons are two public reports on #3501: a 160× slower draw on an RTX 4090, and a GPU hang on a 3090. The body matches a50fa7ab^ character for character, the test now pins the form in wave32 and wave64, and the overlap with #3455 is described. Nothing blocks it.

Checked and fine
  • The body of ballot_ps_quad is identical to the one in a50fa7ab^, which I compared directly. Only its doc comment differs.
  • The algorithm is right. For each member p, the ballot of bit p over live lanes is spread across each quad's nibble (v | v>>1 | v>>2, & 0x1111_1111) and moved to bit 4k + p. Any live lane of a quad, helper lanes included, gives the quad's bits. This matches the HLSL prelude's ballot_px_quad.
  • Wave64 uses both words of the same four ballots. A wave32 program in 64-lane hardware takes its half's word through gfx_half. Plain wave32 returns only the low word.
  • The description gives a reason to prefer ballots in pixel shaders: a helper lane gets the wave's result of a ballot, but WaveActiveBitOr gave helper lanes another value on an RTX 5070 Ti (gpu_ps_helper_lanes_see_ballots_and_quads_are_lanes_4k).
  • The test now runs in wave32 and wave64. It reads VCC_LO as a word, so it does not depend on v_cndmask taking only its own bit. It counts exactly four non-constant waveActiveBallot calls, so the entry EXEC ballot does not count, and it checks that there is no waveActiveBit. Those checks fail on main.
  • No cache bump is needed: the file is under gpu/dxil, whose sources are hashed into XCL_DIRECT_DXIL_SOURCE_ID.
  • #3455 changes the caller ballot_ps and rewrites the same test. The description says the two conflict only in that test, and that whichever lands second redoes it.
  • The whole suite on this head, with the two debug-layer failures this machine has without the commit, and a smoke pair on a retail title (495 → 523 frames).
  • Not tested is explicit: no world run reaching the draw that hung, no AMD or Intel, and the direct GPU helper-lane test skips on this card (no wave64).

Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7385249522 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3797#issuecomment-7385249522). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-03T16:40:40Z. --- **Review** of `f95100fc` (code reading only, not a merge-check; draft) · no findings **In short:** The PR restores the direct-DXIL `Lw::ballot_ps_quad` to its body from before #3501 (`a50fa7ab`): four `WaveActiveBallot` calls, one per quad member, instead of a `WaveActiveBit(Or)` of shifted bits. The reasons are two public reports on #3501: a 160× slower draw on an RTX 4090, and a GPU hang on a 3090. The body matches `a50fa7ab^` character for character, the test now pins the form in wave32 and wave64, and the overlap with #3455 is described. Nothing blocks it. <details><summary>Checked and fine</summary> - The body of `ballot_ps_quad` is identical to the one in `a50fa7ab^`, which I compared directly. Only its doc comment differs. - The algorithm is right. For each member `p`, the ballot of bit `p` over live lanes is spread across each quad's nibble (`v | v>>1 | v>>2`, `& 0x1111_1111`) and moved to bit `4k + p`. Any live lane of a quad, helper lanes included, gives the quad's bits. This matches the HLSL prelude's `ballot_px_quad`. - Wave64 uses both words of the same four ballots. A wave32 program in 64-lane hardware takes its half's word through `gfx_half`. Plain wave32 returns only the low word. - The description gives a reason to prefer ballots in pixel shaders: a helper lane gets the wave's result of a ballot, but `WaveActiveBitOr` gave helper lanes another value on an RTX 5070 Ti (`gpu_ps_helper_lanes_see_ballots_and_quads_are_lanes_4k`). - The test now runs in wave32 and wave64. It reads `VCC_LO` as a word, so it does not depend on `v_cndmask` taking only its own bit. It counts exactly four non-constant `waveActiveBallot` calls, so the entry EXEC ballot does not count, and it checks that there is no `waveActiveBit`. Those checks fail on `main`. - No cache bump is needed: the file is under `gpu/dxil`, whose sources are hashed into `XCL_DIRECT_DXIL_SOURCE_ID`. - #3455 changes the caller `ballot_ps` and rewrites the same test. The description says the two conflict only in that test, and that whichever lands second redoes it. - The whole suite on this head, with the two debug-layer failures this machine has without the commit, and a smoke pair on a retail title (495 → 523 frames). - **Not tested** is explicit: no world run reaching the draw that hung, no AMD or Intel, and the direct GPU helper-lane test skips on this card (no wave64). </details> <sub>Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.</sub>
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3797:upstream-pr/3797'
git switch 'upstream-pr/3797'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3797'
git switch 'upstream-pr/3797'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3797'
git switch 'upstream-pr/3797'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3797'
git switch 'main'
git merge --squash 'upstream-pr/3797'
git switch 'main'
git merge --ff-only 'upstream-pr/3797'
git switch 'main'
git merge 'upstream-pr/3797'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3797
No description provided.