Skip to content

Performance improvements tested with Path of Exile 2: TSO metadata fix, memcpy/memmove and leaf-call inlining, invalidation skips, alias redirect - #40

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

Conversation

@Dragooon

@Dragooon Dragooon commented Nov 22, 2026 •

Copy link
Copy Markdown

These changes were developed on an iPhone Duo (A20 Pro, 12 GB) while tuning Path of Exile 2 under Madeira between 4 and 28 Nov 2026, and every one of them ran there with the game. None of them is tied to that phone: they are in FEX's core, its JIT and its iOS ARM64EC module, and they help any game whose code looks like MSVC-built Path of Exile 2 (byte-copy loops, the CRT memcpy/memmove, tiny leaf functions, pool-copy aliases). Most are on by default with a switch to turn them off; Defaults lists them.

In short

  • Correctness: a TSO fix for volatile-metadata ranges (every block in a range drops TSO, not only blocks with a listed instruction) and an out-of-range dereference removed.
  • Copy idioms: MSVC's byte-at-a-time copy loop becomes one MemCpy; the CRT memcpy/memmove gets an inline small-copy path at its entry and at its direct call sites, with a two-test length tree and 32-byte rounds; an opt-in form runs the call-site copy without starting the CALL.
  • Leaf calls: straight-line callees of up to 12 instructions (opt-in up to 32) are compiled inline at direct call sites.
  • Lookups and invalidation: pool-copy aliases redirected before the cache-hit fast path, a reverse-translation cache, a hash-map code-page index with a CompileBlock fast path, a hashed L1 index, and invalidation skipped for section unmaps and frees that were never executable.
  • Memory: an opt-out of the read-and-verify pass over new call-return stacks and L1 tables, which on Darwin makes ~10 MiB a thread resident.
  • Diagnostics: JIT block map and code dump, lock and L1-miss counters, a per-thread copy census.

Impact

No run compared this branch with an unmodified FEX at matched content, so this PR claims no overall figure. Measured on the Duo with Path of Exile 2, per change:

  • Call-site small copy (new): the CRT memcpy/memmove routine went from 9.4% to 2.3% of profiled CPU (main thread 10.0% to 1.8%, the two job threads 14.0% to 2.1% and 15.4% to 2.5%): the copies now run inline in their callers. The net frame-thread gain was not isolated (different maps).
  • Leaf inline (new): 15,137 of 262,144 direct calls were inlined in a session, with no opcode the allow-list refused more than a few hundred times. The CPU saved was not isolated. Raising the limit to 24 instructions (MADEIRA_FEX_LEAF_MAX) added only 0.6 points to the inlined share.
  • Entry small-copy path (new): no measurable gain on its own: the routine's dispatch blocks went away but its entry block grew. The call-site form above is what pays.
  • Copy without the CALL's push (new, opt-in): in an A/B run with both forms on sibling threads, about 7-8% less loop time per copy, under 1% of a frame thread; the loop is bound by memory stalls, not instructions. Off by default.
  • Zero-scrub opt-out (new): the pass found no stale byte in 15,182 thread creations over 121 sessions and made ~350 MiB resident with 35 threads alive. It stays on by default.
  • 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.

  10. 0404a418d Core: inline small-copy path at MSVC's memcpy/memmove entry (MADEIRA_FEX_MEMMOVE=0 turns it off). A block that is the routine's entry (its first 46 bytes) gets a new IR op, MadeiraSmallMove, in front: an inline copy for 0..256 bytes between ranges that do not overlap, writing only JIT temporaries; anything else falls through to the routine.

  11. 3347f3d25 unittests: ASM tests for the byte-copy-loop and memmove idioms, run through the real ARM64 code generator by the x86-64 host build with the VIXL simulator: every length 0..300 at sixteen distances against REP MOVSB, and the byte loop at lengths 1..40.

  12. 52fd63256 Core: compile tiny leaf functions inline at direct call sites (MADEIRA_FEX_LEAF_INLINE=0 turns it off). A direct CALL's callee of at most 12 instructions and 64 bytes of allow-listed straight-line code ending in RET is compiled into the caller's block, with the return address pushed and popped as the CALL and RET would.

  13. 3562a1523 JIT: MadeiraSmallMove copies 64 to 256 bytes 32 at a time.

  14. c4980fb6d Core: inline small copy at direct call sites of MSVC's memcpy/memmove (MADEIRA_FEX_MEMMOVE_SITES=0 turns it off). The leaf inliner's callee decode recognises the routine; the call site does the CALL's push, then the inline copy and the RET's check. A copy it declines (over 256 bytes, overlapping) enters the routine with the return address already pushed.

  15. c64e462a4 Allocator: MADEIRA_FEX_ZERO_SCRUB=0 skips the read-and-verify pass over new call-return stacks and L1 tables. On by default.

  16. 3772a418a JIT: MadeiraSmallMove takes two length tests (MADEIRA_FEX_SMALLMOVE_TREE=0 keeps the chain). Each range from 4 bytes up moves as four possibly overlapping pieces of one width.

  17. f90d93c3a Call-site memmove without the CALL's push, a per-thread copy census, both forms in one run. MADEIRA_FEX_MOVE_NOPUSH 1 = the copy runs with the CALL not started; 2 = both forms compiled, alternating by thread; MADEIRA_FEX_MOVE_CENSUS = 1 counts copies per thread. Default 0 (no change).

  18. e948dd11a Frontend: MADEIRA_FEX_LEAF_MAX takes callees of up to 32 instructions inline (default 12). Test: unittests/ASM/Madeira/leaf_inline_long.asm (20- and 26-instruction callees).

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, the memcpy/memmove entry and call-site copies with the two-test tree, leaf inline at 12 instructions, the zero-scrub pass.
  • Opt-in: JIT map and code dump, the copy without the CALL's push, the copy census, longer leaves.

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 had any of the first nine commits by patch id on 22 Nov, and none has moved since, so none has the nine newer ones either.
  • It merged cleanly into codex/fex-runtime-consolidation (the FEX that codex/dxmt-upstream-build-port pins) and into every other active branch on 22 Nov; not repeated for the nine new commits.

Testing

  • The phone runs these changes in xtajit64 14e08070b8f9 (since 28 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), 4 warnings, all in code the base already has.
  • The Madeira ASM tests (copy idioms, leaf inline, long leaves) pass in the x86-64 host build through the VIXL simulator; with the leaf inline active FEX's 64-bit ASM suite passes 6,366 tests with the same three failures as the base.
  • 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>
ShitizZip and others added 9 commits November 28, 2026 15:21
…FEX_MEMMOVE=0 turns it off)

MSVC's memcpy and memmove are one routine. It picks its path through a
jump table per size up to 15 bytes, and through compares, a 128-byte loop
and a second jump table above that, so a small copy translates to two or
three guest indirect branches (each a lookup of the next block) around a
handful of loads and stores. In Path of Exile 2 the routine was 5.8% of
all process CPU in a fight, with the loads and stores themselves about
15% of that.

When a decoded block is the routine's entry (matched by its first 46
bytes), a new IR op, MadeiraSmallMove, goes in front of it: an inline
copy for 0..256 bytes between ranges that do not overlap. It writes only
the JIT temporaries, so the guest registers and flags are untouched. If
it copied, the block sets RAX to the destination and returns. If not
(more than 256 bytes, or overlapping ranges) nothing was written and the
routine's own instructions run as before.

Overlapping copies stay with the routine because a fault inside the
inline copy is reported at the routine's first instruction; a guest
handler that resumes there repeats the copy, which is only right while
the source is untouched.

It applies only where TSO is off for the routine, and not under full SMC
checks, hot SMC blocks or extended debug info. On by default; set
MADEIRA_FEX_MEMMOVE=0 to turn it off. The first 8 matches log
[memmove-fast].

The emitted sequence was run under qemu-aarch64 against a reference
memmove: 120,400 cases over every length 0..300 and every kind of
overlap, checking the bytes, the result, and that the inputs and a
declined destination stay untouched.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Two tests under unittests/ASM/Madeira, run through the real ARM64 code
generator by the x86-64 host build with the VIXL simulator:

- crt_memmove_fast.asm: a routine that starts like MSVC's memcpy/memmove,
  called for every length 0..300 at sixteen source/destination distances
  (overlapping both ways included), each result compared byte for byte
  with a REP MOVSB copy, bytes around the copy included.
- byte_copy_loop.asm: MSVC's byte-at-a-time copy loop for lengths 1..40
  at seven distances, memory, registers and flags compared with the same
  loop in a shape the idiom does not match.

The idioms are compiled only into the iOS ARM64EC module, so the test
build defines FEX_MADEIRA_IDIOM_TESTS to compile them in as well. Nothing
changes for any other build.

With the fast path compiled in, 3,228 of the 4,816 memmove calls reach
the routine's own code (the long and the overlapping ones), which is the
number the fast path's conditions predict; without it all 4,816 do.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…A_FEX_LEAF_INLINE=0 turns it off)

Path of Exile 2 spends about 4% of its CPU in functions of one to a few
instructions (`mov rax, rcx; ret` alone is 1.3% of a fight, run-1125a),
nearly all of it the call and the return: a block entry each, and for the
return an L1 lookup and an indirect branch, 31 to 35 host instructions
around a callee of one. Most of them are reached by direct CALLs (2,506
sites for that one function).

