Skip to content

Three trees T6x-a3: the stack's reads give references, and conditionals place and return handles - #349

Merged
jfrolich merged 3 commits into
ocaml-to-rust-rewritefrom
t6x-a3-stmt-handle-lowering
Nov 18, 2026
Merged

jfrolich merged 3 commits into
ocaml-to-rust-rewritefrom
t6x-a3-stmt-handle-lowering

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Slice T6x-a3 of the backend rewrite (compiler-rust/docs/THREE_TREES.md 11.4, "T6x-a: the statement stack, after the probe"). The statement stack's reads now give references to statement handles. Five functions of cond.rs that place or return statements now take and give handles. if_in_place calls JsStmt::if_head over the stack, which implements Runs<B>. The default build does not run this code, and the generated JavaScript does not change.

What changed

out.rs, the stack

  • first, last, only and first_single_slot give &B::S. first_slot and last_slot give the slot. at, head and head_mut read a slot in place.
  • take(slot) moves a statement out and leaves the slot Dead(1). It is one write.
  • take_run becomes pub(super). take_run_rev is new beside it.
  • The empty-block table is now Vec<(u32, u32)>, keyed by JsStmt::stmt_id. drop_empty_blocks compares js.stmt_id. Both cost notes now include the O(E) shift of the vector's tail.
  • impl Runs<B> for StatementStack<B> with Run = Span.
  • Deleted: peek, clear, statements_rev, slot_row and the production statements.
  • New: a #[cfg(test)] statements on StatementStack<Rows>, for the 23 tests that release a run row by row. Those tests are not edited.
  • Private helpers added: slot_ref (O(1)) and last_at (one step per trailing hole or dead slot), which last and last_slot share.

cond.rs

  • New signatures:
    • place(u32, B::S)
    • if_statement(..) -> B::S
    • unnest(..) -> Option<u32>
    • single_assignment(..) -> Option<u32>
    • assignment_statement(..) -> B::S
    • merge_tail_guard -> B::S
    • single_return -> bool
  • terminal is deleted, and its test is written out in effect_conditional.
  • The adapter StackLists and its norm::IfLists implementation are deleted.
  • use crate::bir::j::norm; moves into mod tests.
  • All three empty-block producers now read the stmt_id before the fill or push.

Callers

  • exit.rs, loops.rs, switch.rs, untagged.rs and variant.rs lose their wraps. switch.rs and untagged.rs take the statement with take(slot).
  • location.rs uses first_slot, then head and head_mut.
  • export.rs::build_rows reads stmt_id by reference.
  • mod.rs::append_outputs releases the dropped run with take_run and release_stmt.

Tests edited, only so that they compile; no expected value changed

  • The Hand helper in cond.rs passes statement.0 to note_empty_block, as the task says.
  • if_statement_is_simplify_if_for_every_rule pushes the statement if_statement now returns. Its stmt_from_node wrap had to go because the function changed signature.

Decision not set by the task: in merge_tail_guard, last_slot(run) is read only when the guard qualifies. Trunk's && chain reads it at the same point, so scanned is unchanged.

Gates

$ cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::lower
test result: ok. 567 passed; 0 failed; 0 ignored; 0 measured; 2410 filtered out
$ cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::j
test result: ok. 58 passed; 0 failed; 0 ignored; 0 measured; 2919 filtered out

The tests pin the four allocation counts of unit.rs and every size_of assertion, and all of them pass unchanged.

cargo check --all-targets and cargo clippy --all-targets -- -D warnings print no warning.

RUSTDOCFLAGS="-D warnings" cargo doc --locked --manifest-path compiler-rust/Cargo.toml --all-features --no-deps: no warning, exit 0.

$ python3 scripts/check_lowering_cost_docs.py --base origin/ocaml-to-rust-rewrite
cost docs: production functions=548; changed/new=50; missing/generic=0; removed=0

CI Backend bridge coverage: every *_differences field is 0. These are decision count, decision site, decision status, finish, free and name, in all three reports.

CI Instruction count: base 4,135,005,023, head 4,134,577,182, −0.010%.

Check mode, run on Linux under cachegrind against tests/tests. Both sides are fat-LTO release builds: the base at 109b2bd and the head at this branch. The head checkout uses the base's tests/tests and tests/dependencies, as CI does.

$ scripts/compare_build_instructions.py --base rescript-base base --head rescript-head head --runs 5 --sequential --allow-increase
| base | 4,141,363,994 | 4,141.7, 4,141.4, 4,141.9, 4,144.4, 4,143.5 | 788 |
| head | 4,141,191,483 | 4,141.2, 4,142.7, 4,141.7, 4,143.4, 4,143.2 | 788 |
| change | -172,511 (-0.004%) | | |

git status was clean afterwards.

Transition-call totals (python3 ../scripts/count_lowering_ids.py | grep '^| total' from compiler-rust):

this branch: | total | 15704 | 192 | 98 | 517 | 271 | 143 | 68 | 15 | 12 | 5 | 3 | 0 | 5 | 161 |
probe:       | total | 15681 | 193 | 99 | 527 | 271 | 143 | 68 | 15 | 12 | 10 | 8 | 0 | 5 | 161 |

Trunk, after T6x-a2, had 556 transition calls; this branch has 517. Compared with the probe there are 5 fewer stmt_from_node and 5 fewer stmt_node. The probe still wrapped the statement at several sites where this slice passes handles: place in if_in_place, and single_assignment, which kept a value this task drops. The line count is 23 higher than the probe's, from the cost notes.

End state

  • rg -n 'fn (peek|clear|statements_rev|terminal|only_row|last_row)\b' src/bir/lower/out.rs src/bir/lower/cond.rs: empty.
  • rg -n 'IfLists|simplify_if_head' src/bir/lower shows four lines. One is the comment in close.rs:27. The other three are the doc comment of js_stmt.rs::if_head (js_stmt.rs:314, which names norm::simplify_if_head as the body of if_head) and two test lines of oracle_tests.rs (1044, 1048). None of them is code.
  • rg -n 'B::stmt_node\(|B::stmt_from_node\(' src/bir/lower/out.rs: empty. In cond.rs above mod tests there are two lines, 356 and 421, in the #[cfg(test)] helpers empty_block and raw_if.
  • rg -n 'note_empty_block' compiler-rust/src/bir/lower above the tests shows the definition (out.rs:348) and three calls (cond.rs:348, 473, 700). Each call passes a u32 id.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 2 commits November 17, 2026 23:06
…ls place and return handles

The statement stack (out.rs) reads by reference:
`first`, `last`, `only`, `first_single_slot` give `&B::S`; `first_slot`
and `last_slot` give the slot; `at`, `head`, `head_mut` read a slot in
place. `take(slot)` moves the statement of a slot out and leaves the slot
`Dead(1)` (one write). `take_run` becomes `pub(super)`, `take_run_rev`
gives the statements last first. The empty-block table is keyed by
`JsStmt::stmt_id` (`empties: Vec<(u32, u32)>`), read by the three callers
before the push or fill; `drop_empty_blocks` compares `js.stmt_id`.
Deleted: `peek`, `clear`, `statements_rev`, `slot_row` and the production
`statements`; a `#[cfg(test)]` `statements` on `StatementStack<Rows>`
serves the tests that release a run row by row.

The stack implements `Runs<B>` (`Run = Span`), and `cond.rs::if_in_place`
calls `JsStmt::if_head` over it. The adapter `StackLists` and its
`norm::IfLists` implementation are deleted with `peek`; they cannot
outlive it, so both land in this commit.

cond.rs: `place(u32, B::S)`, `if_statement(..) -> B::S`,
`unnest(..) -> Option<u32>`, `single_assignment(..) -> Option<u32>`,
`assignment_statement(..) -> B::S`, `merge_tail_guard -> B::S`,
`single_return -> bool`; `terminal` is deleted (written out where it was
called). Callers in exit.rs, loops.rs, switch.rs, untagged.rs and
variant.rs lose their wraps; location.rs reads the first slot once;
export.rs::build_rows keys rows by `stmt_id`; mod.rs::append_outputs
releases the dropped run with `take_run`.

Private helpers added in out.rs: `slot_ref` (O(1)) and `last_at` (one
step per trailing hole or dead slot), which `last` and `last_slot` share.

Tests edited so that they compile: the `Hand` helper passes `statement.0`
to `note_empty_block`; `if_statement_is_simplify_if_for_every_rule` pushes
the statement `if_statement` now gives without the `stmt_from_node` wrap.
No expected value changed.

Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-95248a7a-965c-7675-8050-84109b97ebb4
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-95248a7a-965c-7675-8050-84109b97ebb4
@jfrolich
jfrolich marked this pull request as ready for review November 17, 2026 23:25
@jfrolich

Copy link
Copy Markdown
Owner Author

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

First-pass review (checklist, not an approval):

Setup: git fetch origin; D=origin/ocaml-to-rust-rewrite...origin/t6x-a3-stmt-handle-lowering; git diff $D > $TMPDIR/pr.diff (1016 lines). The merge base is the trunk tip 109b2bd4c. Commits: 5311030c82 (the code) and 65095095a9 (the changelog), in the order the task gives. Task text: THREE_TREES.md on trunk, "T6x-a: the statement stack, after the probe" (line 4306), "Common to every prompt of phase C" (5203), "Common to T6x-a1 … T6x-a4" (6260) and "Task T6x-a3:" (6588). I read files of the branch with git show origin/t6x-a3-stmt-handle-lowering:PATH. Below, "added lines" means + lines of pr.diff, tagged with file and new line number, and "above tests" means before each file's mod tests (cond 982, exit 598, export 1730, location 144, loops 198, mod 1212, out 752, switch 1117, untagged 1140, variant 507).


1. Scope

Commit 1 of the task, item by item:

  • first/last/only/first_single_slot return &B::S; first_slot and last_slot return u32: out.rs:254, 302, 277, 266, 260, 309. Done.
  • at, head, head_mut: out.rs:222, 240, 244. Done.
  • take(slot): out.rs:232. Done.
  • take_run is pub(super) (out.rs:138) and take_run_rev is new (out.rs:166). Done.
  • note_empty_block(slot, id: u32) (out.rs:348), empties: Vec<(u32, u32)> (out.rs:67), and drop_empty_blocks compares js.stmt_id (out.rs:366). Done.
  • Deleted peek, clear, statements_rev and the production statements; added the test-only statements (out.rs:523-528). Done.
  • The readers in cond.rs: single_return (382), unnest (481), effect_output (496), merge_assignments (583-587), merge_tail_guard (782), effect_conditional (631-637), returns_after_effects (826-838), fold_return_tail (846) and release_run (373). Done.
  • The three empty-block producers read the id first: cond.rs:343-350, 469-474 and 694-701. Done.
  • location.rs:134-139, export.rs:350, mod.rs:772, exit.rs (the first read), untagged.rs:843. Done.
  • The five signatures and their callers: see 11 and 18. Done.
  • impl Runs<B> for StatementStack<B> is at out.rs:533-566. StackLists and its norm::IfLists implementation are deleted, if_in_place is at cond.rs:444-456, use …norm moved into mod tests (cond.rs:983), and raw_if names crate::bir::ListId in full (cond.rs:419). Done.
  • Commit 2: the changelog fragment. Done.

The body names nothing as not done. Work the task did not name: the private helpers slot_ref (which replaces the deleted slot_row) and last_at. The common block allows private helpers, and the commit message names both with their costs. PASS

2. Default path

