◐ Shell
clean mode source ↗

Align bytecode CFG cleanup with CPython by youknowone · Pull Request #7781 · RustPython/RustPython

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

One cohesive PR: add a percent-format AST preprocessor; restrict constant folding and pre-evaluate boolean prefixes; refactor try/with/f-string emission; add layout-aware IR NOP/layout helpers, short-circuit-aware jump threading, and a fast-scan path; unify symboltable scope handling; add tests and dictionary entries.

Changes

Codegen Control-Flow and IR Optimization Refactoring

Layer / File(s) Summary
Preprocess: percent-format -> f-string
crates/codegen/src/preprocess.rs, crates/codegen/src/lib.rs
Add preprocess_mod and AstPreprocessor to rewrite simple %-format expressions into Expr::FString with parsed interpolations and basic format-specs; register mod preprocess;.
Symbol Table Scope Unification
crates/codegen/src/symboltable.rs
Centralize scope-type determination via is_function_like_scope; pass explicit CompilerScope into enter_scope_with_parameters; track unevaluated annotations and extend named-expression scoping to match CPython.
Compile entry & constant folding
crates/codegen/src/compile.rs
Wire preprocess_mod into compile_top; restrict constant folding to expressions reported constant; ensure boolean-op prefix-values are compiled before short-circuit threading; add nested-with tail detection helper.
Try/Except end-path & With cleanup NOPs
crates/codegen/src/compile.rs
Refactor try normal-exit to route through a cleanup_block and directly compile orelse; compute preserve_outer_cleanup_target_nop from nested-with tail preservation and terminal suppress-exit predecessor detection to decide NOP retention.
F-string literal no-location tracking
crates/codegen/src/compile.rs
Thread pending_literal_no_location through f-string buffering/join paths and call set_no_location() when emitting marked pending literal fragments.
IR: Finalize & Peephole
crates/codegen/src/ir.rs
Replace exception-specific materialization with materialize_empty_conditional_exit_targets; convert peephole optimizer to indexed iteration and preserve pure self-loop block-start NOP anchors; update debug tracing.
IR: Layout-aware NOP Removal & Helpers
crates/codegen/src/ir.rs
Compute layout predecessors and add predicates (keep_target_start_no_location_nop, layout_predecessor_ends_with_pop_iter, is_async_loop_cleanup_block, block_has_simple_scope_exit, leading_bool_guard_has_scope_exit_successor) to drive NOP removal/retention and named-cleanup deopt logic.
IR: Jump Threading Short-Circuit Rules
crates/codegen/src/ir.rs
Add short_circuit_stub_conditional, opposite_short_circuit_target, and same_short_circuit_target and use them in jump_threading_impl to retarget conditional jumps for CPython-style boolean patterns; unit tests added.
IR: Fast-scan for Many Locals
crates/codegen/src/ir.rs
Add fast_scan_many_locals and route uninitialized-load checks through it when nlocals > 64, then clamp back to the smaller path’s supported range.
Tests / Validation
crates/codegen/src/compile.rs, crates/codegen/src/ir.rs
New tests for percent-format preprocess effects, fast-scan parameter initialization behavior, boolean short-circuit threading, loop header/backedge NOP ordering, return-in-loop cleanup sequencing, nested-with NOP retention/drop, and for-loop conditional-raise ordering.
Spell dictionary
.cspell.dict/cpython.txt
Add CPython-specific identifiers used in code/tests.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • fanninpm

Poem

🐰 I hopped the AST with eager cheer,

turned percent signs to f-strings clear,
folded constants only when told,
threaded jumps and kept anchors bold,
tests clapped softly — off I steer.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.90% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Align bytecode CFG cleanup with CPython' is directly related to the main changes across multiple files that refactor and align bytecode control-flow-graph cleanup behavior with CPython standards.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.