Decoder: when a block ends in a direct CALL, the callee is decoded beside
the blocks. If it is at most 12 instructions and 64 bytes of plain
straight-line code ending in RET (an allow-list by encoding: moves, ALU,
shifts, LEA, CMOV/SETcc, PUSH/POP, SSE moves and arithmetic; no branches,
calls, string or system instructions, LOCK, VEX, x87, MMX), it is recorded
in BlockInfo.InlineLeaves and its pages join the function's code pages, so
a write to the callee invalidates the caller. Not for EC code, pool-copy
aliases, writable mappings, or while a code cache is being generated.

Dispatcher: at such a CALL, GenerateIR emits the push of the return
address, the callee's instructions through their ordinary handlers (with
the callee's own TSO policy and guest-address markers), then the RET as a
compare of [RSP] with the address just pushed. Equal: RSP moves past it
and the block continues after the CALL. Not equal (the callee replaced its
return address): an ordinary RET. The guest stack, registers and flags go
through the same states as with the call made, so a callee that reads its
stack arguments or faults behaves as before. A handler that reports a
failure ends the block at that instruction, which then compiles on its
own as it always did.

GuestOpcode's offset operand becomes 64 bits: an inlined callee can sit
below the function entry, and the RIP tables already store signed deltas.

Not used with full SMC checks, hot SMC pages, forced SMC detection,
extended debug info, single-instruction blocks or 32-bit guests. Custom IR
entry points are never inlined.

Tests: unittests/ASM/Madeira/leaf_inline.asm (13 cases: results, flags in
both directions, stack arguments, the pushed word, PUSH/POP in the callee,
a callee that rewrites its return address, `rep ret`, callees below and
above the caller, the 12-instruction limit, a callee with a branch). The
x86-64 host build with the VIXL simulator runs the 64-bit ASM suite with
the inline active: 6,366 pass, the same three failures as before the
change (Secondary/07_XX_04).

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The 64..256 byte class looped over single 16-byte vectors: 4 to 15 rounds
of four instructions. In Path of Exile 2 that loop held about a sixth of
the routine's time (run-1125b). It now moves a pair of vectors per round,
(length - 1) / 32 rounds, then the last 32 bytes: the rounds cover all but
at most the last 32 bytes and never pass the end.

unittests/ASM/Madeira/crt_memmove_fast.asm sweeps every length 0..300 at
sixteen distances and compares the bytes around each copy as well.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… (MADEIRA_FEX_MEMMOVE_SITES=0 turns it off)

The entry fast path is one piece of host code shared by every caller: its
size checks see every caller's lengths in turn, and each call still pays
the call, the routine's block entry and the return lookup. In Path of
Exile 2 the routine was 8.2% of process CPU in a map (run-1125b; 10% of
the main thread and 14% of the two job threads), about 40% of it at the
instructions right after the size branches and 10% in the return. The
game calls it from 5,350 direct CALL sites; only 4% of them pass a
constant length.

Decoder: the callee decode of the leaf inline stops at a branch after
three instructions for this routine; when OpDispatchBuilder::MatchCRTMemmove
accepts those plus the JA and the bytes after it, the call is recorded in
BlockInfo.InlineLeaves with no instructions (InlineLeaf::IsCRTMemmove) and
the routine's matched bytes join the function's code pages.

Dispatcher (OpDispatchBuilder::InlineCRTMemmoveCall): the CALL's push,
then MadeiraSmallMove. Copied: RAX = the destination and the RET of the
leaf inline (compare [RSP] with the address pushed, move RSP past it,
carry on after the CALL). Not copied (more than 256 bytes, or overlapping
ranges): nothing was written and the routine is entered through the same
linked call exit a CALL uses, its return address already on the stack. A
fault inside the copy is reported at the routine's first instruction with
the return address pushed, as with the entry fast path.

Applied under the entry fast path's rule (TSO off for the whole routine)
and the leaf inline's (not under full SMC checks, extended debug info, a
custom IR handler on the callee, EC code, pool-copy aliases, writable
mappings, or while a code cache is generated). MADEIRA_FEX_MEMMOVE=0
turns both memmove paths off; MADEIRA_FEX_LEAF_INLINE=0 no longer affects
this one. The [leaf-stats] line counts these calls as "memmove", and the
first eight sites are logged as [memmove-site].

Tests: crt_memmove_fast.asm now runs its sweep twice, through a direct
CALL (the call-site copy) and through a CALL of a register (the entry
copy), and checks the stack pointer. crt_memmove_site.asm covers fixed
lengths with the word under the stack pointer, the callee-saved
registers, a copy that lands on its own return address, calls back to
back, an overlapping call and a call at the end of a function.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…er new call-return stacks and L1 tables

ZeroScrub verifies by reading that a fresh range is zero. On Linux that
maps the shared zero page; on Darwin every read fault hands the task a
private zero-filled page, so the pass makes the whole range resident:
8 MiB of call-return stack and 2 MiB of L1 table for every thread, about
640 page faults a thread and ~350 MiB of footprint with 35 threads alive
(Path of Exile 2 on the iPhone, [phys-map]).

The pass found no stale byte in 15,182 thread creations over 121 sessions.
It stays on by default; the switch turns it off, and both log lines say so.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…REE=0 keeps the chain)

The inline copy tested the length one power of two at a time: three to five
tests before a copy of 4 to 256 bytes. Path of Exile 2 fills its constant
buffers with one memcpy per value, lengths from 4 to 256 bytes with no pattern
from one call to the next. In a map that loop (exe+0x129cce0) was 8% of the
main thread and 11% of a job thread, and 83% of its samples were on the
instructions after those tests.

The default form now takes two tests: 1..16 or 17..256, then 1..3 or 4..16, or
17..64 or 65..256. Each range from 4 up is moved as four pieces of one width
(4, 16 or 64 bytes) that may lie on top of one another, so nothing inside a
range depends on the length. The ranges still must not overlap and more than
256 bytes still goes to the routine's own code. 80 instructions (73 before).

Tests: tools/fex-smallmove-test (every length 0..300, 400 placements each,
both forms: 120,400 cases, 0 failures, same declines); the Madeira ASM tests;
the 64-bit suite has the same three failures as before.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…both forms in one run

MADEIRA_FEX_MOVE_NOPUSH (default 0 = no change):
  1  the inline copy at a direct CALL to CRT memmove runs with the CALL not started. Copied:
     RAX = destination and the block carries on; no return address is pushed, read back,
     compared or popped (11 host instructions, a store and a load per copy). Not copied: the
     whole CALL runs. The eight bytes below RSP keep their old content, a copy that would
     have overwritten its own return address carries on after the CALL, and a fault inside
     the copy is reported at the CALL instruction.
  2  both forms are compiled and each thread runs one of them
     (CpuStateFrame::MadeiraMoveForm, alternating as threads start; MADEIRA_FEX_MOVE_FORM=0/1
     gives every thread the same one). Two threads doing the same work then compare the forms
     in one run, whatever the content.

MADEIRA_FEX_MOVE_CENSUS=1: the emitted copy adds itself and its length to per-thread
counters (six instructions a copy, no flags), by slot: at the routine's entry, call site
with the push, call site without. Each thread logs its totals about every 10 s as [move-t].
With the census off the emitted copy is byte for byte what it was.

Why: in run-1125d the two-test copy did not change the cost of Path of Exile 2's
constant-buffer loop (exe+0x129cce0, 9-16% of each frame thread), and shares between
different maps cannot show a change of a few percent. The census gives copies per thread, so
the cost per copy can be compared between threads and between runs.

JIT: a call exit in the last block of a function left its return landing without a branch
to the return block (the branch is emitted when the next block starts). The form without
the push ends functions that way; the landing now gets its branch after the last block.

Tests: unittests/ASM/Madeira/crt_memmove_site*.asm and crt_memmove_fast*.asm share their
bodies (unittests/ASM/Includes/madeira_crt_memmove_*.inc) and run each mode; the test
runner puts a test's MADEIRA_* "Env" entries into the environment. 30 of 30 pass in the
x86-64 simulator build; the 64-bit suite has the same three failures as before.
tools/fex-smallmove-test runs the emitter with the census off and on (120,400 cases each).

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… inline (default 12)

run-1127g: a quarter of the direct calls the leaf inliner looked at stopped
at the 12-instruction limit. Above 12 the byte limit is 6 bytes an
instruction. Test: unittests/ASM/Madeira/leaf_inline_long.asm (a 20- and a
26-instruction callee), passes with the variable unset and at 24.

Signed-off-by: Shitiz Garg <mail@shitiz.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Dragooon Dragooon changed the title iPhone Duo work: volatile-metadata TSO, byte-copy loops, invalidation skips, alias cache and redirect, JIT map, cache fast paths Performance improvements tested with Path of Exile 2: TSO metadata fix, memcpy/memmove and leaf-call inlining, invalidation skips, alias redirect Nov 28, 2026
@Dragooon
Dragooon marked this pull request as ready for review November 28, 2026 07:56
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