Skip to content

Commit 0be59d5

Browse files
committed
Extract skip_caches_if_fallthrough and use cache_entries() in JIT
- Extract duplicated cache-skip logic into skip_caches_if_fallthrough helper in frame.rs (3 call sites) - Use Instruction::cache_entries() in JIT instruction_target instead of hard-coded cache widths
1 parent 470c04d commit 0be59d5

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

crates/jit/src/instructions.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,20 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
186186
instruction: Instruction,
187187
arg: OpArg,
188188
) -> Result<Option<Label>, JitCompileError> {
189+
let caches = instruction.cache_entries() as u32;
189190
let target = match instruction {
190-
Instruction::JumpForward { .. } => Some(Self::jump_target_forward(offset, 0, arg)?),
191-
Instruction::JumpBackward { .. } => Some(Self::jump_target_backward(offset, 1, arg)?),
192-
Instruction::JumpBackwardNoInterrupt { .. } => {
193-
Some(Self::jump_target_backward(offset, 0, arg)?)
191+
Instruction::JumpForward { .. } => {
192+
Some(Self::jump_target_forward(offset, caches, arg)?)
193+
}
194+
Instruction::JumpBackward { .. } | Instruction::JumpBackwardNoInterrupt { .. } => {
195+
Some(Self::jump_target_backward(offset, caches, arg)?)
194196
}
195197
Instruction::PopJumpIfFalse { .. }
196198
| Instruction::PopJumpIfTrue { .. }
197199
| Instruction::PopJumpIfNone { .. }
198200
| Instruction::PopJumpIfNotNone { .. }
199201
| Instruction::ForIter { .. }
200-
| Instruction::Send { .. } => Some(Self::jump_target_forward(offset, 1, arg)?),
202+
| Instruction::Send { .. } => Some(Self::jump_target_forward(offset, caches, arg)?),
201203
_ => None,
202204
};
203205
Ok(target)

crates/vm/src/frame.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,7 @@ impl ExecutingFrame<'_> {
580580

581581
let lasti_before = self.lasti();
582582
let result = self.execute_instruction(op, arg, &mut do_extend_arg, vm);
583-
// Skip past CACHE code units, but only if the instruction did not
584-
// modify lasti (i.e., it did not jump). Jump targets already point
585-
// past CACHE entries since labels include them.
586-
if self.lasti() == lasti_before {
587-
let base_op = op.to_base().unwrap_or(op);
588-
let caches = base_op.cache_entries();
589-
if caches > 0 {
590-
self.update_lasti(|i| *i += caches as u32);
591-
}
592-
}
583+
self.skip_caches_if_fallthrough(op, lasti_before);
593584
match result {
594585
Ok(None) => {}
595586
Ok(Some(value)) => {
@@ -2865,14 +2856,7 @@ impl ExecutingFrame<'_> {
28652856
let mut do_extend_arg = false;
28662857
self.execute_instruction(original_op, arg, &mut do_extend_arg, vm)
28672858
};
2868-
// Skip CACHE entries for the real instruction (only if it didn't jump)
2869-
if self.lasti() == lasti_before_dispatch {
2870-
let base = original_op.to_base().unwrap_or(original_op);
2871-
let caches = base.cache_entries();
2872-
if caches > 0 {
2873-
self.update_lasti(|i| *i += caches as u32);
2874-
}
2875-
}
2859+
self.skip_caches_if_fallthrough(original_op, lasti_before_dispatch);
28762860
result
28772861
}
28782862
Instruction::InstrumentedInstruction => {
@@ -2904,14 +2888,7 @@ impl ExecutingFrame<'_> {
29042888
let mut do_extend_arg = false;
29052889
self.execute_instruction(original_op, arg, &mut do_extend_arg, vm)
29062890
};
2907-
// Skip CACHE entries for the real instruction (only if it didn't jump)
2908-
if self.lasti() == lasti_before_dispatch {
2909-
let base = original_op.to_base().unwrap_or(original_op);
2910-
let caches = base.cache_entries();
2911-
if caches > 0 {
2912-
self.update_lasti(|i| *i += caches as u32);
2913-
}
2914-
}
2891+
self.skip_caches_if_fallthrough(original_op, lasti_before_dispatch);
29152892
result
29162893
}
29172894
_ => {
@@ -3575,6 +3552,19 @@ impl ExecutingFrame<'_> {
35753552
self.update_lasti(|i| *i = target);
35763553
}
35773554

3555+
/// Skip past CACHE code units after an instruction, but only if the
3556+
/// instruction did not modify lasti (i.e., it did not jump).
3557+
#[inline]
3558+
fn skip_caches_if_fallthrough(&mut self, op: Instruction, lasti_before: u32) {
3559+
if self.lasti() == lasti_before {
3560+
let base = op.to_base().unwrap_or(op);
3561+
let caches = base.cache_entries();
3562+
if caches > 0 {
3563+
self.update_lasti(|i| *i += caches as u32);
3564+
}
3565+
}
3566+
}
3567+
35783568
#[inline]
35793569
fn pop_jump_if_relative(
35803570
&mut self,

0 commit comments

Comments
 (0)