Shaders: intersect compare bounds with existing range bounds in Ranges::edge_hi #2489

Open
reeledMarek wants to merge 1 commit from upstream-pr/2491 into main
reeledMarek commented 2026-11-25 14:48:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T14:48:18Z.


Problem

In crates/victory-shader/src/range.rs, Ranges::compare_bound() computes:

let h = self.operand_hi(inst, other);
Some(if below { h.saturating_sub(1) } else { h })

When other is unbounded (h == u32::MAX) and below is true (e.g. a < unknown or !(a >= unknown)), compare_bound() returns Some(u32::MAX - 1) instead of None.

In Ranges::edge_hi(), best then becomes u32::MAX - 1 != u32::MAX, so the fallback if best == u32::MAX { self.hi(a) } else { best } skips self.hi(a) and discards any tighter upper bound already proven on a (for example s0 = s2 & 3), causing relative moves (v_movrel*) to lose their proven index bound and fall back to the full VGPR range.

Fix

  • Return None from Ranges::compare_bound() when h == u32::MAX (unbounded compare operand).
  • Added synthetic regression test range::tests::compare_against_unbounded_operand_preserves_existing_bound covering both s_cmp_lt_u32 (taken edge) and s_cmp_ge_u32 (not-taken edge).

Verification

  • cargo test -p victory-shader --locked (145 passed, 0 failed)
  • cargo test -p xtask --locked (repository_passes_policy passed)
  • cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings (0 warnings)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5493618371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T14:48:18Z. --- ### Problem In `crates/victory-shader/src/range.rs`, `Ranges::compare_bound()` computes: ```rust let h = self.operand_hi(inst, other); Some(if below { h.saturating_sub(1) } else { h }) ``` When `other` is unbounded (`h == u32::MAX`) and `below` is `true` (e.g. `a < unknown` or `!(a >= unknown)`), `compare_bound()` returns `Some(u32::MAX - 1)` instead of `None`. In `Ranges::edge_hi()`, `best` then becomes `u32::MAX - 1 != u32::MAX`, so the fallback `if best == u32::MAX { self.hi(a) } else { best }` skips `self.hi(a)` and discards any tighter upper bound already proven on `a` (for example `s0 = s2 & 3`), causing relative moves (`v_movrel*`) to lose their proven index bound and fall back to the full VGPR range. ### Fix - Return `None` from `Ranges::compare_bound()` when `h == u32::MAX` (unbounded compare operand). - Added synthetic regression test `range::tests::compare_against_unbounded_operand_preserves_existing_bound` covering both `s_cmp_lt_u32` (taken edge) and `s_cmp_ge_u32` (not-taken edge). ### Verification - `cargo test -p victory-shader --locked` (145 passed, 0 failed) - `cargo test -p xtask --locked` (`repository_passes_policy` passed) - `cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings` (0 warnings)
oni-swr commented 2026-11-25 15:08:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T15:08:48Z.

Review: COMMENTED


Independent verification on Windows, VICTORY_D3D12_WARP=1 (WARP only, no hardware GPU touched). Tested commit 98c0e07d45b914276940cacf216cdf8a2163ec15 (clean detached checkout).

  • cargo test -p victory-shader --locked --lib compare_against_unbounded — 1 passed, 0 failed, 0 ignored.
  • Mutation check: with the new h == u32::MAX early return disabled, the test fails, so it guards the change.
  • cargo test -p victory-shader --locked --lib — 248 passed, 0 failed, 3 ignored (the ignored ones are pre-existing).

Code read: before this change, an unbounded operand (u32::MAX) gave Some(u32::MAX - 1) for lt/ge-false. That is a Some, so it bypassed the existing self.hi(a) bound in edge_hi. Returning None falls back to the tighter bound. The only precision lost is a ge-false or lt compare against a literal 0xFFFFFFFF, which gave MAX-1 before. That is conservative and not meaningful. The test covers both polarities (lt/scc1 and ge/scc0). No problems found.

AI-assisted (Claude Code / Claude Sonnet 5.5)

