Skip to content

d3d12-shader: preserve non-EXEC writelanes and full/exempt LDS in paired Wave64 form (D3D12-SHADER-PAIRED-MULTI-BARRIER-EXACTNESS) - #3262

Open
zmasi wants to merge 3 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-shader-paired-multi-barrier-exactness
Open

zmasi wants to merge 3 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-shader-paired-multi-barrier-exactness

Conversation

@zmasi

@zmasi zmasi commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-SHADER-PAIRED-MULTI-BARRIER-EXACTNESS
(#2570 (comment))

What and why

@enlight3d found that naming six heavy compute programs in LEONIDA_PAIRED_FOR changes the picture, although the
paired form is described as exact (#2570, #issuecomment-7352754262). The paired form runs console lanes n and
n + 32 in host thread n. I ran it beside the split form on an RTX 3090, each form from its own entry point, against
a 64-lane model of the console's wave. The programs were hand-written ones and 300 seeded random ones: ballots as
numbers and through the 64-bit scalar operations, counts, ranks, first-lane and lane reads and writes, the compiler's
DPP reductions and prefix sums, permlanes, a DPP row mask, and EXEC counted with its SCC. They ran under lane branches,
scalar branches and loops that lanes leave apart, in groups of one to sixteen console waves, with multi-barrier LDS
rounds. Three things the paired form did not keep. Each test fails before its fix and passes now:

  • v_writelane_b32 to a lane outside EXEC was lost. The instruction writes the lane it names whatever EXEC is
    (docs/design.md, "Lane select"). The form guarded it like a lane's own write. It is now lowered for both lanes of
    the thread without the guard (paired_cross).
  • The programs named LDS-unbounded (LEONIDA_LDS_BOUNDS_EXEMPT, by default the face programs) were held to their
    header's LDS in the paired form.
    That form checks every access against the group's own array: past the header a
    read gave 0 and a write was dropped. That is what left the faces undrawn before the exemption. Such a program now
    has the group's 32 KiB in this form. A header that gives more than 32 KiB is still refused, named or not.
  • A group of several waves using all 32 KiB of LDS was refused for a counter dword the paired form no longer keeps.

With these fixes, all 300 random programs match the model in the paired form. Half of them use everything the
generator has. The other half are held to what the split form keeps; 141 of those 150 also match in the split form,
which refuses the other 9.

Recorded in d3d12/docs/findings.md and left as it is, since all of it is outside paired.rs:

  • The paired form: a lane mask made of a 64-bit number whose two dwords were written apart (two s_mov_b32, as a
    64-bit constant is loaded) is wrong in lanes 32–63 (structure.rs's mask shortcut). The split form refuses the
    same program.
  • Both forms:
    • s_wqm_b64 gives every lane.
    • A DPP16 source lane outside EXEC with fi 0, and a ds_swizzle_b32 source lane outside EXEC, are read as they
      are.
    • f32 sums are joined in the host's order.
  • The split form:
    • lane writes guarded by EXEC;
    • lane selects past 63 taken as they are;
    • per-lane scalars after a lane branch or loop;
    • the prefix-sum section inside such a loop.

The paired form is exact for every one of the split form's programs.

Not measured in the game. The six programs of the report are not named by hash there, so which of these each one hit
is open. Naming them in LEONIDA_PAIRED_FOR again on this change, at the same shot, is the check.

Tested

  • New GPU tests in d3d12/crates/leonida-shader/src/emit/paired/:
    • lane_tests.rs:
      • writelane outside EXEC, including selects of 64 or more;
      • ballot dwords and both mbcnt ranks under EXEC;
      • sibling reads and writes with first lane;
      • prefix sums;
      • EXEC counted with its SCC;
      • permlanes with sources outside EXEC;
      • a DPP row mask.
    • barrier_tests.rs:
      • multi-barrier LDS rounds with an EXEC-narrowed branch;
      • two waves exchanging snapshots;
      • the last dword of a full 32 KiB with two waves;
      • the named program's LDS past its header;
      • refusals that run no dispatch.
    • fuzz_tests.rs: 36 seeded programs in the suite (300 in a sweep), shrunk when one goes wrong.
    • differential_tests.rs: the shared harness. The paired form is judged first, so a split-form fault never leaves
      it unjudged.
  • Red on main, green here: the writelane, full-32-KiB and named-LDS tests, and the fuzz test. Checked by hand (the
    change's production files reverted) and with python -I scripts/red_green.py: verdict "red then green", those 4
    tests red then green. The other new tests pass on main too: they check behaviour this change keeps.
  • cargo test --locked -p leonida-shader (x86_64-pc-windows-msvc): 440 passed, 0 failed, 6 ignored.
  • cargo clippy --locked -p leonida-shader --all-targets --no-deps: no warning in the touched files.
  • cargo xtask policy: no violation in the touched files (the three it reports are on main).
  • python -I scripts/check_no_game_data.py, python -I tests/test_text_write_line_endings.py,
    python -I tests/test_docs_design_rows.py, python -m unittest discover -s tests -p "test_docs_*.py": pass.

GPUs tested: NVIDIA GeForce RTX 3090 24 GB, driver 610.88, Windows 11 Pro 10.0.26200 (Microsoft WARP has no 32-lane
waves, so it skips these tests).

AI-assisted: yes.

  • Author: GPT-6.2 Astra, which also wrote the lane tests through a subagent.
  • Read-only audits: GPT-6.2 Sol and Claude Fable 5.2. Fable also wrote the barrier tests through a subagent.
  • Separate read-only reviewers: Claude Opus 5.5 (two passes on the first commit; all findings addressed), Claude Fable 5.2 (live advisor), and Gemini 4 Neon XHigh (independent pass on the second commit; no blockers).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No new rule:
    the writelane fix applies the existing "Lane select" row.)

zmasi added 3 commits December 2, 2026 12:44
…face programs' LDS (D3D12-SHADER-PAIRED-MULTI-BARRIER-EXACTNESS)

The paired form (one host thread runs console lanes n and n + 32) ran beside the split form
on an RTX 3090 (driver 610.88), both against a 64-lane model of the console's wave: hand-
written programs and seeded random ones. Three things it did not keep, each failing before
and passing now:

- v_writelane_b32 to a lane outside EXEC was lost: the form guarded it like a lane's own
  write. It now writes the lane its select names (S1[5:0]) whatever EXEC is, as the "Lane
  select" row of docs/design.md says.
- A program named in LEONIDA_LDS_BOUNDS_EXEMPT (the face programs) was held to its header's
  LDS all the same, since the form bounds every access: past the header a read gave 0 and a
  write was dropped. It now has the group's 32 KiB; a header that gives more is refused, as
  before.
- A group of several waves using all 32 KiB of LDS was refused for a counter dword the form
  no longer keeps.

Tests: emit/paired/{lane,barrier,fuzz,differential}_tests.rs, each form from its own entry
point, against the model; the random programs are shrunk when one goes wrong. d3d12/docs/
findings.md: what the runs showed, with what is not the paired form's, left as it is (the
split form's lane writes, lane selects past 63 and per-lane scalars; in both forms a mask
made of two 32-bit writes and the host's order of f32 sums).

AI-assisted: GPT-6.2 Astra (author; the LaneGpu tests), GPT-6.2 Sol (LaneAudit, read-only),
Claude Fable 5.2 (ControlAudit, read-only; the BarrierGpu tests), Claude Opus 5.5 (separate
read-only reviewer, two passes).
… it is judged first (D3D12-SHADER-PAIRED-MULTI-BARRIER-EXACTNESS)

Hand-written programs, each form against the console's model (rules of tools/crosslane_fuzz.py,
measured on RDNA 2), for the cross-lane reads the audit had only read in the code:

- Exact in both forms, now tests: s_bcnt1_i32_b64 and s_ff1_i32_b64 of EXEC with its SCC through
  s_cselect_b32 and s_cbranch_scc0; v_permlane16_b32 and v_permlanex16_b32 with sources outside
  EXEC (FI 0 keeps the lane's value, FI 1 reads the source); a DPP row mask over both halves.
- Forward ds_permute_b32 is refused by both forms, with LDS or without.
- Not exact in either form, recorded in d3d12/docs/findings.md and left as it is (emit/lower.rs):
  s_wqm_b64 gives every lane; a DPP16 source lane outside EXEC with fi 0, and a ds_swizzle_b32
  source lane outside EXEC, are read as they are.

findings.md also says now that a lane mask made of two separately written dwords is wrong in
lanes 32-63 in the paired form, where the split form refuses the program; and that the split
form's lane writes, lane selects past 63, per-lane scalars and prefix sums were each run against
the model on the card (the paired form exact for every one of them).

differential_tests::forms puts the paired form first, so a form stopping at its first fault
never leaves the paired form unjudged for one of the split form's.

AI-assisted: GPT-6.2 Astra (author). Prompted by review notes from other models.

@merzleckeier merzleckeier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent translation check on 3643c2879 with Rust 1.98.1 on Linux. I reused the PR’s synthetic two-wave fixture and called the paired emitter directly with a 32-lane host selection.

  • The two-wave shader with the full 32 KiB of LDS now translates in the paired form.
  • A shader with a 512-byte header and lds_unbounded=true emits byte-identical DXIL to the same shader with an explicit 32 KiB header. The ordinary 512-byte translation emits different DXIL, so the comparison exercises the allocation change.

Both probes fail with only emit/mod.rs restored from main (ef35d683e). The Linux shader unit suite plus the two probes passed 256, with two ignored. The checks were limited to translation and emitted bytes. The paired dispatch results, including the EXEC-disabled v_writelane_b32 case, remain GPU coverage. git diff --check passed and the PR also merges cleanly with #3215. AI-assisted.

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