Skip to content

Commit 37fe0cf

Browse files
authored
Seperate between scope exit & unconditional jump opcodes (#6841)
See: https://github.com/python/cpython/blob/1fa166888bd33538aab3f501174d512d6df22408/Include/internal/pycore_opcode_utils.h#L39-L44 and https://github.com/python/cpython/blob/1fa166888bd33538aab3f501174d512d6df22408/Include/internal/pycore_opcode_utils.h#L52-L55 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Updated compiler control flow analysis for improved accuracy in dead-code elimination and stack-depth tracking. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 5c7f6a2 + 2ee48bc commit 37fe0cf

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

crates/codegen/src/ir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl CodeInfo {
439439
for block in &mut self.blocks {
440440
let mut last_instr = None;
441441
for (i, ins) in block.instructions.iter().enumerate() {
442-
if ins.instr.unconditional_branch() {
442+
if ins.instr.is_scope_exit() || ins.instr.is_unconditional_jump() {
443443
last_instr = Some(i);
444444
break;
445445
}
@@ -545,7 +545,7 @@ impl CodeInfo {
545545
);
546546
}
547547
depth = new_depth;
548-
if instr.unconditional_branch() {
548+
if instr.is_scope_exit() || instr.is_unconditional_jump() {
549549
continue 'process_blocks;
550550
}
551551
}

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,19 @@ impl InstructionMetadata for Instruction {
432432
}
433433
}
434434

435-
fn unconditional_branch(&self) -> bool {
435+
fn is_unconditional_jump(&self) -> bool {
436436
matches!(
437437
self,
438438
Self::JumpForward { .. }
439439
| Self::JumpBackward { .. }
440440
| Self::JumpBackwardNoInterrupt { .. }
441-
| Self::ReturnValue
442-
| Self::RaiseVarargs { .. }
443-
| Self::Reraise { .. }
441+
)
442+
}
443+
444+
fn is_scope_exit(&self) -> bool {
445+
matches!(
446+
self,
447+
Self::ReturnValue | Self::RaiseVarargs { .. } | Self::Reraise { .. }
444448
)
445449
}
446450

@@ -997,7 +1001,11 @@ impl InstructionMetadata for PseudoInstruction {
9971001
}
9981002
}
9991003

1000-
fn unconditional_branch(&self) -> bool {
1004+
fn is_scope_exit(&self) -> bool {
1005+
false
1006+
}
1007+
1008+
fn is_unconditional_jump(&self) -> bool {
10011009
matches!(self, Self::Jump { .. } | Self::JumpNoInterrupt { .. })
10021010
}
10031011

@@ -1085,7 +1093,9 @@ macro_rules! inst_either {
10851093
impl InstructionMetadata for AnyInstruction {
10861094
inst_either!(fn label_arg(&self) -> Option<Arg<Label>>);
10871095

1088-
inst_either!(fn unconditional_branch(&self) -> bool);
1096+
inst_either!(fn is_unconditional_jump(&self) -> bool);
1097+
1098+
inst_either!(fn is_scope_exit(&self) -> bool);
10891099

10901100
inst_either!(fn stack_effect(&self, arg: OpArg) -> i32);
10911101

@@ -1142,16 +1152,9 @@ pub trait InstructionMetadata {
11421152
/// Gets the label stored inside this instruction, if it exists.
11431153
fn label_arg(&self) -> Option<Arg<Label>>;
11441154

1145-
/// Whether this is an unconditional branching.
1146-
///
1147-
/// # Examples
1148-
///
1149-
/// ```
1150-
/// use rustpython_compiler_core::bytecode::{Arg, Instruction, InstructionMetadata};
1151-
/// let jump_inst = Instruction::JumpForward { target: Arg::marker() };
1152-
/// assert!(jump_inst.unconditional_branch())
1153-
/// ```
1154-
fn unconditional_branch(&self) -> bool;
1155+
fn is_scope_exit(&self) -> bool;
1156+
1157+
fn is_unconditional_jump(&self) -> bool;
11551158

11561159
/// What effect this instruction has on the stack
11571160
///

0 commit comments

Comments
 (0)