Shaders: cap BDD memo table and bail out early on overflow #288

Merged
Yaekai merged 1 commit from upstream-pr/288 into main 2026-11-25 00:19:29 +00:00
Yaekai commented 2026-11-24 02:03:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Yaekai on 2026-11-24T02:03:34Z.


Problem

During live GTA VI world loading (with concurrent background pipeline compilation in pipeline::make), victory_shader::structure::Bools::memo (HashMap<(Bool, Bool, Bool), Bool>) can grow unbounded and trigger a 35.6 MB (2^21 buckets) HashMap::reserve_rehash allocation failure:

memory allocation of 35651600 bytes failed
  hashbrown::raw::RawTable::reserve_rehash<((Bool, Bool, Bool), Bool), ...>
  victory_gpu::pipeline::make::Pending::make

Two issues cause this in crates/victory-shader/src/structure.rs:

  1. Bools::nodes is capped at MAX_NODES, but Bools::memo had no cap and can grow quadratically/cubically in the number of nodes.
  2. Once mk() sets self.overflow = true, ite() and forget() did not bail out early and continued recursing and inserting into self.memo.

Changes

  • Add MAX_MEMO = 1 << 18 (~4.5 MB max) and clear self.memo when it reaches MAX_MEMO (it is purely a cache).
  • Return Bool::FALSE immediately at the top of Bools::ite() and Bools::forget() when self.overflow is set, and skip self.memo.insert on overflow.

Verification

  • cargo test -p victory-shader — 71/71 passed.
  • rustfmt --edition 2024 --check crates/victory-shader/src/structure.rs — clean.
  • victory translate on shaders.rpf (26,084 programs): all 26,084 programs structure identically with 0 regressions.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6572928301 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/288). Originally posted by [Yaekai](https://github.com/Yaekai) on 2026-11-24T02:03:34Z. --- ### Problem During live GTA VI world loading (with concurrent background pipeline compilation in `pipeline::make`), `victory_shader::structure::Bools::memo` (`HashMap<(Bool, Bool, Bool), Bool>`) can grow unbounded and trigger a 35.6 MB (`2^21` buckets) `HashMap::reserve_rehash` allocation failure: ```text memory allocation of 35651600 bytes failed hashbrown::raw::RawTable::reserve_rehash<((Bool, Bool, Bool), Bool), ...> victory_gpu::pipeline::make::Pending::make ``` Two issues cause this in `crates/victory-shader/src/structure.rs`: 1. `Bools::nodes` is capped at `MAX_NODES`, but `Bools::memo` had no cap and can grow quadratically/cubically in the number of nodes. 2. Once `mk()` sets `self.overflow = true`, `ite()` and `forget()` did not bail out early and continued recursing and inserting into `self.memo`. ### Changes - Add `MAX_MEMO = 1 << 18` (~4.5 MB max) and clear `self.memo` when it reaches `MAX_MEMO` (it is purely a cache). - Return `Bool::FALSE` immediately at the top of `Bools::ite()` and `Bools::forget()` when `self.overflow` is set, and skip `self.memo.insert` on overflow. ### Verification - `cargo test -p victory-shader` — 71/71 passed. - `rustfmt --edition 2024 --check crates/victory-shader/src/structure.rs` — clean. - `victory translate` on `shaders.rpf` (26,084 programs): all 26,084 programs structure identically with 0 regressions.
jeremygold02 commented 2026-11-24 06:55:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:17:08Z.


Tested PR #288 at head 176c766ab1814ab4be0602709f2f80abb466fd84 (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at 176c766ab1814ab4be0602709f2f80abb466fd84.

  • cargo test --locked -p victory-shader structure — passed (ok. 5 passed; 0 failed; 0 ignored; 0 measured; 64 filtered out; finished in 0.00s ); cases: structure::tests::divergent_if_becomes_a_lane_branch, structure::tests::exec_narrowed_without_a_branch_leaves_a_guard, structure::tests::divergent_loop_exits_per_lane, structure::tests::scalar_branches_stay_uniform, and 1 more.

Behavior covered: Exercises shader control-flow structure analysis after bounding the BDD ite memo table; no dedicated new test was added in this PR.
Code: crates/victory-shader/src/structure.rs

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121878201 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/288#issuecomment-7121878201). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:17:08Z. --- Tested PR #288 at head `176c766ab1814ab4be0602709f2f80abb466fd84` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `176c766ab1814ab4be0602709f2f80abb466fd84`. - `cargo test --locked -p victory-shader structure` — passed (ok. 5 passed; 0 failed; 0 ignored; 0 measured; 64 filtered out; finished in 0.00s ); cases: structure::tests::divergent_if_becomes_a_lane_branch, structure::tests::exec_narrowed_without_a_branch_leaves_a_guard, structure::tests::divergent_loop_exits_per_lane, structure::tests::scalar_branches_stay_uniform, and 1 more. Behavior covered: Exercises shader control-flow structure analysis after bounding the BDD ite memo table; no dedicated new test was added in this PR. Code: [crates/victory-shader/src/structure.rs](https://github.com/Marvisak/VIctory/blob/176c766ab1814ab4be0602709f2f80abb466fd84/crates/victory-shader/src/structure.rs)
mercuryVM commented 2026-11-24 07:26:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:26:59Z.

Review: APPROVED


Tested 176c766 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines.

  • Returning Bool::FALSE from ite/forget once overflow is set is safe. Both consumers check self.b.overflow and return TOO_LARGE (structure.rs:575 and :1017), so a result computed after overflow is never used, the same as mk's existing FALSE return.
  • Clearing memo at MAX_MEMO is sound because it's only a cache: unique and nodes hold the canonical BDD, so a cleared memo costs recomputation, never correctness. The 2^18 cap bounds the table at a few MB.
  • The bail-outs also stop the wasted recursion that kept filling memo after the node cap was hit, which is likely where most of the growth came from.

No issues found. I can't reproduce the 35 MB allocation failure without a game package.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035247837 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/288#pullrequestreview-6035247837). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:26:59Z. **Review:** APPROVED --- Tested 176c766 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (264 passed, 0 failed). `victory translate <title exe> --validate --pipelines` gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. - Returning `Bool::FALSE` from `ite`/`forget` once `overflow` is set is safe. Both consumers check `self.b.overflow` and return `TOO_LARGE` (`structure.rs:575` and `:1017`), so a result computed after overflow is never used, the same as `mk`'s existing `FALSE` return. - Clearing `memo` at `MAX_MEMO` is sound because it's only a cache: `unique` and `nodes` hold the canonical BDD, so a cleared memo costs recomputation, never correctness. The 2^18 cap bounds the table at a few MB. - The bail-outs also stop the wasted recursion that kept filling `memo` after the node cap was hit, which is likely where most of the growth came from. No issues found. I can't reproduce the 35 MB allocation failure without a game package. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:03:16 +00:00
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!288
No description provided.