Skip to content

Three trees T6x-a: the statement stack on handles, cut after its probe - #344

Merged
jfrolich merged 6 commits into
ocaml-to-rust-rewritefrom
three-trees-t6x-a-design
Nov 17, 2026
Merged

jfrolich merged 6 commits into
ocaml-to-rust-rewritefrom
three-trees-t6x-a-design

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Design turn 6 of slice T6 of the backend rewrite (compiler-rust/docs/THREE_TREES.md, 11.4 and 11.9): the first slice of T6x, in which the statement stack of the lowering holds statement handles. It adds what the conversion lacks in the interface, writes the decisions into the document, and cuts the row into four build slices with their prompts. It converts nothing: no signature of the lowering changes here, and the default build runs none of this code.

What it adds

Function Body on rows Probe site that calls it Test
JsStmt::stmt_id(&self, a, &S) -> u32, O(1) the row out.rs note_empty_block and drop_empty_blocks; export.rs::build_rows a_statement_in_hand_gives_the_id_its_readers_know_it_by
JsStmt::if_head with the trait Runs and IfHead: rules 0 to 6 of J-S3 over two runs of the caller's stack; one step, plus what the runs release and take; a kept run is not scanned norm::simplify_if_head through a 16-byte adapter; the rules exist once cond.rs::if_in_place if_head_over_two_runs_is_rules_0_to_6_of_j_s3, if_head_costs_what_it_consumes_and_does_not_read_the_tail (1,000, 2,000, 10,000)
rows_stmt::S::row, #[cfg(test)], not in the trait the row the stack's test-only statements, called by 23 tests the stmt_id test

Tests for the decisions: the_actions_of_ten_thousand_switches_leave_only_the_slots_of_their_runs (out.rs), a_loop_function_over_a_list_is_the_one_over_a_block_in_fewer_steps and the_ids_of_ten_thousand_statements_of_a_block_cost_no_step (js_stmt_tests.rs).

The probe check

The probe's code (draft pull request 336) was put on this interface in a working copy that was not pushed, and its two gaps were closed with the functions above:

cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::lower
test result: ok. 565 passed; 0 failed; 0 ignored      (562 of trunk and the 3 interface tests that existed then)
cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::j
test result: ok. 58 passed; 0 failed; 0 ignored
cargo clippy --manifest-path compiler-rust/Cargo.toml --all-targets -- -D warnings      (clean)

Each of the four slices was also composed from the probe's commits and run alone on the slice before it: 562 passed after a1, a2 and a3; a4 is the probe's head.

The probe's script-tests job is red because of scripts/check_lowering_cost_docs.py (27 changed functions without a cost note, most of them the stack's, whose impl header changes). Every prompt names that script.

The cut of T6x-a (measured on the probe's commits)

Slice What Changed lines After
T6x-a1 StatementStack<B> owns B::S; every caller wraps at the call; the reads still give rows 511 (about 550 with cost notes) this pull request
T6x-a2 the statement-holding fields of one file; loop_function_body on lists; function_of_block deleted 154 (about 200 with the deletion) a1
T6x-a3 reads by reference, take(slot), Runs and if_head, the five signatures of cond.rs with all callers about 475 a2
T6x-a4 a switch's actions on the stack (take_actions); switch_output_nodes and ActionState::cached on handles about 210 a3

One after the other: out.rs or mod.rs is in every one.

Decisions for the review to confirm

  1. stmt_id as proposed by the probe; on the builder it is the node_id of the owned node.
  2. if_head takes the stack through Runs, and its body on rows calls norm::simplify_if_head through an adapter instead of moving the rules: js3_simplify_if runs the same head, so IfLists and simplify_if_head stay in norm.rs until T6s; T6x-a3 deletes the lowering's implementation of IfLists.
  3. The stack's reads return references (with the slot where the caller takes), not ids.
  4. held is Vec<Option<B::S>>, 8 bytes an entry on rows: accepted. The size on the builder was not measured.
  5. Six signatures change in T6x-a with all their callers (five in a3, loop_function_body in a2); the rule of the cut is rewritten and T6x-d loses five rows.
  6. single_assignment returns the slot only: no caller reads the value (checked on the copy).
  7. Action lists go over the stack and are taken with a new take_actions (no close: an action is a block). untagged.rs reserves holes, which leaves the last clause run as a dead range. Staging in switch_output_nodes was tried and fails five allocation tests.
  8. T6x-a1 keeps the probe's peek (three transition calls in the stack) and T6x-a3 deletes it.
  9. function_of_block is deleted by T6x-a2, not T6x-f; clear, statements_rev and terminal by T6x-a3; norm::jm14_push_effect_expression gets #[cfg(test)] in T6x-f.
  10. T6x-a1 is accepted at about 550 lines.
  11. The 23 tests that release a run row by row keep their text through a test-only statements on StatementStack<Rows>.
  12. St::Empty was added to a test helper enum; two existing tests gained a match arm, no expected value changed.

Gates on this branch

cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::lower     568 passed; 0 failed; 0 ignored
cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::j         58 passed; 0 failed; 0 ignored
cargo clippy --manifest-path compiler-rust/Cargo.toml --all-targets -- -D warnings     clean
RUSTDOCFLAGS="-D warnings" cargo doc --locked --manifest-path compiler-rust/Cargo.toml --all-features --no-deps     clean
python3 scripts/check_lowering_cost_docs.py --base origin/ocaml-to-rust-rewrite
cost docs: production functions=536; changed/new=0; missing/generic=0; removed=0

size_of::<Rows>() stays 416; the allocation counts of unit.rs are untouched (86, 103, 86, 156).

Not run, not done

  • On the probe copy: the JavaScript, standard-library and genType parity suites, Backend bridge coverage, Instruction count, check mode on the application.
  • The commit orders inside the prompts of a3 and a4 were not built commit by commit; the slices as wholes were.
  • Not done from the small items: a --self-test for scripts/count_lowering_ids.py, and the count of merged guards in the guard-chain test of tail.rs.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 5 commits November 17, 2026 20:19
…over two runs

What the statement stack on handles (three trees, T6x-a) lacks in the
interface, found by its probe conversion.

Before: a statement in flight could be read for its kind, its value and
its location, not for its id, so a stack that owns handles turned each
statement into a row and back to key its empty-block table or to give
the top-level rows their ids. `norm::simplify_if_head` was called by the
lowering directly, over an implementation of `norm::IfLists` by the
statement stack that hands out rows.

After:
- `JsStmt::stmt_id(&self, a, &S) -> u32`: the id of the statement, read
  in place. O(1); on rows the row.
- `JsStmt::if_head(a, runs, &mut test, &mut yes, &mut no)` with the
  trait `Runs<B>` (`trim`, `empty`, `last`, `only`, `release`, `take`)
  and the result `IfHead<S, Run>`: rules 0 to 6 of J-S3 over two runs
  the caller holds. On rows it is `norm::simplify_if_head`, the head
  `js3_simplify_if` runs over pool lists, reached through an adapter
  that gives the rule each function of `Runs`: the rules exist once.
  One step a call; the runs count what they release and take; a kept
  run is not scanned.

Nothing in the lowering calls either yet: the build slices of T6x-a do.

Tests (`--lib bir::lower::js_stmt`, 25 passed):
- the id is what `owner`, `kind_of` and `block_ids` know the statement
  by, and reading it counts no step;
- `if_head` and what follows it give the tree `js3_simplify_if` builds,
  for 11 tests and every pair of 10 branch shapes, empty blocks among
  them;
- the steps of `if_head` at 1,000, 2,000 and 10,000 statements follow
  the run it consumes and are the same whatever the length of the run
  it keeps, which stays whole.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-f1c2bf71-2bd2-7807-9319-0fbabb12f9b7
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…loop function over a list

Three additions under test modules; no production line of the lowering
changes.

- `rows_stmt::S::row` (`#[cfg(test)]`): the row of a statement on rows,
  for a test that reads the arena beside the handle. The statement
  stack's test-only reader of a run's rows is built on it when the
  stack holds handles, so the 23 tests that release a run row by row
  keep their text.
- `out.rs`, `the_actions_of_ten_thousand_switches_leave_only_the_slots_of_their_runs`:
  the slot protocol the probe gave `untagged.rs` (the actions reserved
  above the clause runs, filled as each run is taken, taken as one
  list), run on today's stack. A switch leaves its statement and the
  slots its runs had, at 1,000 and at 10,000 switches; a read from the
  end of the run crosses the dead ranges of the last switch only.
- `js_stmt_tests.rs`, `a_loop_function_over_a_list_is_the_one_over_a_block_in_fewer_steps`:
  the body of a tail-loop function kept as a list gives the same
  function as today's block, in `statements + 3` fewer steps (at 1,000
  and 10,000).

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-f1c2bf71-2bd2-7807-9319-0fbabb12f9b7
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
…ur slices

The probe of row T6x-a (the statement stack on handles) came to 1,093
changed lines against 500 estimated and left four questions open. This
writes the answers into 11.4 and cuts the row.

New subsection "T6x-a: the statement stack, after the probe":
- what was added to the interface, with the probe site that calls each
  function, and how it was checked (the probe's code on this
  interface: 565 and 58 passed, clippy clean);
- (a) the stack's reads return references, `take(slot)` for the seven
  sites that consumed a statement the stack still held, the list of the
  stack's functions with their costs, and `held` as a vector of options;
- (b) six signatures change in T6x-a with all their callers, and the
  rule of the cut is rewritten for it;
- (c) a switch's actions are staged on the stack and taken with
  `take_actions` (no close); `untagged.rs` reserves holes, and what the
  dead range costs; the alternative that was tried and fails five
  allocation tests;
- (d) `loop_function_body` on lists saves n + 3 steps a function, and
  which slice deletes each function that loses its last caller;
- (e) the changes to earlier sections;
- the cut, measured on the probe's commits: a1 511, a2 154, a3 about
  475, a4 about 210 changed lines, one after the other;
- the counts of `scripts/count_lowering_ids.py` on trunk and after.

"T6x: the cut": the row of T6x-a becomes four rows; T6x-d loses five
rows of its table; T6x-f no longer deletes `function_of_block`; the
figures of b, c3, c4, d and f are marked as estimates.
"T6: the interface" (a): the read-back table names `kind_at`, which is
what the trait has.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-f1c2bf71-2bd2-7807-9319-0fbabb12f9b7
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Section 11.9 gets a common block for the four slices of T6x-a and one
verbatim prompt each. A prompt names the trunk it starts from, its
first edit, its commits in order, the end state as searches, the tests
it may touch only to compile, the cost-note check (the reason the
probe's `script-tests` job is red), its gates, and how the probe's code
may be read.

Also from the probe of the next slice: the read-back table says that a
read by reference of a list's statement is open for the design turn of
T6x-b, and the row of T6x-b is marked to be re-cut by that turn (838
changed lines measured against about 450 estimated).

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-f1c2bf71-2bd2-7807-9319-0fbabb12f9b7
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Owed by the review of the previous design turn: `block_ids` is a loop
over the statements of a block and had no test at size. At 1,000 and at
10,000 statements it gives one id per statement, in order, and leaves
`steps` where it was.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-f1c2bf71-2bd2-7807-9319-0fbabb12f9b7
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich

Copy link
Copy Markdown
Owner Author

First-pass checklist review at 51066a4 (a checklist for the final reviewer, not an approval).

Done on the host with git and gh only: git fetch origin; D=origin/ocaml-to-rust-rewrite...origin/three-trees-t6x-a-design; git diff $D > pr.diff (1,714 lines). Branch files were read with git show origin/three-trees-t6x-a-design:PATH and the probe's files with git show origin/bir-lower-js-interface-2:PATH. No cargo, build or test was run. Line numbers are from the branch.

1. Scope

  • K1 (the two functions the probe lacked):
    • JsStmt::stmt_id: js_stmt.rs:140, body at rows_stmt.rs:135.
    • JsStmt::if_head: js_stmt.rs:320, body at rows_stmt.rs:391.
    • Runs (js_stmt.rs:62), IfHead (js_stmt.rs:89) and the adapter RunLists (rows_stmt.rs:426-462).
  • K2: the new subsection "T6x-a: the statement stack, after the probe" at THREE_TREES.md:4306-4604. It sits in 11.4 after "T6x: the cut".
  • K3: the common block and the four prompts in 11.9, at THREE_TREES.md:6259-6852.
  • K4: the_ids_of_ten_thousand_statements_of_a_block_cost_no_step (js_stmt_tests.rs:1197), commit 51066a4.
  • Done beyond the four items, all supporting the design:
    • #[cfg(test)] S::row (rows_stmt.rs:83).
    • St::Empty and the shape helper.
    • The decision tests in out.rs:668 and js_stmt_tests.rs:1068.
    • Rewrites of earlier document text: the read-back table at 3197, the "T6x: the cut" status, and the T6x table rows a1 to a4, b, c3, c4, d and f.
  • Not done, per the body:
    • The parity suites, Backend bridge coverage, Instruction count and check mode were not run on the probe copy.
    • The commit orders inside the a3 and a4 prompts were not built commit by commit.
    • A --self-test for count_lowering_ids.py, and the guard count in tail.rs.

PASS

2. Default path

Not applicable: design pull request. No changed function runs in an ordinary build:

  • stmt_id and if_head are new trait items whose only caller is a test (git grep -n 'if_head(\|stmt_id(' origin/three-trees-t6x-a-design -- compiler-rust/src/bir/lower hits only js_stmt_tests.rs and doc text).
  • out.rs changes only inside mod tests (from line 549).
  • The Instruction count compare job reads change | -267,042 (-0.006%) (section 19).

PASS

3. Engineering standard

All greps run over the added lines of pr.diff, from line 982 on (the code files).

  • (a) Cost in doc comments.

    • Each new trait item states its cost: stmt_id "O(1) … one load"; if_head "Its cost: …"; each Runs item has one; IfHead "16 bytes"; RunLists "16 bytes, made once per JsStmt::if_head".
    • The bodies on rows (stmt_id, if_head) and the six RunLists methods have no doc comment of their own. They rely on the trait docs.
    • In CI, check_lowering_cost_docs.py read production functions=536; changed/new=0; missing/generic=0; removed=0, so the checker does not count these trait items and impl methods. PASS, with that note.
  • (b) Step test at 10,000. No new production loop or recursion: if_head on rows has no loop, and the loop of simplify_if_head is in norm.rs, which is untouched. if_head_costs_what_it_consumes_and_does_not_read_the_tail runs 1,000, 2,000 and 10,000 (section 14). PASS

  • (c) HashMap, HashSet or BTreeMap: none (the grep is empty). PASS

  • (d) Allocation in a walk outside tests: none. All ten hits are in tests:

    • js_stmt_tests.rs: diff lines 1305, 1328, 1360, 1381, 1394, 1517, 1547, 1564 and 1583 (vec!, String::new, Vec::new, collect).
    • out.rs mod tests: diff line 1671 (collect into Vec<Span>).

    PASS

  • (e) Lock, atomic, thread_local or RefCell: none. PASS

  • (f) Second pass over a tree: none in production. PASS

  • (g) size_of tests.

    • IfHead<S, Span> is pinned at 16 (js_stmt_tests.rs:162).
    • RunLists (two &mut, documented at 16 bytes) has no size test. It is made once per call and not stored per node.

    PASS, with that note.

  • (h) allow(, unsafe, todo!, unimplemented!, #[ignore]: none. PASS

4. Tests and oracles

  • git diff --quiet $D -- on compiler-rust/tests/backend_coverage.txt, printer_coverage.json, tc_coverage.txt, scripts and .github is quiet for all five: untouched.
  • Existing tests with changed text:
    • sizes: one assertion added, size_of::<IfHead<S, Span>>() == 16.
    • a_list_is_the_pool_list_of_its_statements and a_guard_is_taken_apart_and_rebuilt_over_its_blocks: each gains the arm St::Empty => Kind::Block. That arm is unreachable there: SHAPES (js_stmt_tests.rs:153) has no Empty.
    • The use line imports IfHead and Runs.
  • No expected value changed. Note that the body's decision 12 does not mention the assertion added to sizes.

PASS

5. New scripts and fixtures

Not applicable: no script, fixture or workflow was added. PASS

6. Privacy

The grep tella|submodule\.nl|rescript-bench-corpus|/Users/|opensession|jake over pr.diff exits 1: no hit. The commit messages carry only the Session trailer and the bot sign-off, which are not findings. PASS

7. Changelog

  • changes/three-trees-t6x-a-design.internal.md exists and starts with "- Three trees T6x-a, design: …".
  • git diff --quiet $D -- CHANGELOG.md is quiet: untouched.

PASS

8. Numbers

  • Shown with their commands: 568/58 passed, clippy, cargo doc, the cost-doc line, and 565/58 on the probe copy. They are pasted output, not rerun here.
  • Checked here:
    • 568 = 562 + 6. The diff adds six #[test]: five in js_stmt_tests.rs, one in out.rs. The #[test] count under bir/lower goes from 441 on trunk to 447 on the branch.
    • The probe's 1,093: git diff --shortstat reads 657 + 436.
    • a1's 511: the first probe commit 32f05a6 is 324 + 188 = 512, including the 1-line changes file.
    • a2's 154: the probe commits on args, loops, try_, rec, tail, prim and expr are 24 + 29 + 17 + 20 + 6 + 7 + 51 = 154.
  • Not reproducible:
    • a3 "about 475" and a4 "about 210" were measured on a working copy that was not pushed.
    • The count_lowering_ids totals are given with the script name but without output.
    • size_of::<Rows>() == 416 is stated with no command.
    • "Five allocation tests fail" for the staging alternative is stated with no output.

UNCLEAR (a3, a4 and the alternative's failures cannot be checked)

9. Focused tests

Not run (host review). CI compiler-tests passed at the head SHA.

10. Where to look hardest

  1. The a3 commit order (section 17d). Commit 1 deletes peek, and with it last_row and only_row. Commit 1 also gives the reads &B::S. But the IfLists adapter in cond.rs that a1 creates (the probe's StackLists, probe cond.rs:69-95) gets rows from last_row/only_row, and it is only deleted in commit 2. Commit 1 therefore cannot compile as written. The only workaround, NodeId(stmt_id), is a listed failure ("stmt_id used to find a statement again").

  2. The cost sentence of if_head (js_stmt.rs:316, repeated in the 11.4 table) says "plus one per negation stripped". Nothing counts that step:

    • Rows' if_head adds 1 (rows_stmt.rs:399).
    • RunLists counts nothing.
    • norm::simplify_if_head takes no step counter.

    The module table in rows_stmt.rs says only "if_head one". The same doc says "of a run that is kept, at most Runs::last and Runs::only are asked". In fact simplify_if_head also calls trim on both runs, the kept one included (norm.rs:722-723).

  3. The untagged layout change in a4. It is the one slice that moves statements on the stack, and its parity was never run. Also, a run whose last statement is a switch ends in dead clause ranges, so Runs::last on it is not O(1). The out.rs test measures (n+1)·w scanned slots. The Slots test double cannot show this, because its last counts one touch however many slots it scans.

11. Files

git diff --stat $D:

 changes/three-trees-t6x-a-design.internal.md |   3 +
 compiler-rust/docs/THREE_TREES.md            | 932 ++++++++++++++++++++++++++-
 compiler-rust/src/bir/j/rows_stmt.rs         |  73 ++-
 compiler-rust/src/bir/lower/js_stmt.rs       |  82 ++-
 compiler-rust/src/bir/lower/js_stmt_tests.rs | 344 +++++++++-
 compiler-rust/src/bir/lower/out.rs           |  68 ++
 6 files changed, 1480 insertions(+), 22 deletions(-)

Every file is under compiler-rust/src/bir, compiler-rust/docs or changes/.

  • out.rs: one hunk, @@ -610,6 +610,74 @@ mod tests {, below #[cfg(test)] mod tests at out.rs:549-550. No production line.
  • rows_stmt.rs: hunks @@ -33,6 +33,7 @@, @@ -44,6 +45,7 @@, @@ -58,12 +60,13 @@, @@ -73,6 +76,15 @@, @@ -119,6 +131,11 @@ impl JsStmt for Rows, @@ -371,6 +388,19 @@, @@ -389,6 +419,43 @@. Production lines in them:
    • Module-doc table rows for if_head and stmt_id, and the steps paragraph.
    • The import gains IfHead and Runs.
    • The stmt_id body (new trait item).
    • The if_head body (new trait item).
    • struct RunLists and impl norm::IfLists for RunLists (new).
    • S::row is under #[cfg(test)].
  • No existing signature of the lowering changed: js_stmt.rs only adds items.
  • norm.rs is untouched (git diff --quiet origin/ocaml-to-rust-rewrite origin/three-trees-t6x-a-design -- compiler-rust/src/bir/j/norm.rs printed norm-untouched).

PASS

12. JsStmt::stmt_id

Trait item, js_stmt.rs:132-140:

/// The id of the statement, read in place: what [`Js::owner`] gives for
/// its value, what [`JsStmt::block_ids`] gives for it once a block
/// holds it, and a key for [`Js::kind_of`]. ... O(1): on
/// rows the row; on the builder the `node_id` of the owned node, one
/// load. ...
fn stmt_id(&self, a: &Pools, s: &Self::S) -> u32;

Body on rows (rows_stmt.rs:134-137):

#[inline]
fn stmt_id(&self, _: &Pools, s: &S) -> u32 { s.0.0 }

The cost is stated as O(1) and the body counts no step.

The test a_statement_in_hand_gives_the_id_its_readers_know_it_by (js_stmt_tests.rs:1217) checks:

  • steps is unchanged by the reads.
  • kind_of(id) == stmt_kind(s).
  • owner(id(value)) == Some(id).
  • Reading twice gives the same id, and the ids are unique.
  • block_ids of a block gives the same ids.
  • block.row().0 == stmt_id(&block).

So it compares against the node id of the row, which is what readers already use. PASS

13. if_head, Runs, IfHead

All three are quoted in pr.diff lines 1125-1222. They are summarised here.

  • (a) The body calls norm::simplify_if_head(a, &mut lists, &mut test.0, yes, no)? (rows_stmt.rs:391-403) and wraps the result. No rule is copied. PASS

  • (b) RunLists { rows: &mut Rows, runs: &mut R } is two references, 16 bytes as documented. No size test pins it (section 3g). It is made on the stack once per call.

  • (c) Each IfLists method maps to one Runs method:

    IfLists Runs
    trim runs.trim(rows, b, run)
    empty runs.empty() (the pools are ignored)
    last runs.last(run).map(|s| s.0)
    only runs.only(run).map(|s| s.0)
    release runs.release(rows, b, run)
    take runs.take(rows, b, run)

    simplify_if_head asks of the retained tail last, only and trim (norm.rs:722-723 trims both runs before the loop). The doc's "at most Runs::last and Runs::only are asked" of a kept run is therefore inexact. trim is bounded by the noted empty blocks, so this is not a scan, but it does release the tail's empty blocks.

  • (d) Steps are counted in two places: rows if_head adds 1, and the Runs implementation counts its release and take. The trait doc's "plus one per negation stripped" has no counter anywhere (section 10.2). FAIL (doc and code disagree)

  • (e) Per call nothing allocates a Vec, collects or clones. The rule's own b.lists.push(&[ret]) and b.lists.push(&[]) in rule 5 (norm.rs) are pool growth and existed before. No test measures allocations of if_head.

FAIL (on d), the rest PASS.

14. Tests of if_head

if_head_over_two_runs_is_rules_0_to_6_of_j_s3 runs 11 VALUES × 10 HEAD_SHAPES × 10 HEAD_SHAPES. Rules exercised:

Rule Exercised by
0 (empty blocks) the shapes containing St::Empty ([Empty], [Empty, Return], [Call, Empty], [Empty, Call, Empty, Break, Empty])
1 (boolean test) True, False
2 (!c, else not empty) NotX
3 (then empty) [] against [], with a pure test (X, Int) and an impure one (Call, CallThenCall, …); and [] against a non-empty else
4 (two lone returns) [Return], and [Empty, Return] after trim
5 (lone return in else) [Return] as else against [Call] and others
6 (then ends in a jump) then ending in Return, Break or Throw
  • Not exercised: continue in rule 6, since St has no Continue variant.
  • What it is compared against: shape of the in-place result against shape of norm::js3_simplify_if on pool lists, built in the same arena. Both sides run the same simplify_if_head, so this checks the adapter and the stack protocol, not the rules themselves.
  • It also asserts heads > 600 && heads < 1210.

if_head_costs_what_it_consumes_and_does_not_read_the_tail uses the sizes [1_000, 2_000, 10_000] and asserts:

  • Guard: guard[2].0 - guard[0].0 == 9 * (guard[1].0 - guard[0].0) and guard[1].0 - guard[0].0 == 1_000.
  • Tail: assert_eq!((cost.0, cost.1), (tail[0].0, tail[0].1)) for every tail size, cost.2 == size, and tail[0].1 < 16.
  • The same constant-cost form for rule 5 (lone) and for kept; dropped is linear.

"Tail unread" is shown by touched, the statements handed out, staying constant.

The test double can hide faults the real stack would show:

  • Slots::last scans with iter().rev().find_map and counts only 1 touch.
  • only iterates with flatten.
  • trim scans every noted empty block linearly, not with a binary search.

So a scanning last or only on the real stack would not be caught. The real stack's costs are only tested once a3 implements Runs. Also, steps here counts Rows' steps, which do not include the stack's scanned.

UNCLEAR (the oracle is the same rule code; rule 6 continue is not covered)

15. Decision tests

out.rs:668 asserts:

assert_eq!((thousand, ten_thousand), (1_000 * per_switch, 10_000 * per_switch));
assert_eq!(scanned, scanned_thousand);
assert_eq!(scanned, ((clauses + 1) * width) as usize);

Inside the helper, per switch: assert_eq!(stack.mark().start, before + 1 + (clauses + 1) * width);. At the end: stack.mark() == Span::default().

How it compares with the probe (untagged.rs:900-990 on bir-lower-js-interface-2):

  • The probe reserves action_count holes above the clause runs (lines 932-936).
  • It fills clause i (line 966), then the default (take_list of default.run, then fill).
  • It takes since(actions) and fills the switch at start.start, a slot reserved before the runs.

The test models the same layout: head reserved, default run first, clause runs, holes, clauses filled first and the default last. It does not model has_default == false (the probe kills the default run instead), nor the single-case typeof path.

The bound is exact: 1 + (n+1)·w slots per switch (9 here), 90,000 at 10,000 switches, all popped by the enclosing take.

js_stmt_tests.rs:1068 asserts:

assert_eq!(loop_function(3, true).1, loop_function(3, false).1);
assert_eq!(ten_thousand - thousand, 9_000, ...);
assert_eq!(thousand, 1_000 + 3);

The tree equality is checked at n = 3 only.

js_stmt_tests.rs:1197 asserts:

  • r.steps() == before after block_ids.
  • The ids are strictly increasing, and each has kind_of == Kind::Exp.
  • (given(1_000), given(10_000)) == (1_000, 10_000).

Existing tests: see section 4. Two gained an unreachable match arm, sizes gained one assertion, and no expected value changed.

PASS

16. The document

Each of the body's twelve decisions is in the subsection:

  1. The stmt_id table row.
  2. The adapter paragraph after the table.
  3. Paragraph (a), first sentence.
  4. "Sizes", where held is Vec<Option<B::S>> at 8 bytes, accepted.
  5. Table (b).
  6. single_assignment "(no caller reads the value)".
  7. (c), with "Not chosen".
  8. "Gone: … peek, which T6x-a1 adds and T6x-a3 deletes".
  9. The deletion table.
  10. "a1 is accepted at about 550 lines".
  11. The test-only statements row and the 23 tests.
  12. (e), last bullet.

The rewritten rule (4445-4450):

a function that takes or returns a statement or a statement list changes its signature in the slice of T6x-a that changes what the stack gives or takes there, together with all its callers; a function of the callers' table that takes or returns an expression keeps its signature until T6x-d, and a c slice wraps at those calls.

The T6x table rows are in pr.diff lines 34-75:

  • a1: 511 / about 550.
  • a2: 154 / about 200.
  • a3 names place, if_statement, unnest, single_assignment and assignment_statement, with all callers.
  • a4: about 210.
  • b: "to be re-cut by its own design turn (probe: pull request 345)".
  • d: goes from 36 rows to 31. It loses place, unnest, single_assignment, loop_function_body and locate_switch_run, and keeps if_statement, assignment_statement and merge_assignments for their expression.

So place, unnest and single_assignment left T6x-d. if_statement and assignment_statement stay in T6x-d for their expression argument, and are also named in the a3 row for their statement result. locate_switch_run leaves d for a reason unrelated to T6x-a: its signature names a source node only.

The read-back table (line 3197) now reads kind_at(a, l, i) and adds "(a read by reference of a list's statement is open: the design turn of T6x-b)". row_at is still named at THREE_TREES.md:2570-2571 (Builder::row_at in the T6 design table) and at 3600 (an older correction). These are not the read-back table.

PASS

17. The four build prompts

a1

  • (a) Starts from trunk with this PR merged. The precondition greps for stmt_id, if_head and row, and requires 568/58.
  • (b) The first edit is enum Slot<S> and StatementStack<B: JsStmt> in out.rs.
  • (c) End state:
    • struct StatementStack<B: JsStmt>: 1 line.
    • B::stmt_node(/stmt_from_node( above tests: 3 matches, all inside peek.
    • B::list_id(: 1 match, in take_list.
    • (push|fill)\(B::stmt_node\(: empty.
    • bir\.(view|release)\( in out.rs: empty.
    • The totals line | total | 15647 | 213 | 108 | 621 | … |.
    • 568/58.
  • (d) Commit 1 is the stack with all callers (it cannot be split), commit 2 the changes file. Commit 1 compiles alone.

a2

  • (a) Starts after a1; the precondition is the StatementStack<B: JsStmt> grep.
  • (b) The first edit is the field types of args, loops, try_, rec and tail.
  • (c) End state: function_of_block grep empty; the totals line 15661 | 209 | …; 566/58.
  • (d) Commits in order: fields, hold, loop_function_body, deletion of function_of_block, changes file. Each compiles alone: the deletion follows the removal of its only caller (trunk expr.rs:297), and commit 1 keeps the loops.rs wrap for if_statement.

a3

  • (a) Starts after a2; the precondition is held_mark present and function_of_block absent.

  • (b) The first edit is the reads by reference in out.rs.

  • (c) End state:

    • fn (peek|clear|statements_rev|terminal|only_row|last_row): empty.
    • IfLists|simplify_if_head outside tests: 1 line, in close.rs.
    • stmt_node/stmt_from_node: none in out.rs, 2 test helpers in cond.rs.
    • The five signatures.
    • 566/58.
  • (d) Commit 1 cannot compile alone. It deletes peek and makes last/only return &B::S, while the a1 IfLists adapter in cond.rs, which returns Option<NodeId> through last_row/only_row, survives until commit 2. The fix is for commits 1 and 2 to be one commit, or for commit 1 to keep the adapter's two rows.

    The five signatures are in one commit together with their callers in exit, loops, switch, untagged and variant. That is coherent: if_statement -> B::S cannot give a row once peek is gone.

a4

  • (a) Starts after a3; the precondition is the impl … Runs<B> for StatementStack<B> grep.
  • (b) The first edit is switch_output_nodes: Vec<Option<B::S>> and ActionState<S>.
  • (c) End state:
    • lists\.(set|truncate|push_iter)\( in the four files: empty.
    • take_actions\(: the definition, the call in take_list, and 4 calls.
    • stmt_node/stmt_from_node outside tests: cond.rs 2, export.rs 5, mod.rs 4.
    • The totals line 15689 | 191 | …; 566/58.
  • (d) Commit 1 converts the fields and turns actions back with B::stmt_node where they enter the reserved pool list (exit.rs:428-444, switch.rs:801/820, untagged's set). It needs Pool::get_ref, which is not on trunk (git grep -n 'fn get_ref' origin/ocaml-to-rust-rewrite -- compiler-rust/src/bir is empty; the probe adds it at untagged.rs:114), and copy_dispatch_output on &B::S. Nothing found here forces a break, but it was not built alone: UNCLEAR.

(e) Deletions. Each was checked with git grep -n '\bNAME\b' origin/ocaml-to-rust-rewrite -- compiler-rust/src.

Deleted Callers on trunk Covered by the prompt?
function_of_block expr.rs:297 (a2's file); test js_stmt_tests.rs:1003; this PR's loop_function yes: both tests are deleted in a2's commit 4
clear (the stack's) cond.rs:519, 532, 821 only (a3's file); no test yes
statements_rev cond.rs:866 only yes
terminal cond.rs:664 only; no test yes
peek not on trunk (a1 adds it) yes
lowering's impl IfLists cond.rs:67/479; norm_tests.rs:2084 reads only the text of norm.rs; oracle_tests.rs:1044 names simplify_if_head only in a test name and a comment yes
production statements 4 production callers (cond.rs:395, cond.rs:848, export.rs:350, mod.rs:772), all in a3's files; 23 test calls in 19 files (args, call ×2, exit, expr, external, import, jsx, konst ×2, label, loops, oracle_tests, prim ×2, prim_block, rec, record, switch, tail, untagged ×2, variant) yes: they are kept by the #[cfg(test)] statements on StatementStack<Rows>; 23 and 19 match the doc

No deletion leaves an uncovered caller.

  • (f) Targets and counts. The targets are only the standing gates: Instruction count +0.02% and check mode +0.1% (reported, not steered). The count_lowering_ids lines are pasted and explained, not targets. The counts add up:
    • 568 = 562 + 6 (this PR).
    • 566 = 568 − 2 (a2 commit 4 deletes a_function_over_a_block_is_the_function_over_its_list and a_loop_function_over_a_list_is_the_one_over_a_block_in_fewer_steps).
    • a3 and a4 delete no test: 566.
    • The doc's "562 passed after each" (4326) is for the probe compositions, which do not have this PR's tests or the a2 deletion (the probe keeps function_of_block).
  • (g) The common block names check_lowering_cost_docs.py, clippy -D warnings, cargo doc with -D warnings, Backend bridge coverage and the allocation counts 86/103/86/156. All four prompts include it ("with both common blocks").
  • (h) The probe may be read with git show origin/bir-lower-js-interface-2:…/FILE. "Merge nothing and cherry-pick nothing from that branch."
  • (i) a2 requires Backend bridge coverage at zero, read in CI before the PR is marked ready. a4 requires bridge coverage plus the JavaScript, standard-library and genType parity suites, read in CI before ready, and to stop on the first difference.

FAIL (a3 commit 1 does not compile as ordered; a4 commit 1 is unverified)

18. The cut's numbers

  • The document says only "each slice is the probe's commits of its files applied on the slice before, and for a3 and a4 the copy with the gaps closed". It names no commit SHAs and gives no shortstat command.

  • a1 and a2 reproduce from the probe's commits (section 8). a3 and a4 do not: the copy was never pushed.

  • The probe total, 1,093, checks.

  • Files per slice are as listed in the 11.4 table.

  • Functions edited by two slices:

    • out.rs take_list: a1 and a4.
    • out.rs drop_empty_blocks, take_run and the reads (first, last, only): a1 and a3.
    • exit.rs exit_dispatch: a3 (if_statement, trunk exit.rs:422) and a4 (the actions, 428-444).
    • untagged.rs untagged_clause: a3 (the statements.last read at trunk untagged.rs:842) and a4 (the return type becomes B::S).
    • The cond.rs wraps of a1 are rewritten in a3; the document notes that 39 of 80 wraps cancel.

    The slices are sequential, so there is no conflict, but the reviewer should expect rework of the same lines.

UNCLEAR

19. CI

gh pr checks 344 -R jfrolich/rescript-rust: 23 pass, 0 fail. These include script-tests, Backend bridge coverage, the parity suites, compiler-tests and the base/head/compare jobs.

The Instruction count run is 44173718910 at head 51066a4; its compare job is 134363517474:

| base | 4,135,388,542 | ... | 788 |
| head | 4,135,121,500 | ... | 788 |
| change | -267,042 (-0.006%) | | |

script-tests (job 134359518820) ran check_lowering_cost_docs.py --self-test and printed cost docs: production functions=536; changed/new=0; missing/generic=0; removed=0.

The earlier runs at dffe63e, 490075c and 152498d were cancelled when newer commits were pushed.

PASS

20. Privacy and hygiene

Same as sections 6 and 3h over the whole diff: no hits, no unsafe or allow(, and CHANGELOG.md is untouched. The PR body names "draft pull request 336" and "pull request 345" (repository-internal) and the attribution footer only. PASS

…he commit order of T6x-a3

The cost sentence of JsStmt::if_head named a step per negation stripped
that nothing counts, and said a kept run is asked last and only alone,
where the rule also drops the empty blocks the run noted. The prompt of
T6x-a3 had the reads by reference and if_head as two commits: the first
deletes what the adapter of the second's file reads, so they are one.
The prompt of T6x-a4 says what to do if its first commit does not
compile on its own.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich

Copy link
Copy Markdown
Owner Author

Coordinator review, final, at c1b7779 (the author did not write this review). I read the first-pass checklist above whole, every changed line of js_stmt.rs, rows_stmt.rs and out.rs, norm::simplify_if_head beside the new body, the twelve decisions, and the four build prompts whole.

Verdict: merge, with one reviewer commit (c1b7779) for the two findings below.

What it adds. JsStmt::stmt_id (the row on rows, no step) and JsStmt::if_head with the trait Runs and IfHead. The body on rows is norm::simplify_if_head behind a two-reference adapter: the rules exist once, and norm.rs is untouched. out.rs changes below mod tests only; no signature of the lowering changes; the default build runs none of it (CI Instruction count −0.006% at the designer's head).

The two findings of the first pass, both upheld and corrected in c1b7779.

  1. The cost sentence of if_head named a step per negation stripped that nothing counts, and said a kept run is asked last and only alone, where the rule first drops the empty blocks the run noted (trim, on both runs). The doc comment and the row of the 11.4 table now say what the code does: one step, what the runs count, no step for a negation; a kept run is trimmed and asked last and only.
  2. The commit order of T6x-a3. Its first commit deletes peek, last_row and only_row and makes the reads return references; the adapter that T6x-a1 leaves in cond.rs gives rows through exactly those, and the prompt removed it only in its second commit. The two are one commit now. The prompt of T6x-a4 says what to do if its first commit, which the design turn did not build alone, does not compile on its own.

The twelve decisions.

  1. stmt_id as the probe proposed: confirmed.
  2. if_head through an adapter, rules not moved; IfLists and simplify_if_head stay in norm.rs until T6s because js3_simplify_if runs the same head: confirmed. T6x-a3 deletes the lowering's own implementation.
  3. The stack's reads return references, with the slot where the caller takes: confirmed.
  4. held as Vec<Option<B::S>>, 8 bytes an entry on rows: accepted. The size on the builder is T6v's to pin.
  5. Six signatures change in T6x-a with all their callers (five in a3, loop_function_body in a2), and the rule of the cut is rewritten: confirmed. if_statement and assignment_statement stay in T6x-d's table for their expression argument.
  6. single_assignment returns the slot only: confirmed.
  7. Action lists over the stack, taken with take_actions; untagged.rs reserves holes: accepted. What it costs is stated by the test: a switch leaves one slot and the slots of its clause runs until the enclosing run is taken, and a read of the last statement of a run that ends in a switch crosses those slots ((n+1)·w where trunk crosses n·w). Same order as today; a dead range that is skipped in one step is a possible later change, not part of this row. This is the one place where the slot layout moves and no parity suite has run: T6x-a4's gate is Backend bridge coverage and the three parity suites, read in CI before it is marked ready.
  8. T6x-a1 keeps peek (three transition calls, a placeholder for the length of one read) and T6x-a3 deletes it: accepted as a transition of two slices.
  9. Who deletes what (function_of_block in a2; clear, statements_rev, terminal in a3; jm14_push_effect_expression under #[cfg(test)] in T6x-f): confirmed; the first pass checked every caller on trunk, tests included.
  10. T6x-a1 at about 550 lines: accepted; the type cannot change without its callers.
  11. The 23 tests keep their text through a test-only statements: confirmed.
  12. St::Empty in a test helper, two match arms, no expected value changed: confirmed.

On the tests of if_head (first pass, section 14): right that the comparison against js3_simplify_if checks the adapter and the protocol of the runs, not the rules, since both sides run one rule; that is what this function is, and the rules have their own tests in norm_tests.rs. Right also that the test stack cannot show a scanning last on the real one: the real stack's cost is shown by the tests of cond.rs and out.rs (scanned) once T6x-a3 implements Runs, and its prompt keeps those tests unchanged. Rule 6 with continue has no case: owed, with the first slice that touches the test.

The cut. a1 511 and a2 154 reproduce from the probe's commits; a3 (about 475) and a4 (about 210) were measured on a copy that was not pushed and are estimates until their builders report. The slices are sequential, each from a trunk with the one before it merged.

Not run by me: the tests (CI: 23 of 23 on the designer's head, and on this head; Instruction count -0.014% on this head); the probe copy. Not done by the design turn and still owed: a self-test for scripts/count_lowering_ids.py; the count of merged guards in the guard-chain test.

Privacy grep 0.

@jfrolich
jfrolich marked this pull request as ready for review November 17, 2026 21:32
@jfrolich
jfrolich merged commit 61f7f41 into ocaml-to-rust-rewrite Nov 17, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant