Skip to content

MIR drop-state: chunked, hash-consed block states (#1343, #1315, #1282) - #1353

Merged
ehartford merged 1 commit into
mainfrom
fix-1343-drop-state-hang
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1343-drop-state-hang

Conversation

@ehartford

@ehartford ehartford commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1343, #1315, #1282.

Root cause: mir_drop_state_blocks_new (src/MirCore.w) set up a dense out-state row for every block: for _ in 0..bb_count * keys.len(): rows.push(MirDropState.Absent). Every visit then copied, joined and compared rows as wide as the full key set. In lldb on the hanging stage1, bb_count was 41,818 and keys.len() was 43,194. That is 1.8 G i32 cells (the 7.3 GB RSS in all three reports), plus O(blocks × keys) work per sweep. The body is pcre2's match_.

Why only stage1 showed it: this is not a seed miscompile. A stage1 embeds no .wo bundles, so the prelude's std.regex resolves std.re.* to the pcre2 source files and every program lowers all of pcre2 (hello.w: 428 bodies on stage1, 160 on the release binary). The release binary has the same defect on any body this large: with check lib/std/re/pcre2_match.w --validate-all on the installed compiler was still running when timeout killed it at 300 s.

Why it seemed to start with #1319: #1319's code is not involved. Before #1319, analyze failed earlier on hello world, so it never got to the drop-state driver. check --validate-all already hung on a stage1 built before #1319 (#1282, #1315).

Fix: states are stored as chunked, hash-consed rows of chunk ids. A map copies a chunk only when a write changes it. Joins of chunk pairs are memoized. The fixpoint loop reuses one scratch map. The algorithm and visit order are unchanged, and predecessors are still folded in CSR order, so every state is identical to the dense version.

Evidence (fresh :dev stage1):

  • analyze examples/hello.w audit:all: violations=0 ok, 17.7 s (before: never returned)
  • check examples/hello.w --validate-all: ok, 12.8 s, 472 MB peak (before: never returned, 7.3 GB). The bootstrap stage1 check --validate-all takes 15+ min at 7 GB RSS on a 20-line file (release binary: 0.07 s) #1315 repro passes validate-all and validate-ownership in 11.9 s.
  • Old stage1 (ae609cd) vs new stage1, both run with --link-bundle out/wo/pcre2 so the old one can finish: --dump-drop-state, --dump-drop-plan and --validate-all output is byte-identical on 101 files (every 12th test/behavior file plus examples/*.w), 303 of 303 comparisons.
  • --validate-all verdicts match out/release/bin/with on hello.w and five behavior tests.

Not run: :fixpoint and :test. This is a draft until the seed-driven battery has run and passed. The change affects drop elaboration (mir_elaborate_dead_drops uses the same driver), so it should be in a batch by itself and needs :drop-audit.

Fixes #1343
Fixes #1315
Fixes #1282

…ger needs blocks × keys cells (#1343, #1315, #1282)

Root cause. mir_drop_state_blocks_new (src/MirCore.w) allocated one dense
out-state row per block, `for _ in 0..bb_count * keys.len(): rows.push(...)`,
and every visit copied, joined and compared whole key-width rows
(load_block / input / equal / store_block). lldb on the hanging stage1
(`check hello.w --validate-all`, break after mir_drop_state_keys_new with
the condition bb_count > 10000) read bb_count = 0xa35a (41,818) and
keys.len() = 0xa8ba (43,194): 1.8 G i32 cells, the 7.3 GB RSS of every
report, then O(blocks × keys) work per sweep. The body is pcre2's `match_`
(`--dump-mir`: "body too large locals=39712 bbs=41818 stmts=45104").

Why only stage1. Not a miscompile: a stage1 embeds no .wo bundles, so the
prelude's std.regex resolves std.re.* to lib/std/re source and every program
lowers all of pcre2 (stage1 `--dump-mir` of hello.w: 428 bodies; the release
binary: 160). The release binary has the same defect on any body that size:
`with check lib/std/re/pcre2_match.w --validate-all` on the installed release
compiler ran for 300 s before timeout killed it.

Why #1319. #1319's code is not involved: before it, `analyze` on a stage1
failed earlier on hello world, so it never reached the driver; `check
--validate-all` already hung on stage1 before #1319 (#1282, #1315; reproduced
on a stage1 built before #1319 merged).

Fix. A state is stored in chunks of up to 128 keys, and stored chunks are
hash-consed in the key table's chunk store, so equal content always has one
id. A block row is one chunk id per chunk; a map copies a chunk only on the
first write that changes it; the join of two stored chunks is memoized on the
id pair; the fixpoint loop reuses one scratch map; the entry state is interned
once. The join is not associative, so input() still folds predecessors in CSR
order, chunk by chunk — the same computation per key, and the driver visits
blocks in the same order, so every state matches the dense version exactly.

Evidence (fresh :dev stage1):
- `analyze examples/hello.w audit:all`: facts=639634 violations=0 ok in 17.7 s
  (was: never returned).
- `check examples/hello.w --validate-all`: ok in 12.8 s, 472 MB peak (was:
  never returned, 7.3 GB); #1315's repro: validate-all and
  validate-ownership ok in 11.9 s.
- Old stage1 (ae609cd) vs this stage1, both with --link-bundle
  out/wo/pcre2 so the old one finishes, over 101 files (every 12th
  test/behavior file plus examples/*.w): --dump-drop-state,
  --dump-drop-plan and --validate-all output byte-identical, 303/303,
  including one ownership-verdict failure that both report the same way.
- validate-all verdicts match out/release/bin/with on hello.w and five
  behavior tests.

(cherry picked from commit 6a176f8)
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, alone per the isolation rule, on main dd786a4 → ef701be, force-pushed so the PR is the tested tree): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. #1360 (restoring .clone() here) waits for the seed bump (#1359).

@ehartford
ehartford force-pushed the fix-1343-drop-state-hang branch from 6a176f8 to ef701be Compare September 23, 2026 00:23
@ehartford
ehartford marked this pull request as ready for review September 23, 2026 00:23
@ehartford
ehartford merged commit 4d1b9e2 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1343-drop-state-hang branch September 24, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant