Skip to content

Lockstep: match the native block's loop-exit point, not the first arrival - #4

Open
dougchansan wants to merge 1 commit into
ExpansionPak:moderngekko-vendorfrom
dougchansan:lockstep-loop-boundary
Open

Lockstep: match the native block's loop-exit point, not the first arrival#4
dougchansan wants to merge 1 commit into
ExpansionPak:moderngekko-vendorfrom
dougchansan:lockstep-loop-boundary

Conversation

@dougchansan

@dougchansan dougchansan commented Aug 4, 2026

Copy link
Copy Markdown

The lockstep shadow interpreter stopped at the first arrival at a loop header. A generated chunk does not: emitter.c:324 leaves an inlined loop at its header only once the block's charge crosses DOLRECOMP_C_LOOP_CYCLE_BUDGET (default 256). That stopping point is timing-determined, so the shadow cannot infer it from the code — and the two sides were compared at the same PC in different iterations of the same loop.

The signature

entry=0x8000341C  r3: N=0x3299  I=0x32a2     r4: N=0x803640dc  I=0x80363fbc

r3 decrements, r4 advances 32 bytes per iteration, and 0x120 = 9 * 32. Both registers independently say the native side ran exactly 9 more iterations. A per-step trace confirmed the shadow burned 172 cycles against the block's charge=263.

The fix

interp_cycles is already accumulated and native_charge is already a parameter, so the shadow can simply keep iterating until it has done as much work.

Result

Mario Kart: Double Dash, 150 s from a race savestate, MODERNGEKKO_NO_FALLBACK_JIT=1:

271 → 107 divergences, with every loop-header entry gone from the report.

Two hypotheses that died first

Recorded so nobody repeats them:

  • stepcap truncation — refuted: 0 CTRLFLOW lines, every shadow reached end_pc
  • LsIsLoopHeader's 2048-instruction scan window — widening it to 32768 moved 271 → 268, so the window was never the problem

What remains

107 divergences, each at a distinct entry PC. None diverge on pc, none fail to reach end_pc. 31 carry memory diffs, 9 FP, and 2 touch MMIO (0xCC003000) which a replay cannot reproduce by construction. Undiagnosed — this PR does not claim to fix them.

Performance impact

None on normal execution. ShouldCheck short-circuits when STATICRECOMP_LOCKSTEP is unset, so none of this code runs outside verification.

Inside a lockstep run the shadow now executes more instructions per block — it iterates until it matches the native charge instead of stopping at the first loop-header arrival. Measured on a 150 s MKDD race run, lockstep-enabled speed went 0.930x to 0.890x, about 4% slower verification. That is the cost of the shadow doing the same work the native block did, and it is what makes the comparison valid.

…ival

The shadow interpreter stopped at the first arrival at a loop header. A
generated chunk leaves an inlined loop at its header only once the block's
charge crosses DOLRECOMP_C_LOOP_CYCLE_BUDGET (emitter.c:324, default 256), so
its stopping iteration is timing-determined and cannot be inferred from the
code. The two sides were therefore compared at the same PC but in different
iterations of the same loop, producing GPR divergences that had nothing to do
with codegen.

Example: entry=0x8000341C reported r3 N=0x3299/I=0x32a2, r4
N=0x803640dc/I=0x80363fbc. r3 decrements, r4 advances 32 bytes per iteration,
and 0x120 = 9 * 32 -- both agree the native side ran 9 more iterations. A
per-step trace showed the shadow burning 172 cycles against charge=263.

interp_cycles is already accumulated and native_charge is already a parameter,
so the shadow can simply keep going until it has done as much work.

Measured on Mario Kart: Double Dash, 150 s from a race savestate,
MODERNGEKKO_NO_FALLBACK_JIT=1: 271 -> 107 divergences, with every loop-header
entry gone. The remaining 107 are each at a distinct entry PC, none diverge on
pc, and none fail to reach end_pc; 2 touch MMIO (0xCC003000), which a replay
cannot reproduce by construction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant