Skip to content

Lower bodies from MIR - #67

Merged
kixelated merged 21 commits into
mainfrom
quest/m2/ts-mir
Oct 26, 2026
Merged

kixelated merged 21 commits into
mainfrom
quest/m2/ts-mir

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Finishes quest/m2/ts-mir.md.

Problem

rust2-ts had two lowerings. THIR was the default, and MIR was the prototype from the spike (#36), behind --lower mir. The spike decided that MIR should be the only one. THIR could not see drops, unwinding, or coroutines the way MIR's elaboration does.

Approach

  • Delete THIR. Every body now lowers from optimized_mir. The THIR expression and pattern lowering (body.rs's, pat.rs), the drop scopes, rt.future's generators, --lower, and RUST2_TS_ABORT are gone. Panics unwind under -C panic=unwind. The diff is -4,707 / +1,318 lines in ts/src and ts/runtime.
  • Speed and size of the MIR output.
    • Values are returned in place.
    • More temporaries fold: reads of locals' fields that nothing changes, a residual made in place, constant scalar locals, and bounds checks through rt.idx.
    • Empty switch arms are skipped.
    • A swap through a temporary is a no-op.
    • Loops over a slice, Vec, or range step in place, as two variables rather than an rt.Iter. An object of both made V8 1.25x slower on the BTreeMap workload.
  • Coverage gaps closed for MIR:
    • ? on a Result in a function returning Poll<Result<..>>.
    • Constant inclusive ranges.
    • An empty array MIR never assigns.
  • A crash turned into a refusal. A dropped closure that owns a value that drops used to fail inside std's drop glue. It now refuses at its drop, with a span.
  • Merged main. I merged #46 (collections) and ported its calls to MIR operands only. I also merged #65 and #68. feat(ts): multiply wide integers on halves, and join u64 halves in one call #68's fused rt.u64FromHalves was MIR-only, so it now applies to every body, including the codec's varint::from_halves.

Tests

  • Every snapshot, differential, unwinding, and census test runs from MIR.
  • The reject fixture now holds only what MIR refuses. The 14 constructs THIR refused and MIR lowers moved into differential fixtures:
    • The task fixture got a new temporaries. Its guards log each drop against native Rust: temporaries, scrutinees, a pattern's skipped parts, empty repeats, a closure consumed by its call, and an iterator dropped after its loop. It also logs guards held across an .await that a cancel drops.
    • The lang fixture got or-bindings, a move closure's copy, an empty repeat, and checked arithmetic nothing reads, which must still panic on overflow.
  • I checked stack traces through the source maps under node --enable-source-maps. A panic, assert, unwrap, bounds check, or overflow points at the Rust line and column of the call, and through each caller.
  • just check and just test pass.

Impact

Measured with just ts bench at 24b608b, after #68, against origin/main's THIR output at d7bff79 (--route thir). Host load was 15-40. The three runs below agree, and the bench's A/A route stays within 1.5% at the geomean.

Node Bun Chrome
codec, MIR ÷ THIR, geomean of 43 workloads, per run 0.96 / 0.97 / 0.97 0.94 / 0.95 / 0.94 0.98 / 0.96 / 0.97
async handoff, THIR ÷ MIR, median of runs, depth 1 / 4 / 16 1.4 / 2.3 / 3.0x 2.2 / 3.1 / 5.9x 1.6 / 2.3 / 3.5x
bundle (Bun minify) THIR raw MIR raw THIR gzip MIR gzip
codec 30629 30487 (-0.5%) 9422 9835 (+4.4%)
async handoff 10949 18281 (+67%) 3458 4005 (+16%)

The census of moq-net at the pinned revision has 13,744 refusals, against 14,891 from THIR before collections landed. 16 function instances that THIR translated are refused; ts-mir-census tracks them.

Consumer: moq. moq-net's codec and model translate through this lowering, and the census tracks the rest.

Public API

  • --lower is removed. With it goes the driver's only option, so rust2-driver's cargo::Args::parse drops its options parameter and Args drops its options field. rust2-ffi is the other caller.
  • An async fn keeps its signature, returning rt.Future<T>. The value is now an exported class of its own, fooFuture. The exported fooBody generator and fooDrop helpers are gone, and so is rt.future from the runtime.
  • Nothing else in the output's exports changes. Loops step variables in place, inside bodies.
  • There is no MIGRATE.md entry for these breaks, as decided below.

Alternatives

  • Keep THIR's generators for async bodies. That would be smaller, but they measured 1.4-5.9x slower on the handoff, and they need THIR's drop scopes.
  • Step iterators as an object { start, end }. Measured up to 1.25x slower in V8 on BTreeMap workloads (A/A within 1-3%).

Follow-ups

  • quest/m2/ts-mir-size.md: bring the bundles back to THIR's size, coroutine classes first.
  • quest/m2/ts-mir-census.md: the 16 lost functions and the refusals only MIR has: user ControlFlow values, switches and fields on unmapped external types, char switches, and type refusals at the function span instead of the local's.

Decisions

The maintainer decided:

  • ✅ Land with the size gap. The quest asked for output no larger than THIR's. Speed is met, but size is not: +16% gzip on the async fixture, from about 520 bytes minified per coroutine class against about 140 per generator, and +4% gzip on the codec. ts-mir-size takes it.
  • ✅ Accept the 16 census regressions. Most are temporaries' types THIR never declared, or drops THIR's scopes never saw. ts-mir-census takes them.
  • ✅ No MIGRATE.md entry. Nothing is released, the same ruling as on #50.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 19 commits October 25, 2026 16:21
Quest-Claim: 13f5cafd4d89a95e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
The reject fixture held constructs THIR refused that MIR lowers: temporaries,
scrutinees, skipped pattern parts, empty repeats, closures consumed by their
call, iterators that drop after their loop, and values held across an await.
They move into the task fixture, whose guards log each drop against native
Rust, and the lang fixture, which gains or-bindings, a move closure's copy,
and an empty repeat.

Two bugs they found: an empty array MIR never assigns read as an undeclared
name, and a dropped closure that owns a value that drops failed inside std's
drop glue span. It now refuses at its drop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Ports the collections' lowering to MIR only: their calls take the operands
MIR already lowered, and the THIR pattern and expression paths go with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A range or an array iterator stepped in place was an object of its bounds or
its array and index, whose fields V8 stepped up to 1.25x slower over the
bench's BTreeMap, which THIR's counters had run as fast as. Each is now two
variables, which an iterator moved into another and read nowhere else shares.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
The README drops `--lower` and the THIR lowering's generators, scopes, and
refusals, and records what MIR measured against THIR. The quest is done; what
it leaves is two quests: the census refusals MIR has that THIR didn't, and the
output size the coroutine classes and temporaries add.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated
kixelated marked this pull request as ready for review October 26, 2026 03:51
@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T04:09:07.153229Z 24b608b New commits
🔒 Security Review ✅ Completed 2026-10-26T04:12:18.984565Z 24b608b New commits
ℹ️ About Codex in GitHub

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

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

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5f930c9273

ℹ️ About Codex in GitHub

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

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/mir/mod.rs Resolved

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 5f930c9273c51d66e7e2986a275867db9f23b0b8 (against base d7bff791fb7f9111ec7fe850df98a9c35b90ac3c).

Existing actionable finding: I agree with the independently posted P1 operand-use finding at ts/src/lower/mir/mod.rs:461–463. drop_unread treats binary/unary evaluations as pure, whereas rvalue::effect and set_local preserve potentially panicking arithmetic; pruning its inputs can leave the retained expression reading an unassigned value. Share the actual effect classification when pruning uses and add chained, discarded checked-arithmetic differential coverage. Referencing that thread rather than duplicating it. No additional actionable bugs found in the areas inspected.

Direction: One MIR lowering is simpler than retaining THIR's separate drop/async machinery. The scalarized-iterator escape guards and expanded temporary-drop/cancellation tests fit that direction. The PR now explicitly accepts the size gap and 16 census regressions with dedicated follow-ups, and records the pre-release migration decision; these are disclosed tradeoffs, not new findings.

Verification limits: Static GitHub inspection of base/head source changes and relevant fixtures, including an independent optimization pass. I did not run builds, differential tests, benchmarks, or reproduce the reported measurements.

drop_unread uncounted the operands of a checked operation nothing reads, though set_local still lowers it for its panic, leaving it reading a local never assigned or declared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 24b608b30b05760e7319ff820b127d36f0cd98ce, compared with my previous review of 5f930c9273c51d66e7e2986a275867db9f23b0b8; base remains d7bff791fb7f9111ec7fe850df98a9c35b90ac3c.

Previous P1 addressed: The operand-use finding is fixed in ts/src/lower/mir/mod.rs:462–468: pruning now uses the same rvalue::effect classification as lowering, retaining dependencies of arithmetic evaluated for its possible panic. The new integer-fixture case covers discarded chained results, the snapshots retain the initialization before those operations, and the wide differential loop includes the new case. The original thread remains unresolved administratively; this is a code-fix assessment, not a new finding.

No new actionable bugs found in this delta. Reusing the existing effect predicate is the simpler, consistent fix rather than maintaining another purity rule. Static GitHub inspection only; I did not run builds or tests.

@kixelated
kixelated merged commit 0a5c69d into main Oct 26, 2026
1 check 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