GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Align bytecode CFG cleanup with CPython by youknowone · Pull Request #7781 · RustPython/RustPython

Align bytecode CFG cleanup with CPython #7781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
youknowone merged 76 commits into RustPython:main from youknowone:bytecode-parity
May 12, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Align bytecode CFG cleanup with CPython #7781

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
c09c475
Align CFG cleanup bytecode with CPython
youknowone Apr 28, 2026
fe64200
remove test
youknowone Apr 30, 2026
94e2ae3
Align bytecode CFG cleanup with CPython
youknowone May 5, 2026
a743ffc
fix
youknowone May 5, 2026
c998628
Align named-except borrow deopts with CPython
youknowone May 5, 2026
68610b9
Remove duplicated Lib test codegen cases
youknowone May 5, 2026
c82c43f
Cache CFG layout predecessors during NOP cleanup
youknowone May 6, 2026
cd9a711
Address bytecode parity review feedback
youknowone May 7, 2026
15a88df
Align nested with cleanup bytecode
youknowone May 7, 2026
4bd27be
Align conditional raise loop backedge ordering
youknowone May 7, 2026
9b257b1
Align percent format optimization with CPython preprocess
youknowone May 7, 2026
8fbe367
Preserve shared finally reraises in CFG cleanup
youknowone May 7, 2026
3ccda96
Align CFG cleanup with CPython finally layout
youknowone May 7, 2026
2aa954f
Align loop CFG anchors with CPython
youknowone May 7, 2026
742ff9b
Preserve loop false-path CFG bodies
youknowone May 7, 2026
6cfa6f2
Align protected store-subscript CFG bytecode
youknowone May 7, 2026
cc59c46
Align borrow deopts with CPython CFG
youknowone May 7, 2026
891e972
Align delete-loop CFG with CPython
youknowone May 7, 2026
19b7427
Align CFG inlining with CPython jumps
youknowone May 7, 2026
d734c06
Align protected CFG jump threading
youknowone May 7, 2026
0e2b226
Narrow handler resume borrow deopt
youknowone May 7, 2026
3bb0430
Preserve branch-local implicit continue targets
youknowone May 7, 2026
55ec30d
Avoid duplicating boolop continue backedges
youknowone May 7, 2026
95c4492
Preserve same-line assert message borrows
youknowone May 7, 2026
8ce6677
Handle nested handler update tail borrows
youknowone May 7, 2026
d37b510
Align terminal handler borrow deopts with CPython CFG
youknowone May 7, 2026
ac086bf
Refine borrow deopts for reraise handler continuations
youknowone May 7, 2026
644d3e3
Refine try-else terminal handler borrow deopts
youknowone May 7, 2026
7adc9f4
Refine protected tail borrow parity
youknowone May 7, 2026
33ec4a6
Refine exception borrow deopt parity
youknowone May 7, 2026
de00801
Align while loop CFG layout with CPython
youknowone May 7, 2026
309b936
Align loop backedge CFG with CPython
youknowone May 7, 2026
f3b8ba1
Handle multi-block scope-exit CFG segments
youknowone May 7, 2026
aa2ca06
Preserve CPython-normalized call-body CFG
youknowone May 7, 2026
cea61f2
Preserve CPython empty if-end return anchor
youknowone May 7, 2026
8be2a54
Match CPython borrow CFG boundaries
youknowone May 7, 2026
f155436
Match CPython tuple unpack constant folding
youknowone May 7, 2026
85c3367
Match CPython implicit continue CFG layout
youknowone May 7, 2026
bb7be66
Keep implicit continue CFG targets in layout
youknowone May 7, 2026
058bc6d
Align try-except end label location with CPython
youknowone May 7, 2026
e4eb97c
Remove folded operand NOPs before line propagation
youknowone May 8, 2026
b022111
Align no-location return exit handling
youknowone May 8, 2026
5e4c096
Align nested protected import bytecode
youknowone May 8, 2026
f7d8240
Align conditional loop backedge layout
youknowone May 8, 2026
5001df7
Align protected loop exit duplication
youknowone May 8, 2026
6b604df
Preserve protected jump-back duplicates
youknowone May 8, 2026
83f109e
Align loop call-body backedge layout
youknowone May 8, 2026
3e11e1f
Align future annotation setup ordering
youknowone May 8, 2026
88ec625
Align named-except cleanup and borrow parity
youknowone May 8, 2026
e24a9b7
Align redundant jump removal with CPython
youknowone May 8, 2026
7f1376b
Preserve finally cleanup jump NOPs
youknowone May 8, 2026
6f95452
Align finally cleanup CFG with CPython
youknowone May 8, 2026
1d078e3
Fix finally cleanup CFG regression
youknowone May 8, 2026
85c70ca
Align async cleanup CFG marker handling
youknowone May 8, 2026
606e816
Preserve CPython continue CFG layout before conditional bodies
youknowone May 8, 2026
b340ca8
Refine protected CFG bytecode parity
youknowone May 8, 2026
330c44c
Align protected CFG cleanup layout
youknowone May 8, 2026
9c737eb
Align CFG cleanup and peephole parity
youknowone May 8, 2026
d35c13d
Align nested loop conditional CFG layout
youknowone May 8, 2026
bba340b
Align named expression comprehension scope
youknowone May 8, 2026
ea757cf
Align CFG cleanup with CPython line markers
youknowone May 8, 2026
555e8fc
Align CFG line marker cleanup with CPython
youknowone May 8, 2026
8abe32a
Align loop CFG fallthrough with CPython
youknowone May 8, 2026
05d96e4
Align conditional CFG fallthrough cases with CPython
youknowone May 8, 2026
a891818
Align protected CFG layout with CPython
youknowone May 8, 2026
24dde16
Align CFG cleanup and type-param calls with CPython
youknowone May 8, 2026
93e8bc4
Align annotation and super call bytecode parity
youknowone May 8, 2026
1b414fe
Align finally reraise tail inlining
youknowone May 8, 2026
7268983
Align protected loop CFG cleanup
youknowone May 8, 2026
a24a791
Align loop CFG bytecode layout with CPython
youknowone May 9, 2026
0941b7a
Align nested loop jump-back layout
youknowone May 9, 2026
448e1f2
Align conditional jump threading with CPython
youknowone May 9, 2026
a448fb6
fix
youknowone May 9, 2026
b3b8329
Skip test_stack_overflow under -u cpu
youknowone May 11, 2026
7790e54
Fix CI lint and clippy errors
youknowone May 12, 2026
19bf583
Speed up CFG cleanup for large compile inputs
youknowone May 12, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Align CFG cleanup with CPython line markers
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@youknowone
youknowone committed May 11, 2026
commit ea757cf5e47ca240f602c8db20f437d4e27dead3

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

93 changes: 93 additions & 0 deletions 93 crates/codegen/src/compile.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23483,6 +23483,99 @@ def f(self):
);
}

#[test]
fn test_reraising_except_loop_break_tail_keeps_post_loop_borrows() {
let code = compile_exec(
"\
def f(flag=1, count=0):
value = 2
while value:
count += 1
try:
if flag and value == 1:
flag -= 1
break
value -= 1
continue
except:
raise
return count > 2 or value != 1
",
);
let f = find_code(&code, "f").expect("missing f code");
let instructions: Vec<_> = f
.instructions
.iter()
.filter(|unit| !matches!(unit.op, Instruction::Cache))
.collect();
let return_idx = instructions
.iter()
.position(|unit| matches!(unit.op, Instruction::ReturnValue))
.expect("missing return");
let tail = &instructions[return_idx.saturating_sub(12)..return_idx];

let load_name = |unit: &CodeUnit| match unit.op {
Instruction::LoadFast { var_num } | Instruction::LoadFastBorrow { var_num } => {
let arg = OpArg::new(u32::from(u8::from(unit.arg)));
Some(f.varnames[usize::from(var_num.get(arg))].as_str())
}
_ => None,
};
for name in ["count", "value"] {
assert!(
tail.iter()
.any(|unit| matches!(unit.op, Instruction::LoadFastBorrow { .. })
&& load_name(unit) == Some(name)),
"post-loop condition should borrow {name}, got tail={tail:?}"
);
assert!(
!tail
.iter()
.any(|unit| matches!(unit.op, Instruction::LoadFast { .. })
&& load_name(unit) == Some(name)),
"post-loop condition should not deopt {name} to LOAD_FAST, got tail={tail:?}"
);
}
}

