Skip to content

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
willfaust:ios-port-2607from
Dragooon:duo-all
Draft

Dragooon wants to merge 9 commits into
willfaust:ios-port-2607from
Dragooon:duo-all

Conversation

@Dragooon

@Dragooon Dragooon commented Nov 22, 2026 •

Copy link
Copy Markdown

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 redirect before the cache-hit fast path (new): a pool-copy alias never gets a block, so each call through one took the invalidation mutex and the lookup read lock before being redirected. Before, the frame threads called through aliases about 450K times a second each and FEX was 28.6% of their CPU. It went onto the phone together with the Madeira PR's jr-pe-entries, after which FEX was 3.0% and no [alias-redir] line was logged, so most of that gain belongs to the Madeira change. This one covers aliases from any other hybrid DLL.
  • Byte-copy-loop idiom: the function holding the game's byte-copy loop (exe+0x1029bb0) went from 8.7% to 2.1% of process CPU.
  • Alias reverse-translation cache: the walk was 7.5% of process CPU and 16% of the main thread. On the device the function left the profile. A host test of 12M lookups against the walk gave 0 mismatches, 44 ns -> 2.2 ns per lookup.
  • Volatile-metadata TSO fix: the game's hottest JIT block, a byte-copy loop, compiled to ldapurb/stlurb per byte before and to ldrb/strb after. It also removes an out-of-range dereference of ForceTSOInstructions.end(). The frame-time effect was not isolated.
  • Section-unmap invalidation skip: about 60 data-section unmaps a second each took the exclusive invalidation lock and walked every thread's cache. They now return early. The CPU saved was not isolated.
  • CachedCodePages hash map and CompileBlock fast path: the fextl::map descent was 1.25% of all CPU in a capped profile. The after-figure was not measured separately.
  • Freed-data skip, L1 hash, lock counters: aimed at capped-CPU stalls. Not isolated.
  • JIT block map and code dump: diagnostic. The code dump now takes up to 128 functions, so a profile's hottest code can be dumped in one run.

Commits

  1. 91c5df971 Core: 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.
  2. 999d366df Core: 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.
  3. 71d788095 Windows: skip code invalidation when an unmapped section was never executable.
  4. 24557ed7c ARM64EC 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.
  5. 85dfcd3a2 ARM64EC iOS: opt-in JIT block map and code dump (MADEIRA_JITMAP, MADEIRA_JITCODE).
  6. 5aa515f35 Core: hash-map CachedCodePages and a cache-hit fast path in CompileBlock. MADEIRA_FEX_CB_FAST=0 turns the fast path off.
  7. e6906d843 Core, 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.
  8. 380fdf1d1 Core: 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=0 turns it off. Each thread logs its redirect rate and heaviest targets every 30 s.
  9. 3b70cde73 ARM64EC: MADEIRA_JITCODE takes up to 128 entries.

Defaults

  • On by default: the TSO fix (no switch), byte-copy loop, section-unmap skip, alias cache, alias redirect, CB fast path, freed-data skip, L1 hash.
  • Opt-in: JIT map and code dump.

Left out

  • Inline QPC and SRW import calls: they read a data block only the Duo's wine build provides, and the wine PR leaves that out.
  • A big L1 for the hottest threads (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.
  • A per-thread compile census: it answered its question (frame threads compile almost nothing during play).

Other branches

  • No branch of willfaust/FEX already has any of these commits (checked by patch id on 22 Nov).
  • It merges cleanly into codex/fex-runtime-consolidation (the FEX that codex/dxmt-upstream-build-port pins) and into every other active branch.

Testing

  • The phone runs these changes in xtajit64 5ff472ec (since 21 Nov), which also has the three left-out commits.
  • This branch builds as the app's xtajit64.dll (ARM64EC, llvm-mingw 20260421, Madeira's FEX patches applied): c43bb48d, 4 warnings, all in code the base already has.
  • 380fdf1d1 was 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

ShitizZip and others added 9 commits November 22, 2026 17:12
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>
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.

2 participants