Skip to content

Commit 56bb7b8

Browse files
committed
x
1 parent 0ffd745 commit 56bb7b8

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

Lib/test/test_peepholer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ def f(cond, true_value, false_value):
545545
self.assertEqual(len(returns), 2)
546546
self.check_lnotab(f)
547547

548+
@unittest.expectedFailure # TODO: RUSTPYTHON; absolute jump encoding
548549
def test_elim_jump_to_uncond_jump(self):
549550
# POP_JUMP_IF_FALSE to JUMP_FORWARD --> POP_JUMP_IF_FALSE to non-jump
550551
def f():
@@ -639,12 +640,14 @@ def g()->1+1:
639640
self.assertNotInBytecode(f, 'BINARY_OP')
640641
self.check_lnotab(f)
641642

643+
@unittest.expectedFailure # TODO: RUSTPYTHON; no BUILD_LIST to BUILD_TUPLE optimization
642644
def test_in_literal_list(self):
643645
def containtest():
644646
return x in [a, b]
645647
self.assertEqual(count_instr_recursively(containtest, 'BUILD_LIST'), 0)
646648
self.check_lnotab(containtest)
647649

650+
@unittest.expectedFailure # TODO: RUSTPYTHON; no BUILD_LIST to BUILD_TUPLE optimization
648651
def test_iterate_literal_list(self):
649652
def forloop():
650653
for x in [a, b]:

crates/codegen/src/ir.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,13 @@ impl CodeInfo {
257257
.filter(|b| b.next != BlockIdx::NULL || !b.instructions.is_empty())
258258
{
259259
// Collect lines that have non-NOP instructions in this block
260-
let non_nop_lines: std::collections::HashSet<_> = block
260+
let non_nop_lines: IndexSet<_> = block
261261
.instructions
262262
.iter()
263263
.filter(|ins| !matches!(ins.instr.real(), Some(Instruction::Nop)))
264264
.map(|ins| ins.location.line)
265265
.collect();
266-
let mut kept_nop_lines: std::collections::HashSet<OneIndexed> =
267-
std::collections::HashSet::new();
266+
let mut kept_nop_lines: IndexSet<OneIndexed> = IndexSet::default();
268267
block.instructions.retain(|ins| {
269268
if matches!(ins.instr.real(), Some(Instruction::Nop)) {
270269
let line = ins.location.line;
@@ -703,10 +702,8 @@ impl CodeInfo {
703702
if matches!(ins.instr.real(), Some(Instruction::Nop)) {
704703
let line = ins.location.line;
705704
if prev_line == Some(line) {
706-
// Same line as previous instruction — safe to remove
707705
return false;
708706
}
709-
// This NOP introduces a new line — keep it
710707
}
711708
prev_line = Some(ins.location.line);
712709
true

crates/compiler-core/src/bytecode.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ pub fn decode_exception_table(table: &[u8]) -> Vec<ExceptionTableEntry> {
116116
let Some(depth_lasti) = read_varint(table, &mut pos) else {
117117
break;
118118
};
119+
let Some(end) = start.checked_add(size) else {
120+
break;
121+
};
119122
entries.push(ExceptionTableEntry {
120123
start,
121-
end: start + size,
124+
end,
122125
target,
123126
depth: (depth_lasti >> 1) as u16,
124127
push_lasti: (depth_lasti & 1) != 0,
@@ -402,8 +405,10 @@ impl CodeUnits {
402405
/// Replace the opcode at `index` in-place without changing the arg byte.
403406
///
404407
/// # Safety
405-
/// Caller must ensure `index` is in bounds and `new_op` has the same
406-
/// arg semantics as the original opcode.
408+
/// - `index` must be in bounds.
409+
/// - `new_op` must have the same arg semantics as the original opcode.
410+
/// - The caller must ensure exclusive access to the instruction buffer
411+
/// (no concurrent reads or writes to the same `CodeUnits`).
407412
pub unsafe fn replace_op(&self, index: usize, new_op: Instruction) {
408413
unsafe {
409414
let ptr = self.0.as_ptr() as *mut CodeUnit;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl Instruction {
434434
/// Panics if called on an already-instrumented opcode.
435435
pub fn to_instrumented(self) -> Option<Self> {
436436
debug_assert!(
437-
self.to_base().is_none(),
437+
!self.is_instrumented(),
438438
"to_instrumented called on already-instrumented opcode {self:?}"
439439
);
440440
Some(match self {

crates/vm/src/builtins/code.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,18 @@ impl PyCode {
953953
| Instruction::PopJumpIfTrue { .. }
954954
| Instruction::PopJumpIfNone { .. }
955955
| Instruction::PopJumpIfNotNone { .. } => {
956-
// left = skip NOT_TAKEN (fall-through)
956+
// left = fall-through (skip NOT_TAKEN if present)
957957
// right = jump target (condition met)
958-
(i * 2, (i + 2) * 2, oparg as usize * 2)
958+
let next_op = instructions
959+
.get(i + 1)
960+
.map(|u| u.op.to_base().unwrap_or(u.op));
961+
let fallthrough =
962+
if matches!(next_op, Some(Instruction::NotTaken)) {
963+
(i + 2) * 2
964+
} else {
965+
(i + 1) * 2
966+
};
967+
(i * 2, fallthrough, oparg as usize * 2)
959968
}
960969
Instruction::EndAsyncFor => {
961970
// src = END_SEND position (next_i - oparg)

crates/vm/src/frame.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,20 +2552,25 @@ impl ExecutingFrame<'_> {
25522552
}
25532553
Err(exc) => {
25542554
// Fire C_RAISE on failure
2555-
if let Some((global_super, arg0)) = call_args {
2556-
let _ = monitoring::fire_c_raise(
2555+
let exc = if let Some((global_super, arg0)) = call_args {
2556+
match monitoring::fire_c_raise(
25572557
vm,
25582558
self.code,
25592559
offset,
25602560
&global_super,
25612561
arg0,
2562-
);
2563-
}
2562+
) {
2563+
Ok(()) => exc,
2564+
Err(monitor_exc) => monitor_exc,
2565+
}
2566+
} else {
2567+
exc
2568+
};
25642569
Err(exc)
25652570
}
25662571
}
25672572
}
2568-
Instruction::InstrumentedJumpForward => {
2573+
Instruction::InstrumentedJumpForward | Instruction::InstrumentedJumpBackward => {
25692574
let src_offset = (self.lasti() - 1) * 2;
25702575
let target = bytecode::Label::from(u32::from(arg));
25712576
self.jump(target);
@@ -2574,15 +2579,6 @@ impl ExecutingFrame<'_> {
25742579
}
25752580
Ok(None)
25762581
}
2577-
Instruction::InstrumentedJumpBackward => {
2578-
let src_offset = (self.lasti() - 1) * 2;
2579-
let dest = bytecode::Label::from(u32::from(arg));
2580-
self.jump(dest);
2581-
if self.monitoring_mask & monitoring::EVENT_JUMP != 0 {
2582-
monitoring::fire_jump(vm, self.code, src_offset, dest.0 * 2)?;
2583-
}
2584-
Ok(None)
2585-
}
25862582
Instruction::InstrumentedForIter => {
25872583
let src_offset = (self.lasti() - 1) * 2;
25882584
let target = bytecode::Label::from(u32::from(arg));

0 commit comments

Comments
 (0)