Repository navigation
feat(codegen): recursive reassignment + read scan in optimizer passes - #1465
Merged
Merged
Conversation
Three surgical fixes to the optimizer that together let mut-inference (PR #1461) actually deliver clean gen/rust on downstream repos: 1. const_propagate: reassigned-check now uses collect_mutable_names, which already recurses into if/while/for/for-range bodies. Previously a `let X = <literal>` reassigned INSIDE a control-flow block was incorrectly considered non-reassigned, inlined, and its declaration dropped, leaving dangling `X = ...` reassignments in emit. 2. copy_propagate: same recursive reassigned-check guard added. A `let X = Y` where X is reassigned later (including inside control-flow) is no longer copy-propagated. 3. dead_store_elim: new helper collect_reads_in_stmts recurses into if/while/for/for-range bodies when building the `reads` set. Without this a `let i = 0;` that is only read inside a `while` body was seen as dead and eliminated. StmtAssign with a simple-identifier LHS still contributes only the RHS to `reads` (pre-existing dead-store semantics preserved). Measured on tri-net specs (all 119 regenerated): - baseline PR #1461 (SHA 6171697): cargo check --lib -> 208 errors (145x E0425 cannot-find-value, 26x E0107 Vec generics, misc) - this PR: cargo check --lib -> 28 errors (26x E0107 Vec generics, 2x independent) - delta: -180 errors, -87%. E0425 -> 0. t27c own tests: 1491 passed / 3 failed. Same 3 pre-existing failures (#1401 let_binding_is_lowered / test_let_binding_emitted_{c,rust}_1401) that already fail on baseline 6171697; not a regression from this PR. FROZEN_HASH re-sealed via bootstrap/stage0 ceremony. Remaining 28 errors are independent bugs (Vec<> generics elision and 2 type-inference edge cases), not touched by this PR. phi^2 + phi^-2 = 3
Contributor
|
📓 NotebookLM Notebook linked to this PR
This notebook contains session context, decisions, and artifacts for this work. |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three optimizer passes (
const_propagate,copy_propagate,dead_store_elim) checked for reassignment/reads only at the top-level statement list, without recursing into if/while/for bodies. Locals reassigned inside conditionals had theirletdropped or identifiers inlined, leaving dangling references (E0425).Fixes #1464
How
Recursive reassignment and read scanning in all three passes:
const_propagate: usesassigns_to_recursive()instead of top-level scancopy_propagate: same recursive check before inliningdead_store_elim:collect_reads_in_stmts()recurses into control-flow bodiesassigns_to_recursive(): mirrorscollect_mutable_namespatternMeasured impact (tri-net, cross-env verified)
Cross-env verified: 28 errors on linux, 26 on macOS arm64.
Files
bootstrap/src/compiler.rs— +50 lines (3 pass fixes + helper function)Supersedes closed #1462 (base branch deleted by squash merge of #1461).
phi^2 + phi^-2 = 3