[pr-performance][shaders] Shaders: four exact translator forms, on by default (MASK_RECIPES, GUARD_FACTS, FIRSTLANE_UNIFORM, FOLD_MOVREL; =0 turns each off) #312

Open
cafe wants to merge 6 commits from cafe/translator-forms into main AGit
Contributor

Contributor role (optional)

  • Fluxer ID: 1577752157942198273 (Cafe#9189)

What it changes

  • The problem on main: 64-wide console shaders run on 32-lane cards carry long chains of per-lane mask steps, guard selects that simplify_guarded_uses keeps depending on the order it visits the code, first-lane reads of values every lane already holds, and CS_WriteRasterClassificationResults cannot be folded because of v_movrel (1,637 group barriers in it). Together about 1.25 ms of GPU time a frame on an RTX 2080 Ti.

Four translator forms, each giving the same results as now by construction, each on by default and turned off with =0. All four are in the shader cache key; each has a README row.

Switch What it does
DARNER_MASK_RECIPES Thread masks of 64-wide shaders are built from the few mask operations that made them (and/or/xor/not/select), where that takes fewer operations than one step per decision node.
DARNER_GUARD_FACTS simplify_guarded_uses keeps what it knows about each guard as two small sets (known true, known false) joined by intersection: the same fixed point whatever the visiting order, and more guard selects removed.
DARNER_FIRSTLANE_UNIFORM A v_readfirstlane of a value every lane holds alike (no loads, no LDS reads) is the lane's own value: no WaveReadLaneFirst.
DARNER_FOLD_MOVREL v_movrels/d/sd are folded like other VALU instructions (M0 indexes from the encoded register; the upper copy's is its twin, so each copy stays in its half). CS_WriteRasterClassificationResults then folds: 1,637 group barriers -> 4. Checked on the CPU: 64 inputs, the same results.

How it was tested

  • Tested revision: main f3840d73d + these commits (c55f1ace7), release build.

  • Changes a default: yes, for every card (the shaders are rebuilt once).

  • A/B result (RTX 2080 Ti, house save, the same 30 s from 20 s after the world shows, pipelines warm, the same exe, runs in turn):

    A (all four =0, as main) B (this PR's defaults)
    GPU time a frame 53.1, 53.0 ms 51.6, 52.0 ms
    fps 18.29, 18.31 18.83, 18.63
    p99 frame 81.8, 84.2 ms 78.0, 79.2 ms

    About -1.25 ms a frame. In the heavier opening pan: 69.7/69.8 -> 69.6/67.7 ms.

  • Picture: a recorded frame of the house translated again with each switch on and off: the same picture to the pixel every time (the replay has no ray tracing). In play: checked by eye in a free run on the house save with these defaults (and DARNER_JOIN_CALLS=1): no visual change seen.

  • Gates run on c55f1ace7: cargo build --locked --release -p darner ok; cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings ok (on the same code with the switches off); cargo test --locked --release -p darner-shader --lib -- --skip oracle64 with the new defaults: 489 passed, 67 failed, the same 67 as on main f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the darner-gpu tests (they create a D3D12 device), and the whole-workspace gates.

  • Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation.

  • Shader wave sizes tested: 32 lanes (NVIDIA). The forms act on 64-wide console shaders run in waves of 32; on 64-lane AMD cards fewer shaders are folded, so less changes there.

  • Hardware dependence: untested on AMD and Intel.

  • Coverage gaps: only the house scene; no game run of each switch alone (each was judged alone in the replay).

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main. (Four translator forms of one kind, each with its own switch; easy to split if you prefer.)
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found for these switches or for fold.rs, structure.rs, emit/bitcode/opt.rs, lower/analysis.rs. #76 (merged) is the earlier set of translator levers.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: `1577752157942198273` (Cafe#9189) ## What it changes - The problem on main: 64-wide console shaders run on 32-lane cards carry long chains of per-lane mask steps, guard selects that `simplify_guarded_uses` keeps depending on the order it visits the code, first-lane reads of values every lane already holds, and `CS_WriteRasterClassificationResults` cannot be folded because of `v_movrel` (1,637 group barriers in it). Together about 1.25 ms of GPU time a frame on an RTX 2080 Ti. Four translator forms, each giving the same results as now by construction, each on by default and turned off with `=0`. All four are in the shader cache key; each has a README row. | Switch | What it does | |---|---| | `DARNER_MASK_RECIPES` | Thread masks of 64-wide shaders are built from the few mask operations that made them (and/or/xor/not/select), where that takes fewer operations than one step per decision node. | | `DARNER_GUARD_FACTS` | `simplify_guarded_uses` keeps what it knows about each guard as two small sets (known true, known false) joined by intersection: the same fixed point whatever the visiting order, and more guard selects removed. | | `DARNER_FIRSTLANE_UNIFORM` | A `v_readfirstlane` of a value every lane holds alike (no loads, no LDS reads) is the lane's own value: no `WaveReadLaneFirst`. | | `DARNER_FOLD_MOVREL` | `v_movrels/d/sd` are folded like other VALU instructions (M0 indexes from the encoded register; the upper copy's is its twin, so each copy stays in its half). `CS_WriteRasterClassificationResults` then folds: 1,637 group barriers -> 4. Checked on the CPU: 64 inputs, the same results. | ## How it was tested - Tested revision: `main` f3840d73d + these commits (c55f1ace7), release build. - Changes a default: yes, for every card (the shaders are rebuilt once). - A/B result (RTX 2080 Ti, house save, the same 30 s from 20 s after the world shows, pipelines warm, the same exe, runs in turn): | | A (all four `=0`, as main) | B (this PR's defaults) | |---|---|---| | GPU time a frame | 53.1, 53.0 ms | 51.6, 52.0 ms | | fps | 18.29, 18.31 | 18.83, 18.63 | | p99 frame | 81.8, 84.2 ms | 78.0, 79.2 ms | About -1.25 ms a frame. In the heavier opening pan: 69.7/69.8 -> 69.6/67.7 ms. - Picture: a recorded frame of the house translated again with each switch on and off: the same picture to the pixel every time (the replay has no ray tracing). In play: checked by eye in a free run on the house save with these defaults (and `DARNER_JOIN_CALLS=1`): no visual change seen. - Gates run on c55f1ace7: `cargo build --locked --release -p darner` ok; `cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings` ok (on the same code with the switches off); `cargo test --locked --release -p darner-shader --lib -- --skip oracle64` with the new defaults: 489 passed, 67 failed, the same 67 as on `main` f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the `darner-gpu` tests (they create a D3D12 device), and the whole-workspace gates. - Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation. - Shader wave sizes tested: 32 lanes (NVIDIA). The forms act on 64-wide console shaders run in waves of 32; on 64-lane AMD cards fewer shaders are folded, so less changes there. - Hardware dependence: untested on AMD and Intel. - Coverage gaps: only the house scene; no game run of each switch alone (each was judged alone in the replay). ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. (Four translator forms of one kind, each with its own switch; easy to split if you prefer.) - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found for these switches or for `fold.rs`, `structure.rs`, `emit/bitcode/opt.rs`, `lower/analysis.rs`. #76 (merged) is the earlier set of translator levers. - Replaces or overlaps: none found
Each lane-mask boolean keeps the and/or/xor/not/select it was first made by (also through given()); lowering emits that one i1 operation over its operands where it takes fewer operations than one per decision node. A folded EXEC test (low half OR high half) no longer rebuilds the OR node by node. Off by default; in the shader cache key. House-pan replay: IrradianceCache_Shade 6.9 -> 4.9 ms, frame -2.3 ms, picture identical.
DARNER_MASK_RECIPES: an operand already built in the block costs nothing when choosing a recipe over the decision nodes; a cost the depth limit cut is kept only for searches as deep or deeper. House-pan replay: IrradianceCache_Shade 6.9 -> 4.3 ms, frame -2.7 ms, picture identical.
simplify_guarded_uses joined a select's false-arm demand (one false guard)
with its later unguarded demand (true guards for its true arm) to nothing
known, so the work list order decided which guard selects stayed. The new
form keeps two small sets (known true, known false), joins by intersection
and is monotone, so the fixed point is the exact one.

house-pan capture, 32-lane host: 920 programs change, 123.7k fewer DXIL
instructions (49k selects, 53k bitcasts); IrradianceCache_Shade folded
101,470 -> 89,988 instructions, 6.9 -> 6.57 ms in two replay judges,
picture identical.
A v_readfirstlane of a VGPR value every lane holds alike (lane_uniform, no
loads or LDS reads) is the lane's own value: no WaveReadLaneFirst, also in
programs without the wave64 exchange. Off by default, keyed. House-pan
replay judge: picture identical, -0.69 ms (CS_Gather_Classified 2/3/18/19
-0.1..-0.23 ms each; their march loop read 19 constants a step this way).
DARNER_FOLD_MOVREL (off by default, keyed): v_movrels/d/sd are folded like any VALU instruction; M0 indexes from the encoded register and the upper copy's is its twin, so each copy stays within its half. Lets CS_WriteRasterClassificationResults fold (1,637 group barriers -> 4). DARNER_GUARD_FACTS joins the shader cache key. README rows for DARNER_FOLD_MOVREL, DARNER_GUARD_FACTS, DARNER_MASK_RECIPES; measured cell of DARNER_FIRSTLANE_UNIFORM.
First-time contributor

Independent Windows build, gate, and code-read review of c55f1ace72ae (full head c55f1ace72ae75fff56e4cc147b5ba2ca8a887a6), based on main f3840d73d3504c30218debfbc8795b9225447317. Not run in-game.

Environment

  • Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (x86_64-pc-windows-msvc), MSVC 19.44.35230.

Measured results

  • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS.
  • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS.
  • cargo xtask policy: PASS (asset policy: 556 files ok).
  • cargo test --locked -p darner-gpu --lib shader_cache::tests: PASS (13 passed; 0 failed; 0 ignored).
  • cargo test --locked -p darner-shader --lib structure::tests: PASS (15 passed; 0 failed; 0 ignored).
  • cargo test --locked -p darner-shader --lib emit::bitcode::opt::tests: PASS (32 passed; 0 failed; 1 ignored).
  • cargo test --locked -p darner-shader --lib emit::lower::tests: PASS (30 passed; 0 failed; 1 ignored).
  • cargo test --locked -p darner-shader --lib emit::folded_tests: PASS (20 passed; 0 failed; 0 ignored).
  • cargo test --locked -p darner-shader --lib emit::wave64_tests: PASS (48 passed; 0 failed; 1 ignored).
  • Full workspace test gate (cargo test --workspace --locked) and gameplay/performance A/B were not run in this review.

Code-read audit

  • Mask recipes retain the first operation recipe for each canonical BDD value, check recipe operands stay within that value's tested variables, choose only a strictly cheaper form, and cap recursive planning/lowering before falling back to decision nodes (crates/darner-shader/src/structure.rs:55-89, 313-335; crates/darner-shader/src/emit/lower.rs:4740-4780, 4830-4895). given_recipe keeps a bounded fallback and has a test-only equality check against node restriction (structure.rs:501-565).
  • The guarded-facts pass is selected by default with =0 retaining the old path (crates/darner-shader/src/emit/bitcode/opt.rs:2740-2755). Its joins intersect facts across uses, propagation excludes integer division/remainder and non-speculatable calls, phi facts are filtered by dominance, and rewriting visits consumers before definitions (emit/bitcode/opt/guarded_facts.rs:143-160, 246-337, 357-462). The new order-sensitive regression and existing trap/read negative controls passed (opt.rs:8040-8073; optimizer test results above).
  • First-lane elision requires the source to be in the lane-uniform set and rejects partial-dispatch activity before substituting the lane's own value (crates/darner-shader/src/emit/lower.rs:1919-1923, 10864-10880). The uniform analysis skips guarded instructions and, with its normal defaults, does not infer uniformity from loads or LDS reads (emit/lower/analysis.rs:500-684). All four new translation switches are included in the shader cache key (crates/darner-gpu/src/shader_cache.rs:71-137).
  • Folding maps each upper-half VGPR base to its twin while retaining the same M0 offset; v_swaprel_b32 remains rejected, and the cache key includes DARNER_FOLD_MOVREL (crates/darner-shader/src/fold.rs:56-68, 370-380, 1611-1617; shader_cache.rs:134-137). I found no dedicated folded-output regression test for v_movrel; the PR description reports a 64-input CPU check, which I did not independently reproduce. No game/performance A/B was run here.

No blocking issue found.

Verdict: PASS.

AI-assisted: yes (GitHub Copilot, Auto mode).

Independent Windows build, gate, and code-read review of `c55f1ace72ae` (full head `c55f1ace72ae75fff56e4cc147b5ba2ca8a887a6`), based on `main` `f3840d73d3504c30218debfbc8795b9225447317`. Not run in-game. ### Environment - Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (`x86_64-pc-windows-msvc`), MSVC 19.44.35230. ### Measured results - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): **PASS**. - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): **PASS**. - `cargo xtask policy`: **PASS** (`asset policy: 556 files ok`). - `cargo test --locked -p darner-gpu --lib shader_cache::tests`: **PASS** (`13 passed; 0 failed; 0 ignored`). - `cargo test --locked -p darner-shader --lib structure::tests`: **PASS** (`15 passed; 0 failed; 0 ignored`). - `cargo test --locked -p darner-shader --lib emit::bitcode::opt::tests`: **PASS** (`32 passed; 0 failed; 1 ignored`). - `cargo test --locked -p darner-shader --lib emit::lower::tests`: **PASS** (`30 passed; 0 failed; 1 ignored`). - `cargo test --locked -p darner-shader --lib emit::folded_tests`: **PASS** (`20 passed; 0 failed; 0 ignored`). - `cargo test --locked -p darner-shader --lib emit::wave64_tests`: **PASS** (`48 passed; 0 failed; 1 ignored`). - Full workspace test gate (`cargo test --workspace --locked`) and gameplay/performance A/B were not run in this review. ### Code-read audit - Mask recipes retain the first operation recipe for each canonical BDD value, check recipe operands stay within that value's tested variables, choose only a strictly cheaper form, and cap recursive planning/lowering before falling back to decision nodes (`crates/darner-shader/src/structure.rs:55-89`, `313-335`; `crates/darner-shader/src/emit/lower.rs:4740-4780`, `4830-4895`). `given_recipe` keeps a bounded fallback and has a test-only equality check against node restriction (`structure.rs:501-565`). - The guarded-facts pass is selected by default with `=0` retaining the old path (`crates/darner-shader/src/emit/bitcode/opt.rs:2740-2755`). Its joins intersect facts across uses, propagation excludes integer division/remainder and non-speculatable calls, phi facts are filtered by dominance, and rewriting visits consumers before definitions (`emit/bitcode/opt/guarded_facts.rs:143-160`, `246-337`, `357-462`). The new order-sensitive regression and existing trap/read negative controls passed (`opt.rs:8040-8073`; optimizer test results above). - First-lane elision requires the source to be in the lane-uniform set and rejects partial-dispatch activity before substituting the lane's own value (`crates/darner-shader/src/emit/lower.rs:1919-1923`, `10864-10880`). The uniform analysis skips guarded instructions and, with its normal defaults, does not infer uniformity from loads or LDS reads (`emit/lower/analysis.rs:500-684`). All four new translation switches are included in the shader cache key (`crates/darner-gpu/src/shader_cache.rs:71-137`). - Folding maps each upper-half VGPR base to its twin while retaining the same M0 offset; `v_swaprel_b32` remains rejected, and the cache key includes `DARNER_FOLD_MOVREL` (`crates/darner-shader/src/fold.rs:56-68`, `370-380`, `1611-1617`; `shader_cache.rs:134-137`). I found no dedicated folded-output regression test for `v_movrel`; the PR description reports a 64-input CPU check, which I did not independently reproduce. No game/performance A/B was run here. No blocking issue found. **Verdict: PASS.** AI-assisted: yes (GitHub Copilot, Auto mode).
This pull request has changes conflicting with the target branch.
  • crates/darner-gpu/src/shader_cache.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/312/head:cafe/translator-forms'
git switch 'cafe/translator-forms'

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 'cafe/translator-forms'
git switch 'cafe/translator-forms'
git rebase 'main'
git switch 'main'
git merge --ff-only 'cafe/translator-forms'
git switch 'cafe/translator-forms'
git rebase 'main'
git switch 'main'
git merge --no-ff 'cafe/translator-forms'
git switch 'main'
git merge --squash 'cafe/translator-forms'
git switch 'main'
git merge --ff-only 'cafe/translator-forms'
git switch 'main'
git merge 'cafe/translator-forms'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!312
No description provided.