From ffc287903e52f5c53b4e37c98eecc73784c0a66a Mon Sep 17 00:00:00 2001 From: Martino Fontana Date: Mon, 19 Oct 2026 10:39:01 +0000 Subject: [PATCH] Jit: Invalidate instruction block cache only with dcbf Dolphin uses `dcbi` and `dcbst` as heuristics to invalidate the instruction block cache, as some games do not use `icbi` properly. However, no games are known to depend on this (unfortunately, the commits that introduced it, e03fd9a942e9851ca29b590b99c7f311b043c021 or b27f47148809ff027e7bdfef6cd0a16fb69a69eb, don't give any details). The problem is that some games are known to be negatively affected by the unnecessary invalidation and suffer heavy stutters due to it (NBA Live 2005/06, Harry Potter and the Prisoner of Azkaban). Removing the heuristic for these instructions completely fixes their stutters. `dcbf` also invalidates the block cache, but it's known to fix a crash after loading an area in games developed by Artificial Mind and Movement (Monster House, Scooby-Doo! Mystery Mayhem, Scooby-Doo! Unmasked), so this one is kept. Maybe this is not the best heuristic, but it's one that currently works. Closes https://bugs.dolphin-emu.org/issues/12825. --- .../Interpreter/Interpreter_LoadStore.cpp | 33 +++++++------------ Source/Core/Core/PowerPC/Jit64/Jit.h | 1 - .../Core/Core/PowerPC/Jit64/Jit64_Tables.cpp | 8 ++--- .../Core/Core/PowerPC/Jit64/Jit_LoadStore.cpp | 21 ++---------- Source/Core/Core/PowerPC/JitArm64/Jit.h | 1 - .../PowerPC/JitArm64/JitArm64_LoadStore.cpp | 21 ++---------- .../Core/PowerPC/JitArm64/JitArm64_Tables.cpp | 8 ++--- 7 files changed, 24 insertions(+), 69 deletions(-) diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStore.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStore.cpp index 035904930d..b237ba22c9 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStore.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStore.cpp @@ -471,9 +471,18 @@ void Interpreter::dcbf(Interpreter& interpreter, UGeckoInstruction inst) const u32 address = Helper_Get_EA_X(ppc_state, inst); if (!ppc_state.m_enable_dcache) { - // Invalidate the JIT cache here as a heuristic to compensate for - // the lack of precise L1 icache emulation in the JIT. (Portable software - // should use icbi consistently, but games aren't portable.) + // Some games do not use `icbi` properly, and the JIT does not emulate the ICache accurately. + // Therefore, we use this instruction as an heuristic for invalidating the block cache. This is + // known to solve a crash after loading an area in games developed by Artificial Mind and + // Movement (Monster House, Scooby-Doo! Mystery Mayhem, Scooby-Doo! Unmasked). + // Note that this could cause unnecessary invalidation that would result in lengthy stutters. In + // the past `dcbi` and `dcbst` invalidated the block cache as well, and games like NBA Live + // 2005/06 or Harry Potter and the Prisoner of Azkaban are known to have suffered because of + // that (some games like The Last Story were fixed by making `dcbst` do nothing if it was + // preceded by `dcbt`/`dcbtst`). + // No games are known to be negatively affected with just `dcbf`, but if some were found, then + // this behavior would probably need to become an option. + interpreter.m_system.GetJitInterface().InvalidateICacheLine(address); return; } @@ -491,15 +500,6 @@ void Interpreter::dcbi(Interpreter& interpreter, UGeckoInstruction inst) } const u32 address = Helper_Get_EA_X(ppc_state, inst); - if (!ppc_state.m_enable_dcache) - { - // Invalidate the JIT cache here as a heuristic to compensate for - // the lack of precise L1 icache emulation in the JIT. (Portable software - // should use icbi consistently, but games aren't portable.) - interpreter.m_system.GetJitInterface().InvalidateICacheLine(address); - return; - } - interpreter.m_mmu.InvalidateDCacheLine(address); } @@ -507,15 +507,6 @@ void Interpreter::dcbst(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; const u32 address = Helper_Get_EA_X(ppc_state, inst); - if (!ppc_state.m_enable_dcache) - { - // Invalidate the JIT cache here as a heuristic to compensate for - // the lack of precise L1 icache emulation in the JIT. (Portable software - // should use icbi consistently, but games aren't portable.) - interpreter.m_system.GetJitInterface().InvalidateICacheLine(address); - return; - } - interpreter.m_mmu.StoreDCacheLine(address); } diff --git a/Source/Core/Core/PowerPC/Jit64/Jit.h b/Source/Core/Core/PowerPC/Jit64/Jit.h index 34275eada4..69b6e279fe 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit.h +++ b/Source/Core/Core/PowerPC/Jit64/Jit.h @@ -253,7 +253,6 @@ public: void negx(UGeckoInstruction inst); void slwx(UGeckoInstruction inst); void srwx(UGeckoInstruction inst); - void dcbt(UGeckoInstruction inst); void dcbz(UGeckoInstruction inst); void subfic(UGeckoInstruction inst); diff --git a/Source/Core/Core/PowerPC/Jit64/Jit64_Tables.cpp b/Source/Core/Core/PowerPC/Jit64/Jit64_Tables.cpp index be5458bffc..fa5e723c25 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit64_Tables.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit64_Tables.cpp @@ -199,11 +199,11 @@ constexpr std::array s_table31{{ {824, &Jit64::srawix}, // srawix {24, &Jit64::slwx}, // slwx - {54, &Jit64::dcbx}, // dcbst + {54, &Jit64::DoNothing}, // dcbst {86, &Jit64::dcbx}, // dcbf - {246, &Jit64::dcbt}, // dcbtst - {278, &Jit64::dcbt}, // dcbt - {470, &Jit64::dcbx}, // dcbi + {246, &Jit64::DoNothing}, // dcbtst + {278, &Jit64::DoNothing}, // dcbt + {470, &Jit64::DoNothing}, // dcbi {758, &Jit64::DoNothing}, // dcba {1014, &Jit64::dcbz}, // dcbz diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_LoadStore.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_LoadStore.cpp index 89599f279e..8500081804 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_LoadStore.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_LoadStore.cpp @@ -238,6 +238,8 @@ void Jit64::dcbx(UGeckoInstruction inst) JITDISABLE(bJITLoadStoreOff); FALLBACK_IF(m_accurate_cpu_cache_enabled); + // See Interpreter::dcbf comment on explanation of why this exists. + // Check if the next instructions match a known looping pattern: // - dcbx rX // - addi rX,rX,32 @@ -400,25 +402,6 @@ void Jit64::dcbx(UGeckoInstruction inst) SetJumpTarget(done); } -void Jit64::dcbt(UGeckoInstruction inst) -{ - INSTRUCTION_START - JITDISABLE(bJITLoadStoreOff); - - // Prefetch. Since we don't emulate the data cache, we don't need to do anything. - - // If a dcbst follows a dcbt, it probably isn't a case of dynamic code - // modification, so don't bother invalidating the jit block cache. - // This is important because invalidating the block cache when we don't - // need to is terrible for performance. - // (Invalidating the jit block cache on dcbst is a heuristic.) - if (CanMergeNextInstructions(1) && js.op[1].inst.OPCD == 31 && js.op[1].inst.SUBOP10 == 54 && - js.op[1].inst.RA == inst.RA && js.op[1].inst.RB == inst.RB) - { - js.skipInstructions = 1; - } -} - // Zero cache line. void Jit64::dcbz(UGeckoInstruction inst) { diff --git a/Source/Core/Core/PowerPC/JitArm64/Jit.h b/Source/Core/Core/PowerPC/JitArm64/Jit.h index 0472b36405..ebbae0205d 100644 --- a/Source/Core/Core/PowerPC/JitArm64/Jit.h +++ b/Source/Core/Core/PowerPC/JitArm64/Jit.h @@ -143,7 +143,6 @@ public: void lmw(UGeckoInstruction inst); void stmw(UGeckoInstruction inst); void dcbx(UGeckoInstruction inst); - void dcbt(UGeckoInstruction inst); void dcbz(UGeckoInstruction inst); void eieio(UGeckoInstruction inst); diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp index 7e5da7a904..e2f6afa31b 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp @@ -706,6 +706,8 @@ void JitArm64::dcbx(UGeckoInstruction inst) JITDISABLE(bJITLoadStoreOff); FALLBACK_IF(m_accurate_cpu_cache_enabled); + // See Interpreter::dcbf comment on explanation of why this invalidates the block cache. + u32 a = inst.RA, b = inst.RB; // Check if the next instructions match a known looping pattern: @@ -878,25 +880,6 @@ void JitArm64::dcbx(UGeckoInstruction inst) gpr.Unlock(loop_counter); } -void JitArm64::dcbt(UGeckoInstruction inst) -{ - INSTRUCTION_START - JITDISABLE(bJITLoadStoreOff); - - // Prefetch. Since we don't emulate the data cache, we don't need to do anything. - - // If a dcbst follows a dcbt, it probably isn't a case of dynamic code - // modification, so don't bother invalidating the jit block cache. - // This is important because invalidating the block cache when we don't - // need to is terrible for performance. - // (Invalidating the jit block cache on dcbst is a heuristic.) - if (CanMergeNextInstructions(1) && js.op[1].inst.OPCD == 31 && js.op[1].inst.SUBOP10 == 54 && - js.op[1].inst.RA == inst.RA && js.op[1].inst.RB == inst.RB) - { - js.skipInstructions = 1; - } -} - void JitArm64::dcbz(UGeckoInstruction inst) { INSTRUCTION_START diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Tables.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Tables.cpp index dc72083d39..438d1ffc15 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Tables.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Tables.cpp @@ -199,11 +199,11 @@ constexpr std::array s_table31{{ {824, &JitArm64::srawix}, // srawix {24, &JitArm64::slwx}, // slwx - {54, &JitArm64::dcbx}, // dcbst + {54, &JitArm64::DoNothing}, // dcbst {86, &JitArm64::dcbx}, // dcbf - {246, &JitArm64::dcbt}, // dcbtst - {278, &JitArm64::dcbt}, // dcbt - {470, &JitArm64::dcbx}, // dcbi + {246, &JitArm64::DoNothing}, // dcbtst + {278, &JitArm64::DoNothing}, // dcbt + {470, &JitArm64::DoNothing}, // dcbi {758, &JitArm64::DoNothing}, // dcba {1014, &JitArm64::dcbz}, // dcbz