Skip to content

Shaders: reuse SSA sums when tracing memory addresses - #139

Open
TG0000 wants to merge 3 commits into
Marvisak:mainfrom
TG0000:perf/shader-address-sums
Open

TG0000 wants to merge 3 commits into
Marvisak:mainfrom
TG0000:perf/shader-address-sums

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Change

Shader resource tracking now caches decomposed address sums by SSA value after copy propagation. Memory accesses and built descriptors can reuse shared additions. Across an unrolled sequence of address increments and loads, this reduces address decomposition from quadratic to linear work.

Synthetic regressions check every address in a shared chain, 32-bit wraparound, preservation of a computed offset, the fallback for sums with two computed terms, and the error for an untraceable base.

Synthetic release benchmark

Windows MSVC on a Ryzen 9 5900X. The benchmark measures the full resource::track call on prebuilt SSA, using the same harness before and after. Times are medians of seven batches.

Address increments × loads per increment Before After Speedup
16 × 4 30.41 µs 11.36 µs 2.7×
128 × 4 1,513.25 µs 209.12 µs 7.2×
384 × 4 13,215.01 µs 606.71 µs 21.8×
cargo +stable-x86_64-pc-windows-msvc test -p victory-shader --release --test resource_tracking bench_unrolled_loads -- --ignored --nocapture

Verification

  • MSVC workspace tests passed serially on WARP with 267 passed and 5 ignored. Workspace Clippy completed with existing warnings. Both runs used a temporary local module rename for the duplicate test module on main. PR #129
  • All three synthetic regression tests passed on both revisions. The asset-policy check passed on the clean branch.

AI-assisted.

@raffclar

Copy link
Copy Markdown
Contributor

Couldn't find any issues with this. A guess a very long chain of additions could cause a stack overflow

@DaviTostes

Copy link
Copy Markdown

Tested 1c5493f on Arch Linux x86_64, rustc 1.98.1: pass.\n\n- cargo test -p victory-shader: 59 passed, 0 failed, 1 ignored (3 test binaries), including the 4 tests this PR adds.\n- The full workspace still needs #112 for the victory-gpu E0425, but the shader crate and these tests run green on Linux.\n\nNo game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

AntoninMichelq pushed a commit to AntoninMichelq/VIctory that referenced this pull request Nov 24, 2026
…dresses) into the integration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ghost ghost mentioned this pull request Nov 24, 2026

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Merges cleanly with current main (8e79c80); cargo test -p victory-shader --locked → lib tests green, and the new integration suite tests/resource_tracking.rs → 3 passed / 0 failed (1 ignored benchmark) on macOS arm64 / rustc 1.98.1.

The sums cache is sound: it's keyed by SSA value and only filled after the value's operands are settled, and inputs/origin don't change during tracing, so every use of a value sees the same sum — including the opaque fallback, which the comment calls out. The &self → &mut self ripple is mechanical, and phi_site is a clean extraction. The integration tests cover the shared-chain, two-dynamic-terms and offset cases the cache could plausibly break.

Co-Authored-By: GPT 6.2 Sol <noreply@openai.com>
TG0000 and others added 2 commits November 24, 2026 23:44
Main's tracer is kept; the sums are cached behind a RefCell, so that its
shared borrows stay, and dropped when the spilled parameters change them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main reads such an address as title memory instead of refusing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged main in (fast-forward). Main's tracer is unchanged; the sums are cached behind a RefCell and dropped when the spilled parameters change the origins.
One test follows main's rule that an address from no root parameter is title memory; the same file passes on main without the cache.
victory-shader 209 + 3 tests pass on Windows (RX 6800). The benchmark was not run again. AI-assisted (Claude Code).

999 perf/shader-lane-condition-search : MERGED, CENSUS BEFORE THE PUSH

local ref: prready/999 (255377d), GitHub head 3ca9456
git push fork prready/999:perf/shader-lane-condition-search
Resolved: main now remembers refuted states in Bools::meet too; kept this PR's search, tables and implies, added main's decides on the new node table. The pass that finds all missing phis was REWRITTEN: it looks at main's live edges only and skips edges main calls never taken or dead.
Checks: Mac lint 0, Mac tests formats 23, shader 120. Windows tests gpu 78, shader 211, 0 failed.
Not done: the census against main. "None goes from translating to refused", the byte-for-byte check and all timings in the body are from ffe310e and the old phi pass. Do not repeat them for this head.
Draft comment (if pushed without the census):
Merged main in (fast-forward). Main's search now remembers failed states as well, so item 1 gains less than the table says.
The pass that finds every missing phi was rewritten: it follows main's live edges and skips the edges main calls never taken or dead.
victory-shader 211 tests pass on Windows (RX 6800), clippy clean. The corpus comparison and the timings in the description are from ffe310e and were not run again on this head.
AI-assisted (Claude Code).

No push needed (mergeable, no failing check, no open question)

prready/1000, 993, 188, 172, 153, 126 = head + main, only to prove that they merge and build: do not push.
All six: clean merge, Mac lint 0, Mac tests pass (formats 23 to 25, shader 118 to 121). Their Windows-only tests (1000 and 153 gpu, 188 loader) were not run on the merge.
#1000: the two nits promised for its next change (a message when the test is skipped, one line on the order) are still open.
#124 is closed on GitHub: nothing done.

Worktree: C:\Users\theo_\victory-prs (detached at origin/main).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Tracer::sum in crates/victory-shader/src/resource.rs now caches each address sum by its SSA value, turning repeated tracing of a shared add chain from quadratic into linear work. Only add results are cached (inputs and spilled values are still looked up fresh), and the cache is dropped after the second propagate_copies in Tracer::new, the only place origins or spilled change after tracing starts, so cached sums cannot go stale. The new integration tests check offsets, 32-bit wraparound, the two-dynamic-term fallback and the title-memory rule.

CI: the check run failed, on emit::synthetic_tests::gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out. This PR does not touch them, and the same two fail on other PRs' runs.


Generated by Claude Code

@Dathinel

Copy link
Copy Markdown
Contributor

Integration note if this lands alongside #1757. On main 940d32f with this PR alone (90cf2f6), cargo test --frozen -p victory-shader --test resource_tracking passes all three regression tests (benchmark ignored).

Adding #1757 (e18d930) merges cleanly in Git, but the same command on the combined tree 914c95f9 fails with E0061 at the Inst::write call in shared_address_keeps_its_dynamic_term_and_wraps_offsets. The arena change makes the owning program an explicit argument. Updating just that test call to program.blocks[0].insts[1].write(&program, Reg::S(4)) restores all three passes.

The current standalone call is correct. This is a one-line adjustment for whichever PR integrates second. AI-assisted; tested locally on Windows MSVC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants