Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

dsp loop unroll fix

Layleed1cb104f2e6
diff --git a/crates/gecko/src/flipper/dsp/jit/block.rs b/crates/gecko/src/flipper/dsp/jit/block.rs
index 38120bb..2ae0231 100644
--- a/crates/gecko/src/flipper/dsp/jit/block.rs
+++ b/crates/gecko/src/flipper/dsp/jit/block.rs
@@ -24,7 +24,7 @@ pub struct BlockSpec {
pub instrs: Vec<InstrEntry>,
pub terminator: TermKind,
pub fallthrough_pc: u16,
- pub unrolled_loop: bool,
+ pub unrolled_loop_start: Option<usize>,
}
fn read_imem_word(iram: &[u8], irom: &[u8], addr: u16) -> Option<u16> {
@@ -114,13 +114,36 @@ pub fn discover(iram: &[u8], irom: &[u8], start_pc: u16) -> BlockSpec {
instrs,
terminator: term,
fallthrough_pc: pc,
- unrolled_loop: false,
+ unrolled_loop_start: None,
};
try_unroll_immediate_loop(iram, irom, &mut spec);
spec
}
+fn can_unroll_instruction(raw: u32) -> bool {
+ use disasm::dsp::GcDspInstruction as I;
+
+ if classify(raw as u16).is_some() {
+ return false;
+ }
+
+ let bytes = [(raw >> 8) as u8, raw as u8, (raw >> 24) as u8, (raw >> 16) as u8];
+ let Some((insn, _)) = I::decode(&bytes) else {
+ return false;
+ };
+ let is_stack_register = |reg: u8| (12..=15).contains(&reg);
+
+ match insn {
+ I::Lri { rd, .. } | I::Lr { rd, .. } => !is_stack_register(rd),
+ I::Sr { rs, .. } => !is_stack_register(rs),
+ I::Lrr { d, .. } | I::Lrrd { d, .. } | I::Lrri { d, .. } | I::Lrrn { d, .. } => !is_stack_register(d),
+ I::Srr { s, .. } | I::Srrd { s, .. } | I::Srri { s, .. } | I::Srrn { s, .. } => !is_stack_register(s),
+ I::Mrr { dst, src } => !is_stack_register(dst) && !is_stack_register(src),
+ _ => true,
+ }
+}
+
fn try_unroll_immediate_loop(iram: &[u8], irom: &[u8], spec: &mut BlockSpec) {
const MAX_EXPANDED_INSTRS: usize = 256;
@@ -152,10 +175,6 @@ fn try_unroll_immediate_loop(iram: &[u8], irom: &[u8], spec: &mut BlockSpec) {
return;
};
- if classify(w0).is_some() {
- return;
- }
-
let size = approx_size(w0);
let raw = if size == 2 {
let Some(w1) = read_imem_word(iram, irom, pc.wrapping_add(1)) else {
@@ -165,6 +184,11 @@ fn try_unroll_immediate_loop(iram: &[u8], irom: &[u8], spec: &mut BlockSpec) {
} else {
w0 as u32
};
+
+ if !can_unroll_instruction(raw) {
+ return;
+ }
+
body.push(InstrEntry { pc, raw, size });
let last_word = pc.wrapping_add(size as u16 - 1);
@@ -199,5 +223,5 @@ fn try_unroll_immediate_loop(iram: &[u8], irom: &[u8], spec: &mut BlockSpec) {
spec.terminator = TermKind::LengthLimit;
spec.fallthrough_pc = end_pc.wrapping_add(1);
- spec.unrolled_loop = true;
+ spec.unrolled_loop_start = Some(prefix_len);
}
diff --git a/crates/gecko/src/flipper/dsp/jit/translator.rs b/crates/gecko/src/flipper/dsp/jit/translator.rs
index f5c12a8..a980383 100644
--- a/crates/gecko/src/flipper/dsp/jit/translator.rs
+++ b/crates/gecko/src/flipper/dsp/jit/translator.rs
@@ -21,7 +21,7 @@ fn szat_liveness(spec: &BlockSpec, loop_end_table: &[u8; 0x10000]) -> Vec<bool>
let is_flag_independent_register = |reg: u8| reg != 19 && !(12..=15).contains(&reg);
for (idx, entry) in spec.instrs.iter().enumerate().rev() {
- if !spec.unrolled_loop && loop_end_table[entry.pc as usize] != 0 {
+ if spec.unrolled_loop_start.is_none_or(|start| idx < start) && loop_end_table[entry.pc as usize] != 0 {
live = true;
}
@@ -144,7 +144,8 @@ pub fn translate(
let natural_nia = entry.pc.wrapping_add(entry.size as u16);
let is_last = idx + 1 == spec.instrs.len();
let dynamic_nia = is_last && spec.terminator != TermKind::LengthLimit;
- let marked_loop_end = !spec.unrolled_loop && loop_end_table[entry.pc as usize] != 0;
+ let marked_loop_end =
+ spec.unrolled_loop_start.is_none_or(|start| idx < start) && loop_end_table[entry.pc as usize] != 0;
if is_last || marked_loop_end {
let nia_v = builder.ins().iconst(types::I16, natural_nia as i64);