d3d12: console lanes 32..63 of a wave64 VS/PS where the code proves them (D3D12-SHADER-UPPER-LANES) #2706

Closed
leonida/mandy
spzero12 wants to merge 0 commits from shader-upper-lanes into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

spzero12 commented 2026-11-30 09:10:58 +00:00

Claimed in the coordination issue as: D3D12-SHADER-UPPER-LANES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7270183812)

Depends on: none

What and why

A 32-lane host runs a wave64 vertex or pixel program as waves of 32, so a v_readlane of a console lane >= 32 reads no host lane: today a constant lane is refused outside compute ("a cross-lane operation outside compute") and a computed one is lowered to WaveReadLaneAt(v, lane & 31).

emit/upper_lanes.rs is the port planned in d3d12/docs/wave64-upper-lanes.md: a forward pass over crate::ir that follows what lanes 32..63 hold, and the v_readlane is lowered from that value instead of the read.

  • State: per VGPR and console lane, a lane is a constant, an IR value (a scalar the code put there, a uniform a section left), or unknown; block joins keep only what both paths agree on.
  • EXEC's upper word starts at anything, not 0: a full vertex wave has every upper lane active, so ordinary VALU writes may have written them. Only a whole-wave section (EXEC all ones, taken as one step from wave::section_lanes) or a v_writelane proves a lane; a plain write to the VGPR clears what the pass knew.
  • A read the pass cannot prove keeps today's lowering (and LEONIDA_UPPER_LANES_WHY=1 says how many it resolved and left). LEONIDA_UPPER_LANES=0 turns it off for an A/B; the shader cache keys on it (the build script lists every LEONIDA_* the translator reads).

What the pack shows, so nobody expects a picture change from it. Before writing it we assumed the compiler's lane-spill and post-section reads were unfixed. They are not:

  • ir::unspill already rewrites a VGPR whose every use is the v_writelane/v_readlane pair with a constant lane into scalar moves, before any lowering.
  • wave::sections' Section.reads already lowers a same-block v_readlane after a whole-wave section (the reduction's broadcast read included).

Over all 26,503 containers of the pack (D:\leonida-work2\shaders\dxbc, translate_cdxil --census) the pass lowers for 353 graphics programs and resolves 0 reads, 355 left. The programs looked at (!PS_FogComposite_57ae119e, _PS_PropFoliageForwardLit_18b3d403) leave a waterfall read at a computed lane (v_readlane sX, v, s2), which no constant stands in for. No translation changed, and the pack's 202 failures are the same as without the pass (195 containers without a program, 2 no wave32 lowering, 5 a descriptor read only as data).

So this adds the exact rule for the cases the pack does not contain — a spill through a lane held in an SGPR, a VGPR with other writes, and a read in another block than its section — and it costs nothing where it proves nothing (a program with no such read is byte-identical with the switch off). A whole-wave reduction over all 64 lanes stays an approximation in VS/PS: those stages have no group-shared memory or barrier to exchange the halves (the separate, larger port in the doc).

Tested

  • cargo test -p leonida-shader upper_lanes: 9 passed (hand-written pixel programs, no game files): a spill through an SGPR lane drawn to a target, a lower-lane spill, a plain write clearing the knowledge, a lane built in an SGPR, a section's identity and a section's uniform value, a compute program untouched, and a program without such a read byte-identical to the switch off.
  • cargo test -p leonida-shader --lib: 393 passed, 0 failed, 4 ignored (the run started before the ninth upper-lane test was added; that one passes separately, so 394 with it).
  • cargo clippy -p leonida-shader --all-targets: no warning from the new files (the two warnings left are the pre-existing ones in lower.rs and bitcode/opt.rs).
  • The target draw test runs the translated pixel program on the hardware adapter and on WARP and checks every lit texel is the spilled 2.0.
  • Corpus: 26,503 containers translated before/after, 0 changed, 0 resolved (above). No spirv-val: this is the d3d12 Rust translator, not tools/rdna2spirv.py; the emitted modules go through dxil.dll in the tests where it is installed.

