Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Fix test__opcode.py
  • Loading branch information
ShaharNaveh committed Mar 17, 2026
commit 5f8f38d2bdccb15ea240fb1126cd355eefa5fb9d
2 changes: 0 additions & 2 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_is_valid(self):
opcodes = [dis.opmap[opname] for opname in names]
self.check_bool_function_result(_opcode.is_valid, opcodes, True)

@unittest.expectedFailure # TODO: RUSTPYTHON; KeyError: 'BINARY_OP_ADD_INT'
def test_opmaps(self):
def check_roundtrip(name, map):
return self.assertEqual(opcode.opname[map[name]], name)
Expand Down Expand Up @@ -116,7 +115,6 @@ def test_stack_effect_jump(self):


class SpecializationStatsTests(unittest.TestCase):
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: 'load_attr' not found in []
def test_specialization_stats(self):
stat_names = ["success", "failure", "hit", "deferred", "miss", "deopt"]
specialized_opcodes = [
Expand Down
33 changes: 21 additions & 12 deletions crates/compiler-core/src/bytecode/instruction.rs
Copy link
Copy Markdown
Contributor Author

@ShaharNaveh ShaharNaveh Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this file are purely "cosmetic". Altering the arms order (and variants order) so the generated output will match CPython

Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ impl TryFrom<u8> for Instruction {
impl Instruction {
/// Returns `true` if this is any instrumented opcode
/// (regular INSTRUMENTED_*, INSTRUMENTED_LINE, or INSTRUMENTED_INSTRUCTION).
pub fn is_instrumented(self) -> bool {
pub const fn is_instrumented(self) -> bool {
self.to_base().is_some()
|| matches!(self, Self::InstrumentedLine | Self::InstrumentedInstruction)
}
Expand Down Expand Up @@ -509,7 +509,7 @@ impl Instruction {
///
/// The returned base opcode uses `Arg::marker()` for typed fields —
/// only the opcode byte matters since `replace_op` preserves the arg byte.
pub fn to_base(self) -> Option<Self> {
pub const fn to_base(self) -> Option<Self> {
Some(match self {
Self::InstrumentedResume => Self::Resume {
context: Arg::marker(),
Expand Down Expand Up @@ -555,11 +555,10 @@ impl Instruction {
_ => return None,
})
}

/// Map a specialized opcode back to its adaptive (base) variant.
/// Map a specialized or instrumented opcode back to its adaptive (base) variant.
/// `_PyOpcode_Deopt`
pub fn deoptimize(self) -> Self {
match self {
pub const fn deopt(self) -> Option<Self> {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have extracted the specializations depot part of the deoptimize method into a separate one called deopt.
The behavior of deoptimize did not change

Some(match self {
// RESUME specializations
Self::ResumeCheck => Self::Resume {
context: Arg::marker(),
Expand Down Expand Up @@ -678,17 +677,27 @@ impl Instruction {
Self::CallKwBoundMethod | Self::CallKwPy | Self::CallKwNonPy => Self::CallKw {
argc: Arg::marker(),
},
// Instrumented opcodes map back to their base
_ => match self.to_base() {
Some(base) => base,
None => self,
},
_ => return None,
})
}

/// Map a specialized opcode back to its adaptive (base) variant.
pub const fn deoptimize(self) -> Self {
match self.deopt() {
Some(v) => v,
None => {
// Instrumented opcodes map back to their base
match self.to_base() {
Some(v) => v,
None => self,
}
}
}
}

/// Number of CACHE code units that follow this instruction.
/// _PyOpcode_Caches
pub fn cache_entries(self) -> usize {
pub const fn cache_entries(self) -> usize {
match self {
// LOAD_ATTR: 9 cache entries
Self::LoadAttr { .. }
Expand Down
5 changes: 5 additions & 0 deletions crates/stdlib/src/_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ mod _opcode {

let opcode = Opcode::try_from_pyint(args.opcode, vm)?;

// Raise ValueError if specialized.
if opcode.inner().real().is_some_and(|op| op.deopt().is_some()) {
return Err(vm.new_value_error("invalid opcode or oparg"));
}

let _ = jump; // Python API accepts jump but it's not used
Ok(opcode.stack_effect(oparg))
}
Expand Down