PlayAgain 0103 (fork side): ?mem2inline=0 sets bit 1 of the cached-interpreter mask (DISABLE polarity); the oracles get MEM2 states: tests/jit-oracle-fuzz.mjs and tests/aot-region-oracle-fuzz.mjs --mem2 1,2,3,4 (placeMem2State in tests/helpers/jit-oracle.mjs; loadAotCore(dir, {mem2: true}); --core DIR for the region fuzz). SPDX-License-Identifier: GPL-2.0-or-later --- a/src/core-host.js +++ b/src/core-host.js @@ -2031,8 +2031,9 @@ // Accepts comma-separated category names, a raw 0xHEX value, or a decimal int. // Unknown category names are logged and ignored (so a typo doesn't pretend to // disable things it isn't actually disabling). -// Bits 0, 1 and 2 are unused (patch 0196 removed the helpers they gated). +// Bits 0, 1 and 2 are unused (patch 0196 removed the helpers they gated); PlayAgain's patches use them. const CACHED_INTERPRETER_DISABLE_BITS = { + mem2inline: 1 << 1, // PlayAgain 0103: DISABLE polarity (default-on; ?mem2inline=0): MEM2 through the MMU helpers again dcbxskiplegacy: 1 << 0, // PlayAgain 0101: ENABLE polarity (default-off; ?dcbxskip=legacy): JIT off skips every dcbx-loop invalidation, as before 0101 dcbxloop: 1 << 3, fastbranch: 1 << 4, @@ -2111,6 +2112,11 @@ // ?dcbxskip=legacy brings back the fork's JIT-off skip of every such invalidation. if (params.get("dcbxskip") === "legacy") { mask |= CACHED_INTERPRETER_DISABLE_BITS.dcbxskiplegacy; + } + // PlayAgain 0103: MEM2 (Wii) inline in compiled blocks and the uncompiled blocks' C++ paths; + // ?mem2inline=0 sends MEM2 through the MMU helpers again. + if (params.get("mem2inline") === "0") { + mask |= CACHED_INTERPRETER_DISABLE_BITS.mem2inline; } // ยง28bt block re-dispatch: DEFAULT-ON. Rigorously verified +11% warm // (4-run non-overlapping same-binary A/B, correctness-clean on battle); --- a/tests/helpers/jit-oracle.mjs +++ b/tests/helpers/jit-oracle.mjs @@ -48,6 +48,9 @@ setFprf: (on) => module._DolphinWeb_SetFprfForTest(on ? 1 : 0), // Patch 0129: the FMA emitter's relaxed_madd mode (returns the previous one). setFusedMadd: (on) => module._DolphinWeb_SetFusedMadd(on ? 1 : 0), + // PlayAgain 0103: the oracle's memory mode (0 GameCube; on a Wii's memory: 1 scratch in MEM1, + // 2 MEM2's middle, 3 MEM2's last 256 bytes, 4 its first 256); the scratch address, 0 refused. + setMem2: (mode) => (module._DolphinWeb_PaOracleMem2ForTest?.(mode) ?? 0) >>> 0, // Patch 0159: compile the oracle's blocks as if MSR.FP were set, run them // with the input MSR. compileWithFp: (on) => module._DolphinWeb_JitOracleCompileWithFpForTest(on ? 1 : 0), @@ -278,7 +281,26 @@ return Array.from(new Uint32Array(d.buffer)); } default: return [r.next(), r.next()]; + } +} + +// PlayAgain 0103: a state's bases for a MEM2 mode of the oracle (setMem2): r1 (the scratch base) +// and r30 (the update base) inside the mode's window, each in the cached 0x9 or the uncached 0xD +// view of MEM2. Mode 3 puts r1 128 bytes below MEM2's end (accesses reach its last byte), mode 4 +// 64 bytes above its start (they reach its first). +export function placeMem2State(state, mode, scratchVa, r) { + const view = (va) => (mode >= 2 && r.chance(0.5) ? (va ^ 0x40000000) >>> 0 : va >>> 0); + if (mode === 3) { + state[1] = view(scratchVa + 0x80); + state[30] = view(scratchVa - 0xf00 + 0x80); + } else if (mode === 4) { + state[1] = view(scratchVa + 0x40); + state[30] = view(scratchVa + 0x800); + } else { + state[1] = view(scratchVa + 0x80); + state[30] = view(scratchVa + 0x80); } + return state; } export function randomState(r) { --- a/tests/jit-oracle-fuzz.mjs +++ b/tests/jit-oracle-fuzz.mjs @@ -28,6 +28,7 @@ instructionNames, loadOracleCore, makeRng, + placeMem2State, randomProgram, randomState } from "./helpers/jit-oracle.mjs"; @@ -71,6 +72,24 @@ core.setFusedMadd(Number(opt("fused")) !== 0); } +// PlayAgain 0103: --mem2 1,2,3,4 runs every state on a Wii's memory, its scratch (and r1/r30) in +// the listed modes in turn (DolphinWeb_PaOracleMem2ForTest): 1 MEM1, 2 MEM2's middle, 3 MEM2's +// last bytes, 4 its first bytes, in the 0x9 or 0xD view. +const mem2Modes = opt("mem2", null)?.split(",").map(Number) ?? null; +let mem2Turn = 0; +if (mem2Modes && !core.setMem2(mem2Modes[0])) { + console.error("--mem2: this core has no MEM2 oracle (PlayAgain 0103)"); + process.exit(2); +} +const mem2Count = {}; +function placeMem2(state, r) { + if (!mem2Modes) return state; + const mode = mem2Modes[mem2Turn++ % mem2Modes.length]; + placeMem2State(state, mode, core.setMem2(mode), r); + state.mem2 = mode; + return state; +} + const findings = new Map(); // key -> {count, example} let compared = 0, notCompiled = 0, rejected = 0, raisedException = 0; function record(kind, name, mask, program, state, result) { @@ -83,7 +102,8 @@ program: Array.from(program, hex), message: result.message, differences: describeDifference(result.interp, result.jit).slice(0, 12), - state: Array.from(state, hex) + state: Array.from(state, hex), + ...(state.mem2 !== undefined ? { mem2: state.mem2 } : {}) }; } findings.set(key, entry); @@ -99,6 +119,7 @@ return; } if (result.code === 2) { notCompiled += 1; return; } + if (state.mem2 !== undefined) mem2Count[state.mem2] = (mem2Count[state.mem2] ?? 0) + 1; if (result.code === -3) { rejected += 1; record("engine-reject", name, mask, program, state, result); return; } if (result.code < 0) throw new Error(`oracle setup error ${result.code} (${name}): ${result.message}`); @@ -181,7 +202,7 @@ for (const name of only ?? instructionNames()) { for (let k = 0; k < perInstruction; k++) { const program = Uint32Array.from([generate(name, r), 0x4e800020]); - const state = randomState(r); + const state = placeMem2(randomState(r), r); for (const m of masks) check(name, program, state, SHORT_PREFIX_MASK | m); } } @@ -233,7 +254,7 @@ const r = makeRng(seed ^ 0x9e3779b9); for (let i = 0; i < programs; i++) { const program = randomProgram(r, length, { names: only }); - const state = randomState(r); + const state = placeMem2(randomState(r), r); for (const m of masks) check(`program`, program, state, baseMask | m); } } @@ -242,6 +263,7 @@ // difference stays, so the report shows the few instructions that matter. function reduce(example) { const state = Uint32Array.from(example.state, (h) => Number.parseInt(h, 16)); + if (example.mem2 !== undefined) core.setMem2(example.mem2); let program = Uint32Array.from(example.program, (h) => Number.parseInt(h, 16)); const mask = Number.parseInt(example.mask, 16); const diverges = (p) => { @@ -286,6 +308,7 @@ compared, notCompiled, raisedException, rejected, divergent: list.filter((f) => f.kind === "divergence").reduce((n, f) => n + f.count, 0), fpscrStatus: list.filter((f) => f.kind === "fpscr-status").reduce((n, f) => n + f.count, 0), ...(fmaCases ? { exactChecked, exactMismatch, singleInexact } : {}), + ...(mem2Modes ? { mem2Modes, comparedByMem2Mode: mem2Count } : {}), seconds: (Date.now() - started) / 1000 }; console.log(JSON.stringify(summary)); --- a/tests/helpers/aot-core-equivalence.mjs +++ b/tests/helpers/aot-core-equivalence.mjs @@ -20,7 +20,7 @@ import { orderedRegions } from "../../src/aot-plan.js"; import { REGION_HEAP_NAMES, assembleRegion, groupRegions } from "../../src/aot-region-module.js"; -import { PRODUCTION_DISABLE_MASK, makeRng, randomProgram, randomState } from "./jit-oracle.mjs"; +import { PRODUCTION_DISABLE_MASK, makeRng, placeMem2State, randomProgram, randomState } from "./jit-oracle.mjs"; import { checkBranchHintSection } from "./branch-hints.mjs"; @@ -39,7 +39,8 @@ const SCRATCH = 0x80c00000; // pointer arguments land in 1 MiB nothing else uses const STACK = 0x80f00000; -export async function loadAotCore(coreDir = new URL("../../cores/dolphin/", import.meta.url)) { +// PlayAgain 0103: { mem2: true } sets the test CPU up on a Wii's memory (MEM2 and its BATs). +export async function loadAotCore(coreDir = new URL("../../cores/dolphin/", import.meta.url), { mem2 = false } = {}) { const { default: createDolphinCore } = await import(new URL("dolphin-core-upstream.js", coreDir)); const m = await createDolphinCore({ wasmBinary: readFileSync(new URL("dolphin-core-upstream.wasm", coreDir)), @@ -48,6 +49,7 @@ printErr: () => {} }); if (typeof m._DolphinWeb_AotTranslateForTest !== "function") return null; + if (mem2 && !m._DolphinWeb_PaOracleMem2ForTest?.(1)) throw new Error("core: no MEM2 oracle (PlayAgain 0103)"); const statePtr = m._DolphinWeb_AotTestSetUp() >>> 0; if (!statePtr) throw new Error("core: test CPU set-up failed"); const ramPtr = m._DolphinWeb_AotTestRam() >>> 0; @@ -431,12 +433,22 @@ return { translated: true, fragment, ...result }; } -export function runOracleProgramsAsRegions(core, { seed, count, length, mask, names = null }) { +// PlayAgain 0103: mem2 = [modes]: each program's state in the next mode (placeMem2State), the +// oracle window restored with RAM before each run. +export function runOracleProgramsAsRegions(core, { seed, count, length, mask, names = null, mem2 = null }) { const r = makeRng(seed); + const m = core.module; + let mem2Turn = 0; const tally = { programs: 0, translated: 0, compared: 0, equal: 0, trap: 0, ends: {}, regionRuns: 0, hintsDropped: 0, hintsChecked: 0, hintSectionErrors: [], mismatches: [] }; for (let n = 0; n < count; n++) { const words = randomProgram(r, length, { names }); const state = randomState(r); + if (mem2) { + const mode = mem2[mem2Turn++ % mem2.length]; + placeMem2State(state, mode, m._DolphinWeb_PaOracleMem2ForTest(mode) >>> 0, r); + tally.mem2 = tally.mem2 ?? {}; + tally.mem2[mode] = (tally.mem2[mode] ?? 0) + 1; + } const smallBudget = 1 + r.below(60); tally.programs += 1; core.load(ORACLE_CODE, bigEndian(words)); @@ -453,9 +465,11 @@ if (handle <= 0) throw new Error(`core rejected a region module (${handle})`); const regionAt = new Map(fragment.entries.map((pc) => [pc >>> 0, handle])); const baseline = core.ram().slice(); + if (mem2) m._DolphinWeb_PaOracleWindowForTest(0); for (const budget of [400000, smallBudget]) { const call = (start) => compareCall(core, regionAt, () => { core.ram().set(baseline); + if (mem2) m._DolphinWeb_PaOracleWindowForTest(1); core.loadState(start, ORACLE_CODE); core.setDowncount(budget); }); --- a/tests/aot-region-oracle-fuzz.mjs +++ b/tests/aot-region-oracle-fuzz.mjs @@ -20,7 +20,10 @@ const length = Number(opt("length", "24")); const seed = Number(opt("seed", "1")); const mask = Number(opt("mask", String(PRODUCTION_DISABLE_MASK))) >>> 0; -const core = await loadAotCore(); +// PlayAgain 0103: --mem2 1,2,3,4 (the oracle's MEM2 modes, on a Wii's memory); --core DIR. +const mem2 = opt("mem2", null)?.split(",").map(Number) ?? null; +const coreDir = opt("core", null); +const core = await loadAotCore(coreDir ? new URL(`file://${coreDir.replace(/\/?$/, "/")}`) : undefined, { mem2: !!mem2 }); if (!core?.has("DolphinWeb_AotTestLoadState")) { console.error("this core has no region test exports (patch 0075)"); process.exit(2); @@ -32,6 +35,6 @@ if (opt("fused", null) !== null) core.module._DolphinWeb_SetFusedMadd(Number(opt("fused")) ? 1 : 0); const names = opt("only", null)?.split(",") ?? null; const t0 = performance.now(); -const tally = runOracleProgramsAsRegions(core, { seed, count: programs, length, mask, names }); +const tally = runOracleProgramsAsRegions(core, { seed, count: programs, length, mask, names, mem2 }); console.log(JSON.stringify({ ...tally, mismatches: tally.mismatches.slice(0, 3), mismatchCount: tally.mismatches.length, seconds: Math.round((performance.now() - t0) / 1000) })); process.exit(tally.mismatches.length ? 1 : 0);