None of the changed functions runs in an ordinary build. All of them are in bir::lower, which runs only under RESCRIPT_BACKEND=check|new: compiler-rust/src/bir/shadow.rs:32 selects Legacy for any other value. CI Instruction count (default build) reads −0.010%. The body's "Check mode" table comes from scripts/compare_build_instructions.py … --runs 5 --sequential --allow-increase. Its command sets no RESCRIPT_BACKEND, and the script has no backend option (git show origin/ocaml-to-rust-rewrite:scripts/compare_build_instructions.py | grep -i backend: empty). That table is therefore the default build too, which matches the task's wording ("with its default build"). So no instruction measurement covers the changed code. Its cost is shown only by the lowering's step tests (see 3b). PASS (on what the task asks)

3. Engineering standard

Grep over the 287 added lines above the tests: grep -n -E 'allow\(|unsafe|MaybeUninit|Clone|Copy|Default|HashMap|HashSet|BTreeMap|Box::new|format!|View::|Op::|plain::|bir\.view\(|bir\.release\(|norm::|todo!|unimplemented!|#\[ignore|thread_local|RefCell|Atomic|Mutex|Vec::new|vec!|String|SmallVec|Rc::|Arc::|\.clone\(\)|collect|to_vec' found no matches (exit 1).

  • (a) Every new or changed function has a cost note, and CI script-tests prints cost docs: production functions=548; changed/new=50; missing/generic=0; removed=0. Two notes are incomplete; see 12.
  • (b) No step test was added. The loops are moved, not new: take_run_rev is the loop of statements_rev, last_at is the loop of the old last, and returns_after_effects now walks with peekable. The existing tests at 10,000 still pass in CI: cond.rs ten_thousand_nested_conditionals_are_linear_in_every_continuation (bounds on pushed, copied and scanned), the_else_chain_of_the_return_regression_is_linear (exact (pushed, copied, scanned) per continuation, including returning (2d+2, d, 2d)), out.rs a_hold_of_ten_thousand_statements_enters_its_list_once. I did not establish whether any of them reaches fold_return_tail/take_run_rev.
  • (c), (d) and (e): none.
  • (f): none.
  • (g): no new struct or enum. StackLists is deleted.
  • (h): none.

UNCLEAR (only (b): the changed returns_after_effects/take_run_rev path may have no test at 10,000)

4. Tests and oracles

The diff stat touches no expected file, ratchet or script. The only changes below mod tests are three cond.rs lines; see 20. No assertion was deleted and no expectation re-recorded. PASS

5. New scripts and fixtures

None added. PASS

6. Privacy

grep -n -i -E "tella|submodule\.nl|rescript-bench-corpus|/Users/|opensession|jake" $TMPDIR/pr.diff exits 1 with no hits. The commit messages contain only the Session: trailer URL, which is not a finding. PASS

7. Changelog

changes/statement-stack-reads.internal.md holds one line starting with "- ". CHANGELOG.md is untouched (git diff --stat $D -- CHANGELOG.md is empty). PASS

8. Numbers in the body

  • Lowering tests 567 and bir::j 58: the command and output are shown, and CI agrees (see 22).
  • Cost-doc line: the command and output are shown, and CI script-tests prints the same line.
  • Instruction count −0.010%: CI, matches the job log.
  • Check-mode table: the command and the five runs a side are shown; see 2 for what it measures.
  • count_lowering_ids totals: the command and output are shown. I reproduced both lines on git archive extracts of trunk and the branch under $TMPDIR (python only, no checkout):
    • trunk: | total | 15664 | 209 | 104 | 556 | 270 | 142 | 68 | 43 | 24 | 5 | 4 | 0 | 5 | 165 |
    • branch: | total | 15704 | 192 | 98 | 517 | 271 | 143 | 68 | 15 | 12 | 5 | 3 | 0 | 5 | 161 | (identical to the body)
  • "−172,511 (−0.004%)": shown with its command.
  • The allocation counts: the body says they are unchanged but does not print the four numbers. The branch's unit.rs:118-130 still asserts 86, 103, 86 and 156, and CI passed.

PASS

9. Focused tests

Not run (host review: no cargo). From the CI compiler-tests log: test result: ok. 2953 passed; 0 failed; 24 ignored. None of the 24 ignored tests is under bir::lower or bir::j (grep -E 'test bir::(lower|j)::.*ignored' is empty). UNCLEAR (not run here)

10. Where I would look hardest

  1. take/at on a hold of one statement. only and first_single_slot accept any slot with slot_len == 1, which includes Slot::Held with len == 1 (out.rs:187-194). The old callers then called clear(slot), which works on any slot. The new take (out.rs:232-237) and at (out.rs:222-227) panic on a held slot. The callers that can now panic are:

    • unnest: a held block.
    • effect_output: at(slot).
    • merge_assignments and the switch.rs declaration fold, through single_assignment.
    • merge_tail_guard: first_single_slot then take.
    • untagged.rs Op::Block.

    Holds of one statement exist: hold_effect (expr.rs:308), fill_held (args.rs:213, external.rs:34, ffi_args.rs:100) and hold_run (exit.rs:520, one param = value per argument). From expr.rs:226-270 I could not find a path where such a hold is the only statement of a run that reaches these readers, but I could not rule one out. The task's own wording ("a slot that holds one … panics on any other slot") leaves this open. The check-mode table built tests/tests (788 modules on both sides), but under the default backend (see 2), so it says nothing here. A test with a branch whose only output is a hold of one statement (a static raise with two arguments where one assigns nothing, or a sequence head held before a tail with no statement), fed to unnest/single_assignment, would settle it. Otherwise take/at could accept Held with len == 1.

  2. Step counters that now move. Runs::release and mod.rs::append_outputs now release through js.release_stmt, which adds a.release(..) to Rows::steps (rows_stmt.rs:357-359); the old code called Bir::release directly. Rows::if_head adds one step per call (rows_stmt.rs:393). Both are the task's own lines. git grep -n -E 'js\.steps\(\)|\.js\.steps' origin/t6x-a3-stmt-handle-lowering -- compiler-rust/src/bir/lower compiler-rust/src/bir/j is empty, and the only Js::steps readers are js_tests.rs/js_stmt_tests.rs, which call Rows directly. So no lowering test reads it across if_in_place, but any stats consumer of Rows::steps will see different numbers.

  3. returns_after_effects' stopping rule. It changed from row equality with last(run) to "the iterator has no next" (cond.rs:826-838). The two are equivalent only if statement_refs ends at the same statement last_at finds; both skip holes, dead slots and empty holds, so I believe they agree. effect_conditional now reads the terminal kind before calling single_return(no) rather than after; stmt_kind is a pure &self read (rows_stmt.rs:109), so this is harmless.

