Lockstep: match the native block's loop-exit point, not the first arrival - #4
Open
dougchansan wants to merge 1 commit into
Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The lockstep shadow interpreter stopped at the first arrival at a loop header. A generated chunk does not:
emitter.c:324leaves an inlined loop at its header only once the block's charge crossesDOLRECOMP_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
r3decrements,r4advances 32 bytes per iteration, and0x120 = 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'scharge=263.The fix
interp_cyclesis already accumulated andnative_chargeis 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:
CTRLFLOWlines, every shadow reachedend_pcLsIsLoopHeader's 2048-instruction scan window — widening it to 32768 moved 271 → 268, so the window was never the problemWhat remains
107 divergences, each at a distinct entry PC. None diverge on
pc, none fail to reachend_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.
ShouldCheckshort-circuits whenSTATICRECOMP_LOCKSTEPis 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.