Align load-fast borrow barriers with CPython#7930
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughRefines 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. ChangesLoad-Fast Label Reuse Passthrough Refinement
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
crates/codegen/src/compile.rs (1)
15228-15446: 💤 Low valueConsider extracting common test patterns into helper functions.
These tests share significant boilerplate for variable/instruction lookup patterns (e.g., finding loads by varname, finding attribute accesses by name, window matching). Extracting helpers like
find_loads_for_var(code, var_name)orfind_attr_instruction(instructions, attr_name)would reduce duplication and make future test additions easier.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/codegen/src/compile.rs` around lines 15228 - 15446, Many tests (e.g., test_while_before_folded_boolop_if_keeps_successor_load_fast_strong, test_with_try_except_tail_keeps_successor_load_fast_strong, test_loop_try_orelse_nested_try_before_next_try_keeps_load_fast_strong, test_try_orelse_with_before_next_try_keeps_load_fast_strong, test_with_try_finally_nested_with_keeps_successor_load_fast_strong) duplicate the same varname/instruction scanning boilerplate; extract shared helpers like find_loads_for_var(code_or_func, "varname") that returns the filtered LoadFast/LoadFastBorrow ops and find_instrs_excluding_cache(instructions) or find_attr_instruction(instructions, "attr") to encapsulate the unit.op matching and OpArg conversion logic used in these tests, then replace the inline iterator chains in each test with calls to those helpers to remove duplication and improve readability.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/codegen/src/ir.rs`:
- Around line 5487-5493: The code is testing load_fast_passthrough on the
already-normalized target (after next_nonempty_block()), so the empty-label case
is never detected; change the check to inspect the raw successor chain (the
block that may be an empty passthrough) before calling next_nonempty_block() or
use the predecessor/raw successor index directly. Concretely, compute or capture
the successor block index prior to normalization (the raw successor used to
produce target), and test blocks[raw_successor_idx].load_fast_passthrough
(instead of blocks[target.idx()].load_fast_passthrough) when deciding the
finally-cleanup escape hatch in the falls_through_from_finally_cleanup /
has_fallthrough_predecessor_matching path; keep the existing calls to
next_nonempty_block()/block_has_finally_cleanup_handler otherwise.
- Around line 6919-6925: has_warm_fallthrough_predecessor currently only checks
immediate predecessors (block.next == target) and thus misses warm predecessors
that reach target via preserved empty passthrough/label-reuse blocks; change it
to perform the same backward fallthrough walk used by the try-end barrier logic:
starting from each candidate block follow fallthrough successors/links (using
block_has_fallthrough and the block.next chain) backwards/forward as that logic
does until you hit a non-empty/real block or a cold/except boundary, and return
true if any such walked chain reaches the target while remaining warm and not an
except_handler. Apply the identical traversal change to the other occurrence
around handler_resume_end (the similar function at the other location noted in
the comment).
---
Nitpick comments:
In `@crates/codegen/src/compile.rs`:
- Around line 15228-15446: Many tests (e.g.,
test_while_before_folded_boolop_if_keeps_successor_load_fast_strong,
test_with_try_except_tail_keeps_successor_load_fast_strong,
test_loop_try_orelse_nested_try_before_next_try_keeps_load_fast_strong,
test_try_orelse_with_before_next_try_keeps_load_fast_strong,
test_with_try_finally_nested_with_keeps_successor_load_fast_strong) duplicate
the same varname/instruction scanning boilerplate; extract shared helpers like
find_loads_for_var(code_or_func, "varname") that returns the filtered
LoadFast/LoadFastBorrow ops and find_instrs_excluding_cache(instructions) or
find_attr_instruction(instructions, "attr") to encapsulate the unit.op matching
and OpArg conversion logic used in these tests, then replace the inline iterator
chains in each test with calls to those helpers to remove duplication and
improve readability.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: fb5ff390-c16f-444b-9356-3e690bd6f79c
📒 Files selected for processing (3)
.cspell.jsoncrates/codegen/src/compile.rscrates/codegen/src/ir.rs
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/codegen/src/ir.rs`:
- Around line 6926-6938: The DFS currently calls block_has_fallthrough() before
checking for empty passthrough labels, causing empty synthetic labels with
load_fast_passthrough or load_fast_label_reuse_passthrough to be treated as
having fallthrough and stop traversal; move the existing empty-passthrough
branch (the condition using block.instructions.is_empty() &&
(block.load_fast_passthrough || block.load_fast_label_reuse_passthrough) &&
!seen[block_idx]) to execute before the block_has_fallthrough(block) check, mark
seen[block_idx] and push the block on the stack there, and only call
block_has_fallthrough(block) afterward so passthrough predecessors are traversed
correctly (references: block_has_fallthrough, load_fast_passthrough,
load_fast_label_reuse_passthrough, seen, stack).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 60ad3cbf-583d-40ed-abce-52fa159968e5
📒 Files selected for processing (1)
crates/codegen/src/ir.rs
Summary by CodeRabbit
Refactor
Tests
Chores