shader/lower: preserve builder_bounds root header size guard when WIDE loads are enabled (D3D12-SHADER-WIDE-BUILDER-BOUNDS) #2860

Merged
djrolfes merged 1 commit from upstream-pr/2860 into main 2026-12-01 20:22:24 +00:00
djrolfes commented 2026-12-01 10:24:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by djrolfes on 2026-12-01T10:24:47Z.


Claimed in the coordination issue as: D3D12-SHADER-WIDE-BUILDER-BOUNDS (claim comment)

What and why

Preserve the builder_bounds sanity clamp (x < (1 << 20) ? x : 0) on 1-dword spilled root SRV header size loads (offset == 4) when bitcode::opt::WIDE (LEONIDA_SHADER_OPT bit 64) is enabled.

  • Bug on main: In d3d12/crates/leonida-shader/src/emit/lower.rs (Lower::smem), if Self::wide_loads() returns early after calling self.raw_dwords(h, addr, count), completely skipping the if size && root sanity clamp 20 lines below.
  • Fix: Guard the early return with if Self::wide_loads() && !(size && root) (1-line production change). All multi-dword loads and non-header 1-dword loads still take raw_dwords; only the 1-dword builder header size load falls through to apply the IULT (1 << 20) clamp.
  • Evidence classification: READ IN CODE (d3d12/crates/leonida-shader/src/emit/lower.rs:8582–8625) + red→green unit test in leonida-shader + MEASURED 300 s in-game gate on exact PR head 60ef5bd2e.

Tested

  • python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/lower.rs → exit 0.
  • Native Linux cargo test --locked -p leonida-shader:
    • RED on origin/main (only the #[cfg(test)] hook + new test applied, no production change):
      running 1 test
      test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... FAILED
      # assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4
      #   left: Inst(48)
      #  right: Inst(50)
      test result: FAILED. 0 passed; 1 failed; 0 ignored
      
    • GREEN on PR head 60ef5bd2e (rebased onto cd73ddab7):
      running 245 tests
      ...
      test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... ok
      test result: ok. 243 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out
      
  • 300 s in-game gate (docs/game_gate.md) on exact PR head 60ef5bd2e (leonida.exe --profile play --features mimalloc,dlss,diagnostics, Series S + DLSS Quality 1280×720 → 1920×1080):
    • Verdict: PASS
    • In-world duration: 310.97 s (world_at_s = 99.55 s, >= 300 s required)
    • Frames (world + 60 s to stop): 3,320 presented frames (3,429 total world presents), 15.31 median FPS, 106.10 ms p95 frame time (15.55 end-window median FPS, 4,354.5 median draws/frame)
    • Longest present gap in world: 6.597 s (<= 10.0 s required)
    • Peak VRAM: 6,645 MiB (<= 7,400 MiB guard)
    • Stability: 0 device losses, 0 crashes/faults, 0 panics, 0 kernel NVRM: Xid errors

GPUs tested: NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Arch Linux (Wine 11.22 + VKD3D-Proton 3.2.0).

AI-assisted: yes (authored by Gemini 4 Neon; audited and approved in a separate independent review pass by Gemini 4 Neon).

Checklist

  • Based on current main (cd73ddab7) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (check_no_game_data.py clean).
  • A new translator rule is in the contract table of docs/design.md (n/a — restores existing builder_bounds guard under WIDE).
  • Independent fresh reviewer agent audit completed (APPROVED).
  • 300 s in-game gate completed and PASSED on exact PR head (60ef5bd2e).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5583744390 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860). Originally posted by [djrolfes](https://github.com/djrolfes) on 2026-12-01T10:24:47Z. --- Claimed in the coordination issue as: `D3D12-SHADER-WIDE-BUILDER-BOUNDS` ([claim comment](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305583476)) ### What and why Preserve the `builder_bounds` sanity clamp (`x < (1 << 20) ? x : 0`) on 1-dword spilled root SRV header size loads (`offset == 4`) when `bitcode::opt::WIDE` (`LEONIDA_SHADER_OPT` bit 64) is enabled. - **Bug on `main`:** In `d3d12/crates/leonida-shader/src/emit/lower.rs` (`Lower::smem`), `if Self::wide_loads()` returns early after calling `self.raw_dwords(h, addr, count)`, completely skipping the `if size && root` sanity clamp 20 lines below. - **Fix:** Guard the early return with `if Self::wide_loads() && !(size && root)` (1-line production change). All multi-dword loads and non-header 1-dword loads still take `raw_dwords`; only the 1-dword builder header size load falls through to apply the `IULT (1 << 20)` clamp. - **Evidence classification:** **READ IN CODE** (`d3d12/crates/leonida-shader/src/emit/lower.rs:8582–8625`) + red→green unit test in `leonida-shader` + **MEASURED** 300 s in-game gate on exact PR head `60ef5bd2e`. ### Tested - `python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/lower.rs` → exit 0. - Native Linux `cargo test --locked -p leonida-shader`: - **RED on `origin/main`** (only the `#[cfg(test)]` hook + new test applied, no production change): ```text running 1 test test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... FAILED # assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4 # left: Inst(48) # right: Inst(50) test result: FAILED. 0 passed; 1 failed; 0 ignored ``` - **GREEN on PR head `60ef5bd2e` (rebased onto `cd73ddab7`)**: ```text running 245 tests ... test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... ok test result: ok. 243 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out ``` - **300 s in-game gate (`docs/game_gate.md`) on exact PR head `60ef5bd2e`** (`leonida.exe` `--profile play --features mimalloc,dlss,diagnostics`, Series S + DLSS Quality 1280×720 → 1920×1080): - **Verdict:** **PASS** - **In-world duration:** `310.97 s` (`world_at_s = 99.55 s`, `>= 300 s` required) - **Frames (`world + 60 s` to stop):** `3,320` presented frames (`3,429` total world presents), `15.31` median FPS, `106.10 ms` p95 frame time (`15.55` end-window median FPS, `4,354.5` median draws/frame) - **Longest present gap in world:** `6.597 s` (`<= 10.0 s` required) - **Peak VRAM:** `6,645 MiB` (`<= 7,400 MiB` guard) - **Stability:** `0` device losses, `0` crashes/faults, `0` panics, `0` kernel `NVRM: Xid` errors GPUs tested: NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Arch Linux (Wine 11.22 + VKD3D-Proton 3.2.0). AI-assisted: yes (authored by Gemini 4 Neon; audited and approved in a separate independent review pass by Gemini 4 Neon). ### Checklist - [x] Based on current `main` (`cd73ddab7`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (`check_no_game_data.py` clean). - [ ] A new translator rule is in the contract table of `docs/design.md` (n/a — restores existing `builder_bounds` guard under `WIDE`). - [x] Independent fresh reviewer agent audit completed (APPROVED). - [x] 300 s in-game gate completed and PASSED on exact PR head (`60ef5bd2e`).
Neutron-0 commented 2026-12-01 12:50:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Neutron-0 on 2026-12-01T12:50:17Z.


Measured on Windows 11, cargo 1.98.1, Rust 1.98.1, with the Windows SDK's dxil.dll (101.7.2308.24) - the control bites: with the test alone on main the test fails, and with the pull request's one-line change it passes.

The control is origin/main c60ce461a with this pull request's test-only hunks (the #[cfg(test)] hook in wide_loads(), the TEST_WIDE_LOADS thread-local and the new test) and without the production change:

cargo test -p leonida-shader --lib --locked -- wide_loads_keeps_builder_root_header_size_clamp

  without the change:  FAILED
      assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4
        left: Inst(48)
       right: Inst(50)
      test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 414 filtered out

  with the change:     ok
      test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 414 filtered out

So the early return in Lower::smem really does skip the builder_bounds clamp for the 1-dword root SRV header size load (offset == 4) when LEONIDA_SHADER_OPT bit 64 (bitcode::opt::WIDE) is on, and the one-line guard Self::wide_loads() && !(size && root) restores it. The test hook is the reason this is checkable at all: wide_loads() is otherwise decided by the build-time option, so the test needs a way to turn it on for its own thread - which is what the TEST_WIDE_LOADS thread-local is for (thread-local, so it cannot leak into another test).

One note for the record:

  • The builder_bounds clamp itself is a sanity clamp (x < (1 << 20) ? x : 0), so the effect of the bug is a header size of 0xFFFFFFFF reaching the root descriptor's bound instead of 0 - the same shape the scalar path guards against.

No game files and no GPU were needed for this check; the validator is the Windows SDK's dxil.dll, and the test builds its program by hand.

Verified on the pull request's head 60ef5bd2e.

One more note, measured on the same machine: on Windows this pull request's head is red in four tests - emit::barycentric_tests::a_masked_pixel_program_reads_the_position_where_the_vertex_program_writes_it, emit::barycentric_tests::first_half_values_render_on_hardware_and_warp, emit::rect::tests::rectangles_are_drawn_whole and emit::rect::tests::the_rectangle_stage_is_a_geometry_shader:

cargo test -p leonida-shader --locked     (head 60ef5bd2e, Windows 11)
  test result: FAILED. 402 passed; 4 failed; 5 ignored; 0 measured; 0 filtered out
called `Result::unwrap()` on an `Err` value: "error: Container part 'Pipeline State Validation' does not match expected for module.
Validation failed."

Those four are the tests that run the DXIL validator, and they are red on main as well - the PSV0 part's name table holds one copy of a semantic's name for all the elements that share it, while the validator computes the table with a copy per element. It is not this pull request's change (the one-line guard here is about the builder_bounds clamp, not the container), and the Linux run in the pull request's description cannot see it: validate() is #[cfg(windows)], so on Linux the four tests never call the validator.

The fix for that is #2864 (one line's worth of name table, plus its own test): with it the four are green on the same machine, and the whole crate reads 410 tests OK, 0 failed, 5 ignored. Worth knowing before a Windows reviewer sees the red, and worth saying that the two are independent.

No game files and no GPU were needed for this check; the validator is the Windows SDK's dxil.dll (101.7.2308.24).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311718986 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#issuecomment-7311718986). Originally posted by [Neutron-0](https://github.com/Neutron-0) on 2026-12-01T12:50:17Z. --- Measured on Windows 11, cargo 1.98.1, Rust 1.98.1, with the Windows SDK's `dxil.dll` (101.7.2308.24) - **the control bites**: with the test alone on `main` the test fails, and with the pull request's one-line change it passes. The control is `origin/main` `c60ce461a` with this pull request's test-only hunks (the `#[cfg(test)]` hook in `wide_loads()`, the `TEST_WIDE_LOADS` thread-local and the new test) and **without** the production change: ``` cargo test -p leonida-shader --lib --locked -- wide_loads_keeps_builder_root_header_size_clamp without the change: FAILED assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4 left: Inst(48) right: Inst(50) test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 414 filtered out with the change: ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 414 filtered out ``` So the early return in `Lower::smem` really does skip the `builder_bounds` clamp for the 1-dword root SRV header size load (`offset == 4`) when `LEONIDA_SHADER_OPT` bit 64 (`bitcode::opt::WIDE`) is on, and the one-line guard `Self::wide_loads() && !(size && root)` restores it. The test hook is the reason this is checkable at all: `wide_loads()` is otherwise decided by the build-time option, so the test needs a way to turn it on for its own thread - which is what the `TEST_WIDE_LOADS` thread-local is for (thread-local, so it cannot leak into another test). One note for the record: - The `builder_bounds` clamp itself is a sanity clamp (`x < (1 << 20) ? x : 0`), so the effect of the bug is a header size of `0xFFFFFFFF` reaching the root descriptor's bound instead of `0` - the same shape the scalar path guards against. No game files and no GPU were needed for this check; the validator is the Windows SDK's `dxil.dll`, and the test builds its program by hand. Verified on the pull request's head `60ef5bd2e`. One more note, measured on the same machine: **on Windows this pull request's head is red in four tests** - `emit::barycentric_tests::a_masked_pixel_program_reads_the_position_where_the_vertex_program_writes_it`, `emit::barycentric_tests::first_half_values_render_on_hardware_and_warp`, `emit::rect::tests::rectangles_are_drawn_whole` and `emit::rect::tests::the_rectangle_stage_is_a_geometry_shader`: ``` cargo test -p leonida-shader --locked (head 60ef5bd2e, Windows 11) test result: FAILED. 402 passed; 4 failed; 5 ignored; 0 measured; 0 filtered out ``` ``` called `Result::unwrap()` on an `Err` value: "error: Container part 'Pipeline State Validation' does not match expected for module. Validation failed." ``` Those four are the tests that run the DXIL validator, and they are red on `main` as well - the `PSV0` part's name table holds one copy of a semantic's name for all the elements that share it, while the validator computes the table with a copy per element. **It is not this pull request's change** (the one-line guard here is about the `builder_bounds` clamp, not the container), and the Linux run in the pull request's description cannot see it: `validate()` is `#[cfg(windows)]`, so on Linux the four tests never call the validator. The fix for that is #2864 (one line's worth of name table, plus its own test): with it the four are green on the same machine, and the whole crate reads **410 tests OK, 0 failed, 5 ignored**. Worth knowing before a Windows reviewer sees the red, and worth saying that the two are independent. No game files and no GPU were needed for this check; the validator is the Windows SDK's `dxil.dll` (101.7.2308.24).
lixonik commented 2026-12-01 14:01:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T14:01:51Z.


RUN (native Windows 10 + RTX 5070 verification; #2857, #2860, and #2862 merged cleanly together onto main ed867d7b2)

  • Unit test: cargo test --locked -p leonida-shader --lib wide_loads_keeps_builder_root_header_size_clamp: 1 passed, 0 failed.
  • In-game gate: Series S, -NoFrameGen, 731.6 s cold precompile, 180 s in world (321.3 s total, 38 world samples >= 1000 draws, median 22.2 fps / 45.1 ms, gpu_wait 0.0 ms, 4,424 median / 5,346 max draws): 0 crashes, 0 panics, 0 device-lost events.

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)

AI-assisted: Antigravity

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7313580111 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#issuecomment-7313580111). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-01T14:01:51Z. --- RUN (native Windows 10 + RTX 5070 verification; #2857, #2860, and #2862 merged cleanly together onto `main` `ed867d7b2`) - **Unit test:** `cargo test --locked -p leonida-shader --lib wide_loads_keeps_builder_root_header_size_clamp`: `1 passed, 0 failed`. - **In-game gate:** `Series S`, `-NoFrameGen`, `731.6 s` cold precompile, `180 s` in world (`321.3 s` total, `38` world samples `>= 1000` draws, median `22.2 fps` / `45.1 ms`, `gpu_wait 0.0 ms`, `4,424` median / `5,346` max draws): `0` crashes, `0` panics, `0` device-lost events. GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver `621.40`, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T) AI-assisted: Antigravity
KelboLive commented 2026-12-01 14:13:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T14:13:21Z.

Review: COMMENTED


Verified: main's WIDE fast path does skip the builder_bounds clamp — git show origin/main:.../lower.rs has the unconditional if Self::wide_loads() { … return } at the same site, so an offset == 4 header-size load never reaches IULT (1 << 20). The fix's !(size && root) is the minimal correct exclusion, and count == 1 is already implied by the size predicate, so no multi-dword or bulk path regresses. leonida-shader is 406 passed / 5 ignored here, and merge-tree against current main (3e7b34a52, not the PR's base) is clean.

The guard is pinned: reverting it to main's if Self::wide_loads() fails the new test with exactly the signature in the body (left: Inst(48), right: Inst(50)).

One nit, and it is environment-dependent: the new test asserts the clamp without pinning builder_bounds(). With LEONIDA_BVH_BOUNDS=0 in the environment it fails on this branch too (44 vs 46), so a developer running the suite with that switch set gets a red test on correct code. Set/force the flag in the test (the #[cfg(test)] thread-local override the diff already adds is the natural place) or gate the assertion on it.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6132998445 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#pullrequestreview-6132998445). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T14:13:21Z. **Review:** COMMENTED --- Verified: main's WIDE fast path does skip the builder_bounds clamp — `git show origin/main:.../lower.rs` has the unconditional `if Self::wide_loads() { … return }` at the same site, so an `offset == 4` header-size load never reaches `IULT (1 << 20)`. The fix's `!(size && root)` is the minimal correct exclusion, and `count == 1` is already implied by the `size` predicate, so no multi-dword or bulk path regresses. `leonida-shader` is 406 passed / 5 ignored here, and merge-tree against current main (`3e7b34a52`, not the PR's base) is clean. The guard is pinned: reverting it to main's `if Self::wide_loads()` fails the new test with exactly the signature in the body (`left: Inst(48), right: Inst(50)`). One nit, and it is environment-dependent: the new test asserts the clamp without pinning `builder_bounds()`. With `LEONIDA_BVH_BOUNDS=0` in the environment it fails on this branch too (44 vs 46), so a developer running the suite with that switch set gets a red test on correct code. Set/force the flag in the test (the `#[cfg(test)]` thread-local override the diff already adds is the natural place) or gate the assertion on it.
imShangLi commented 2026-12-01 15:51:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:51:06Z.


VERIFY-2860 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files

Verdict: PASS. The half-line condition is what carries the fix, and the new test pins both the value and the instruction sequence.

Head 60ef5bd2, base the current main 88a9f133, merge base cd73ddab, 1 commit, 1 file, +109/−1: d3d12/crates/leonida-shader/src/emit/lower.rs.

1. The change

When the shader optimisation's WIDE loads are on, a root SRV load was lowered through raw_dwords, which loads the dwords without the sanity clamp. The scalar path below it does clamp:

// the scalar path, further down
let mask = self.s.m.i8(1);
let (u, al) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4));
…  // cmp(IULT, x, 1 << 20) + select(sane, x, 0)

The pull request keeps the structure header's size load on the scalar path, so the builder_bounds clamp is not lost:

if Self::wide_loads() && !(size && root) {

size is the case the code above it describes — "The size in a structure's header (its second dword), which a copy of it loops over": offset 4, one dword, no extra operand, a null scalar operand, and builder_bounds() enabled. root is that the source turned out to be the root descriptor's memory.

2. Red and green, and the discrimination

cargo test -p leonida-shader on the pull request's branch: 243 passed, 0 failed, 2 ignored.

With only && !(size && root) removed (the pull request's test kept):

test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... FAILED
  panicked at crates/leonida-shader/src/emit/lower.rs:17008:9:
  assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4
    left: Inst(48)
   right: Inst(50)

The two instruction ids differ by exactly the two instructions the clamp is made of.

3. What the new test pins

wide_loads_keeps_builder_root_header_size_clamp builds a spilled compute shader by hand —

s_and_b32 s3, s1, 0xff
s_mov_b32 s2, s0
s_load_dwordx2 s[4:5], s[2:3], 0
s_mov_b32 s6, 0xFFFF_FFFF
s_mov_b32 s7, 0x0002_0000
s_buffer_load_dword s8, s[4:7], <offset>
s_endpgm

— lowers it four ways (offset 4 and offset 8, wide and scalar), and asserts three things:

  • the value at offset 4 with WIDE on equals the value at offset 4 with WIDE off (the clamp is there);
  • the whole bitcode at offset 4 with WIDE on equals the bitcode of the clamped scalar path — not just the value;
  • the instruction id at offset 4 is exactly two above the one at offset 8, "offset 4 must emit the 2-instruction sanity clamp" — cmp(IULT, x, 1 << 20) and select(sane, x, 0).

The last one is what makes the test worth having: it distinguishes "the clamp is there" from "the wide path happens to produce the same number here".

To switch WIDE on and off inside a test, the pull request adds a test-only override:

#[cfg(test)]
thread_local! {
    static TEST_WIDE_LOADS: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}

read at the top of wide_loads(). It is thread-local and #[cfg(test)], so it cannot reach a shipped build.

4. What I did not check

  • A real shader translation. Everything above is the crate's unit tests over hand-built IR. I did not translate a game shader (they are not in the repository), so I did not see the clamp in a real shader's DXIL, and I did not check the corpus for how many shaders hit this case.
  • Whether the size condition is complete. The clamp is wanted for the header size load; the condition requires offset 4, count == 1, no extra operand, a null scalar operand and builder_bounds() on. I did not look for another shape of load that wants the same clamp and would still take the wide path.
  • The other crates in the workspace — leonida-gpu, leonida-hle and leonida-loader do not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ran leonida-shader on its own.
  • cargo clippy (already failing on main, run as a report by the workflow) and cargo fmt (left out of the d3d12 workflow).

AI-assisted: MiniMax M3.1 Flash.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7316615251 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#issuecomment-7316615251). Originally posted by [imShangLi](https://github.com/imShangLi) on 2026-12-01T15:51:06Z. --- ## VERIFY-2860 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files **Verdict: PASS.** The half-line condition is what carries the fix, and the new test pins both the value and the instruction sequence. Head `60ef5bd2`, base the current `main` `88a9f133`, merge base `cd73ddab`, 1 commit, 1 file, +109/−1: `d3d12/crates/leonida-shader/src/emit/lower.rs`. ### 1. The change When the shader optimisation's `WIDE` loads are on, a root SRV load was lowered through `raw_dwords`, which loads the dwords without the sanity clamp. The scalar path below it does clamp: ```rust // the scalar path, further down let mask = self.s.m.i8(1); let (u, al) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4)); … // cmp(IULT, x, 1 << 20) + select(sane, x, 0) ``` The pull request keeps the **structure header's size** load on the scalar path, so the `builder_bounds` clamp is not lost: ```rust if Self::wide_loads() && !(size && root) { ``` `size` is the case the code above it describes — "The size in a structure's header (its second dword), which a copy of it loops over": offset 4, one dword, no extra operand, a null scalar operand, and `builder_bounds()` enabled. `root` is that the source turned out to be the root descriptor's memory. ### 2. Red and green, and the discrimination `cargo test -p leonida-shader` on the pull request's branch: **243 passed, 0 failed, 2 ignored**. With **only `&& !(size && root)`** removed (the pull request's test kept): ``` test emit::lower::tests::wide_loads_keeps_builder_root_header_size_clamp ... FAILED panicked at crates/leonida-shader/src/emit/lower.rs:17008:9: assertion `left == right` failed: wide_loads must emit the same builder_bounds size clamp for offset 4 left: Inst(48) right: Inst(50) ``` The two instruction ids differ by exactly the two instructions the clamp is made of. ### 3. What the new test pins `wide_loads_keeps_builder_root_header_size_clamp` builds a spilled compute shader by hand — ``` s_and_b32 s3, s1, 0xff s_mov_b32 s2, s0 s_load_dwordx2 s[4:5], s[2:3], 0 s_mov_b32 s6, 0xFFFF_FFFF s_mov_b32 s7, 0x0002_0000 s_buffer_load_dword s8, s[4:7], <offset> s_endpgm ``` — lowers it four ways (offset 4 and offset 8, wide and scalar), and asserts three things: - the value at offset 4 with WIDE on equals the value at offset 4 with WIDE off (the clamp is there); - the **whole bitcode** at offset 4 with WIDE on equals the bitcode of the clamped scalar path — not just the value; - the instruction id at offset 4 is exactly **two** above the one at offset 8, "offset 4 must emit the 2-instruction sanity clamp" — `cmp(IULT, x, 1 << 20)` and `select(sane, x, 0)`. The last one is what makes the test worth having: it distinguishes "the clamp is there" from "the wide path happens to produce the same number here". To switch WIDE on and off inside a test, the pull request adds a test-only override: ```rust #[cfg(test)] thread_local! { static TEST_WIDE_LOADS: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) }; } ``` read at the top of `wide_loads()`. It is thread-local and `#[cfg(test)]`, so it cannot reach a shipped build. ### 4. What I did not check - **A real shader translation.** Everything above is the crate's unit tests over hand-built IR. I did not translate a game shader (they are not in the repository), so I did not see the clamp in a real shader's DXIL, and I did not check the corpus for how many shaders hit this case. - **Whether the `size` condition is complete.** The clamp is wanted for the header size load; the condition requires offset 4, `count == 1`, no extra operand, a null scalar operand and `builder_bounds()` on. I did not look for another shape of load that wants the same clamp and would still take the wide path. - **The other crates in the workspace** — `leonida-gpu`, `leonida-hle` and `leonida-loader` do not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ran `leonida-shader` on its own. - `cargo clippy` (already failing on `main`, run as a report by the workflow) and `cargo fmt` (left out of the d3d12 workflow). AI-assisted: MiniMax M3.1 Flash.
OreoFR commented 2026-12-01 20:22:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T20:22:26Z.


Merging. Head 60ef5bd2e merges cleanly with main 19d5f83cd. Claim: D3D12-SHADER-WIDE-BUILDER-BOUNDS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305583476). Red/green: Neutron-0 (Windows 11, on this head: the control bites), imShangLi (Linux, PASS), KelboLive review on this head. I ran: the five PRs #2860, #2861, #2862, #2824 and #2873 merged together onto main ab374c7d5 (main has since gained only #2935, a CI workflow change), with the shader (418 passed, 0 failed), gpu (644/0) and hle (247/0) cargo tests. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, a cold start to the world then a 330 s warm walking run, 0 device lost, no panic. CI: green on head and main. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7324571608 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#issuecomment-7324571608). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T20:22:26Z. --- Merging. Head 60ef5bd2e merges cleanly with main 19d5f83cd. Claim: D3D12-SHADER-WIDE-BUILDER-BOUNDS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305583476). Red/green: Neutron-0 (Windows 11, on this head: the control bites), imShangLi (Linux, PASS), KelboLive review on this head. I ran: the five PRs #2860, #2861, #2862, #2824 and #2873 merged together onto main ab374c7d5 (main has since gained only #2935, a CI workflow change), with the shader (418 passed, 0 failed), gpu (644/0) and hle (247/0) cargo tests. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, a cold start to the world then a 330 s warm walking run, 0 device lost, no panic. CI: green on head and main. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.
enlight3d commented 2026-12-01 23:07:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T23:07:13Z.


RUN, posted after the merge: the review points below are for a follow-up. (independent run on Windows, #2860 head 60ef5bd2e, merged locally into main 0ff8a532f with git merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

Commands, in d3d12/, target x86_64-pc-windows-gnu (the host this machine's rustup selects; CI builds x86_64-pc-windows-msvc), toolchain 1.98.1, all with --locked, at low priority, RUST_TEST_THREADS=8:

  • cargo test -p leonida-shader --no-fail-fast: main 414 passed / 0 failed / 5 ignored; merge 415 / 0 / 5 (the new test). Some of these tests run on the GPU, here an RTX 3090; host_forms_give_what_the_spelled_out_ones_give passes on both sides.
  • cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 414 / 0 / 5; merge 415 / 0 / 5.
  • Red, by assertion: the merge with the one-line change taken back (if Self::wide_loads() {, as on main), the #[cfg(test)] hook and the new test kept. cargo test -p leonida-shader --lib wide_loads_keeps_builder_root_header_size_clamp fails on the first assertion, "wide_loads must emit the same builder_bounds size clamp for offset 4", left Inst(48), right Inst(50): the values in the body.
  • Second mutation: the change kept, the clamp itself taken out of the scalar path (if size && root && false). The test fails on its last assertion, "offset 4 must emit the 2-instruction sanity clamp", left 48, right 50. So the test pins the clamp, not only that the wide and scalar paths agree.
  • Green: the file restored (git status clean), the test passes.
  • cargo clippy -p leonida-shader --all-targets, default and --features diagnostics, on main and on the merge: 6 warnings on each side, the same set (all in valu_tests.rs), none in the changed lines.
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md is clean on the merge (no switch added).
  • Read: size needs count == 1, offset 4, no extra offset and builder_bounds(), so the only load that now leaves the wide path is the one-dword header size read through a root view. For that load the wide path made the same single rawBufferLoad (mask 1) as the scalar path, so the merge emits the scalar path's code plus the clamp, which the test's whole-bitcode comparison checks. Every other load is unchanged.
  • Not covered here: the game. WIDE (bit 64 of LEONIDA_SHADER_OPT) is off unless asked (unset is 447), so a run with default switches never reaches the changed line; the body's 300 s gate does not say that LEONIDA_SHADER_OPT was set, so it probably did not exercise the change either. The test also depends on builder_bounds() being on, which LEONIDA_BVH_BOUNDS=0 in the environment would turn off (read, not run).

Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7328844955 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2860#issuecomment-7328844955). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-01T23:07:13Z. --- RUN, posted after the merge: the review points below are for a follow-up. (independent run on Windows, #2860 head 60ef5bd2e, merged locally into `main` 0ff8a532f with `git merge --no-ff`, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.) Commands, in `d3d12/`, target x86_64-pc-windows-gnu (the host this machine's rustup selects; CI builds x86_64-pc-windows-msvc), toolchain 1.98.1, all with `--locked`, at low priority, `RUST_TEST_THREADS=8`: - `cargo test -p leonida-shader --no-fail-fast`: main 414 passed / 0 failed / 5 ignored; merge 415 / 0 / 5 (the new test). Some of these tests run on the GPU, here an RTX 3090; `host_forms_give_what_the_spelled_out_ones_give` passes on both sides. - `cargo test -p leonida-shader --no-fail-fast --features diagnostics`: main 414 / 0 / 5; merge 415 / 0 / 5. - Red, by assertion: the merge with the one-line change taken back (`if Self::wide_loads() {`, as on main), the `#[cfg(test)]` hook and the new test kept. `cargo test -p leonida-shader --lib wide_loads_keeps_builder_root_header_size_clamp` fails on the first assertion, "wide_loads must emit the same builder_bounds size clamp for offset 4", left `Inst(48)`, right `Inst(50)`: the values in the body. - Second mutation: the change kept, the clamp itself taken out of the scalar path (`if size && root && false`). The test fails on its last assertion, "offset 4 must emit the 2-instruction sanity clamp", left 48, right 50. So the test pins the clamp, not only that the wide and scalar paths agree. - Green: the file restored (`git status` clean), the test passes. - `cargo clippy -p leonida-shader --all-targets`, default and `--features diagnostics`, on main and on the merge: 6 warnings on each side, the same set (all in `valu_tests.rs`), none in the changed lines. - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` is clean on the merge (no switch added). - Read: `size` needs `count == 1`, offset 4, no extra offset and `builder_bounds()`, so the only load that now leaves the wide path is the one-dword header size read through a root view. For that load the wide path made the same single `rawBufferLoad` (mask 1) as the scalar path, so the merge emits the scalar path's code plus the clamp, which the test's whole-bitcode comparison checks. Every other load is unchanged. - Not covered here: the game. `WIDE` (bit 64 of `LEONIDA_SHADER_OPT`) is off unless asked (unset is 447), so a run with default switches never reaches the changed line; the body's 300 s gate does not say that `LEONIDA_SHADER_OPT` was set, so it probably did not exercise the change either. The test also depends on `builder_bounds()` being on, which `LEONIDA_BVH_BOUNDS=0` in the environment would turn off (read, not run). Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.
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
ProjectLeonida/mandy!2860
No description provided.