GPUs tested: NVIDIA GeForce GTX 1070 8 GB, driver 582.78, Windows 11 (hardware and WARP).

AI-assisted: yes (Claude).

Checklist

  • Based on current main and merges cleanly (branch shader-upper-lanes, one commit on 1bb726b30).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption; the tests build their input by hand and the corpus counts quote no shader bytes.
  • The docs/design.md contract table is the Python translator's (tools/rdna2spirv.py); this rule is in the d3d12 Rust translator and is written up with how it was confirmed in d3d12/docs/wave64-upper-lanes.md and d3d12/docs/findings.md.
Claimed in the coordination issue as: `D3D12-SHADER-UPPER-LANES` (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7270183812) Depends on: none ### What and why A 32-lane host runs a wave64 vertex or pixel program as waves of 32, so a `v_readlane` of a console lane >= 32 reads no host lane: today a constant lane is refused outside compute ("a cross-lane operation outside compute") and a computed one is lowered to `WaveReadLaneAt(v, lane & 31)`. `emit/upper_lanes.rs` is the port planned in `d3d12/docs/wave64-upper-lanes.md`: a forward pass over `crate::ir` that follows what lanes 32..63 hold, and the `v_readlane` is lowered from that value instead of the read. - State: per VGPR and console lane, a lane is a constant, an IR value (a scalar the code put there, a uniform a section left), or unknown; block joins keep only what both paths agree on. - EXEC's upper word starts at *anything*, not 0: a full vertex wave has every upper lane active, so ordinary VALU writes may have written them. Only a whole-wave section (EXEC all ones, taken as one step from `wave::section_lanes`) or a `v_writelane` proves a lane; a plain write to the VGPR clears what the pass knew. - A read the pass cannot prove keeps today's lowering (and `LEONIDA_UPPER_LANES_WHY=1` says how many it resolved and left). `LEONIDA_UPPER_LANES=0` turns it off for an A/B; the shader cache keys on it (the build script lists every `LEONIDA_*` the translator reads). **What the pack shows, so nobody expects a picture change from it.** Before writing it we assumed the compiler's lane-spill and post-section reads were unfixed. They are not: - `ir::unspill` already rewrites a VGPR whose every use is the `v_writelane`/`v_readlane` pair with a constant lane into scalar moves, before any lowering. - `wave::sections`' `Section.reads` already lowers a same-block `v_readlane` after a whole-wave section (the reduction's broadcast read included). Over **all 26,503 containers** of the pack (`D:\leonida-work2\shaders\dxbc`, `translate_cdxil --census`) the pass lowers for **353 graphics programs and resolves 0 reads, 355 left**. The programs looked at (`!PS_FogComposite_57ae119e`, `_PS_PropFoliageForwardLit_18b3d403`) leave a waterfall read at a computed lane (`v_readlane sX, v, s2`), which no constant stands in for. **No translation changed**, and the pack's 202 failures are the same as without the pass (195 containers without a program, 2 no wave32 lowering, 5 a descriptor read only as data). So this adds the exact rule for the cases the pack does not contain — a spill through a lane held in an SGPR, a VGPR with other writes, and a read in another block than its section — and it costs nothing where it proves nothing (a program with no such read is byte-identical with the switch off). A whole-wave *reduction* over all 64 lanes stays an approximation in VS/PS: those stages have no group-shared memory or barrier to exchange the halves (the separate, larger port in the doc). ### Tested - `cargo test -p leonida-shader upper_lanes`: **9 passed** (hand-written pixel programs, no game files): a spill through an SGPR lane drawn to a target, a lower-lane spill, a plain write clearing the knowledge, a lane built in an SGPR, a section's identity and a section's uniform value, a compute program untouched, and a program without such a read byte-identical to the switch off. - `cargo test -p leonida-shader --lib`: **393 passed, 0 failed, 4 ignored** (the run started before the ninth upper-lane test was added; that one passes separately, so 394 with it). - `cargo clippy -p leonida-shader --all-targets`: no warning from the new files (the two warnings left are the pre-existing ones in `lower.rs` and `bitcode/opt.rs`). - The target draw test runs the translated pixel program on the hardware adapter and on WARP and checks every lit texel is the spilled 2.0. - Corpus: 26,503 containers translated before/after, 0 changed, 0 resolved (above). No `spirv-val`: this is the d3d12 Rust translator, not `tools/rdna2spirv.py`; the emitted modules go through `dxil.dll` in the tests where it is installed. GPUs tested: NVIDIA GeForce GTX 1070 8 GB, driver 582.78, Windows 11 (hardware and WARP). AI-assisted: yes (Claude). ### Checklist - [x] Based on current `main` and merges cleanly (branch `shader-upper-lanes`, one commit on `1bb726b30`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption; the tests build their input by hand and the corpus counts quote no shader bytes. - [ ] The `docs/design.md` contract table is the Python translator's (`tools/rdna2spirv.py`); this rule is in the d3d12 Rust translator and is written up with how it was confirmed in `d3d12/docs/wave64-upper-lanes.md` and `d3d12/docs/findings.md`.
merzleckeier commented 2026-11-30 09:24:25 +00:00

Independent check of 0bfcb3a1 on main 1bb726b30. I found a false resolution in exec_after() in emit/upper_lanes.rs. The s_and* shortcut preserves Hi::Zero for any destination of EXEC. An ordinary s_and_b64 reads two explicit operands, so it can replace EXEC without reading the old mask.

This hand-written wave64 pixel fixture reproduces it (assembled with llvm-mc -arch=amdgcn -mcpu=gfx1030):

s_mov_b32 exec_hi, 0
s_mov_b32 s5, 2.0
s_mov_b32 s4, 40
v_writelane_b32 v1, s5, s4
s_and_b64 exec, -1, -1
v_mov_b32_e32 v1, 1.0
v_readlane_b32 s6, v1, s4
v_mov_b32_e32 v0, s6
exp mrt0 v0, v0, v0, v0 done vm
s_endpgm

The AND sets every EXEC bit, so the v_mov_b32 overwrites lane 40 with 1.0. analyse() instead resolves the read to Some(Value(Value(4))), the old 2.0 SGPR value. As a control, s_and_b64 exec, exec, -1 really does preserve an empty upper mask and should continue to resolve the spill. A locally tested conservative rule kept Hi::Zero for the explicit AND only if one source was current EXEC (for ANDN2, the first source), and kept the implicit 64-bit AND/ANDN2 SAVEEXEC forms. The overwrite case then returned None and the control still resolved. This report's overwrite evidence is from IR analysis. The separate GPU probe for that case produced neither of the reference colors.

Measured under Wine 11.22 / VKD3D-Proton 3.1.0: all nine stock upper-lane tests pass on RTX 4070 SUPER (NVIDIA 620.73.04) and Radeon 610M (RADV 26.3.1). A separate draw that requires a real hardware device read back [2.0; 4] in all 64 pixels for the ordinary lane-40 spill on both. The conservative local comparison passed 12/12 on each GPU, including both IR cases and the live draw. Native Linux leonida-shader --lib passed 228 on both trees, 2 ignored.

AI-assisted: yes (OpenAI Codex).