<!-- greyfinch-sync:Marvisak/VIctory:review:6060181872 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#pullrequestreview-6060181872). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T15:08:48Z. **Review:** COMMENTED --- Independent verification on Windows, `VICTORY_D3D12_WARP=1` (WARP only, no hardware GPU touched). Tested commit `98c0e07d45b914276940cacf216cdf8a2163ec15` (clean detached checkout). - `cargo test -p victory-shader --locked --lib compare_against_unbounded` — 1 passed, 0 failed, 0 ignored. - Mutation check: with the new `h == u32::MAX` early return disabled, the test fails, so it guards the change. - `cargo test -p victory-shader --locked --lib` — 248 passed, 0 failed, 3 ignored (the ignored ones are pre-existing). Code read: before this change, an unbounded operand (`u32::MAX`) gave `Some(u32::MAX - 1)` for `lt`/`ge`-false. That is a `Some`, so it bypassed the existing `self.hi(a)` bound in `edge_hi`. Returning `None` falls back to the tighter bound. The only precision lost is a `ge`-false or `lt` compare against a literal `0xFFFFFFFF`, which gave `MAX-1` before. That is conservative and not meaningful. The test covers both polarities (`lt`/scc1 and `ge`/scc0). No problems found. AI-assisted (Claude Code / Claude Sonnet 5.5)
cloph-dsp commented 2026-11-25 16:09:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T15:41:45Z.


Independent local validation for PR #2491 (Windows, Rust 1.100.0/MSVC, head 98b0e07, no game data): cargo test --locked -p victory-shader --lib -> 249 passed, 0 failed, 3 ignored, exit 0; git diff --check clean. CPU-only regression run; synthetic compare-range scenario not replayed. AI-assisted (Hy4 flash). GPU/driver N/A for CPU tests.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167238635 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#issuecomment-7167238635). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T15:41:45Z. --- Independent local validation for PR #2491 (Windows, Rust 1.100.0/MSVC, head 98b0e07, no game data): `cargo test --locked -p victory-shader --lib` -> 249 passed, 0 failed, 3 ignored, exit 0; `git diff --check` clean. CPU-only regression run; synthetic compare-range scenario not replayed. AI-assisted (Hy4 flash). GPU/driver N/A for CPU tests.
Marvisak commented 2026-11-25 16:09:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:59:53Z.


Automated triage for the maintainers.

Merge candidate. Ranges::edge_hi (crates/victory-shader/src/range.rs) now returns best.min(self.hi(a)) instead of falling back to self.hi(a) only when no compare fact was found. self.hi(a) bounds a on every edge, so taking the minimum is always sound. A compare against an unbounded or looser operand can no longer hide a tighter bound already proven on a, and v_movrel* indexing keeps its narrow VGPR range. The new test checks s_cmp_lt_u32 (taken edge) and s_cmp_ge_u32 (not-taken edge) against both an unbounded s3 and s3 = s4 & 255, with s0 = s2 & 3. I checked the instruction encodings by hand. The looser case would read far more than v8–v11 on main.

Reply to the author: this is the change the last triage asked for, including the bounded-but-looser test case. One small thing: the PR description still describes the earlier compare_bound change, so it's worth updating before merge.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167500611 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#issuecomment-7167500611). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:59:53Z. --- <!-- victory-triage v1 head=b7d4f762918c358ae5043debc40e4f664a80317a verdict=merge-candidate at=2026-11-25T20:18:54Z --> Automated triage for the maintainers. **Merge candidate.** `Ranges::edge_hi` (`crates/victory-shader/src/range.rs`) now returns `best.min(self.hi(a))` instead of falling back to `self.hi(a)` only when no compare fact was found. `self.hi(a)` bounds `a` on every edge, so taking the minimum is always sound. A compare against an unbounded or looser operand can no longer hide a tighter bound already proven on `a`, and `v_movrel*` indexing keeps its narrow VGPR range. The new test checks `s_cmp_lt_u32` (taken edge) and `s_cmp_ge_u32` (not-taken edge) against both an unbounded `s3` and `s3 = s4 & 255`, with `s0 = s2 & 3`. I checked the instruction encodings by hand. The looser case would read far more than v8–v11 on `main`. Reply to the author: this is the change the last triage asked for, including the bounded-but-looser test case. One small thing: the PR description still describes the earlier `compare_bound` change, so it's worth updating before merge. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
reeledMarek commented 2026-11-25 20:06:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T20:05:19Z.


