Skip to content

fix(native): version reload snapshots per byte range instead of walking clobbers - #2058

Merged
mergify[bot] merged 2 commits into
masterfrom
feature/perf/2032-memory-ssa-versioning
Oct 25, 2026
Merged

mergify[bot] merged 2 commits into
masterfrom
feature/perf/2032-memory-ssa-versioning

Conversation

@tignear

@tignear tignear commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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-analysis memory_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:

[RELOAD_RECIPE.STATE_SNAPSHOT_CURRENT] expected_write=Phi(bb47) actual_write=Phi(bb40)   (#2041)
[RELOAD_RECIPE.STATE_SNAPSHOT_CURRENT] expected_write=Phi(bb5) actual_write=LiveOnEntry  (#2044)

In #2041, Phi(bb47)'s inputs (bb45, bb48, bb49) and Phi(bb40)'s inputs (bb99, bb126) all resolve to Phi(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

  • New celox-analysis memory_versions.rs: a minimal SSA per byte range, built directly.
    • Atoms are the maximal ranges that no write boundary splits.
    • Each atom gets phis exactly at the iterated dominance frontier of its write blocks. These phis are non-trivial on any CFG, irreducible ones included, so no cycle heuristics are needed.
    • Renaming is one dominator-tree pass with a persistent range-assign tree; unknown writes assign all atoms.
  • reload.rs:
    • ReloadMemorySsa wraps MemoryVersions.
    • A MemorySnapshot is 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.
    • The final-MIR verifier compares run by run.
    • ClobberWalker use, ClobberMemo, clobber_cache and fix(native): bound the memory of memoized reload clobber walks #2037's budget are removed.
    • The verify rerun is unchanged.
  • memory_ssa.rs keeps the walker as API and test oracle, with its divergence documented.

Complexity

Cost
Before O(queries × definitions) walk time, or Θ(ranges × accesses) memo memory
After: build O((W + F) log A) nodes, where W is write ranges, F phi ranges and A atoms
After: query O(k log A) for k result segments, plus an intern lookup
After: phi equations stored once per function

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

Change-Id: I9dc1f7023336a74e02ed52bfe8eb1a0a438d3993
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T18:43:22.754985Z 96d1d50 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tignear tignear added merge-ready Request merging through Mergify after required checks and removed merge-ready Request merging through Mergify after required checks labels Oct 25, 2026
…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
tignear force-pushed the feature/perf/2032-memory-ssa-versioning branch from c5ad7ad to 96d1d50 Compare October 25, 2026 18:41
@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 25, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 25, 2026
@mergify

mergify Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-25 19:02 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • ✅ Checks passed · on draft merge queue: checking #2058 on master (a1d1c8a) #2069
  • ✅ Merged — 2026-10-25 20:09 UTC · at f5df8c4090fb0889ca8db097c3bad8abc0f664b8 · merge

This pull request spent 1 hour 7 minutes 5 seconds in the queue, including 31 minutes 51 seconds running CI.

Required conditions to merge
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify
mergify Bot merged commit f5df8c4 into master Oct 25, 2026
24 checks passed
@mergify
mergify Bot deleted the feature/perf/2032-memory-ssa-versioning branch October 25, 2026 20:09
@mergify mergify Bot removed the queued Mergify queue entry label Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

1 participant