Align load-fast borrow barriers with CPython by youknowone · Pull Request #7930 · RustPython/RustPython
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 69f5a9d5-9166-4cf0-81f7-4c91018ec757
📒 Files selected for processing (1)
crates/codegen/src/compile.rs
📝 Walkthrough
Walkthrough
Refines LOAD_FAST_BORROW optimization to match CPython empty-label reuse by adding a Block passthrough flag, CFG fallthrough/barrier analysis, stack/AST helpers, and compile-time preservation for folded ifexp, try/finally, loop tails, and with-cleanup; includes many tests.
Changes
Load-Fast Label Reuse Passthrough Refinement
| Layer / File(s) | Summary |
|---|---|
IR block field and folded-load escape detection crates/codegen/src/ir.rs |
Block struct gains load_fast_label_reuse_passthrough. Adds folded_load_escapes_block and integrates per-instruction folded-escape checks into optimize_load_fast_borrow. |
Fallthrough predecessor matching & empty-try barrier crates/codegen/src/ir.rs |
Adds has_fallthrough_predecessor_matching and rewrites has_cpython_empty_try_end_barrier to consider finally-cleanup fallthrough and refined predicates; tightens canonical_target traversal. |
Passthrough redirection predicates crates/codegen/src/ir.rs |
redirect_load_fast_passthrough_targets extended with warm-fallthrough, assertion NOP/failure, try-else/orelse entry, and labeled-successor predicates; passthrough_target rewired to use these and the new flag. |
Compiler helpers and AST/stack inspection crates/codegen/src/compile.rs |
Adds current_block_stack_depth, mark_load_fast_label_reuse_passthrough_block, expands statements_contain_with for nested forms, and adds statements_end_with_try_finally_finalbody_with. |
Constant-folded ifexp assignment handling crates/codegen/src/compile.rs |
When assignment RHS is constant-foldable ifexp, detects empty end blocks using stack inspection and inserts load-fast barriers plus continuation blocks to preserve successor load strength. |
Try-finally exit preservation and marking crates/codegen/src/compile.rs |
Extends preservation checks to consider preserved empty labels for try-finally exits and switches marking to mark_load_fast_label_reuse_passthrough_block at relevant points. |
While-loop / try-orelse / with tail preservation crates/codegen/src/compile.rs |
Adds flags for preserving successor-strong loads after nested try/orelse and with/orelse ends, preserves empty test-prefix boundaries, disables load-fast borrowing when needed, and marks passthrough targets on normal/with-cleanup exits. |
Flag rename, OR-merge update, tests, and cspell crates/codegen/src/compile.rs, crates/codegen/src/ir.rs, .cspell.json |
Renames load_fast_passthrough → load_fast_label_reuse_passthrough, updates OR-merge logic, adds many unit tests validating LoadFast vs LoadFastBorrow across control-flow shapes, and adds ifexp to cspell allowlist. |
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
- RustPython/RustPython#7870: Modifies
optimize_load_fast_borrow/ empty-label fallthrough and CFG traversal behavior in the same LOAD_FAST_BORROW parity area. - RustPython/RustPython#7773: Also changes
crates/codegen/src/ir.rsoptimize_load_fast*traversal and passthrough/barrier handling affecting borrow vs strong load decisions.
Poem
🐰 I hopped through labels, folds, and tries,
I nudged the stack where mystery lies,
With passthrough flags and careful art,
Strong loads keep strength and play their part,
A tiny rabbit's bytecode prize.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 35.21% 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 clearly summarizes the main objective of the pull request: aligning load-fast borrow barriers with CPython behavior. The changes across three files (compile.rs, ir.rs, and .cspell.json) all work toward this central goal of improving CPython parity in the bytecode compiler. |
| 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.
Comment @coderabbitai help to get the list of available commands and usage tips.