Skip to content

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

Open
djrolfes wants to merge 1 commit into
ProjectLeonida:mainfrom
djrolfes:djrolfes/shader-wide-builder-bounds
Open

djrolfes wants to merge 1 commit into
ProjectLeonida:mainfrom
djrolfes:djrolfes/shader-wide-builder-bounds

Conversation

@djrolfes

@djrolfes djrolfes commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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).

@Neutron-0

Neutron-0 commented Dec 1, 2026 •

Copy link
Copy Markdown

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

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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 KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

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 Linux: build leonida.exe and run it under Proton or Wine #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.

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.

5 participants