Independent check of `0bfcb3a1` on `main` `1bb726b30`. I found a false resolution in `exec_after()` in `emit/upper_lanes.rs`. The `s_and*` shortcut preserves `Hi::Zero` for any destination of EXEC. An ordinary `s_and_b64` reads two explicit operands, so it can replace EXEC without reading the old mask. This hand-written wave64 pixel fixture reproduces it (assembled with `llvm-mc -arch=amdgcn -mcpu=gfx1030`): ```asm s_mov_b32 exec_hi, 0 s_mov_b32 s5, 2.0 s_mov_b32 s4, 40 v_writelane_b32 v1, s5, s4 s_and_b64 exec, -1, -1 v_mov_b32_e32 v1, 1.0 v_readlane_b32 s6, v1, s4 v_mov_b32_e32 v0, s6 exp mrt0 v0, v0, v0, v0 done vm s_endpgm ``` The AND sets every EXEC bit, so the `v_mov_b32` overwrites lane 40 with `1.0`. `analyse()` instead resolves the read to `Some(Value(Value(4)))`, the old `2.0` SGPR value. As a control, `s_and_b64 exec, exec, -1` really does preserve an empty upper mask and should continue to resolve the spill. A locally tested conservative rule kept `Hi::Zero` for the explicit AND only if one source was current EXEC (for ANDN2, the first source), and kept the implicit 64-bit AND/ANDN2 SAVEEXEC forms. The overwrite case then returned `None` and the control still resolved. This report's overwrite evidence is from IR analysis. The separate GPU probe for that case produced neither of the reference colors. **Measured under Wine 11.22 / VKD3D-Proton 3.1.0:** all nine stock upper-lane tests pass on RTX 4070 SUPER (NVIDIA 620.73.04) and Radeon 610M (RADV 26.3.1). A separate draw that requires a real hardware device read back `[2.0; 4]` in all 64 pixels for the ordinary lane-40 spill on both. The conservative local comparison passed 12/12 on each GPU, including both IR cases and the live draw. Native Linux `leonida-shader --lib` passed 228 on both trees, 2 ignored. AI-assisted: yes (OpenAI Codex).
spzero12 commented 2026-11-30 09:41:54 +00:00

Thanks, that is a real one and a good catch. Fixed in 70b273f6a on the branch.

exec_after() now keeps Hi::Zero only where the new EXEC is old EXEC & <anything>:

  • the AND/ANDN2 saveexec forms and the ANDN2 wrexec forms (the old mask is implicit);
  • an explicit s_and_b64 with EXEC as either source;
  • an explicit s_andn2_b64 with EXEC as the first source (S0 & ~S1 only clears by S0; your s_andn2_b64 exec, -1, exec case is not preserved).

Every other s_and*, including your s_and_b64 exec, -1, -1, is Hi::Any now. A low-word-only write also leaves the upper word unchanged for every instruction, not just s_mov_b32.

Regression tests use your fixture: s_and_b64 exec, -1, -1 leaves the read unresolved; s_and_b64 exec, exec, -1, s_andn2_b64 exec, exec, -1 and s_and_saveexec_b64 s[8:9], -1 still resolve the spill; s_andn2_b64 exec, -1, exec does not. cargo test -p leonida-shader upper_lanes: 13 passed (the target-draw test ran on a GTX 1070 and WARP here). The corpus measurement in the description is unchanged (0 reads resolved), so no translation moves.

AI-assisted (Claude).

Thanks, that is a real one and a good catch. Fixed in `70b273f6a` on the branch. `exec_after()` now keeps `Hi::Zero` only where the new EXEC is `old EXEC & <anything>`: - the AND/ANDN2 `saveexec` forms and the ANDN2 `wrexec` forms (the old mask is implicit); - an explicit `s_and_b64` with EXEC as either source; - an explicit `s_andn2_b64` with EXEC as the **first** source (`S0 & ~S1` only clears by S0; your `s_andn2_b64 exec, -1, exec` case is not preserved). Every other `s_and*`, including your `s_and_b64 exec, -1, -1`, is `Hi::Any` now. A low-word-only write also leaves the upper word unchanged for every instruction, not just `s_mov_b32`. Regression tests use your fixture: `s_and_b64 exec, -1, -1` leaves the read unresolved; `s_and_b64 exec, exec, -1`, `s_andn2_b64 exec, exec, -1` and `s_and_saveexec_b64 s[8:9], -1` still resolve the spill; `s_andn2_b64 exec, -1, exec` does not. `cargo test -p leonida-shader upper_lanes`: **13 passed** (the target-draw test ran on a GTX 1070 and WARP here). The corpus measurement in the description is unchanged (0 reads resolved), so no translation moves. AI-assisted (Claude).
merzleckeier commented 2026-11-30 09:46:53 +00:00

