Skip to content
Merged
Changes from 1 commit
Commits
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
Next Next commit
mark_except_handlers
  • Loading branch information
ShaharNaveh committed Jun 23, 2026
commit a45271734c88323bb4e8d116ffcc9bce48e26b38
59 changes: 29 additions & 30 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,34 @@ impl Blocks {

Ok(res)
}

/// Mark exception handler target blocks.
/// flowgraph.c mark_except_handlers
pub(crate) fn mark_except_handlers(&mut self) -> crate::InternalResult<()> {
#[cfg(debug_assertions)]
{
let mut block_idx = BlockIdx(0);
while block_idx != BlockIdx::NULL {
assert!(!self[block_idx].except_handler);
block_idx = self[block_idx].next;
}
}

let mut block_idx = BlockIdx(0);
while block_idx != BlockIdx::NULL {
let next = self[block_idx].next;
let instr_count = self[block_idx].instruction_used;
for i in 0..instr_count {
let instr = self[block_idx].instructions[i];
if is_block_push(&instr) {
debug_assert!(instr.target != BlockIdx::NULL);
self[instr.target].except_handler = true;
}
}
block_idx = next;
}
Ok(())
}
}

impl From<Vec<Block>> for Blocks {
Expand Down Expand Up @@ -2825,7 +2853,7 @@ fn optimize_code_unit(
// Phase 1: _PyCfg_OptimizeCodeUnit (flowgraph.c)
*blocks = cfg_from_instruction_sequence(instr_sequence)?;
translate_jump_labels_to_targets(blocks)?;
mark_except_handlers(blocks)?;
blocks.mark_except_handlers()?;
label_exception_targets(blocks)?;
optimize_cfg(metadata, blocks, metadata.firstlineno)?;
blocks.remove_unused_consts(&mut metadata.consts)?;
Expand Down Expand Up @@ -5317,35 +5345,6 @@ fn assemble_exception_table(
Ok(table.into_boxed_slice())
}

/// Mark exception handler target blocks.
/// flowgraph.c mark_except_handlers
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn mark_except_handlers(blocks: &mut Blocks) -> crate::InternalResult<()> {
#[cfg(debug_assertions)]
{
let mut block_idx = BlockIdx(0);
while block_idx != BlockIdx::NULL {
assert!(!blocks[block_idx].except_handler);
block_idx = blocks[block_idx].next;
}
}

let mut block_idx = BlockIdx(0);
while block_idx != BlockIdx::NULL {
let next = blocks[block_idx].next;
let instr_count = blocks[block_idx].instruction_used;
for i in 0..instr_count {
let instr = blocks[block_idx].instructions[i];
if is_block_push(&instr) {
debug_assert!(instr.target != BlockIdx::NULL);
blocks[instr.target].except_handler = true;
}
}
block_idx = next;
}
Ok(())
}

/// flowgraph.c mark_cold (two-pass to match CPython).
///
/// Phase 1 (mark_warm): propagate "warm" from entry via fall-through and
Expand Down