Repository navigation
iPhone Duo work: volatile-metadata TSO, byte-copy loops, invalidation skips, alias cache and redirect, JIT map, cache fast paths - #40
Draft
Dragooon wants to merge 9 commits into
Conversation
GenerateIR set BlockInForceTSOValidRange only for blocks that contain an instruction listed in the volatile metadata. A block inside a valid range with no listed access (nearly all code) kept full TSO, so neither the image's own MSVC volatile metadata nor a module-wide FEX_EXTENDEDVOLATILEMETADATA entry relaxed it. When the range held no listed instruction at or after the block, the old test also dereferenced ForceTSOInstructions.end(). Now any block inside a valid range drops TSO for every access the metadata does not list; listed accesses keep their ordering as before. Seen with Path of Exile 2 on iOS: with the exe in FEX_EXTENDEDVOLATILEMETADATA its hottest JIT block (a byte-copy loop at exe+0x102a480) still compiled to ldapurb/stlurb per byte. With this change it compiles to ldrb/strb. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
…ns it off) MSVC emits byte-at-a-time copy loops for copies whose size it cannot see (movzx load, byte store, two pointer steps, sub count 1, jne back). FEX translates them one guest instruction at a time, about eight host instructions per byte; in Path of Exile 2 the function holding such a loop (exe+0x1029bb0) was 8.7% of all process CPU. When a decoded block is exactly that loop, OpDispatchBuilder:: MatchByteCopyLoop recognises it and EmitByteCopyLoop emits one forward MemCpy (REP MOVSB semantics, ascending bytes, so overlapping copies match the loop), then leaves the registers and flags the loop would: Src/Dst advanced by n, count 0, the byte register holding the last byte copied, and the flags of SUB 1,1. It applies only where TSO is off for the whole block, not under full SMC checks, hot SMC blocks or extended debug info. On by default; set MADEIRA_FEX_BYTELOOP=0 to turn it off. The first 16 matches log [byteloop]. With it on, the same function fell to 2.1% of process CPU. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
…ecutable InvalidateContainingSection took the exclusive code-invalidation lock, walked every thread's lookup cache and logged an [iOS-xrem] line for every freed section view. Path of Exile 2 streams assets through file views and unmaps about 60 a second (around 23,000 such lines in a 6-minute session), all of them data. Nothing is compiled from memory outside XIntervals: code is only translated from executable memory, and taking a range out of XIntervals invalidates it first. So a freed section that does not intersect XIntervals is returned without the invalidation; the log line becomes [iOS-xrem] via=section-data, printed for the first 8 and then every 1024th. Executable sections take the full path as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
Guest PE images sit inside [g_JitLow, g_JitHigh), so the bounds test let every address in the game's own exe through, and each one walked all ~160 alias entries to answer "not an alias". CompileBlock, QueryGuestExecutableRange, IsHotSMCCodePage and LookupExecutableFileSection all ask; in a Path of Exile 2 profile the walk was 7.5% of process CPU and 16% of the main thread. A 4096-slot cache keyed by 64 KB chunk now holds the answer (entry index or "no entry touches this chunk") in one 64-bit word per slot, tagged with the table generation. BTCpu64IosAddAliasMapping brackets its table changes with a sequence count, so answers from an older table never match and a lookup racing a change takes the walk. Hits are re-checked against their entry every time. A page key thrashed past ~4000 pages; the chunk key fits the game's 49 MB of code. A host test of 12M lookups against the walk gave 0 mismatches, 44 ns -> 2.2 ns per lookup; on device the function left the profile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
…IRA_JITCODE) A CPU sampler sees host PCs; inside JIT code they say nothing about the guest until they are mapped back to the block compiled there. With MADEIRA_JITMAP=<executable-name substring>, that process writes one 32-byte record per block placed in the code buffer (host begin, guest RIP, host size, guest size, ms, seq) to %LOCALAPPDATA%\fex-jitmap-<pid>.bin; guest RIP 0 marks a code cache clear. MADEIRA_JITCODE=<rip|module+0xrva>[,...] additionally dumps the host code of blocks compiled at those entries to fex-jitcode-<pid>.bin. The hook (FEXCore::Core::MadeiraJitMapHook) is null unless enabled and runs under the code buffer write lock, after the block's bytes are copied to their final address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
With L2 off (the iOS default) every L1 miss that L3 answers runs CacheBlockMapping, whose fextl::map descent was 1.25% of all CPU in a capped PoE2 profile, and CompileBlock ran its reports, RIP checks and shared telemetry counters before the lookup. CachedCodePages becomes a robin_map (InvalidateCacheRange walks the shorter of the range's pages or the map), and on FEX_IOS_HOST a hit returns before that code. MADEIRA_FEX_CB_FAST=0 turns the fast path off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shitiz Garg <mail@shitiz.me>
…ock counters Three changes for the capped-CPU stalls in dig2 (C1, C2 and its section 3 counter), each behind a madeira.cfg env toggle: - MADEIRA_FEX_FREE_SKIP (default 1): InvalidateAlignedInterval returns before the exclusive invalidation when a freed range overlaps no X interval and no sub-floor image window, logging a sampled "via=free-data" line. The rule is the one the section-unmap path already uses. InvalidateCacheRange also returns early when the range lies outside the thread's lowest..highest cached page. - MADEIRA_FEX_L1_HASH (default 1): the L1 index becomes (rip ^ rip >> 17) & mask at every site: FindBlock, InvalidateCache (all sizes), CacheBlockMapping, the BranchOps indirect lookup and the three Dispatcher sequences. Read once, shared by codegen and C++. - [fexlock-stats] every ~30 s for CodeInvalidationMutex: exclusive acquisitions/s, hold ms/s and max hold, reader-blocked ms/s and writer-wait ms/s; [l1-miss-t] per-thread L1 miss rate every ~30 s. MADEIRA_FEX_CB_FAST from the previous commit is unchanged. For the Madeira fork only; not meant for FEX-Emu. Signed-off-by: Shitiz Garg <mail@shitiz.me> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…as-redir] stats A pool-copy alias never gets a block, so CompileBlock's fast path could only miss for one under CodeInvalidationMutex and the lookup read lock before ml315 redirected it to the PE VA. Path of Exile 2 reaches x64 code in hybrid DLLs (madeira_d3d12's list-journal methods and trampolines, ntdll's syscall entry) through alias addresses about 450K times a second per frame thread; the locks, FindBlock and the block-map probe were 27% of those threads' CPU. Check the lock-free reverse translation first and return to the dispatcher with the PE VA, as ml315 does. MADEIRA_FEX_ALIAS_FIRST=0 turns it off. Each thread logs its redirect rate and heaviest PE targets every 30 s ([alias-redir]). Signed-off-by: Shitiz Garg <mail@shitiz.me> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Was 16. A profile's hottest functions can then be dumped in one run for a review of the code the JIT emits for them. Measurement only. Signed-off-by: Shitiz Garg <mail@shitiz.me> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
ios-port-2607(1dc6ed4, the commit Madeira'scodex/d3d12-consolidationpins) ←Dragooon:duo-all(9 commits, 3b70cde; 14 files changed, 1,031 insertions, 99 deletions)Impact
No run compared this branch with an unmodified FEX at matched content, so this PR claims no overall figure. Measured on an iPhone with Path of Exile 2, per change:
[alias-redir]line was logged, so most of that gain belongs to the Madeira change. This one covers aliases from any other hybrid DLL.ForceTSOInstructions.end(). The frame-time effect was not isolated.Commits
91c5df971Core: drop TSO in every block inside a volatile-metadata range. GenerateIR only relaxed blocks that contained a listed instruction. Now any block inside a valid range drops TSO for every access the metadata does not list. Listed accesses keep their ordering.999d366dfCore: byte-copy-loop idiom (on by default, MADEIRA_FEX_BYTELOOP=0 turns it off). A block that is exactly MSVC's byte-at-a-time copy loop becomes one forward MemCpy with REP MOVSB semantics, leaving registers and flags as the loop would. Only where TSO is off for the whole block, not under full SMC checks, hot SMC blocks or extended debug info.71d788095Windows: skip code invalidation when an unmapped section was never executable.24557ed7cARM64EC iOS: cache IosJitReverseTranslate answers per 64 KB chunk. A 4096-slot cache tagged with the table generation; a lookup racing a table change takes the walk.85dfcd3a2ARM64EC iOS: opt-in JIT block map and code dump (MADEIRA_JITMAP, MADEIRA_JITCODE).5aa515f35Core: hash-map CachedCodePages and a cache-hit fast path in CompileBlock.MADEIRA_FEX_CB_FAST=0turns the fast path off.e6906d843Core, Windows: skip invalidation for freed data, hash the L1 index, lock counters.MADEIRA_FEX_FREE_SKIP(default 1),MADEIRA_FEX_L1_HASH(default 1, at every L1 site including the dispatcher sequences),[fexlock-stats]and[l1-miss-t]lines.380fdf1d1Core: redirect pool-copy aliases before the cache-hit fast path, [alias-redir] stats. The lock-free reverse translation is checked first and the dispatcher re-enters at the PE address, as the existing redirect does.MADEIRA_FEX_ALIAS_FIRST=0turns it off. Each thread logs its redirect rate and heaviest targets every 30 s.3b70cde73ARM64EC: MADEIRA_JITCODE takes up to 128 entries.Defaults
Left out
MADEIRA_FEX_L1_BIG): the main thread got a million entries and still missed 350-590K times a second, because the misses were aliases, not capacity.Other branches
codex/fex-runtime-consolidation(the FEX thatcodex/dxmt-upstream-build-portpins) and into every other active branch.Testing
380fdf1d1was rebuilt without the big-L1 commit it sat on in the device tree; it only shares a thread-id helper with it.🤖 Generated with Claude Code