11. Files and signatures

git diff --stat $D:

 changes/statement-stack-reads.internal.md |   1 +
 compiler-rust/src/bir/lower/cond.rs       | 250 +++++++++++++----------------
 compiler-rust/src/bir/lower/exit.rs       |   2 +-
 compiler-rust/src/bir/lower/export.rs     |   2 +-
 compiler-rust/src/bir/lower/location.rs   |   7 +-
 compiler-rust/src/bir/lower/loops.rs      |   2 +-
 compiler-rust/src/bir/lower/mod.rs        |   2 +-
 compiler-rust/src/bir/lower/out.rs        | 254 +++++++++++++-----------------
 compiler-rust/src/bir/lower/switch.rs     |   8 +-
 compiler-rust/src/bir/lower/untagged.rs   |   7 +-
 compiler-rust/src/bir/lower/variant.rs    |   4 +-
 11 files changed, 291 insertions(+), 248 deletions(-)

Every file is one the task names as "Yours". git diff --stat $D -- compiler-rust/src/bir/j compiler-rust/src/bir/lower/js.rs compiler-rust/src/bir/lower/js_stmt.rs is empty: js.rs, js_stmt.rs, norm.rs and everything else under bir/j are untouched.

Changed fn lines above mod tests, outside out.rs (all in cond.rs):

