shader/lower: preserve builder_bounds root header size guard when WIDE loads are enabled (D3D12-SHADER-WIDE-BUILDER-BOUNDS) #2860
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2860"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_boundssanity clamp (x < (1 << 20) ? x : 0) on 1-dword spilled root SRV header size loads (offset == 4) whenbitcode::opt::WIDE(LEONIDA_SHADER_OPTbit 64) is enabled.main: Ind3d12/crates/leonida-shader/src/emit/lower.rs(Lower::smem),if Self::wide_loads()returns early after callingself.raw_dwords(h, addr, count), completely skipping theif size && rootsanity clamp 20 lines below.if Self::wide_loads() && !(size && root)(1-line production change). All multi-dword loads and non-header 1-dword loads still takeraw_dwords; only the 1-dword builder header size load falls through to apply theIULT (1 << 20)clamp.d3d12/crates/leonida-shader/src/emit/lower.rs:8582–8625) + red→green unit test inleonida-shader+ MEASURED 300 s in-game gate on exact PR head60ef5bd2e.Tested
python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/lower.rs→ exit 0.cargo test --locked -p leonida-shader:origin/main(only the#[cfg(test)]hook + new test applied, no production change):60ef5bd2e(rebased ontocd73ddab7):docs/game_gate.md) on exact PR head60ef5bd2e(leonida.exe--profile play --features mimalloc,dlss,diagnostics, Series S + DLSS Quality 1280×720 → 1920×1080):310.97 s(world_at_s = 99.55 s,>= 300 srequired)world + 60 sto stop):3,320presented frames (3,429total world presents),15.31median FPS,106.10 msp95 frame time (15.55end-window median FPS,4,354.5median draws/frame)6.597 s(<= 10.0 srequired)6,645 MiB(<= 7,400 MiBguard)0device losses,0crashes/faults,0panics,0kernelNVRM: XiderrorsGPUs 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
main(cd73ddab7) and merges cleanly.check_no_game_data.pyclean).docs/design.md(n/a — restores existingbuilder_boundsguard underWIDE).60ef5bd2e).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 onmainthe test fails, and with the pull request's one-line change it passes.The control is
origin/mainc60ce461awith this pull request's test-only hunks (the#[cfg(test)]hook inwide_loads(), theTEST_WIDE_LOADSthread-local and the new test) and without the production change:So the early return in
Lower::smemreally does skip thebuilder_boundsclamp for the 1-dword root SRV header size load (offset == 4) whenLEONIDA_SHADER_OPTbit 64 (bitcode::opt::WIDE) is on, and the one-line guardSelf::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 theTEST_WIDE_LOADSthread-local is for (thread-local, so it cannot leak into another test).One note for the record:
builder_boundsclamp itself is a sanity clamp (x < (1 << 20) ? x : 0), so the effect of the bug is a header size of0xFFFFFFFFreaching the root descriptor's bound instead of0- 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_wholeandemit::rect::tests::the_rectangle_stage_is_a_geometry_shader:Those four are the tests that run the DXIL validator, and they are red on
mainas well - thePSV0part'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 thebuilder_boundsclamp, 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).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
mained867d7b2)cargo test --locked -p leonida-shader --lib wide_loads_keeps_builder_root_header_size_clamp:1 passed, 0 failed.Series S,-NoFrameGen,731.6 scold precompile,180 sin world (321.3 stotal,38world samples>= 1000draws, median22.2 fps/45.1 ms,gpu_wait 0.0 ms,4,424median /5,346max draws):0crashes,0panics,0device-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
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.rshas the unconditionalif Self::wide_loads() { … return }at the same site, so anoffset == 4header-size load never reachesIULT (1 << 20). The fix's!(size && root)is the minimal correct exclusion, andcount == 1is already implied by thesizepredicate, so no multi-dword or bulk path regresses.leonida-shaderis 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(). WithLEONIDA_BVH_BOUNDS=0in 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.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 currentmain88a9f133, merge basecd73ddab, 1 commit, 1 file, +109/−1:d3d12/crates/leonida-shader/src/emit/lower.rs.1. The change
When the shader optimisation's
WIDEloads are on, a root SRV load was lowered throughraw_dwords, which loads the dwords without the sanity clamp. The scalar path below it does clamp:The pull request keeps the structure header's size load on the scalar path, so the
builder_boundsclamp is not lost:sizeis 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, andbuilder_bounds()enabled.rootis that the source turned out to be the root descriptor's memory.2. Red and green, and the discrimination
cargo test -p leonida-shaderon the pull request's branch: 243 passed, 0 failed, 2 ignored.With only
&& !(size && root)removed (the pull request's test kept):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_clampbuilds a spilled compute shader by hand —— lowers it four ways (offset 4 and offset 8, wide and scalar), and asserts three things:
cmp(IULT, x, 1 << 20)andselect(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:
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
sizecondition 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 andbuilder_bounds()on. I did not look for another shape of load that wants the same clamp and would still take the wide path.leonida-gpu,leonida-hleandleonida-loaderdo not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ranleonida-shaderon its own.cargo clippy(already failing onmain, run as a report by the workflow) andcargo fmt(left out of the d3d12 workflow).AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T20:22:26Z.
Merging. Head
60ef5bd2emerges cleanly with main19d5f83cd. 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 mainab374c7d5(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.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 intomain0ff8a532fwithgit 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_givepasses on both sides.cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 414 / 0 / 5; merge 415 / 0 / 5.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_clampfails on the first assertion, "wide_loads must emit the same builder_bounds size clamp for offset 4", leftInst(48), rightInst(50): the values in the body.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.git statusclean), 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 invalu_tests.rs), none in the changed lines.python -I tools/d3d12_switches.py --check d3d12/docs/switches.mdis clean on the merge (no switch added).sizeneedscount == 1, offset 4, no extra offset andbuilder_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 singlerawBufferLoad(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.WIDE(bit 64 ofLEONIDA_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 thatLEONIDA_SHADER_OPTwas set, so it probably did not exercise the change either. The test also depends onbuilder_bounds()being on, whichLEONIDA_BVH_BOUNDS=0in 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.