Updated to address the triage feedback:

  • Replaced the u32::MAX sentinel check in Ranges::edge_hi with best.min(self.hi(a)) and removed the h == u32::MAX special case in compare_bound, so both unbounded and bounded-but-looser compare operands preserve tighter existing bounds.
  • Expanded the unit test to cover both unbounded s3 and bounded-but-looser s3 = s4 & 255 compared against s0 = s2 & 3.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7173173847 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#issuecomment-7173173847). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T20:05:19Z. --- Updated to address the triage feedback: - Replaced the `u32::MAX` sentinel check in `Ranges::edge_hi` with `best.min(self.hi(a))` and removed the `h == u32::MAX` special case in `compare_bound`, so both unbounded and bounded-but-looser compare operands preserve tighter existing bounds. - Expanded the unit test to cover both unbounded `s3` and bounded-but-looser `s3 = s4 & 255` compared against `s0 = s2 & 3`.
upstream-sync changed title from [PR #2491] Shaders: do not let unbounded compare operands override tighter range bounds to [PR #2491] Shaders: intersect compare bounds with existing range bounds in Ranges::edge_hi 2026-11-25 21:02:30 +00:00
fuzeofficlal commented 2026-11-26 18:42:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T18:42:38Z.

Review: APPROVED


Verified b7d4f762918c358ae5043debc40e4f664a80317a merged onto main (63bcca43) on Windows 11 x64:

  • best.min(self.hi(a)) in Ranges::edge_hi (crates/victory-shader/src/range.rs:212) soundly intersects existing upper bounds with proven unsigned compare facts across all predicates (lt/le/gt/ge/eq), operand orders, and saturating arithmetic, without letting looser or unbounded compare operands mask tighter bounds.
  • rustfmt --edition 2024 --check crates/victory-shader/src/range.rs: clean (exit 0).
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed (asset policy: 191 files ok).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: clean.
  • cargo test --locked -p victory-shader --lib range::: 4 passed, 0 failed (compare_against_looser_or_unbounded_operand_preserves_existing_bound passes).

Minor doc note: worth updating the PR description to match the edge_hi best.min(self.hi(a)) change. Ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079109651 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#pullrequestreview-6079109651). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T18:42:38Z. **Review:** APPROVED --- Verified `b7d4f762918c358ae5043debc40e4f664a80317a` merged onto `main` (`63bcca43`) on Windows 11 x64: - `best.min(self.hi(a))` in `Ranges::edge_hi` (`crates/victory-shader/src/range.rs:212`) soundly intersects existing upper bounds with proven unsigned compare facts across all predicates (`lt`/`le`/`gt`/`ge`/`eq`), operand orders, and saturating arithmetic, without letting looser or unbounded compare operands mask tighter bounds. - `rustfmt --edition 2024 --check crates/victory-shader/src/range.rs`: clean (exit 0). - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: `1 passed` (`asset policy: 191 files ok`). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings`: clean. - `cargo test --locked -p victory-shader --lib range::`: `4 passed, 0 failed` (`compare_against_looser_or_unbounded_operand_preserves_existing_bound` passes). Minor doc note: worth updating the PR description to match the `edge_hi` `best.min(self.hi(a))` change. Ready to merge.
Dukestar69 commented 2026-11-26 19:51:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T19:51:58Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Intersecting compare bounds with existing hi bounds in Ranges::edge_hi preserves tighter outer bounds when nested comparisons yield a looser bound.
  • cargo test -p victory-shader --lib range:: -- --test-threads=1: all passed.
  • 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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6079793329 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2491#pullrequestreview-6079793329). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T19:51:58Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Intersecting compare bounds with existing `hi` bounds in `Ranges::edge_hi` preserves tighter outer bounds when nested comparisons yield a looser bound. - `cargo test -p victory-shader --lib range:: -- --test-threads=1`: all passed. - `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).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2491:upstream-pr/2491'
git switch 'upstream-pr/2491'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/2491'
git switch 'upstream-pr/2491'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2491'
git switch 'upstream-pr/2491'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2491'
git switch 'main'
git merge --squash 'upstream-pr/2491'
git switch 'main'
git merge --ff-only 'upstream-pr/2491'
git switch 'main'
git merge 'upstream-pr/2491'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2489
No description provided.