[pr-fix][shaders] shader: clamp cbufferLoadLegacy row index to 4096 to prevent Ada LDC device removal #223
Loading…
Reference in a new issue
No description provided.
Delete branch "hingeowl/Darner:pr/clamp-cbuffer-load-row"
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?
What it changes
Clamps dynamic
dx.op.cbufferLoadLegacy.i32row indices incbuffer_dword(and guards constant offsets>= 64 KiBinsmem) to< 4096, selecting row 0 for the load and masking out-of-range results to 0, preventing NVIDIA Ada (LDC) invalid-constant-address warp exceptions past D3D12's 64 KiB CBV limit. Includes a unit test covering OOB dynamic CBV offsets.Notes
main(f3840d7).CS_RaytracingResolve0x887A0005crash on RTX 4090 was separately root-caused to NVIDIA's 512 MiB shader-code heap limit (see PR #187), but this clamp remains a clean defensive correctness guard against OOB dynamic CBV row indices on Ada. Standalone A/B on currentmainhas not been run yet.Independent Windows build, red/green test, and code-read review of
0f5138d48(based onmainf3840d73d). Not run in-game.Environment
x86_64-pc-windows-msvc), MSVC 19.44.35230.Measured results
cargo clippy --workspace --all-targets --locked -- -D warnings): PASScargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASScargo xtask policy: PASS (asset policy: 555 files ok)cargo test --locked -p darner-shader --lib emit::valu_tests::cbv -- --nocapture: PASS (3 passed; 0 failedin 1.85 s)maincheck (f3840d73d+ PR'svalu_tests.rsonly):cargo test --locked -p darner-shader --lib emit::valu_tests::cbv_dynamic_oob_load_returns_zero_without_faulting -- --exact --nocapture: PASSED on unmodifiedmain(1 passed; 0 failedin 1.02 s).Findings
smempath (crates/darner-shader/src/emit/lower.rs:8160-8162):let byte = base.wrapping_add(4 * k); if (byte >> 4) >= 4096wraps back into valid rows whenbaseis nearu32::MAX(for example,Fields::Smem.offsetis signed 21-bit atgfx10/fields.rs:267andbaseis(*offset as u32).wrapping_add(built_base)atlower.rs:7990). For a multi-dword load withbase = 0xFFFF_FFFC(-4) andcount = 4,k = 0returns0whilek = 1..3wrap tobyte = 0, 4, 8and readcbv[0..3]instead of0.base.checked_add(4 * k).filter(|&b| (b >> 4) < 4096)(returning0onNone) avoids the wrap.cbuffer_dword(note):smemdynamic (lower.rs:8181-8183), picked-heap CBV (lower.rs:8080-8082), vectorbuffer_load(lower.rs:13862), and 16-bitload_subword(lower.rs:8351,byte + 1) add4 * k/1ini32before callingcbuffer_dword.cbuffer_dword's clamp still keeps the row index< 4096(so AdaLDCwill not fault), though dynamic OOB addresses nearu32::MAXcan wrap into row 0 instead of returning0.valu_tests.rs:2735-2776):cbv_dynamic_oob_load_returns_zero_without_faultingpasses on unmodifiedmainon both Ampere (RTX A2000) and WARP because they already return0for OOBcbufferLoadLegacy.cbuffer_dwordemits theIULT 4096/selectclamp (and a statics_buffer_load_dwordtest at>= 0x1_0000verifying zerocbufferLoadLegacycalls / zero result, plus the0xFFFF_FFFCwrap case) would make the regression test fail onmainacross all hardware.opt.rsinteraction:simplify_guarded_uses(opt.rs:2405-2423, 2982-2989) and LICM (opt.rs:2465-2468) do not hoist or break the dynamic clamp; the only othercbufferLoadLegacy.i32emitter (user_at,lower.rs:3385) is bounded by user-data dwords.Verdict: Changes requested (
checked_addon the static path + IR-level test assertions so the test goes red onmainwithout Ada hardware).AI-assisted: yes (Gemini 4 Neon).
Fixed in
eb0ce54:smemCBV path incrates/darner-shader/src/emit/lower.rstobase.checked_add(4 * k).filter(|&b| (b >> 4) < 4096)so signed offsets nearu32::MAX(e.g.-4=0xFFFF_FFFCwithcount = 4) return0for every dword instead of wrapping into row 0.IULTbounds check,selects >= 5,cbv_loads == 1) tocbv_dynamic_oob_load_returns_zero_without_faultingso it fails on unmodifiedmainregardless of GPU vendor/WARP, and addedcbv_static_oob_and_wraparound_loads_emit_no_cbuffer_loadcovering both0x1_0000and-4(0xFFFF_FFFC, 4 dwords) withcbv_loads == 0and GPU/WARP readback[0, 0].Validation on
eb0ce54:cargo test -p darner-shader --lib --locked -- emit::valu_tests::cbv --nocapture— 4 passed, 0 failed.cargo clippy -p darner-shader --all-targets --all-features --locked -- -D warnings— passed.Ready for re-review.
Code review only (read the diff at
eb0ce543), nothing built or run. lewis499's static-path and test points are fixed ineb0ce54. Their point 2 (dynamic paths wrap pastu32::MAXbefore the clamp) is still open, though it can't fault.cbuffer_dwordand static row code (the two conflict) and does this clamp behindDARNER_CB_ROWS.0x887A0005. #292's runs report0x887A0006for the LDC kind, and the description here traces the 0x887A0005 resolve crash to the shader heap (#187). Worth wording it as "device removed" without a code.Suggest merging one of the two, #292 for its wider coverage.
Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.