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
Prev Previous commit
Next Next commit
more cleanup
  • Loading branch information
ShaharNaveh committed Apr 15, 2026
commit c96f81ee4f75c7d9c809a0c94758d74b7683f593
106 changes: 23 additions & 83 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use num_traits::{ToPrimitive, Zero};
use rustpython_compiler_core::{
OneIndexed, SourceLocation,
bytecode::{
AnyInstruction, AnyOpcode, Arg, CO_FAST_CELL, CO_FAST_FREE, CO_FAST_HIDDEN, CO_FAST_LOCAL,
AnyInstruction, AnyOpcode, CO_FAST_CELL, CO_FAST_FREE, CO_FAST_HIDDEN, CO_FAST_LOCAL,
CodeFlags, CodeObject, CodeUnit, CodeUnits, ConstantData, ExceptionTableEntry,
InstrDisplayContext, Instruction, InstructionMetadata, Label, OpArg, Opcode,
PseudoInstruction, PseudoOpcode, PyCodeLocationInfoKind, encode_exception_table, oparg,
Expand Down Expand Up @@ -967,10 +967,7 @@ impl CodeInfo {
let (const_idx, _) = self.metadata.consts.insert_full(ConstantData::Tuple {
elements: Vec::new(),
});
block.instructions[i].instr = Instruction::LoadConst {
consti: Arg::marker(),
}
.into();
block.instructions[i].instr = Opcode::LoadConst.into();
block.instructions[i].arg = OpArg::new(const_idx as u32);
i += 1;
continue;
Expand Down Expand Up @@ -1035,10 +1032,7 @@ impl CodeInfo {
}

// Replace BUILD_TUPLE with LOAD_CONST
block.instructions[i].instr = Instruction::LoadConst {
consti: Arg::marker(),
}
.into();
block.instructions[i].instr = Opcode::LoadConst.into();
block.instructions[i].arg = OpArg::new(const_idx as u32);

i += 1;
Expand Down Expand Up @@ -1108,20 +1102,14 @@ impl CodeInfo {
let eh = block.instructions[i].except_handler;

// slot[start_idx] → BUILD_LIST 0
block.instructions[start_idx].instr = Instruction::BuildList {
count: Arg::marker(),
}
.into();
block.instructions[start_idx].instr = Opcode::BuildList.into();
block.instructions[start_idx].arg = OpArg::new(0);
block.instructions[start_idx].location = folded_loc;
block.instructions[start_idx].end_location = end_loc;
block.instructions[start_idx].except_handler = eh;

// slot[start_idx+1] → LOAD_CONST (tuple)
block.instructions[start_idx + 1].instr = Instruction::LoadConst {
consti: Arg::marker(),
}
.into();
block.instructions[start_idx + 1].instr = Opcode::LoadConst.into();
block.instructions[start_idx + 1].arg = OpArg::new(const_idx as u32);
block.instructions[start_idx + 1].location = folded_loc;
block.instructions[start_idx + 1].end_location = end_loc;
Expand All @@ -1134,7 +1122,7 @@ impl CodeInfo {
}

// slot[i] (was BUILD_LIST) → LIST_EXTEND 1
block.instructions[i].instr = Instruction::ListExtend { i: Arg::marker() }.into();
block.instructions[i].instr = Opcode::ListExtend.into();
block.instructions[i].arg = OpArg::new(1);

i += 1;
Expand Down Expand Up @@ -1224,20 +1212,14 @@ impl CodeInfo {
block.instructions[j].location = folded_loc;
}

block.instructions[i].instr = Instruction::LoadConst {
consti: Arg::marker(),
}
.into();
block.instructions[i].instr = Opcode::LoadConst.into();
block.instructions[i].arg = OpArg::new(const_idx as u32);
i += 2;
continue;
}
}

block.instructions[i].instr = Instruction::BuildTuple {
count: Arg::marker(),
}
.into();
block.instructions[i].instr = Opcode::BuildTuple.into();
i += 2;
} else {
i += 1;
Expand Down Expand Up @@ -1308,19 +1290,13 @@ impl CodeInfo {
let end_loc = block.instructions[i].end_location;
let eh = block.instructions[i].except_handler;

block.instructions[start_idx].instr = Instruction::BuildSet {
count: Arg::marker(),
}
.into();
block.instructions[start_idx].instr = Opcode::BuildSet.into();
block.instructions[start_idx].arg = OpArg::new(0);
block.instructions[start_idx].location = folded_loc;
block.instructions[start_idx].end_location = end_loc;
block.instructions[start_idx].except_handler = eh;

block.instructions[start_idx + 1].instr = Instruction::LoadConst {
consti: Arg::marker(),
}
.into();
block.instructions[start_idx + 1].instr = Opcode::LoadConst.into();
block.instructions[start_idx + 1].arg = OpArg::new(const_idx as u32);
block.instructions[start_idx + 1].location = folded_loc;
block.instructions[start_idx + 1].end_location = end_loc;
Expand Down Expand Up @@ -1606,12 +1582,7 @@ impl CodeInfo {
let idx2 = u32::from(next.arg);
if idx1 < 16 && idx2 < 16 {
let packed = (idx1 << 4) | idx2;
Some((
Instruction::LoadFastLoadFast {
var_nums: Arg::marker(),
},
OpArg::new(packed),
))
Some((Opcode::LoadFastLoadFast.into(), OpArg::new(packed)))
} else {
None
}
Expand All @@ -1631,12 +1602,7 @@ impl CodeInfo {
let idx2 = u32::from(next.arg);
if idx1 < 16 && idx2 < 16 {
let packed = (idx1 << 4) | idx2;
Some((
Instruction::StoreFastStoreFast {
var_nums: Arg::marker(),
},
OpArg::new(packed),
))
Some((Opcode::StoreFastStoreFast.into(), OpArg::new(packed)))
} else {
None
}
Expand Down Expand Up @@ -1670,12 +1636,7 @@ impl CodeInfo {
.metadata
.consts
.insert_full(ConstantData::Boolean { value: !value });
Some((
(Instruction::LoadConst {
consti: Arg::marker(),
}),
OpArg::new(const_idx as u32),
))
Some(((Opcode::LoadConst.into()), OpArg::new(const_idx as u32)))
}
_ => None,
}
Expand Down Expand Up @@ -1749,7 +1710,7 @@ impl CodeInfo {
// LOAD_SMALL_INT oparg is unsigned, so only 0..=255 can be encoded
if let Some(small) = value.to_i32().filter(|v| (0..=255).contains(v)) {
// Convert LOAD_CONST to LOAD_SMALL_INT
instr.instr = Instruction::LoadSmallInt { i: Arg::marker() }.into();
instr.instr = Opcode::LoadSmallInt.into();
// The arg is the i32 value stored as u32 (two's complement)
instr.arg = OpArg::new(small as u32);
}
Expand Down Expand Up @@ -1865,10 +1826,7 @@ impl CodeInfo {
let idx2 = u32::from(next.arg);
if idx1 < 16 && idx2 < 16 {
let packed = (idx1 << 4) | idx2;
block.instructions[i].instr = Instruction::StoreFastLoadFast {
var_nums: Arg::marker(),
}
.into();
block.instructions[i].instr = Opcode::StoreFastLoadFast.into();
block.instructions[i].arg = OpArg::new(packed);
// Replace second instruction with NOP (CPython: INSTR_SET_OP0(inst2, NOP))
set_to_nop(&mut block.instructions[i + 1]);
Expand Down Expand Up @@ -2054,10 +2012,7 @@ impl CodeInfo {
Some(Instruction::LoadFast { var_num }) => {
let var_idx = usize::from(var_num.get(info.arg));
if var_idx < nlocals && unsafe_mask[var_idx] {
info.instr = Instruction::LoadFastCheck {
var_num: Arg::marker(),
}
.into();
info.instr = Opcode::LoadFastCheck.into();
changed = true;
}
if var_idx < nlocals {
Expand All @@ -2075,28 +2030,19 @@ impl CodeInfo {
if needs_check_1 || needs_check_2 {
let mut first = info;
first.instr = if needs_check_1 {
Instruction::LoadFastCheck {
var_num: Arg::marker(),
}
Opcode::LoadFastCheck
} else {
Instruction::LoadFast {
var_num: Arg::marker(),
}
Opcode::LoadFast
}
.into();
first.arg = OpArg::new(idx1 as u32);

let mut second = info;
second.instr = if needs_check_2 {
Instruction::LoadFastCheck {
var_num: Arg::marker(),
}
Opcode::LoadFastCheck.into()
} else {
Instruction::LoadFast {
var_num: Arg::marker(),
}
}
.into();
Opcode::LoadFast.into()
};
second.arg = OpArg::new(idx2 as u32);

new_instructions.push(first);
Expand Down Expand Up @@ -2591,10 +2537,7 @@ fn push_cold_blocks_to_end(blocks: &mut Vec<Block>) {
..Block::default()
};
jump_block.instructions.push(InstructionInfo {
instr: PseudoInstruction::JumpNoInterrupt {
delta: Arg::marker(),
}
.into(),
instr: PseudoOpcode::JumpNoInterrupt.into(),
arg: OpArg::new(0),
target: warm_next,
location: SourceLocation::default(),
Expand Down Expand Up @@ -4043,10 +3986,7 @@ pub(crate) fn convert_pseudo_ops(blocks: &mut [Block], cellfixedoffsets: &[u32])
let cell_relative = i.get(info.arg) as usize;
let new_idx = cellfixedoffsets[cell_relative];
info.arg = OpArg::new(new_idx);
info.instr = Instruction::LoadFast {
var_num: Arg::marker(),
}
.into();
info.instr = Opcode::LoadFast.into();
}
// Jump pseudo ops are resolved during block linearization
PseudoInstruction::Jump { .. } | PseudoInstruction::JumpNoInterrupt { .. } => {}
Expand Down
Loading