I rebuilt 70b273f6a merged onto main 4014af7fe and confirmed that the original explicit-AND reproducer is fixed. All 13 stock upper_lanes tests passed under Wine 11.22 / VKD3D-Proton 3.1.0 on RTX 4070 SUPER (620.73.04) and Radeon 610M (RADV 26.3.1).

Two implicit-mask forms still produce the same false resolution. AMD's RDNA 2 SOP1 tables specify new EXEC = S0 & ~old EXEC for both s_andn2_saveexec_b64 and s_andn2_wrexec_b64. They differ in the value written to the destination SGPRs. With S0=-1, each sets the previously clear upper mask bits. Your explicit s_andn2_b64 exec, -1, exec control has that same effect on EXEC.

Using the PR's spill_read helper, these are the two additional cases (encodings checked with llvm-mc -arch=amdgcn -mcpu=gfx1030):

assert!(spill_read(0xBE88_27C1).is_none()); // s_andn2_saveexec_b64 s[8:9], -1
assert!(spill_read(0xBE88_3AC1).is_none()); // s_andn2_wrexec_b64 s[8:9], -1

Both return Some(Value(Value(4))) at 70b273f6a, substituting the stale 2.0 for the overwritten lane 40. I removed the implicit ANDN2 forms from clears_upper in a local comparison, keeping the explicit AND/ANDN2 operand checks and the AND SAVEEXEC controls. Both new cases returned None, and all 15 targeted tests then passed on both machines' adapter paths. This reproduction checks the data-flow analysis. The repository's separate SPIR-V test_andn2_exec_b64.py also passes 4/4 and agrees with AMD's semantics.

AI-assisted: yes (OpenAI Codex).

I rebuilt `70b273f6a` merged onto `main` `4014af7fe` and confirmed that the original explicit-AND reproducer is fixed. All 13 stock `upper_lanes` tests passed under Wine 11.22 / VKD3D-Proton 3.1.0 on RTX 4070 SUPER (620.73.04) and Radeon 610M (RADV 26.3.1). Two implicit-mask forms still produce the same false resolution. [AMD's RDNA 2 SOP1 tables](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content) specify `new EXEC = S0 & ~old EXEC` for both `s_andn2_saveexec_b64` and `s_andn2_wrexec_b64`. They differ in the value written to the destination SGPRs. With `S0=-1`, each sets the previously clear upper mask bits. Your explicit `s_andn2_b64 exec, -1, exec` control has that same effect on EXEC. Using the PR's `spill_read` helper, these are the two additional cases (encodings checked with `llvm-mc -arch=amdgcn -mcpu=gfx1030`): ```rust assert!(spill_read(0xBE88_27C1).is_none()); // s_andn2_saveexec_b64 s[8:9], -1 assert!(spill_read(0xBE88_3AC1).is_none()); // s_andn2_wrexec_b64 s[8:9], -1 ``` Both return `Some(Value(Value(4)))` at `70b273f6a`, substituting the stale 2.0 for the overwritten lane 40. I removed the implicit ANDN2 forms from `clears_upper` in a local comparison, keeping the explicit AND/ANDN2 operand checks and the AND SAVEEXEC controls. Both new cases returned `None`, and all 15 targeted tests then passed on both machines' adapter paths. This reproduction checks the data-flow analysis. The repository's separate SPIR-V `test_andn2_exec_b64.py` also passes 4/4 and agrees with AMD's semantics. AI-assisted: yes (OpenAI Codex).
Sign in to join this conversation.
No description provided.