Repository navigation
fix(native): version reload snapshots per byte range instead of walking clobbers - #2058
Merged
Merged
Conversation
Change-Id: I9dc1f7023336a74e02ed52bfe8eb1a0a438d3993
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ng clobbers The reload-recipe analysis derived a query-specific view of one whole-memory SSA graph by walking it and folding every MemoryPhi whose inputs agreed for the query. Folding is not well defined on cycles: a phi whose inputs were all still on the walk stack was kept as a version of its own, so a join inside a loop became a distinct version although nothing in the loop wrote the queried bytes, and the result depended on where the walk started. The in-session reuse of such results and the per-(load, start) clobber cache then froze whichever answer came first in dominator order. The spill planner's probe function and the final MIR query different points in a different order, so their certificates differed on identical memory states and materialization failed with RELOAD_RECIPE.STATE_SNAPSHOT_CURRENT (#2041, #2044). celox-analysis gains `memory_versions`: the minimal SSA of every elementary byte range, built directly. A phi for a range is placed exactly at the iterated dominance frontier of the range's writes, with live-on-entry as the root definition, so every phi merges two different reaching versions on any CFG, including irreducible ones, and the version of every byte at every point is a canonical function of the program. Renaming runs once over the dominator tree with a persistent range-assign tree: O((W + F) log A) nodes for W write ranges, F phi ranges and A atoms, and a query costs O(k log A) for k result segments, replacing walks that were O(queries x definitions) or, with the clobber memo, Θ(ranges x accesses) memory. Reload snapshots become per-byte segment maps that reference the function's phi equations, interned once per function as pieces with one input tuple each; snapshots with equal roots share one allocation. The final-MIR verifier compares snapshots run by run with the same ordinal shifting and factoring inlining as before. The clobber walker stays in celox-analysis as a published API and test oracle, without the memo of #1997 and its budget of #2037, which no client uses any more; its module docs record the divergence. Tests: random CFGs with loops, nested loops, irreducible cycles, overlapping ranges and unknown writes against one sparse SSA per atom; random acyclic CFGs against the clobber walker's snapshot equality, where it is exact; the #2041 loop shape as a documented divergence; and the #2044 derived-edge repro. Closes #2041, closes #2044. Change-Id: I2badec66e4eea726e39282d6f994151cdd87e4d2
tignear
force-pushed
the
feature/perf/2032-memory-ssa-versioning
branch
from
October 25, 2026 18:41
c5ad7ad to
96d1d50
Compare
Contributor
Merge Queue Status
This pull request spent 1 hour 7 minutes 5 seconds in the queue, including 31 minutes 51 seconds running CI. Required conditions to merge
|
This was referenced Oct 25, 2026
This was referenced Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2041, closes #2044. Part of #2032.
Problem
The reload-recipe analysis answers "which write last defined these state bytes at this point" with clobber walks over a single memory-SSA chain (
celox-analysismemory_ssa.rs,find_clobber).The walk is wrong on loops. A MemoryPhi whose inputs are all still on the walk's stack resolves to itself, and inputs on the stack are ignored when the other inputs resolve. That rule is only valid for
phi(x, phi) = x. A loop-header phi together with a join phi inside the loop (any if/else in a loop) forms a strongly connected group. There the walk returns a phi whose inputs all resolve to one value, and which phi it returns depends on where the walk started.reload.rs's(load, start)cache then freezes whichever answer came first. The spill planner's probe function and the final MIR query different points in a different order, so the two get different certificates for the same memory state, and the reload-recipe verifier rejects the function:In #2041,
Phi(bb47)'s inputs (bb45, bb48, bb49) andPhi(bb40)'s inputs (bb99, bb126) all resolve toPhi(bb1). A minimal SSA has no such phis. No wrong generated code was found, because the comparisons are dominator-scoped; the certificates are just non-canonical and imprecise.The walks also cost O(queries × definitions). #1997's memo traded that for Θ(ranges × accesses) memory, and #2037 had to cap it.
Change
celox-analysismemory_versions.rs: a minimal SSA per byte range, built directly.reload.rs:ReloadMemorySsawrapsMemoryVersions.MemorySnapshotis a canonical certificate (Rc-shared): per-byte versions plus a reference to a per-function phi-equation table. Two snapshots are equal exactly when every byte has the same version.ClobberWalkeruse,ClobberMemo,clobber_cacheand fix(native): bound the memory of memoized reload clobber walks #2037's budget are removed.memory_ssa.rskeeps the walker as API and test oracle, with its divergence documented.Complexity
Generated code
On acyclic CFGs the snapshot-equality relation is the same as before (tested), so the code is identical. On CFGs with loops, the old non-canonical self-phi versions are gone: a loop that does not write the queried bytes no longer introduces a version, so a value can stay in a register across such a loop where it was reloaded before. The perf gate is therefore "equal or better", not "identical".
Validation
celox-analysis80 passed, including 10 new tests:ssa::buildper atom;celox-backend-x86615 passed.celoxlib (systemverilog, cranelift) passed, including the native: reload recipe disagrees with its rebuild (RELOAD_RECIPE.STATE_SNAPSHOT_CURRENT) on a derived-edge unit with many gated clocks #2044 reproderived_edge::tests::an_edge_reaching_60_gated_domains_runs_as_the_plan_walk.celoxintegration (systemverilog, cranelift, wasm-runtime):systemverilogsuite 5062 passed, and 38 native-exercising targets with 0 failed.native: reload recipe disagrees with its rebuild (RELOAD_RECIPE.STATE_SNAPSHOT_CURRENT) on a two-state always @* unit #2041's case (
always_comb::two_state_sensitivity_lists_run_at_time_zero, from fix(sv): evaluate sensitivity-list comb blocks at time 0 in two-state mode, like Verilator #2021's branch) passes on native, native_parallel, cranelift, cranelift_parallel and wasm.clippy
-D warningsand fmt clean.An independent review found no correctness issue. Its findings are addressed: phi equations interned per function, the verifier compares per run, and ignored segment lookups are now errors.
rtlmeter C906 (four-state, native): the compile window drops from about 235 s to about 34 s, and peak RSS from 3.1 GB to 2.96 GB, measured against fix(native): bound the memory of memoized reload clobber walks #2037.
b2_vm_dispatch_bit callgrind (release,
celox run --backend native --top bench_vm_dispatch --two-state --unlimited,--toggle-collect='*SimulationState*step_until*'), origin/master 6e5297e vs 96d1d50:BENCH_DONE cycles=200000 checksum=136on both.b2's generated code has no loop with a join inside it, so the old certificates were already canonical there.