PlayAgain's hooks in PPSSPP 3039c4ea96e3430cf4dcad9d34f00e298ac9ace1 for its WebAssembly JIT of the IR (source/pa_wasmjit.cpp; docs/superpowers/specs/2026-11-09-psp-wasm-jit-design.md). Applied by build.sh after ppsspp-3039c4e.patch and ppsspp-3039c4e-webgl.patch, with patch -p1. With the JIT off (pa_set_jit) the two new ops never appear in the arena; FMul's NaN rule (below) holds either way. - Core/MIPS/IR/IRInst.h, IRInst.cpp: two interpreter-only ops, appended to IROp (meta "_C", like Downcount). Each takes the place of a block's leading Downcount and keeps its amount in the constant. BlockEntry charges the downcount and counts the block's entries down in dest | src1 << 8; at 0 the block is hot, the op turns back into a Downcount and the JIT is told (g_paWasmJitHot). CallWasm calls the block's compiled code, the function at table slot dest | src1 << 8 | src2 << 16 (it charges the downcount itself), and exits to the PC it returns with IR_EXIT, so chains of compiled and interpreted blocks go on as before. - Core/MIPS/IR/IRInterpreter.cpp: their handlers, in the threaded dispatch table. CallWasm is a plain call through a function pointer (call_indirect in wasm). If the arena moved during the call (a helper compiled a block: IRJit.cpp's "can be called from IRInterpret"), g_paArenaGen changed and it returns the PC to the dispatcher instead of chaining through the stale arena. g_paCallWasmCount and g_paBlockEntryCount count the two ops for the JIT's stats. FMul: a NaN product (a NaN operand, or inf * 0 either way round) is rebuilt from the operands' bits, the first NaN of (src1, src2) with its quiet bit set, else 0x7fc00000; a product that is a number is stored as it was. Which NaN f32.mul returns for two NaNs is the engine's choice in WebAssembly (an "arithmetic NaN"), and the hardware's rule looks at operand order: V8's x64 optimising tier swaps this multiply's operands (its right operand has no later use), so on x86-64 the handler returned src1's NaN while V8 ran it in Liftoff and src2's once optimised, and the JIT's compiled FMul src1's. ARM64 picks a signalling NaN first. Integer ops give every engine, tier and CPU the same bits; the rule is x86's in the IR's order, the answer x86-64 Liftoff and the JIT gave before. Changed: two NaNs on x86-64 once optimised, and a quiet src1 times a signalling src2 on ARM64. The PSP's own rule for two NaNs is not known (PPSSPP models only inf * 0 and the square root of a negative), and native PPSSPP's answer depends on the host and its register allocation. - Core/MIPS/IR/IRJit.h: IRInterpreterJit's arena is protected instead of private, for the subclass that patches block heads and follows where the arena lives. - Core/MIPS/MIPS.cpp: the main CPU's IR interpreter comes from g_paCreateIRJit when PlayAgain's shim set it (the JIT, a subclass of IRInterpreterJit). The Media Engine keeps the plain IR interpreter. --- a/Core/MIPS/IR/IRInst.h +++ b/Core/MIPS/IR/IRInst.h @@ -275,6 +275,15 @@ Nop, Bad, + + // PlayAgain's WebAssembly JIT (pa_wasmjit.cpp), interpreter-only like the Opt* ops. Each takes the + // place of a block's leading Downcount, keeping its amount in the constant. + // BlockEntry: downcount -= constant, and counts the block's entries down in dest | src1 << 8; at 0 + // the block becomes hot and the op a Downcount again. + BlockEntry, + // CallWasm: runs the block's compiled code, the function at table slot dest | src1 << 8 | src2 << 16 + // (it charges the downcount itself), and exits to the PC it returns. + CallWasm, }; enum IRComparison { --- a/Core/MIPS/IR/IRInst.cpp +++ b/Core/MIPS/IR/IRInst.cpp @@ -218,6 +218,9 @@ { IROp::UpdateRoundingMode, "UpdateRoundingMode", "" }, { IROp::LogIRBlock, "LogIRBlock", "" }, + + { IROp::BlockEntry, "BlockEntry", "_C" }, + { IROp::CallWasm, "CallWasm", "_C" }, }; const IRMeta *metaIndex[256]; --- a/Core/MIPS/IR/IRInterpreter.cpp +++ b/Core/MIPS/IR/IRInterpreter.cpp @@ -32,6 +32,14 @@ #define mips mips #endif +// PlayAgain's WebAssembly JIT (pa_wasmjit.cpp): told when a BlockEntry's count runs out; bumps the +// generation each time it sees its arena move (a compiled block may compile others through a helper); +// and the two entry counts behind its stats. +void (*g_paWasmJitHot)(const IRInst *inst) = nullptr; +u32 g_paArenaGen = 0; +u32 g_paCallWasmCount = 0; +u32 g_paBlockEntryCount = 0; + alignas(16) static const float vec4InitValues[8][4] = { { 0.0f, 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, @@ -374,6 +382,8 @@ dispatch[(int)IROp::LogIRBlock] = &&L_LogIRBlock; dispatch[(int)IROp::Nop] = &&L_Nop; dispatch[(int)IROp::Bad] = &&L_Bad; + dispatch[(int)IROp::BlockEntry] = &&L_BlockEntry; + dispatch[(int)IROp::CallWasm] = &&L_CallWasm; dispatchReady = true; } #endif @@ -1183,13 +1193,18 @@ IR_CASE(FMul) #if 1 { - float a = mips->f[inst->src1]; - float b = mips->f[inst->src2]; - if ((b == 0.0f && my_isinf(a)) || (a == 0.0f && my_isinf(b))) { - mips->fi[inst->dest] = 0x7fc00000; - } else { - mips->f[inst->dest] = a * b; + // PlayAgain: a NaN product (a NaN operand, or inf * 0 either way round) is rebuilt from the + // operands' bits: the first NaN of (src1, src2) with its quiet bit set, else 0x7fc00000, as + // pa_wasmjit.cpp's FMul does. The host's NaN is never kept: for two NaNs it is the engine's + // choice, and V8's x64 optimising tier swapped the operands of upstream's multiply here. + const float r = mips->f[inst->src1] * mips->f[inst->src2]; + u32 ri; + memcpy(&ri, &r, sizeof(ri)); + if ((ri & 0x7FFFFFFF) > 0x7F800000) { + const u32 a = mips->fi[inst->src1], b = mips->fi[inst->src2]; + ri = (a & 0x7FFFFFFF) > 0x7F800000 ? (a | 0x00400000) : (b & 0x7FFFFFFF) > 0x7F800000 ? (b | 0x00400000) : 0x7fc00000; } + mips->fi[inst->dest] = ri; } IR_NEXT; #else @@ -1440,6 +1455,33 @@ mips->downcount -= (int)inst->constant; IR_NEXT; + IR_CASE(BlockEntry) + { + mips->downcount -= (int)inst->constant; + g_paBlockEntryCount++; + IRInst *m = const_cast(inst); + const u16 heat = (u16)((m->dest | (m->src1 << 8)) - 1); + m->dest = (u8)heat; + m->src1 = (u8)(heat >> 8); + if (heat == 0) { + m->op = IROp::Downcount; + g_paWasmJitHot(inst); + } + IR_NEXT; + } + + IR_CASE(CallWasm) + { + g_paCallWasmCount++; + const uintptr_t slot = (uintptr_t)inst->dest | ((uintptr_t)inst->src1 << 8) | ((uintptr_t)inst->src2 << 16); + const u32 gen = g_paArenaGen; + const u32 pc = ((u32 (*)(MIPSState *))slot)(mips); + // The arena moved during the call: chainArena is stale, so leave it to the dispatcher. + if (gen != g_paArenaGen) + return pc; + IR_EXIT(pc); + } + IR_CASE(SetPC) mips->pc = mips->r[inst->src1]; IR_NEXT; --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -275,11 +275,11 @@ return profiling_; } -private: // In IR instructions (8 bytes each). static constexpr u32 MAX_ARENA_SIZE = 4 * 1024 * 1024; - // The blocks' IR. Offsets 0 and 1 are reserved, see RunBlocks. + // The blocks' IR. Offsets 0 and 1 are reserved, see RunBlocks. Protected for PlayAgain's + // WebAssembly JIT (pa_wasmjit.cpp), which patches block heads and follows where it lives. std::vector arena_; }; --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -51,6 +51,10 @@ MIPSState *currentMIPS = &mipsr4k; MIPSDebugInterface debugr4k(&mipsr4k); MIPSDebugInterface *currentDebugMIPS = &debugr4k; + +// PlayAgain: makes the main CPU's IR JIT when set (its WebAssembly JIT, pa_wasmjit.cpp, which +// subclasses the IR interpreter). The Media Engine keeps the plain IR interpreter. +MIPSComp::IRInterpreterJit *(*g_paCreateIRJit)(MIPSState *mips) = nullptr; #if PPSSPP_ARCH(ARM64) @@ -347,7 +351,7 @@ if (PSP_CoreParameter().cpuCore == CPUCore::JIT || PSP_CoreParameter().cpuCore == CPUCore::JIT_IR) { jit = MIPSComp::CreateNativeJit(this); } else if (PSP_CoreParameter().cpuCore == CPUCore::IR_INTERPRETER) { - jit = new MIPSComp::IRInterpreterJit(this); + jit = g_paCreateIRJit ? g_paCreateIRJit(this) : new MIPSComp::IRInterpreterJit(this); } else { jit = nullptr; } @@ -419,7 +423,7 @@ case CPUCore::IR_INTERPRETER: INFO_LOG(Log::CPU, "Switching to IR interpreter"); - newjit = new MIPSComp::IRInterpreterJit(this); + newjit = g_paCreateIRJit && !IsMediaEngine() ? g_paCreateIRJit(this) : new MIPSComp::IRInterpreterJit(this); break; case CPUCore::INTERPRETER: