Skip to content
Merged
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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Align loop CFG fallthrough with CPython
  • Loading branch information
youknowone committed May 11, 2026
commit 8abe32a9a890a87cbb5b3e62e1891e9ccaee9640
129 changes: 129 additions & 0 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20929,6 +20929,69 @@ def f(obj, flags, writer, value, Error):
);
}

#[test]
fn test_nested_continue_shares_backedge_with_fallthrough_body() {
let code = compile_exec(
"\
def f(names, show_empty, keywords, args_buffer, args, cls, object, level):
for name in names:
value = getattr(cls, name)
if not show_empty:
if value == []:
field_type = cls._field_types.get(name, object)
if getattr(field_type, '__origin__', ...) is list:
if not keywords:
args_buffer.append(repr(value))
continue
if not keywords:
args.extend(args_buffer)
args_buffer = []
value, simple = _format(value, level)
if keywords:
args.append('%s=%s' % (name, value))
else:
args.append(value)
",
);
let f = find_code(&code, "f").expect("missing f code");
let ops: Vec<_> = f
.instructions
.iter()
.map(|unit| unit.op)
.filter(|op| !matches!(op, Instruction::Cache | Instruction::NotTaken))
.collect();

assert!(
ops.windows(4).any(|window| {
matches!(
window,
[
Instruction::PopTop,
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
Instruction::LoadFastBorrow { .. } | Instruction::LoadFast { .. },
Instruction::ToBool,
]
)
}),
"expected CPython-style shared continue backedge before outer condition, got ops={ops:?}"
);
assert!(
!ops.windows(2).any(|window| {
matches!(
window,
[
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
]
)
}),
"unexpected duplicated continue/backedge jumps, got ops={ops:?}"
);
}

#[test]
fn test_line_bearing_loop_if_false_backedge_keeps_body_before_jump_back() {
let code = compile_exec(
Expand Down Expand Up @@ -21189,6 +21252,72 @@ def f(keys, parse_int, d, ampm, AM, PM):
);
}

#[test]
fn test_loop_nested_if_before_elif_keeps_body_before_false_backedge() {
let code = compile_exec(
"\
def f(keys, parse_int, found_dict, locale_time):
hour = minute = 0
for group_key in keys:
if group_key == 'I':
hour = parse_int(found_dict['I'])
ampm = found_dict.get('p', '').lower()
if ampm in ('', locale_time.am_pm[0]):
if hour == 12:
hour = 0
elif ampm == locale_time.am_pm[1]:
if hour != 12:
hour += 12
elif group_key == 'M':
minute = parse_int(found_dict['M'])
return hour, minute
",
);
let f = find_code(&code, "f").expect("missing f code");
let ops: Vec<_> = f
.instructions
.iter()
.map(|unit| unit.op)
.filter(|op| !matches!(op, Instruction::Cache))
.collect();

assert!(
ops.windows(7).any(|window| {
matches!(
window,
[
Instruction::CompareOp { .. },
Instruction::PopJumpIfFalse { .. },
Instruction::NotTaken,
Instruction::LoadSmallInt { .. },
Instruction::StoreFast { .. },
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
]
)
}),
"expected CPython-style nested if body before false backedge, got ops={ops:?}"
);
assert!(
!ops.windows(5).any(|window| {
matches!(
window,
[
Instruction::CompareOp { .. },
Instruction::PopJumpIfTrue { .. },
Instruction::NotTaken,
Instruction::JumpBackward { .. }
| Instruction::JumpBackwardNoInterrupt { .. },
Instruction::LoadSmallInt { .. },
]
)
}),
"unexpected inverted nested if body after false backedge, got ops={ops:?}"
);
}

#[test]
fn test_loop_multiblock_conditional_body_keeps_body_before_jump_back() {
let code = compile_exec(
Expand Down
13 changes: 13 additions & 0 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14745,9 +14745,11 @@ fn reorder_conditional_body_and_implicit_continue_blocks(blocks: &mut Vec<Block>
| Instruction::LoadFastBorrow { .. }
| Instruction::LoadFastLoadFast { .. }
| Instruction::LoadFastBorrowLoadFastBorrow { .. }
| Instruction::LoadDeref { .. }
| Instruction::LoadConst { .. }
| Instruction::LoadSmallInt { .. }
| Instruction::LoadAttr { .. }
| Instruction::BinaryOp { .. }
| Instruction::ContainsOp { .. }
| Instruction::CompareOp { .. }
| Instruction::ToBool
Expand Down Expand Up @@ -14812,6 +14814,10 @@ fn reorder_conditional_body_and_implicit_continue_blocks(blocks: &mut Vec<Block>
block_starts_loop_cleanup(blocks, after_jump_target);
let after_jump_continues_conditional_chain = after_jump_target != BlockIdx::NULL
&& block_is_pure_conditional_test(&blocks[after_jump_target.idx()]);
if after_jump_continues_conditional_chain {
current = next;
continue;
}
let simple_single_block_can_reorder = body_is_single_block
&& !body_tail_is_conditional
&& !has_exceptional_duplicate_condition_line
Expand Down Expand Up @@ -15532,6 +15538,13 @@ fn duplicate_fallthrough_jump_back_targets(blocks: &mut Vec<Block>) {
layout_pred = blocks[layout_pred.idx()].next;
continue;
}
if blocks[target.idx()].instructions[0]
.lineno_override
.is_some_and(|lineno| lineno >= 0)
{
layout_pred = blocks[layout_pred.idx()].next;
continue;
}
if !block_has_no_lineno(&blocks[target.idx()])
&& trailing_conditional_jump_index(&blocks[layout_pred.idx()]).is_some()
{
Expand Down