Skip to content

Shader: the loop limit switch works in every build - #1263

Open
pablogonz12 wants to merge 4 commits into
Marvisak:mainfrom
pablogonz12:up/loop
Open

pablogonz12 wants to merge 4 commits into
Marvisak:mainfrom
pablogonz12:up/loop

Conversation

@pablogonz12

@pablogonz12 pablogonz12 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

loop_limit() now reads VICTORY_SHADER_LOOP_LIMIT in every build, not only with diagnostics: same parse rules (decimal or 0x hex, at least 1, unreadable or 0 means the default), same default (2^20), and values above i32::MAX clamp to it (only a cap: the emitted constant takes any u32 and the compare is unsigned). shader_cache already hashes the limit into its key, so translations made with different limits never mix, and the warning says so.

No behaviour change by default.

Why

The limit is the switch to test whether spin-wait loops are what makes a frame slow (the ~6x GPU-time report from another fork), and a release build is the build one runs the game with. #452 changes what the default budget means; this only makes the override reachable, so it should stay useful whichever default lands.

Worth documenting: 32 is unsafe (a real loop of 33 iterations, or a long decal pass, gets cut and the picture breaks in ways that look like a title bug), 256 is cautious, 1024 is the value the report used. That is the advice I would put next to the variable.

How tested

  • cargo test -p victory-shader --lib (Linux): 96 passed, 0 failed, 1 ignored, including the new parse tests (decimal, hex, 0, junk, over the clamp).
  • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings, with and without --all-features: clean.
  • Offline four-stage check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped): unchanged from main ffe310e (656 translate, 10 spirv-val, 0 pipeline) — the default path emits the same code.

Not tested

  • Any in-game run with a non-default limit; the numbers for 256/1024 come from other testers' reports, not from me.

Written with an AI assistant.

VICTORY_SHADER_LOOP_LIMIT was read only with the diagnostics feature, so a
release build could not run the experiment another VIctory fork reports as a
~6x GPU time win. It is now honoured in every build, with the default (2^20)
unchanged, values from 2^31 clamped to i32::MAX (the emitted counter is an i32
compared unsigned, so a bigger constant would act as no limit), and the same
parse rules. The shader cache already hashes the limit into its key, so
translations made with different limits never mix; the warning says so.

The 256/1024 advice and the risk of 32 are in xcl-bin/notes/loop.md.
@adamdotpy

Copy link
Copy Markdown

Reviewed on Windows 11 / Rust 1.98.1, d4ee6a5 on current main (ffe310e, merges as it stands), isolated target dirs. Verified: cargo test -p victory-shader --lib 120 passed / 1 ignored including the extended the_loop_limit_switch_parses_or_falls_back; cargo test -p victory-gpu --lib shader_cache 2 passed; clippy clean with and without --all-features; release build of victory fine.

The headline claim is proven in a release build, not just by reading the diff. I appended a throwaway probe to emit/mod.rs (reverted after, tree clean) that asserts loop_limit() == 8, and ran it with VICTORY_SHADER_LOOP_LIMIT=8 cargo test -p victory-shader --release --lib probe_env — it passes. On main the same run returns 2^20, because loop_limit() was gated on cfg!(feature = "diagnostics"). Removing that gate is the whole fix, and it is now demonstrated end to end in the build type that previously ignored the switch.

The clamp to i32::MAX is correct, and I checked why. count_iteration emits self.s.m.i32(super::loop_limit()) and compares the i32 counter against it (lower.rs:6840), so a limit at or above 2^31 would wrap the counter before the comparison ever fires — two billion iterations, the hang the limit exists to prevent. Clamping to i32::MAX keeps the counter from wrapping first. The new parse tests cover 0X hex, tabs/newlines, and the overflow cases (4294967296, 0x100000000 fall back to the default rather than wrapping).

Interaction with our #1094 branch, so it is on record: this PR edits the doc comment of build() in victory-gpu/src/shader_cache.rs, the same file our refusal-cache branch changes. The edit is comment-only and our change is the entry format, so the merge is trivial, but the two branches will touch the same lines — worth merging one first. The substance agrees: the loop limit is part of the translation, so it belongs in the cache key, and now that the switch works in every build, a release build that changes it gets its own cache entries rather than serving translations made with the default — which is exactly the correctness property #1094 depends on.

One doc nit. The README row now says "32 is a diagnostic value, 256-1024 a safer probe" — useful, and it also warns that a real loop longer than n is cut short, which is the honest caveat for a knob that changes translation output. No code change needed.

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

VICTORY_SHADER_LOOP_LIMIT now honored in every build (was diagnostics-only), with the right clamp: the emitted counter is i32 and compared unsigned, so values >= 2^31 would alias to huge/negative and defeat the limit — clamped to i32::MAX. Also accepts 0X prefix, rejects 0/negative/overflow, README and shader_cache comment updated to match. Complements #452 (which changed the default limit behavior); not a duplicate. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed (parse tests run on macOS).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work: needs the maintainer's decision. Thanks for eb9df73. The parse_loop_limit doc comment now gives the clamp's real reason (a cap: Module::i32 takes a u32 and the compare in Lower::count_iteration is unsigned), and the description matches. Nothing else is left on the code side. CI check passes at eb9df73.

The open question is unchanged. loop_limit() (crates/victory-shader/src/emit/mod.rs) now reads VICTORY_SHADER_LOOP_LIMIT in release builds, but commit 15b0091 deliberately limited it to diagnostics builds. Whether release builds should get the switch is the maintainer's call. The shader cache key already covers the limit (shader_cache::build). If the maintainer agrees, this is ready. #452 and #1819 touch the same limit.


Generated by Claude Code

@pablogonz12

Copy link
Copy Markdown
Contributor Author

Fixed in b7005f4: the doc comment and the test comment now say the compare is unsigned (IULT/IUGE), so 2^31 and up is a large count rather than a negative one, and the clamp only keeps the emitted i32 constant representable. cargo test -p victory-shader --lib 118 pass/1 ignored; xwin clippy workspace clean. Whether the release build reads the switch stays as this PR has it — that one is your call.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@pablogonz12

Copy link
Copy Markdown
Contributor Author

Done: the doc comment now says the clamp is only a cap (Module::i32 takes a u32 and the compare is unsigned), comment-only commit, and the same sentence in the description is fixed. Whether release builds read the switch remains the maintainer's call.

AI-assisted.

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.

4 participants