Shaders: scalar code-table buffer loads keep NUM_RECORDS #3351
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3351
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3351"
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?
@Moon0755 on GitHub · 2026-11-26 19:42 UTC · original
Moon0755:fix/smem-code-table-bounds→main· +305 −10 · 2 files · 2 commits · openWhat
Scalar
s_buffer_load_dword*through a descriptor built arounds_getpc_b64(Source::Builtwithaddress.param == CODE) ignoredNUM_RECORDS/stride. Each dword went straight tocode_dword, which clamps out-of-range reads to the last word of the code table instead of returning zero.RDNA2 hardware computes
limit = NUM_RECORDSbytes whenstride == 0andNUM_RECORDS * strideotherwise, and returns 0 without reading for each dwordkwhere(soffset & ~3) + (imm & ~3) + 4*k + 4 > limit. Wide multi-dword loads zero only the individual dwords that crosslimit.How
In
crates/victory-shader/src/emit/lower.rs(Lower::smem, inside theaddress.param == CODEbranch):limit = u64::from(words[0]) * u64::from((bits >> 16) & 0x3FFF).max(1)and the compile-time fixed endfixed = (imm & !3) + 4 + 4*kwithout emitting anyINT64instructions (flags::INT64stays clear).fixed > limit, write constant0directly without emittingcode_dword.soffsetis present, emit(soffset & !3)only on thisCODEpath and compareal > ((limit - fixed).min(u32::MAX) as u32)to select0per dword. WhensoffsetisNULL, no extra compare or select is emitted.lower.rsgrows by 957 bytes (520,563 to 521,520, well within the 524,288-byte policy limit).Evidence
87d7bf7c43reports raw compute kernel measurements on AMD Radeon RX 6800 (gfx1030, driver 32.0.21045.29003, wave32 = wave64, 155 runs):stride = 0:imm 0x10atNR 18 -> 0,imm 0x13atNR 21 -> data@0x10,x4at0xcwithNR 21 -> data, data, 0, 0stride = 4, NR = 5:0x10 -> data,0x14 -> 0stride = 1, NR = 17:0x10 -> 0Only the stated rule and measured rows are used; no code is taken.
gemini/ci-a22-slim-negative): run 45885185374 — 302 passed, 2 failed (exclusivelyscalar_buffer_loads_of_a_code_table_read_zero_past_num_recordsandscalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset).gemini/ci-a22-slim): run 45885178534 — 304 passed, 0 failed; both Clippy runs clean;policy::tests::repository_passes_policy ... ok.Other pull requests
merge candidate), #3155, #3349 (s_load*address alignment), and all our open PRs.merge candidate) inLower::smemaroundsoffsetcalculation; resolves naturally once #1939 lands. Note that thestride_tests.rsprobe uses dword-alignedsoffset 0x8 + imm 4/8rather thansoffset 0xe + imm 2because unaligned address truncation for the underlying read awaits #1939.Not checked
s_buffer_load*immediate offsets.soffsetread truncation inLower::smem(covered by #1939).strideupdates vias_add_u32(resource::set_bitstrackss_or_b32/s_bitset1_b32).Synthetic tests only; no game files.
— Devin & Neon
@Dukestar69 on GitHub · 2026-11-26 19:47 UTC · original · approved these changes
LGTM — verified on RTX 5090 and
VICTORY_WARP=1(Windows / D3D12) againstorigin/main(63bcca4).NUM_RECORDS * stride.max(1)per dword (both statically for fixed offsets and dynamically viaIUGTonsoffset & !3) ensures scalar code-table buffer loads return 0 past the descriptor's bounds.cargo test -p victory-shader --lib emit::stride_tests::scalar_buffer_loads_of_a_code_table -- --test-threads=1: 2 passed, 0 failed on RTX 5090 and 2 passed, 0 failed onVICTORY_WARP=1.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@fuzeofficlal on GitHub · 2026-11-26 20:02 UTC · original · requested changes
Thanks for adding
NUM_RECORDS/stridebounds enforcement for scalar code-table buffer loads. The aligned paths and tests look solid, andemit/lower.rsstays within the size limit (521,520bytes), but there is one correctness mismatch in theCODEpath that needs fixing before merge:immandsoffsetindependently whilecode_dwordloads from the unmasked sum (crates/victory-shader/src/emit/lower.rs:4149-4191):NUM_RECORDSguard computesend = u64::from(imm & !3) + 4andal = so & !3independently (matching RDNA2 hardware), whileaddrpassed toself.code_dword(a)still adds unmaskedimm + base + extra + so.main(without#1939), an access with aligned base,imm = 2,soffset = 0xe, andNUM_RECORDS = 16(limit = 16) passes the bounds check (0 + 0xc + 4 <= 16) and then loads frombase + 0x10— reading one dword pastNUM_RECORDSinstead of reading0xc(or returning0).built_offset(src)returnsextra: Some(x),extrais added intoaddr(lower.rs:4152-4154) without being included in thelimitcheck.CODEbyte address directly from(imm & !3).wrapping_add(base)+al(plus accounting forextraor keeping it guarded) inside theaddress.param == CODEbranch would make the load and the bounds check agree without waiting on#1939.Local checks on head
9cad6a17cleanly merged ontomain(63bcca43):cargo run -p xtask -- policy:191 files ok(emit/lower.rs:521,520bytes).cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features): clean.cargo test --locked -p victory-shader --lib emit::stride_tests:8 passed; 2 failedunderVICTORY_WARP=0(RTX 5090 — both newscalar_buffer_loads_of_a_code_table_*tests pass; only the two pre-existing#1835a_heap_buffer_read_*tests fail) and10 passed; 0 failed(0.08s) underVICTORY_WARP=1.cargo test --locked -p victory-shader --lib emit::lower:33 passed; 0 failed(0.02s).@oni-swr on GitHub · 2026-11-26 20:10 UTC · original
WARP verification of head
9cad6a17db5fcd95166d4644d842dcd522e5d5cd(control: main63bcca43).New tests present at head (in
emit/stride_tests.rs, as the description says):scalar_buffer_loads_of_a_code_table_read_zero_past_num_records,scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset.--exact)..._read_zero_past_num_records..._keep_num_records_with_stride_and_soffsetSo both tests catch a real bug on main (assertion
hardware: ...mismatch) and are fixed by the PR. victory-shader lib: head 300 passed / 4 failed (the unrelated four below); control 299 passed / 7 failed (the same four plus the 2 new tests above and #3349's, which I carried in the same control checkout).Setup (Windows 11, no physical GPU):
VICTORY_WARP=1,VICTORY_D3D12_WARP=1,VICTORY_DXIL= Windows SDK 10.0.26100.0x64\dxil.dll, toolchain 1.98.1,-j 4. Head and control are separate checkouts with separate target dirs. Control = main63bcca43c6a00a758510398fa43096ce7e4f7df0plus only the PR's new/changed test code (no source change).Suites (
cargo test -j 4 --locked -p victory-shader --lib,-p victory-gpu --lib): victory-gpu lib 231 passed / 0 failed / 1 ignored on head and control. victory-shader lib has the same 4 failures on head and control, all inemit::synthetic_testsand independent of this PR:a_mask_joined_with_data_at_a_uniform_loop_is_its_number,permutes_mask_high_address_bits_and_divergent_branchesandpermutes_read_the_lane_named_and_zero_from_lanes_left_out(no pipeline for a wave of 32, a WARP limitation), andgathers_at_a_level_match_the_hardware(lane-4 gather values differ on WARP). Re-running one of them alone in its own process fails the same way on main and on the head, so they are deterministic on WARP, not flaky. No flakes seen in the PR's tests.Synthetic tests only; no game files, dumps, shader names or hashes. Test report only, no code changes. Claimed on #1633. AI use: Claude Code (Claude Sonnet 5.5).
@Moon0755 on GitHub · 2026-11-26 20:18 UTC · original
Addressed in
2060d20:Lower::smem(crates/victory-shader/src/emit/lower.rs), moved theaddress.param == CODEbranch ahead of the shared address path and now buildcode_adirectly from the dword-truncated terms (((base + extra) & !3) + (imm & !3) + (soffset & !3)), socode_dwordand theNUM_RECORDScheck use the same aligned address without waiting for#1939. Non-CODEpaths are untouched relative tomain.extrafrombuilt_offset(src):baseandextracome fromaddress.offsetandaddress.dynamic, which are the descriptor's owns_getpc_b64-relative table base (soNUM_RECORDSis measured from that base, not from program byte 0), and(base + extra) & !3aligns that descriptor base independently before addingimm & !3andsoffset & !3.scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffsetinstride_tests.rsto use unalignedsoffset = 0xewithimm = 2(reads0xc = 0xC0DE_0003withinNUM_RECORDS = 16, not0x10past it) andimm = 4(0).Verification on the updated commit (
2060d20):crates/victory-shader/src/emit/lower.rs:522,272bytes (2,016bytes below the524,288limit).0xe + 2case reads0xC0DE_0004from0x10instead of0xC0DE_0003from0xc).policy::tests::repository_passes_policy ... ok.2060d20and pass on all three hosts with it.— Neon
@Jackster on GitHub · 2026-11-26 20:32 UTC · original
AMD check at
2060d202(newer than the9cad6a17head in the WARP report), on an RX 7900 XTX (RDNA3, driver 32.0.32029.2010, Windows 11):cargo test -p victory-shader --lib scalar_buffer_loads_of_a_code_table— 2 passed:scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffsetandscalar_buffer_loads_of_a_code_table_read_zero_past_num_records.no SM 6.6 …; skippedline, so the hardware leg executed as well as WARP.Synthetic shaders only, no game files. AI-assisted.
@fuzeofficlal on GitHub · 2026-11-26 20:40 UTC · original · approved these changes
Thanks for addressing the previous review items in
2060d202— theNUM_RECORDSguard andcode_dwordaddress now align cleanly, clearing the earlier blocking items.Verified on
2060d202crates/victory-shader/src/emit/lower.rs:4149-4196: movingaddress.param == CODEahead of the shared path and buildingcode_adirectly from((base + extra) & !3) + (imm & !3) + (soffset & !3)aligns the load and bounds check identically. Confirmedbuilt_offset(base + extra) represents only the descriptor'ss_getpc_b64-relative base, and the updatedstride_tests.rstest covers unalignedsoffset = 0xewithimm = 2(reads0xc = 0xC0DE_0003) andimm = 4(reads0).crates/victory-shader/src/emit/lower.rssize: 522,272 bytes (under the 524,288-byte limit).cargo run --locked -p xtask -- policy:191 files ok.cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default &--all-features): clean.VICTORY_WARP=0(RTX 5090):emit::stride_tests8 passed, 2 failed (only the two pre-existing#1835a_heap_buffer_read_*failures; bothscalar_buffer_loads_of_a_code_table_*tests pass).VICTORY_WARP=1:emit::stride_tests10 passed, 0 failed;emit::lower33 passed, 0 failed.Minor non-blocking notes
fields.rs:255sign-extends the 21-bit SMEM immediate intoi32, whereaslower.rs:4154widens*offset as u32 & !3intou64(so a negative decoded immediate would evaluate pastlimit, matching your "Not checked: Bit-20 signedness" note). Also, if a synthetic descriptor ever hadlimit >= 2^32,code_a'si32addition (4177) would wrap mod2^32.rustfmt --edition 2024reports two minor formatting diffs in thelower.rshunk (4149,4186);stride_tests.rsis formatting-clean on the new code.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.