- 78/80/82/84/88/93: fn trim/empty/last/only/release/take of StackLists (deleted)
- 372: pub(super) fn place(&mut self, slot: u32, statement: NodeId) {
+ 343: pub(super) fn place(&mut self, slot: u32, statement: B::S) {
- 410: fn single_return(&mut self, run: Span) -> Option<(NodeId, NodeId)> {
+ 382: fn single_return(&mut self, run: Span) -> bool {
- 423: pub(super) fn single_assignment(&mut self, run: Span, target: VarId) -> Option<(NodeId, NodeId)> {
+ 392: pub(super) fn single_assignment(&mut self, run: Span, target: VarId) -> Option<u32> {
- 442: fn terminal(&self, statement: NodeId) -> bool {          (deleted)
- 461: fn raw_if(&mut self, test: NodeId, yes: ListId, no: ListId) -> NodeId {
+ 419: fn raw_if(&mut self, test: NodeId, yes: crate::bir::ListId, no: crate::bir::ListId) -> NodeId {
- 469: pub(super) fn if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> NodeId {
+ 427: pub(super) fn if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> B::S {
- 485: fn if_in_place(&mut self, head: u32, mut test: NodeId, mut yes: Span, mut no: Span) {
+ 444: fn if_in_place(&mut self, head: u32, test: NodeId, mut yes: Span, mut no: Span) {
- 520: pub(super) fn unnest(&mut self, start: u32) -> Option<(u32, NodeId)> {
+ 481: pub(super) fn unnest(&mut self, start: u32) -> Option<u32> {
- 813: fn merge_tail_guard(&mut self, run: Span) -> NodeId {
+ 782: fn merge_tail_guard(&mut self, run: Span) -> B::S {

Two more signature changes are not on fn lines:

  • assignment_statement's result type is on its own line: - ) -> NodeId { / + ) -> B::S { (cond.rs ~600).
  • if_in_place only drops mut from a binding, which is the task's own text. raw_if writes out the same type.

The five, beside the task:

Function Task Branch
place place(&mut self, slot: u32, statement: B::S) same
if_statement if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> B::S same
unnest unnest(&mut self, start: u32) -> Option<u32> same
single_assignment single_assignment(&mut self, run: Span, target: VarId) -> Option<u32> same
assignment_statement assignment_statement(..) -> B::S -> B::S

Also single_return -> bool, merge_tail_guard -> B::S and the deletion of terminal, all allowed. No other changed parameter or result type. PASS

12. out.rs, the stack, function by function (base lines 1–FreeId, branch lines 1–570; both read whole)

Function Old signature New signature Task lists it? Status Cost note (quoted) / true?
end, default, with_capacity, reserve_repeat, capacities, mark, since, statement_refs, slot_len, live, trim, kill, has_statements, remove_empty, push, reserve, fill, fill_held, held_mark, hold, fill_held_since, hold_run, append, take_list, close as branch unchanged (no hunk) as "today" unchanged unchanged
peek fn peek(s: &mut B::S) -> NodeId — deleted deleted —
statements (production) pub(super) fn statements(&mut self, span) -> impl Iterator<Item = NodeId> — deleted deleted —
statements_rev pub(super) fn statements_rev(&mut self, span) -> impl Iterator<Item = NodeId> — deleted deleted —
slot_row fn slot_row(&mut self, at: usize, index: usize) -> NodeId — (private) deleted —
clear pub(super) fn clear(&mut self, slot: u32) — deleted deleted —
take_run fn take_run(&mut self, span) -> impl Iterator<Item = B::S> pub(super) fn take_run(…) yes visibility only; moved up "O(slots of the run + held statements), each statement moved once". True: it skips a dead range in one step.
take_run_rev — pub(super) fn take_run_rev(&mut self, span: Span) -> impl Iterator<Item = B::S> + '_ yes new "O(slots of the run + held statements), one step per slot and per held statement, each statement moved once". True: it walks back slot by slot, as statements_rev did.
slot_ref — fn slot_ref(&self, at: usize, index: usize) -> &B::S no (helper) new "O(1)". True: one match and one index. Callers: first, first_single_slot, only, last, head.
at — pub(super) fn at(&self, slot: u32) -> &B::S yes new "O(1). Panics on any other slot". True.
take — pub(super) fn take(&mut self, slot: u32) -> B::S yes new "One write. Panics on any other slot". True.
head — pub(super) fn head(&self, slot: u32) -> &B::S yes new "O(1)". True.
head_mut — pub(super) fn head_mut(&mut self, slot: u32) -> &mut B::S yes new "O(1)". True.
first -> Option<NodeId> -> Option<&B::S> yes changed "Costs the holes and dead ranges before the first statement, one step each". True.
first_slot — -> Option<u32> yes new "Costs the holes and dead ranges before it, one step each". True: one live.
first_single_slot -> Option<(u32, NodeId)> -> Option<(u32, &B::S)> yes changed "O(leading holes/dead ranges)". True.
only -> Option<(u32, NodeId)> -> Option<(u32, &B::S)> yes changed "Costs the holes and dead ranges of the run, one step each". True: two lives.
last_at — fn last_at(&mut self, span: Span) -> Option<(usize, usize)> no (helper) new "One step on a trimmed run; otherwise one per trailing hole or dead slot, counted in scanned". True: it is the loop of the old last. Callers: last, last_slot.
last -> Option<NodeId> -> Option<&B::S> yes changed "One step on a trimmed run; otherwise one per trailing hole or dead slot". True.
last_slot — -> Option<u32> yes new "O(1) on a trimmed run; otherwise one step per trailing hole or dead slot". True.
note_empty_block (slot: u32, statement: NodeId) (slot: u32, id: u32) yes changed See 15. True.
drop_empty_blocks same same yes body changed See 15. True.
test statements — #[cfg(test)] impl StatementStack<Rows> { pub(super) fn statements(&self, span: Span) -> impl Iterator<Item = NodeId> + '_ { self.statement_refs(span).map(|s| s.row()) } } yes new Identical to the task's text, doc comment included. Its note ("One step per slot") leaves out held statements, but that is the task's own text.
Runs::trim / empty / last / only / release / take — see 16 yes new See the nits below.

Two nits in the cost notes, not in the task's list:

  • Runs::trim (out.rs:534-535) reads "two binary searches, one step per noted empty block of the run". It leaves out the O(E) tail shift of the drain that drop_empty_blocks now names.
  • Runs::take (out.rs:562) reads "one step per slot and per statement, each moved once". It leaves out the close of the list, which take_list performs.

Neither omission is a scan. PASS (with the two nits)

13. The reads and scanned

Old → new bodies (from pr.diff):

  • first: let at = self.live(..)?; Some(self.slot_row(at, 0)) → …; Some(self.slot_ref(at, 0)). scanned += sits only in live, once in both versions.
  • first_slot: new, self.live(..).map(|at| at as u32). One live.
  • first_single_slot: live, then slot_len, then slot_row → slot_ref. One live, the same as before.
  • only: two lives, old and new.
  • last: the loop with self.scanned += 1 per trailing empty slot moved verbatim into last_at. last = last_at + slot_ref, last_slot = last_at. The same count.
  • at, head, head_mut: new, with no scanned.

No self.scanned += was added or removed in out.rs; the only ones are in live, last_at (moved from last) and trim.

Call sites:

  • location.rs: old first(run), one live; new first_slot(run), one live, then head/head_mut, which add nothing. Read once.
  • merge_tail_guard: old last(run) sat at the end of the && chain, so it ran only when the guard qualified. New merge.filter(|&slot| self.statements.last_slot(run) != Some(slot)) runs only when merge is Some, which is the same condition, so the scan is the same.
  • returns_after_effects: steps counts the statements consumed by the loop, and the peek adds nothing to scanned (statement_refs never touches it). The same as before.
  • effect_output: unnest's only, then at(slot), which is O(1) and does not scan.

No read scans twice where the old code scanned once. CI's the_else_chain_of_the_return_regression_is_linear asserts exact scanned and passes. PASS

14. take, take_run, take_run_rev

Quoted in 12 and pr.diff out.rs:232-237, 138-163 and 166-185. take: match self.slots[slot as usize].replace(Slot::Dead(1)) { Some(Slot::Statement(s)) => s, _ => panic!(..) }. By kind of slot:

  • One statement: moved out, and the slot becomes Dead(1).
  • A hold of one statement, or of several: panics. The Dead(1) is written before the panic.
  • A hole: panics.
  • A dead slot: panics.

This is what the task text says, but see 10.1: only and first_single_slot hand out held slots of length 1 too.

take_run_rev gives the entries of a hold last first (inner -= 1; held[inner].take()), as statements_rev did. It leaves a hole where a slot or hold gave its statements, restores dead slots and holes (other => self.slots[at] = other), and the callers kill the run (fold_return_tail's caller calls self.kill(yes, no) at cond.rs:728). No collect, Vec::new, vec!, to_vec or clone() in the added lines of out.rs above the tests (grep in 3). PASS (the panic case is in 10.1)

15. The empty-block table

  • Field: empties: Vec<(u32, NodeId)> → empties: Vec<(u32, u32)> (out.rs:67), with the doc "…with the JsStmt::stmt_id of each…".
  • note_empty_block (out.rs:342-351): let at = self.empties.partition_point(|&(other, _)| other < slot); self.empties.insert(at, (slot, id));. Its note: "Cost: O(log E) for a binary search among the unit's E noted empty blocks, and an insertion that moves the entries after its place: O(E) in the worst case, nothing for a block noted above every other (the common case, a block at the top of the stack)." True.
  • drop_empty_blocks (out.rs:352-372): the same two partition_points, the same drain(from..to) in order, and if js.stmt_id(a, held) == id && js.is_empty_block(a, held) where the old code had Self::peek(held) == statement && js.is_empty_block(a, held). Its note: "…two binary searches among the unit's E noted empty blocks, then one step per empty block of the run, each read through JsStmt::stmt_id and released through JsStmt::release_stmt, and the drain that moves the entries after the run's: O(E) in the worst case, nothing when the run's are the last." True. On rows, stmt_id is s.0.0 (rows_stmt.rs:134), the row peek gave, so it makes the same test on the same slot in the same order and releases the same blocks.
  • The three callers read the id before the stack takes the statement:
    • place (cond.rs:343-350): is_empty_block(..).then(|| stmt_id(..)), then fill, then note.
    • block_statement (cond.rs:469-474): the same pattern, then push, then note_empty_block(head, id).
    • free_effect (cond.rs:694-701): the same pattern, then slot = mark().start, push, note.
  • git grep -n 'note_empty_block' origin/t6x-a3-stmt-handle-lowering -- compiler-rust/src/bir/lower gives the definition (out.rs:348), calls at cond.rs:348, 473 and 700 (each with id: u32), the doc mention at cond.rs:342 and the test at cond.rs:1630 (statement.0). No NodeId(..) argument is left.

PASS

16. impl Runs<B> for StatementStack<B> and if_in_place

  • The trait (js_stmt.rs:62-84, unchanged) has type Run: Copy, trim, empty, last -> Option<&B::S>, only -> Option<&B::S>, release ("One step per node released"), take -> B::L.
  • The impl (out.rs:531-566) is quoted in pr.diff, with type Run = Span.
Function Deleted StackLists body New impl Same?
trim self.stack.drop_empty_blocks(self.js, b, span) self.drop_empty_blocks(js, a, run) yes
empty EMPTY (= Span { start: 0, len: 0 }) Span::default(), and Span derives Default (out.rs:18), so {0, 0} yes
last self.stack.last(span) StatementStack::last(self, run) yes
only self.stack.only(span).map(|(_, s)| s) the same yes
release for statement in self.stack.statements(span) { b.release(statement); } self.stack.kill(span); for statement in self.take_run(run) { js.release_stmt(a, statement); } self.kill(run); Same statements in the same forward order (holds in order), then kill. release_stmt on rows is steps += a.release(s.0) (rows_stmt.rs:357-359): the same nodes released, plus a step count (10.2).
take B::list_id(self.stack.take_list(self.js, b, span)) self.take_list(js, a, run) yes, the same list

if_in_place is quoted in pr.diff, cond.rs:444-456. The new body is the task's text verbatim. When if_head returns None, the else branch calls self.if_statement(B::node(condition), yes, no) with the test as if_head left it, and it places at head in both branches, as before.

Rows::if_head (rows_stmt.rs:385-397, unchanged) does steps += 1 and one norm::simplify_if_head through RunLists. On None, if_statement → js.if_ runs js3_simplify_if over pool lists, exactly as the old if_statement did on this path. No second simplification was added. PASS

17. cond.rs readers

  • single_return:

    • Old: only?, then Return?, then value_of?, then Some((stmt, NodeId(js.id(value)))).
    • New: only else false, then kind == Return && value_of.is_some().

    The same predicate. Rows::id is r.0 with no step (rows.rs:220). The callers changed .is_some()/.is_none() to bool. SAME.

  • unnest: only?; if not a block, it returns the slot; otherwise it does take(slot) (old: clear(slot)) and unblock, pushing. The same statements in the same order. SAME, except the held-slot panic (10.1).

  • effect_output:

    • Old: unnest, take_value(row), clear, trim.
    • New: unnest, at(slot) for the kind, take(slot), take_value, trim.

    The same nodes. The statement is read twice (only in unnest, then at), but that follows from the task's unnest -> Option<u32>. SAME.

  • merge_assignments (cond.rs:577-591): both single_assignments run first, as before. Old: take_value(then_ row), keep_operand, take_value(else_ row), keep_operand, kill. New: take(then_), take_value, keep_operand, take(else_), take_value, keep_operand, kill. SAME.

  • merge_tail_guard: the guard comes from first_single_slot, the kind and branch checks are unchanged, then merge.filter(last_slot(run) != Some(slot)) and take(slot) where the old code called clear(slot). The guard's slot has slot_len == 1. last_at returns the last slot with len != 0 and its last index, so last_slot == guard slot holds exactly when the last statement is the guard, which is the old row comparison. That still holds when the guard's slot is a hold of one, but then take panics (10.1). SAME, except that case.

  • effect_conditional: written out as self.statements.last(yes).is_some_and(|last| matches!(stmt_kind(last), Return | Throw | Break | Continue)). The deleted terminal matched Return | Throw | Break | Continue, so it is the same predicate arm by arm. last(yes) is still read before single_return(no); the kind read is now eager and pure. SAME.

  • returns_after_effects (cond.rs:817-841): statement_refs(run).peekable() stops when peek().is_none(), before the Exp check, at the position where the old code hit statement == last. steps counts the same, and drop(statements) comes before self.statements.scanned += steps. SAME.

  • fold_return_tail: take_run_rev folds last first, as statements_rev did, and the caller kills both runs (cond.rs:728). SAME.

  • release_run: take_run forward with release_stmt, then kill, in the same order as the old statements loop. SAME.

PASS (apart from 10.1)

18. Callers of the five functions

Per-function counts from git show <branch>:<file> | grep -n "self.<fn>(":

Function Call sites Count Task
place switch:1109, untagged:800, 819, variant:484 1/2/1 ✓
if_statement exit:422, loops:153, switch:1103, untagged:818, variant:480 1 each ✓
unnest exit:573, switch:897, untagged:625, 787, 821, 1124, variant:491 1/1/4/1 ✓
single_assignment switch:883 1 ✓
assignment_statement switch:1101 1 ✓

Wraps removed:

  • exit.rs:423: push(B::stmt_from_node(statement)) → push(statement).
  • loops.rs:156: the same, with guard.
  • variant.rs:480, 484: B::stmt_from_node(self.if_statement(..)) → self.if_statement(..), and place(plan.head, B::stmt_node(statement)) → place(plan.head, statement).
  • switch.rs:1106-1109: the line let statement = B::stmt_from_node(statement); is removed, and place(plan.head, B::stmt_node(statement)) → place(plan.head, statement).
  • untagged.rs:818-819 needed no edit.

The two callers that read a result:

  • untagged.rs Op::Block (792-799):

    • Old: if let Some((_, statement)) = only { self.statements.kill(run); statement } else { …B::stmt_node(self.js.explicit_block(..)) }.
    • New: if let Some(slot) = only { let statement = self.statements.take(slot); self.statements.kill(run); statement } else { …self.js.explicit_block(..) }.

    The same statement, then the kill.

  • switch.rs declaration fold (882-889):

    • Old: take_value(B::stmt_from_node(statement)), keep_operand, kill(candidate).
    • New: take(slot), take_value, keep_operand, kill(candidate).

    The same nodes in the same order.

The other sites:

  • location.rs: first_slot, then stmt_located(head(slot)), then locate_stmt(head_mut(slot)); scanned read once.
  • export.rs:350: for stmt in self.statements.statement_refs(run).map(|s| NodeId(self.js.stmt_id(&self.arena, s))), which is the task's line.
  • mod.rs:772: for statement in self.statements.take_run(second.stmts) { self.js.release_stmt(&mut self.arena, statement); }, which is the task's line. append then truncates to first.stmts.end() (out.rs:472), so the holes it leaves go away.
  • exit.rs: the first read.
  • untagged.rs:843: stmt_kind(&self.arena, s) on the last read.

PASS

19. Transition calls

  • git grep -n -E 'B::stmt_node\(|B::stmt_from_node\(' <branch> -- compiler-rust/src/bir/lower/out.rs: empty.

  • In cond.rs above mod tests, two lines:

    • cond.rs:356: B::stmt_node(self.js.explicit_block(&mut self.arena, list)), in #[cfg(test)] fn empty_block.
    • cond.rs:421: B::stmt_node(statement), in #[cfg(test)] fn raw_if.
  • git grep -n -E 'IfLists|simplify_if_head' <branch> -- compiler-rust/src/bir/lower:

    • close.rs:27: a /// comment, the line the task names.
    • js_stmt.rs:314: the doc comment of JsStmt::if_head. The file is unchanged by this PR, and the line was already present on trunk.
    • oracle_tests.rs:1044 (test function name) and :1048 (comment): a #[cfg(test)] module (mod.rs:33-34), present on trunk too.

    No line is code.

  • git grep -n -E 'fn (peek|clear|statements_rev|terminal|only_row|last_row)\b' <branch> -- …out.rs …cond.rs: empty (exit 1).

  • No stmt_from_node(stmt_node(..)) pair in the added lines. The grep over + lines finds no stmt_from_node above the tests at all.

PASS

20. Tests

Every changed line below a mod tests:

+    use crate::bir::j::norm;                          (cond.rs:983, moved in)
-                    self.state.statements.note_empty_block(slot, statement);
+                    self.state.statements.note_empty_block(slot, statement.0);   (Hand helper)
-                    hand.state.statements.push(<…Rows as …JsStmt>::stmt_from_node(statement));
+                    hand.state.statements.push(statement);   (if_statement_is_simplify_if_for_every_rule)

That is all. raw_if is a #[cfg(test)] helper above mod tests, already covered in 11. No expected value changed, no assertion or test was deleted, and no #[ignore] was added.

git diff $D | grep -n '^[-+].*\.statements(' gives five hits, all deletions in production code:

  • 48: StackLists::release
  • 88: release_run
  • 422: returns_after_effects
  • 514: export.rs::build_rows
  • 560: mod.rs::append_outputs

statements.statements( is counted 27 times on trunk and 23 on the branch, in 19 files (git grep -c/-l), so the 23 test calls are untouched. PASS

21. Counts as failure

The grep in 3 over the added lines above the tests is empty for every listed pattern (allow(, unsafe, MaybeUninit, Clone, Copy, Default, HashMap, HashSet, Box::new, format!, View::, Op::, plain::, bir.view(, bir.release(, norm::). Span::default() is lower-case and is a Default use on Span, not a bound on B::S/B::L.

From the task's own list:

  • A read that returns an id where a reference serves: none. export.rs maps to an id because build_rows' work list is NodeId, and that is the task's line.
  • A statement read twice: effect_output (only inside unnest, then at), which the task's unnest -> Option<u32> makes necessary. location.rs reads once.
  • stmt_id used to find a statement again: none.
  • A copy of a J-S3 rule in cond.rs: none, since if_in_place calls if_head.
  • An action list touched (switch_output_nodes, ActionState::cached): none; the diff does not touch those names.

PASS

22. Numbers

gh pr checks 349 -R jfrolich/rescript-rust: all 23 checks pass. They are Backend bridge coverage, Derived code check, Playground wasm check, Signature equality, Snapshot equivalence, base, build, build-system-tests, build-system-tests (macOS), compare, compiler-tests, compiler-warnings, head, ocaml, oxc parity (gentype/js/stdlib) and parity (analysis/diagnostics/gentype/javascript/stdlib), plus script-tests. Runs 44196403073 and 44196403068 are both at head 65095095a9.

Instruction count (job 134443348235):

| base | 4,135,005,023 | 4,135.0, 4,136.4, 4,135.4, 4,137.7, 4,136.0 | 788 |
| head | 4,134,577,182 | 4,136.2, 4,135.9, 4,134.6, 4,134.9, 4,136.9 | 788 |
| change | -427,841 (-0.010%) | | |

compiler-tests (job 134438918846):

  • grep -c -E 'test bir::lower::.* \.\.\. ok' gives 567.
  • grep -c -E 'test bir::j::.* \.\.\. ok' gives 58.
  • No ignored test under either.

Backend bridge coverage (job 134440572654): every decision_count/decision_site/decision_status/finish/free/name_differences is 0 in all three reports.

The body's check-mode table, as written:

$ scripts/compare_build_instructions.py --base rescript-base base --head rescript-head head --runs 5 --sequential --allow-increase
| base | 4,141,363,994 | 4,141.7, 4,141.4, 4,141.9, 4,144.4, 4,143.5 | 788 |
| head | 4,141,191,483 | 4,141.2, 4,142.7, 4,141.7, 4,143.4, 4,143.2 | 788 |
| change | -172,511 (-0.004%) | | |

Both sides show five runs. It is the default backend; see 2.

count_lowering_ids totals: the body shows 517 on the branch and 556 on trunk, and I reproduced both (see 8).

Allocation counts: 86, 103, 86 and 156 still asserted at unit.rs:118-130, and they pass in CI. Cost-note line: cost docs: production functions=548; changed/new=50; missing/generic=0; removed=0 (CI script-tests, the same as the body).

Rows::if_head's extra step (and release_stmt's, 10.2): no test of the lowering reads Js::steps() across an if_in_place. git grep -n -E 'js\.steps\(\)|\.js\.steps' <branch> -- compiler-rust/src/bir/lower compiler-rust/src/bir/j is empty. The steps() readers in bir/lower are js_tests.rs and js_stmt_tests.rs (on Rows directly; the if_head tests at js_stmt_tests.rs:1364/1459 run over a test Runs, not the stack) plus unrelated counters (names, untagged grouping, export coercion). The cond.rs step tests read pushed/copied/scanned, not Js::steps. PASS

…slot of one it is

only and first_single_slot give any slot that holds exactly one statement,
a hold of one included. Before this slice their callers cleared the slot,
which worked for either kind; take and at accepted a statement slot alone
and would stop the compiler on a hold of one. Both accept it now, with a
test. The cost notes of Runs::trim and Runs::take name the drain and the
close they left out.

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 d6010b1 (the author did not write this review). The first-pass checklist (comment 6964191018) was read whole; I read the stack of out.rs on the branch and the points the checklist left open.

Verdict: accepted with one reviewer commit. Slice T6x-a3 is its task: the stack's reads give references, a statement is moved out with take, the stack implements Runs and if_in_place calls JsStmt::if_head, the five functions of cond.rs take and return handles, and peek, clear, statements_rev, terminal and the IfLists adapter are gone.

The reviewer commit (d6010b1)

  • take and at on a hold of one statement. only and first_single_slot give any slot that holds exactly one statement, and a hold of one is such a slot. On trunk their callers went on with clear(slot), which worked for either kind. take and at as built accepted a statement slot alone and stopped the compiler on a hold of one, at unnest, effect_output, merge_assignments, the declaration fold of the small switch, merge_tail_guard and the block arm of untagged.rs. Neither the first pass nor I found a program that puts a hold of one there as the only statement of a run, and the check-mode corpora in CI pass; but nothing rules it out, and a panic is the wrong answer to "not seen". Both functions accept a hold of one now, as trunk did, with the test a_hold_of_one_statement_is_read_and_taken_as_a_slot_of_one. The task's sentence ("a slot that holds one ... panics on any other slot") allowed either reading: a gap of the prompt, not counted against the builder.
  • The cost notes of Runs::trim and Runs::take name the drain of the empty-block table and the close of the list.

What else I checked

  • The empty-block table has one kind of key: empties: Vec<(u32, u32)>, all three producers read stmt_id before the push or fill, drop_empty_blocks compares js.stmt_id. Both cost notes name the shift of the vector's tail.
  • impl Runs<B> for StatementStack<B> does what the deleted adapter did, body by body; the rules of J-S3 run once per call, in if_head.
  • Signatures: the five of cond.rs the task names, single_return -> bool, the private merge_tail_guard. norm.rs, js.rs, js_stmt.rs and src/bir/j untouched.
  • Tests: the two edits the builder lists; no expected value changed; the 23 test calls of statements(..) untouched.
  • CI on the final head 23 of 23: bridge coverage at zero differences, lowering tests 568 (567 and the one added), bir::j 58. Instruction count: -0.010% on the builder's head and +0.024% on the final head (4,133,876,137 to 4,134,867,405 on the minima; the base's five runs span 4,133.9 to 4,138.3 M and its minimum stands alone, the medians are 4,135.0 and 4,135.2 M, +0.005%). The two heads differ by the reviewer commit, which changes functions only a check-mode build reaches, so the default build runs the same code in both: the difference is the job's spread.

Accepted as the builder decided

  • The private helpers slot_ref and last_at.
  • IfLists|simplify_if_head still matches the doc comment of JsStmt::if_head in js_stmt.rs, which was not the builder's file. It stays: the comment says where the rules live.

On record

  • Rows::if_head and release_stmt add to the step counter of Rows where the direct calls did not. No test of the lowering reads it across these calls.
  • The builder's local "check mode" figure was measured with the default backend (the script sets no backend), so it says nothing about this code. The application-wide check-mode measurement is made once after T6x-a4.

@jfrolich
jfrolich merged commit 627a27d into ocaml-to-rust-rewrite Nov 18, 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