You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(native): make the register allocator's whole-function analyses near-linear #2032
On large functions the native register allocator does work that grows faster than the size of the function. rtlmeter C906's combinational function takes tens of minutes to compile natively, and with #1997's memo it runs out of memory. Splitting functions to keep them small is not the fix: it gives up cross-function register forwarding and comb-store elimination, and generated code quality has priority over build time (the tiered backend absorbs build cost). The allocator itself has to scale.
Profiles on C906 point at crates/celox-backend-x86/src/backend/native/regalloc:
The allocator's algorithms get fixed so that compile time and memory scale with function size:
Measure on C906 which analyses dominate now. Profile, don't assume.
Where an exact near-linear formulation exists, use it: identical generated code, with equivalence tests.
Where none exists (for example a heuristic search such as the integrated spill schedule), redesign it to scale. Different allocations are acceptable if generated-code performance is equal or better on the perf gates (b2_vm_dispatch_bit callgrind). Document the trade-off in the PR.
One PR per analysis, starting with the biggest cost on C906.
changed the title [-]perf(native): bound the size of functions given to the register allocator[/-][+]perf(native): make the register allocator's whole-function analyses near-linear[/+]on Oct 25, 2026
Review of the allocator (read-only, c906-int at 993c6525d9). Verified points are marked; the rest are review findings to be confirmed while implementing.
Ranked for the C906 function shape:
Reload memory SSA.
Unconditional rebuild (verified).analyze_with_queries always calls ReloadRecipeAnalysis::verify, which runs analyze_unverified_with_queries again (reload.rs:1185-1193, :753-760), whatever the pipeline's verify flag says. It is called from ssa.rs:139 and ssa_state_home.rs:102,146, so there are 6 full memory-SSA builds plus analyze_for_planning.
Function clones (verified).build_probe clones the whole function (ssa_state_home.rs:589), twice.
ClobberMemo. It is Θ(ranges × (defs + phis)) and unbounded (reload.rs:1226, memory_ssa.rs:398-412).
Fix, exact:
Gate the verification on the verify flag. Build the state-home probe as extra memory events instead of cloning, and reuse one graph for both probes.
Replace the clobber walks with per-location versioning over the dominator tree: atoms from all write and query ranges, a persistent segment tree of the dominance-latest access per atom, phis at joins where pred versions differ, and loop-header phis for atoms written in the loop.
Then each clobber(P, R) costs O(log A) with no memo, and snapshot certificates are interned.
Validate with the existing rebuild verifier and sparse_memory_ssa_matches_full_analysis_at_requested_points.
Stack slot first-fit is Θ(homes × slots × segments) (interval_union.rs:750-769, stack_color.rs:995-1219). Fix, exact: per-block slot-occupancy bitsets (any / full). The candidate mask comes from ORing over the bundle's segments, then take the lowest zero bit and check partial-vs-partial conflicts only for that slot.
Spill planner materializes live_entry and S_entry/S_exit per block and iterates all of S_entry[succ] per edge (spill_plan.rs:819-827, 908-924, 962-1003). Fix, exact: keep S implicit (a membership formula memoized per (block, value)), make edge coupling iterate only W_exit(pred) plus phi destinations, and materialize full sets only under verify.
Next use.
anticipated_after_phis keeps a dense unshared set per block (next_use.rs:205, 286-308). Fix: share it like the distance rows, or compute it lazily.
FrozenRow::get decodes up to 64 varints per lookup (distance_map.rs:618-634). Fix: give it O(log) lookup.
Malloc churn: per-instruction BTreeMap in MemoryPointMap (memory_ssa.rs:43) and per-query snapshot allocations.
Order: item 1's quick wins first (verify gating, no clones, exact), then per-location memory SSA, then 2, 3 and 4. All of these keep the generated code identical.
Item 3 (spill planner, implicit S): exact design, not implemented, parked by decision because the spill planner doesn't show up in the C906 compile profile today. Recording it so it isn't lost.
S_exit[b] = S_entry[b] ∪ SpillPhi(b) ∪ Evicted(b). The planner's spilled only grows (BlockTransitionPlanner::new phi spills, evict_value); finish stores it as s_exit.
So every block materializes an O(live) set twice (frozen and compressed at 64 or more), and edge coupling walks all of S_entry[succ] whenever it differs from S_exit[pred] or the edge renames.
Implicit form:
Per block keep only the small sets: W_entry[b] and W_exit[b] (at most R each), Kept[b] = W_entry[b] ∩ (∀ pred in S_exit) (at most R), and Local[b] = SpillPhi(b) ∪ Evicted(b), which is bounded by the block's spill ops.
v ∈ S_entry[b] ⇔ (v ∈ live_entry[b] ∧ v ∉ W_entry[b]) ∨ v ∈ Kept[b], using next_use.entry[b] membership.
v ∈ S_exit[b] ⇔ v ∈ S_entry[b] ∨ v ∈ Local[b].
The planner's spilled.contains and insert become this formula plus Local. spilled_at_entry, init_usual (plan.s_exit[p].contains) and select_recipe_homes only query membership.
Edge coupling (p → s), exact: an op is emitted for a successor value v ∈ S_entry[s] only when it is not skipped (skipped means same home and v ∈ S_exit[p]) and translate(v) ∈ W_exit[p] ∪ S_exit[p].
For a value the edge does not rename (same home), that reduces to v ∈ W_exit[p] ∧ v ∉ S_exit[p].
So the candidates are W_exit[p] mapped to successor names, plus the edge's renamed successor values (EdgeTranslations::by_edge), filtered by v ∈ S_entry[s].
They must be emitted in ascending successor-value order, the order of today's S_entry iteration, so the ops and the generated code are identical.
The SpillPhi loop and the transient-park logic are unchanged.
Verify: under verify, materialize S_entry/S_exit the old way and check membership and the edge ops against the implicit form. Keep the explicit sets only there.
Cost: O(R + spill ops + renames) per block and edge, plus O(log) live-row membership lookups, instead of O(live) per block and per differing edge.
Activity
[-]perf(native): bound the size of functions given to the register allocator[/-][+]perf(native): make the register allocator's whole-function analyses near-linear[/+]tignear commentedon Oct 25, 2026
Review of the allocator (read-only, c906-int at 993c6525d9). Verified points are marked; the rest are review findings to be confirmed while implementing.
Ranked for the C906 function shape:
analyze_with_queriesalways callsReloadRecipeAnalysis::verify, which runsanalyze_unverified_with_queriesagain (reload.rs:1185-1193,:753-760), whatever the pipeline'sverifyflag says. It is called fromssa.rs:139andssa_state_home.rs:102,146, so there are 6 full memory-SSA builds plusanalyze_for_planning.build_probeclones the whole function (ssa_state_home.rs:589), twice.reload.rs:1226,memory_ssa.rs:398-412).clobber(P, R)costs O(log A) with no memo, and snapshot certificates are interned.sparse_memory_ssa_matches_full_analysis_at_requested_points.interval_union.rs:750-769,stack_color.rs:995-1219). Fix, exact: per-block slot-occupancy bitsets (any / full). The candidate mask comes from ORing over the bundle's segments, then take the lowest zero bit and check partial-vs-partial conflicts only for that slot.live_entryandS_entry/S_exitper block and iterates all ofS_entry[succ]per edge (spill_plan.rs:819-827, 908-924, 962-1003). Fix, exact: keep S implicit (a membership formula memoized per (block, value)), make edge coupling iterate onlyW_exit(pred)plus phi destinations, and materialize full sets only under verify.anticipated_after_phiskeeps a dense unshared set per block (next_use.rs:205, 286-308). Fix: share it like the distance rows, or compute it lazily.FrozenRow::getdecodes up to 64 varints per lookup (distance_map.rs:618-634). Fix: give it O(log) lookup.BTreeMapinMemoryPointMap(memory_ssa.rs:43) and per-query snapshot allocations.Order: item 1's quick wins first (verify gating, no clones, exact), then per-location memory SSA, then 2, 3 and 4. All of these keep the generated code identical.
tignear commentedon Oct 25, 2026
Item 3 (spill planner, implicit S): exact design, not implemented, parked by decision because the spill planner doesn't show up in the C906 compile profile today. Recording it so it isn't lost.
What S is today (
spill_plan.rs):S_entry[b] = (live_entry[b] \ W_entry[b]) ∪ { v ∈ W_entry[b] : ∀ pred p, translate(p, b, v) ∈ S_exit[p] }(spilled_at_entry).S_exit[b] = S_entry[b] ∪ SpillPhi(b) ∪ Evicted(b). The planner'sspilledonly grows (BlockTransitionPlanner::newphi spills,evict_value);finishstores it ass_exit.S_entry[succ]whenever it differs fromS_exit[pred]or the edge renames.Implicit form:
W_entry[b]andW_exit[b](at most R each),Kept[b] = W_entry[b] ∩ (∀ pred in S_exit)(at most R), andLocal[b] = SpillPhi(b) ∪ Evicted(b), which is bounded by the block's spill ops.v ∈ S_entry[b] ⇔ (v ∈ live_entry[b] ∧ v ∉ W_entry[b]) ∨ v ∈ Kept[b], usingnext_use.entry[b]membership.v ∈ S_exit[b] ⇔ v ∈ S_entry[b] ∨ v ∈ Local[b].spilled.containsandinsertbecome this formula plusLocal.spilled_at_entry,init_usual(plan.s_exit[p].contains) andselect_recipe_homesonly query membership.Edge coupling (p → s), exact: an op is emitted for a successor value
v ∈ S_entry[s]only when it is not skipped (skipped means same home andv ∈ S_exit[p]) andtranslate(v) ∈ W_exit[p] ∪ S_exit[p].v ∈ W_exit[p] ∧ v ∉ S_exit[p].W_exit[p]mapped to successor names, plus the edge's renamed successor values (EdgeTranslations::by_edge), filtered byv ∈ S_entry[s].S_entryiteration, so the ops and the generated code are identical.Verify: under
verify, materializeS_entry/S_exitthe old way and check membership and the edge ops against the implicit form. Keep the explicit sets only there.Cost: O(R + spill ops + renames) per block and edge, plus O(log) live-row membership lookups, instead of O(live) per block and per differing edge.