From ab21def9d668bcdcd9e008f0cda4205941915dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 15 Oct 2026 15:03:21 +0200 Subject: [PATCH] IR interpreter: Go straight on to the next block at exits Each block was a call into IRInterpret from RunBlocks: register save and restore, the first dispatch through the shared switch, then the shadow lookup in RunBlocks. Now the exits look up the next block themselves and keep dispatching while downcount lasts, the same check RunBlocks makes. Anything else (a hook, an uncompiled block, an invalid address, downcount gone) still returns. Without the vertex JIT, as on iOS: 4.5-10% fewer instructions and 1-4% fewer cycles in the games measured. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/MIPS/IR/IRInst.h | 5 +-- Core/MIPS/IR/IRInterpreter.cpp | 56 +++++++++++++++++++++++----------- Core/MIPS/IR/IRInterpreter.h | 1 - Core/MIPS/IR/IRJit.cpp | 3 +- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/Core/MIPS/IR/IRInst.h b/Core/MIPS/IR/IRInst.h index 2d90f2e3bb..6643b27305 100644 --- a/Core/MIPS/IR/IRInst.h +++ b/Core/MIPS/IR/IRInst.h @@ -406,8 +406,9 @@ struct IRInst { u32 constant; }; -// Returns the new PC. -u32 IRInterpret(MIPSState *ms, const IRInst *inst); +// Returns the new PC. With the block arena, exits go straight on to the next compiled block while +// downcount lasts, instead of returning. +u32 IRInterpret(MIPSState *ms, const IRInst *inst, const IRInst *chainArena = nullptr); // Each IR block gets a constant pool. class IRWriter { diff --git a/Core/MIPS/IR/IRInterpreter.cpp b/Core/MIPS/IR/IRInterpreter.cpp index 28da6e6408..5ff0593c11 100644 --- a/Core/MIPS/IR/IRInterpreter.cpp +++ b/Core/MIPS/IR/IRInterpreter.cpp @@ -83,6 +83,9 @@ u32 IRRunMemCheck(u32 pc, u32 addr) { // With GCC and Clang, each op jumps straight to the next op's handler through a table ("threaded" // dispatch), which predicts much better than the one shared indirect jump of a switch. Other // compilers get the switch. Ops missing from the table fall back to the switch, so they still work. +// Clang keeps one jump per op only while the shared tail stays small, and a direct goto to a handler +// label also makes it give up, leaving a single indirect jump (10-15% slower). Count the `br`s in +// the disassembly after changing the dispatch. #if defined(__GNUC__) || defined(__clang__) #define IR_THREADED_DISPATCH 1 #define IR_CASE(op) case IROp::op: L_##op: @@ -98,11 +101,28 @@ u32 IRRunMemCheck(u32 pc, u32 addr) { #if IR_THREADED_DISPATCH #define IR_NEXT do { IR_CHECK_ZERO_REG(); inst++; goto *dispatch[(int)inst->op]; } while (false) +#define IR_DISPATCH goto *dispatch[(int)inst->op] #else #define IR_NEXT { IR_CHECK_ZERO_REG(); inst++; continue; } +#define IR_DISPATCH goto L_switch #endif -u32 IRInterpret(MIPSState *mips, const IRInst *inst) { +// Leaves the block for pcExpr. While downcount lasts, runs the block there right away if it's +// compiled, the way IRJit::RunBlocks would after we returned (minus the call and the lookup there). +#define IR_EXIT(pcExpr) do { \ + const u32 exitPC = (pcExpr); \ + if (!chainArena || mips->downcount < 0 || !Memory::IsValid4AlignedAddress(exitPC)) \ + return exitPC; \ + const u32 offset = *Memory::GetBlockShadowEntry(exitPC); \ + if (offset <= Memory::BLOCK_SHADOW_HOOK_NO_JIT) \ + return exitPC; \ + mips->pc = exitPC; \ + inst = chainArena + offset; \ + IR_CHECK_ZERO_REG(); \ + IR_DISPATCH; \ +} while (false) + +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const IRInst *chainArena) { #if IR_THREADED_DISPATCH static const void *dispatch[256]; static bool dispatchReady = false; @@ -293,9 +313,7 @@ u32 IRInterpret(MIPSState *mips, const IRInst *inst) { #endif while (true) { -#if IR_THREADED_DISPATCH L_switch: -#endif switch (inst->op) { IR_CASE(SetConst) mips->r[inst->dest] = inst->constant; @@ -1292,47 +1310,47 @@ u32 IRInterpret(MIPSState *mips, const IRInst *inst) { } IR_CASE(ExitToConst) - return inst->constant; + IR_EXIT(inst->constant); IR_CASE(ExitToReg) - return mips->r[inst->src1]; + IR_EXIT(mips->r[inst->src1]); // The next instruction is the ExitToConst to take otherwise, never run itself. IR_CASE(OptExitToConstIfEqElse) - return mips->r[inst->src1] == mips->r[inst->src2] ? inst->constant : inst[1].constant; + IR_EXIT(mips->r[inst->src1] == mips->r[inst->src2] ? inst->constant : inst[1].constant); IR_CASE(OptExitToConstIfNeqElse) - return mips->r[inst->src1] != mips->r[inst->src2] ? inst->constant : inst[1].constant; + IR_EXIT(mips->r[inst->src1] != mips->r[inst->src2] ? inst->constant : inst[1].constant); IR_CASE(OptExitToConstIfGtZElse) - return (s32)mips->r[inst->src1] > 0 ? inst->constant : inst[1].constant; + IR_EXIT((s32)mips->r[inst->src1] > 0 ? inst->constant : inst[1].constant); IR_CASE(OptExitToConstIfGeZElse) - return (s32)mips->r[inst->src1] >= 0 ? inst->constant : inst[1].constant; + IR_EXIT((s32)mips->r[inst->src1] >= 0 ? inst->constant : inst[1].constant); IR_CASE(OptExitToConstIfLtZElse) - return (s32)mips->r[inst->src1] < 0 ? inst->constant : inst[1].constant; + IR_EXIT((s32)mips->r[inst->src1] < 0 ? inst->constant : inst[1].constant); IR_CASE(OptExitToConstIfLeZElse) - return (s32)mips->r[inst->src1] <= 0 ? inst->constant : inst[1].constant; + IR_EXIT((s32)mips->r[inst->src1] <= 0 ? inst->constant : inst[1].constant); IR_CASE(ExitToConstIfEq) if (mips->r[inst->src1] == mips->r[inst->src2]) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(ExitToConstIfNeq) if (mips->r[inst->src1] != mips->r[inst->src2]) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(ExitToConstIfGtZ) if ((s32)mips->r[inst->src1] > 0) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(ExitToConstIfGeZ) if ((s32)mips->r[inst->src1] >= 0) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(ExitToConstIfLtZ) if ((s32)mips->r[inst->src1] < 0) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(ExitToConstIfLeZ) if ((s32)mips->r[inst->src1] <= 0) - return inst->constant; + IR_EXIT(inst->constant); IR_NEXT; IR_CASE(Downcount) @@ -1377,7 +1395,7 @@ u32 IRInterpret(MIPSState *mips, const IRInst *inst) { } IR_CASE(ExitToPC) - return mips->pc; + IR_EXIT(mips->pc); IR_CASE(Interpret) // SLOW fallback. Can be made faster. Ideally should be removed but may be useful for debugging. { @@ -1484,5 +1502,7 @@ u32 IRInterpret(MIPSState *mips, const IRInst *inst) { } #undef IR_NEXT +#undef IR_DISPATCH +#undef IR_EXIT #undef IR_CASE #undef IR_CHECK_ZERO_REG diff --git a/Core/MIPS/IR/IRInterpreter.h b/Core/MIPS/IR/IRInterpreter.h index f555552f49..c970b6ef4f 100644 --- a/Core/MIPS/IR/IRInterpreter.h +++ b/Core/MIPS/IR/IRInterpreter.h @@ -9,7 +9,6 @@ struct IRInst; u32 IRRunBreakpoint(u32 pc); u32 IRRunMemCheck(u32 pc, u32 addr); -u32 IRInterpret(MIPSState *ms, const IRInst *inst); template u32 RunValidateAddress(u32 pc, u32 addr, u32 isWrite) { diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index 5d912650a4..da3117e4e5 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -283,7 +283,8 @@ void IRJit::RunBlocks() { block->profileStats_.executions += 1; block->profileStats_.totalNanos += start.ElapsedNanos(); } else { - mips->pc = IRInterpret(mips, instPtr); + // Runs on through the blocks it can, as long as downcount lasts. + mips->pc = IRInterpret(mips, instPtr, blocks_.GetArenaPtr()); } // Note: this will "jump to zero" on a badly constructed block missing exits. if (!Memory::IsValid4AlignedAddress(mips->pc)) {