Skip to content

Commit 8b0e806

Browse files
committed
Fix rustpython-jit to not use label_map
1 parent 6082049 commit 8b0e806

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

bytecode/src/bytecode.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,7 @@ impl<C: Constant> CodeObject<C> {
595595
}
596596
}
597597

598-
fn display_inner(
599-
&self,
600-
f: &mut fmt::Formatter,
601-
expand_codeobjects: bool,
602-
level: usize,
603-
) -> fmt::Result {
598+
pub fn label_targets(&self) -> BTreeSet<Label> {
604599
let mut label_targets = BTreeSet::new();
605600
for instruction in &self.instructions {
606601
let label = match instruction {
@@ -623,6 +618,16 @@ impl<C: Constant> CodeObject<C> {
623618
};
624619
label_targets.insert(*label);
625620
}
621+
label_targets
622+
}
623+
624+
fn display_inner(
625+
&self,
626+
f: &mut fmt::Formatter,
627+
expand_codeobjects: bool,
628+
level: usize,
629+
) -> fmt::Result {
630+
let label_targets = self.label_targets();
626631

627632
for (offset, instruction) in self.instructions.iter().enumerate() {
628633
let arrow = if label_targets.contains(&Label(offset)) {

jit/src/instructions.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,28 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
103103
}
104104
}
105105

106-
fn get_or_create_block(&mut self, label: &Label) -> Block {
106+
fn get_or_create_block(&mut self, label: Label) -> Block {
107107
let builder = &mut self.builder;
108108
*self
109109
.label_to_block
110-
.entry(*label)
110+
.entry(label)
111111
.or_insert_with(|| builder.create_block())
112112
}
113113

114114
pub fn compile<C: bytecode::Constant>(
115115
&mut self,
116116
bytecode: &CodeObject<C>,
117117
) -> Result<(), JitCompileError> {
118-
let offset_to_label: HashMap<&usize, &Label> =
119-
bytecode.label_map.iter().map(|(k, v)| (v, k)).collect();
118+
// TODO: figure out if this is sufficient -- previously individual labels were associated
119+
// pretty much per-bytecode that uses them, or at least per "type" of block -- in theory an
120+
// if block and a with block might jump to the same place. Now it's all "flattened", so
121+
// there might be less distinction between different types of blocks going off
122+
// label_targets alone
123+
let label_targets = bytecode.label_targets();
120124

121125
for (offset, instruction) in bytecode.instructions.iter().enumerate() {
122-
if let Some(&label) = offset_to_label.get(&offset) {
126+
let label = Label(offset);
127+
if label_targets.contains(&label) {
123128
let block = self.get_or_create_block(label);
124129

125130
// If the current block is not terminated/filled just jump
@@ -190,7 +195,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
190195
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
191196

192197
let val = self.boolean_val(cond)?;
193-
let then_block = self.get_or_create_block(target);
198+
let then_block = self.get_or_create_block(*target);
194199
self.builder.ins().brz(val, then_block, &[]);
195200

196201
let block = self.builder.create_block();
@@ -203,7 +208,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
203208
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
204209

205210
let val = self.boolean_val(cond)?;
206-
let then_block = self.get_or_create_block(target);
211+
let then_block = self.get_or_create_block(*target);
207212
self.builder.ins().brnz(val, then_block, &[]);
208213

209214
let block = self.builder.create_block();
@@ -213,7 +218,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
213218
Ok(())
214219
}
215220
Instruction::Jump { target } => {
216-
let target_block = self.get_or_create_block(target);
221+
let target_block = self.get_or_create_block(*target);
217222
self.builder.ins().jump(target_block, &[]);
218223

219224
Ok(())

0 commit comments

Comments
 (0)