Skip to content

Commit 0aefbc1

Browse files
committed
Remove slow mem patching for writes
1 parent 576ad27 commit 0aefbc1

File tree

5 files changed

+24
-58
lines changed

5 files changed

+24
-58
lines changed

src/jit/assembler/basic_block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct BasicBlock {
1515
pub block_entry_start: *mut BlockInstListEntry,
1616
pub block_entry_end: *mut BlockInstListEntry,
1717

18-
pub pad_label: Option<(BlockLabel, bool, i32)>,
18+
pub pad_label: Option<(BlockLabel, i32)>,
1919
pub pad_size: usize,
2020

2121
pub guest_regs_resolved: bool,
@@ -124,7 +124,7 @@ impl BasicBlock {
124124
add_inst = false;
125125
}
126126
BlockInstKind::SaveReg { .. } | BlockInstKind::MarkRegDirty { .. } => {}
127-
BlockInstKind::PadBlock { label, half, correction } => self.pad_label = Some((*label, *half, *correction)),
127+
BlockInstKind::PadBlock { label, correction } => self.pad_label = Some((*label, *correction)),
128128
_ => {
129129
let (inputs, _) = asm.buf.insts[i].get_io();
130130
for guest_reg in inputs.get_guests() {

src/jit/assembler/block_asm.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,11 +723,7 @@ impl<'a> BlockAsm<'a> {
723723
}
724724

725725
pub fn pad_block(&mut self, label: BlockLabel, correction: i32) {
726-
self.insert_inst(BlockInstKind::PadBlock { label, half: false, correction });
727-
}
728-
729-
pub fn pad_block_half(&mut self, label: BlockLabel, correction: i32) {
730-
self.insert_inst(BlockInstKind::PadBlock { label, half: true, correction });
726+
self.insert_inst(BlockInstKind::PadBlock { label, correction });
731727
}
732728

733729
// Convert guest pc with labels into labels
@@ -1055,13 +1051,10 @@ impl<'a> BlockAsm<'a> {
10551051
self.buf.branch_placeholders.resize(basic_blocks.len(), Vec::new());
10561052

10571053
for i in 0..basic_blocks.len() {
1058-
if let Some((label, half, correction)) = basic_blocks[i].pad_label {
1054+
if let Some((label, correction)) = basic_blocks[i].pad_label {
10591055
let block_to_pad_to = *self.buf.basic_block_label_mapping.get(&label.0).unwrap();
10601056
basic_blocks[block_to_pad_to].emit_opcodes(self, 0, block_to_pad_to, used_host_regs);
10611057
basic_blocks[i].pad_size = (basic_blocks[block_to_pad_to].opcodes.len() as i32 + correction) as usize;
1062-
if half {
1063-
basic_blocks[i].pad_size = (basic_blocks[i].pad_size + 1) >> 1;
1064-
}
10651058
}
10661059
}
10671060

src/jit/assembler/block_inst.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ pub enum BlockInstKind {
267267

268268
PadBlock {
269269
label: BlockLabel,
270-
half: bool,
271270
correction: i32,
272271
},
273272
}
@@ -1008,7 +1007,7 @@ impl Debug for BlockInstKind {
10081007
BlockInstKind::GenericGuestInst { inst, .. } => write!(f, "{inst:?}"),
10091008
BlockInstKind::Prologue => write!(f, "Prologue"),
10101009
BlockInstKind::Epilogue { restore_all_regs } => write!(f, "Epilogue restore all regs {restore_all_regs}"),
1011-
BlockInstKind::PadBlock { label, half, correction } => write!(f, "PadBlock {label:?} half: {half} {correction}"),
1010+
BlockInstKind::PadBlock { label, correction } => write!(f, "PadBlock {label:?} {correction}"),
10121011
}
10131012
}
10141013
}

src/jit/emitter/emit_transfer.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
8080
}
8181

8282
let slow_write_label = block_asm.new_label();
83-
let slow_write_patch_label = block_asm.new_label();
8483
let continue_label = block_asm.new_label();
8584

86-
block_asm.branch(slow_write_label, Cond::NV);
87-
block_asm.pad_block_half(slow_write_label, 0);
88-
8985
let cpsr_backup_reg = block_asm.new_reg();
9086
block_asm.mrs_cpsr(cpsr_backup_reg);
9187

@@ -109,8 +105,7 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
109105
);
110106

111107
block_asm.cmp(fast_mmu_offset_reg, 0);
112-
block_asm.branch(slow_write_patch_label, Cond::EQ);
113-
block_asm.pad_block_half(slow_write_label, 0);
108+
block_asm.branch(slow_write_label, Cond::EQ);
114109

115110
let shm_ptr = get_mem!(self.emu).shm.as_ptr();
116111

@@ -133,12 +128,8 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
133128

134129
block_asm.branch(continue_label, Cond::AL);
135130

136-
block_asm.label_unlikely(slow_write_patch_label);
137-
block_asm.call(inst_slow_mem_patch::<true> as *const ());
138-
block_asm.msr_cpsr(cpsr_backup_reg);
139-
block_asm.branch(slow_write_label, Cond::AL);
140-
141131
block_asm.label_unlikely(slow_write_label);
132+
block_asm.msr_cpsr(cpsr_backup_reg);
142133
block_asm.save_context();
143134

144135
let op0_addr = get_regs!(self.emu, CPU).get_reg(op0) as *const _ as u32;
@@ -266,7 +257,7 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
266257
block_asm.label_unlikely(slow_read_patch_label);
267258
let cpsr_backup_reg = block_asm.new_reg();
268259
block_asm.mrs_cpsr(cpsr_backup_reg);
269-
block_asm.call(inst_slow_mem_patch::<false> as *const ());
260+
block_asm.call(inst_slow_mem_patch as *const ());
270261
block_asm.msr_cpsr(cpsr_backup_reg);
271262
block_asm.branch(slow_read_label, Cond::AL);
272263

@@ -377,8 +368,6 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
377368
debug_assert!(non_gp_regs.is_empty());
378369

379370
if inst_info.op.mem_is_write() {
380-
block_asm.branch(slow_label, Cond::NV);
381-
block_asm.pad_block_half(slow_label, 0);
382371
block_asm.mrs_cpsr(cpsr_backup_reg);
383372

384373
let mmu_ptr = get_mmu!(self.emu, CPU).get_mmu_write_tcm().as_ptr();
@@ -395,8 +384,7 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
395384
block_asm.transfer_read(mmu_offset_reg, mmu_ptr_reg, (mmu_index_reg.into(), ShiftType::Lsl, BlockOperand::from(2)), false, MemoryAmount::Word);
396385

397386
block_asm.cmp(mmu_offset_reg, 0);
398-
block_asm.branch(slow_patch_label, Cond::EQ);
399-
block_asm.pad_block_half(slow_label, 0);
387+
block_asm.branch(slow_label, Cond::EQ);
400388

401389
let shm_ptr = get_mem!(self.emu).shm.as_ptr();
402390

@@ -497,17 +485,18 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
497485
};
498486

499487
if use_fast_mem {
500-
block_asm.label_unlikely(slow_patch_label);
501-
if inst_info.op.mem_is_write() {
502-
block_asm.call(inst_slow_mem_patch::<true> as *const ());
503-
} else {
488+
if !inst_info.op.mem_is_write() {
489+
block_asm.label_unlikely(slow_patch_label);
504490
block_asm.mrs_cpsr(cpsr_backup_reg);
505-
block_asm.call(inst_slow_mem_patch::<false> as *const ());
491+
block_asm.call(inst_slow_mem_patch as *const ());
492+
block_asm.msr_cpsr(cpsr_backup_reg);
493+
block_asm.branch(slow_label, Cond::AL);
506494
}
507-
block_asm.msr_cpsr(cpsr_backup_reg);
508-
block_asm.branch(slow_label, Cond::AL);
509495

510496
block_asm.label_unlikely(slow_label);
497+
if inst_info.op.mem_is_write() {
498+
block_asm.msr_cpsr(cpsr_backup_reg);
499+
}
511500
}
512501
block_asm.save_context();
513502
block_asm.call3(

src/jit/inst_jit_handler.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::mmap::flush_icache;
66
use core::slice;
77
use std::arch::asm;
88

9-
pub unsafe extern "C" fn inst_slow_mem_patch<const WRITE: bool>() {
9+
pub unsafe extern "C" fn inst_slow_mem_patch() {
1010
let mut lr: u32;
1111
asm!(
1212
"mov {}, lr",
@@ -47,31 +47,16 @@ pub unsafe extern "C" fn inst_slow_mem_patch<const WRITE: bool>() {
4747

4848
let mut fast_mem_start = 0;
4949
let mut found_non_op = false;
50-
let mut found_mrs_op = false;
5150
for pc_offset in (4..256).step_by(4) {
5251
let ptr = (fast_mem_end - pc_offset) as *const u32;
5352
let opcode = ptr.read();
54-
if WRITE {
55-
if found_mrs_op {
56-
if opcode == nop_opcode {
57-
fast_mem_start = ptr as usize;
58-
break;
59-
}
60-
} else {
61-
let (op, _) = lookup_opcode(opcode);
62-
if *op == Op::MrsRc {
63-
found_mrs_op = true;
64-
}
65-
}
66-
} else {
67-
if found_non_op {
68-
if opcode == nop_opcode {
69-
fast_mem_start = ptr as usize;
70-
break;
71-
}
72-
} else if opcode != nop_opcode {
73-
found_non_op = true;
53+
if found_non_op {
54+
if opcode == nop_opcode {
55+
fast_mem_start = ptr as usize;
56+
break;
7457
}
58+
} else if opcode != nop_opcode {
59+
found_non_op = true;
7560
}
7661
}
7762
debug_assert_ne!(fast_mem_start, 0);

0 commit comments

Comments
 (0)