#[test]
fn test_try_except_continue_keeps_try_line_nop_before_continue_jump() {
let code = compile_exec(
"\
def f(done=False):
while not done:
done = True
try:
continue
except:
done = False
return done
",
);
let f = find_code(&code, "f").expect("missing f code");
let instructions: Vec<_> = f
.instructions
.iter()
.filter(|unit| !matches!(unit.op, Instruction::Cache))
.collect();
assert!(
instructions.windows(2).any(|window| matches!(
window,
[
CodeUnit {
op: Instruction::Nop,
..
},
CodeUnit {
op: Instruction::JumpBackward { .. },
..
}
]
)),
"try/except continue should keep CPython-style try-line NOP before continue jump, got instructions={instructions:?}"
);
}

#[test]
fn test_handler_resume_before_later_loop_keeps_borrowed_tail_loads() {
let code = compile_exec(
Expand Down
24 changes: 19 additions & 5 deletions 24 crates/codegen/src/ir.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5184,6 +5184,13 @@ impl CodeInfo {
})
}

fn block_jumps_unconditionally_to(block: &Block, target: BlockIdx) -> bool {
block
.instructions
.iter()
.any(|info| info.target == target && info.instr.is_unconditional_jump())
}

fn handler_chain_has_explicit_reraise(blocks: &[Block], handler: BlockIdx) -> bool {
let mut cursor = handler;
let mut visited = vec![false; blocks.len()];
Expand Down Expand Up @@ -5409,6 +5416,9 @@ impl CodeInfo {
if block_jumps_backward_to(pred_block, BlockIdx::new(idx as u32)) {
continue;
}
if block_jumps_unconditionally_to(pred_block, BlockIdx::new(idx as u32)) {
continue;
}
let handlers = nonresuming_reraise_handlers(&self.blocks, pred_block);
follows_protected_body[idx] = !handlers.is_empty()
&& !handlers.iter().any(|handler| {
Expand Down Expand Up @@ -7866,6 +7876,7 @@ impl CodeInfo {
let mut predecessors = vec![Vec::new(); self.blocks.len()];
let mut is_handler_resume_block = vec![false; self.blocks.len()];
for (pred_idx, block) in self.blocks.iter().enumerate() {
let block_idx = BlockIdx::new(pred_idx as u32);
if block
.instructions
.iter()
Expand All @@ -7876,12 +7887,12 @@ impl CodeInfo {
{
is_handler_resume_block[pred_idx] = true;
}
if block.next != BlockIdx::NULL {
predecessors[block.next.idx()].push(BlockIdx::new(pred_idx as u32));
if block_has_fallthrough(block) && block.next != BlockIdx::NULL {
predecessors[block.next.idx()].push(block_idx);
}
for info in &block.instructions {
if info.target != BlockIdx::NULL {
predecessors[info.target.idx()].push(BlockIdx::new(pred_idx as u32));
predecessors[info.target.idx()].push(block_idx);
}
}
}
Expand Down Expand Up @@ -12309,8 +12320,11 @@ fn remove_redundant_nops_in_blocks(blocks: &mut [Block]) -> usize {
} else if src_instructions[src + 1].instr.is_unconditional_jump()
&& src_instructions[src + 1].target != block_idx
{
src_instructions[src + 1].lineno_override = Some(lineno);
remove = true;
let next_lineno = instruction_lineno(&src_instructions[src + 1]);
if next_lineno == lineno || next_lineno < 0 {
src_instructions[src + 1].lineno_override = Some(lineno);
remove = true;
}
} else if src_instructions[src + 1].folded_from_nonliteral_expr {
remove = true;
} else {
Expand Down
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.