From eb8c7ea88897e387947a6925fd386d78e461b6a6 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 13 Nov 2026 19:40:05 +0300 Subject: [PATCH] gen.py --v-locals K keeps the K most used V registers of each function in C locals recomp/vlocals.py rewrites each emitted function: c->v forms become the RT_VLOCALS accessors of runtime/simd.h, the K registers the function names most get bits in VMASK, they are loaded at entry, stored where the dirty mask may hold them before calls and exits, and reloaded after calls; --v-trust skips the reload of V8-V15. A function with a form the rewrite does not know keeps every register in the Cpu. K = 0, the default, emits the same C as before. The manifest counts the rewritten functions and syncs. tests/diff/func.py takes --v-locals, --v-trust and --work (one work directory per concurrent run). Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 4 + recomp/gen.py | 25 ++++- recomp/vlocals.py | 243 +++++++++++++++++++++++++++++++++++++++++++ tests/diff/func.py | 13 ++- 4 files changed, 276 insertions(+), 9 deletions(-) create mode 100644 recomp/vlocals.py diff --git a/docs/architecture.md b/docs/architecture.md index 599257fa..7c22c7e5 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -40,6 +40,10 @@ MacNativeGames cannot run on x86-64 as is: its CPU path executes ARM64 code nati (`recomp/flags.py`, `recomp/regs.py`): registers are stored back where they changed, before calls, SVCs and exits, and reloaded after them. A call reloads only X0-X18 and X30, as AAPCS64 has the callee preserve X19-X29 and SP; `gen.py --no-callee-saved-trust` reloads those too. + - `gen.py --v-locals K` keeps the K most used V registers of each function in a local `u128 vr[32]` that clang + holds in XMM registers (`recomp/vlocals.py`, the `RT_VLOCALS` accessors of `runtime/simd.h`): loaded at entry, + stored where they may have changed before calls and exits, reloaded after calls; `--v-trust` does not reload + V8-V15, whose low halves AAPCS64 has the callee preserve. K = 0, the default, keeps every V register in the `Cpu`. - Coverage oracle: MacNativeGames' NSO rewriter lists every site where game code touches the OS (SVCs, TLS reads, counter reads, `x18`, exclusive windows, jump tables): 68,968 sites in 1.4.3 `main`. The lifter must handle every one. - Memory: guest address equals host address, all below 2^39 (IPC encodes 39-bit addresses). The heap, stacks and the alias region are reserved with `MEM_WRITE_WATCH`, so the graphics back end can ask which pages the CPU wrote since the last upload (`runtime/memory.c`). - Indirect branches go through `rt_dispatch` (`runtime/dispatch.c`), which looks the target up in the address table and tail-calls it. diff --git a/recomp/gen.py b/recomp/gen.py index d50c81f5..220a4d1e 100644 --- a/recomp/gen.py +++ b/recomp/gen.py @@ -2,7 +2,7 @@ """Generate C for the ExeFS modules: function discovery, then parallel emission into chunked C files. Usage: gen.py --exefs build/extracted/v121/exefs --out generated/v121 [--modules main,subsdk0,sdk] [--jobs N] - [--limit N] [--chunk-fns N] [--chunk-bytes B] [--big-fn B] [--patches FILE] + [--limit N] [--chunk-fns N] [--chunk-bytes B] [--big-fn B] [--patches FILE] [--v-locals K [--v-trust]] Output, under --out: layout.h, layout.json module bases (recomp/layout.py) @@ -46,6 +46,7 @@ import hostmem # noqa: E402 import jumptables # noqa: E402 import patches # noqa: E402 import regs # noqa: E402 +import vlocals # noqa: E402 from check_content import GENERATED_MARKER # noqa: E402 from layout import layout, write as write_layout # noqa: E402 from lifter import hx # noqa: E402 @@ -211,13 +212,16 @@ def partition(est, max_fns, max_bytes, big_fn): class Emitter: """C for the functions of one module. starts is the set of every function start of the module.""" - def __init__(self, mod, starts, patched=None, reg_locals=regs.LOCALS, callee_saved=True, spins=None): + def __init__(self, mod, starts, patched=None, reg_locals=regs.LOCALS, callee_saved=True, spins=None, v_locals=0, + v_trust=False): self.mod = mod self.starts = starts self.patched = patched or {} # {address: [(set index, [replacement words])]} (recomp/patches.py) self.spins = spins or {} # {spin wait back-edge: its head} (recomp/patches.py `spin`) self.reg_locals = reg_locals # recomp/regs.py: registers per function in C locals self.kept = regs.CALLEE_SAVED if callee_saved else frozenset() # not reloaded after a call + self.v_locals = v_locals # recomp/vlocals.py: V registers per function in C locals (0: all in c->v) + self.v_trust = v_trust # with v_locals: V8-V15 are not reloaded after a call self.uses_patches = False self.stats = Counter() self.unsupported = Counter() @@ -504,6 +508,8 @@ class Emitter: else: out += [line for _, line in body] out.append('}') + if self.v_locals: + out = vlocals.rewrite('\n'.join(out), self.v_locals, self.v_trust, self.stats).split('\n') self.stats['functions'] += 1 self.stats['instructions'] += len(insns) return out, refs @@ -521,8 +527,9 @@ def chunk_text(name, number, emitter, fns): protos = ''.join(f'void {fname(r)}(Cpu *c);\n' for r in sorted(refs)) if emitter.uses_patches: protos += 'extern uint8_t rt_patch_on[];\n' - return (header(f'{name} chunk {number:04d}') + '#include "cpu.h"\n#include "simd.h"\n\n' + protos + '\n' - + '\n\n'.join(body) + '\n') + vdef = '#define RT_VLOCALS 1\n' if emitter.v_locals else '' # runtime/simd.h: the V-locals accessors + return (header(f'{name} chunk {number:04d}') + vdef + '#include "cpu.h"\n#include "simd.h"\n\n' + protos + + '\n' + '\n\n'.join(body) + '\n') def write_if_changed(path, text): @@ -705,8 +712,15 @@ def main(): ap.add_argument('--no-callee-saved-trust', action='store_true', help='reload X19-X29 and SP after every call too, instead of trusting AAPCS64 (slower; for ' 'telling a broken callee from a generator bug)') + ap.add_argument('--v-locals', type=int, default=0, + help='V registers per function kept in C locals that clang holds in XMM registers, the most used ' + 'ones (recomp/vlocals.py; default 0: all in the Cpu, the output as before)') + ap.add_argument('--v-trust', action='store_true', + help='with --v-locals: do not reload V8-V15 after a call (AAPCS64: the callee preserves their low ' + '64 bits)') args = ap.parse_args() - opts = {'reg_locals': args.reg_locals, 'callee_saved': not args.no_callee_saved_trust} + opts = {'reg_locals': args.reg_locals, 'callee_saved': not args.no_callee_saved_trust, 'v_locals': args.v_locals, + 'v_trust': args.v_trust} t0 = time.perf_counter() mods = layout(args.exefs) @@ -828,6 +842,7 @@ def main(): 'largest_chunk_bytes': max((r['bytes'] for r in rs), default=0), 'chunks_written': sum(r['written'] for r in rs) + table_written, 'stale_chunks_removed': removed, + **{k: v for k, v in sorted(stats.items()) if k.startswith('v_')}, # --v-locals only }, 'unsupported_census': dict(unsupported.most_common(50)), 'timings': { diff --git a/recomp/vlocals.py b/recomp/vlocals.py new file mode 100644 index 00000000..9906c93e --- /dev/null +++ b/recomp/vlocals.py @@ -0,0 +1,243 @@ +"""V registers in C locals (gen.py --v-locals K): a rewrite of one emitted function's C. + +The lifter writes V registers as `c->v[n]`, lane forms `((T *)&c->v[n])[i]`, the register macros of runtime/simd.h +(VREG, VQ, VW, VS32, ...) and helper calls that name registers by number (fp_op_s(c, op, d, n, m), ...). A chunk +generated with K > 0 defines RT_VLOCALS, under which those macros and helpers reach a register through a local array +`u128 vr[32]` when its bit in the function's VMASK is set, else through c->v; every access is a whole-vector load or +store, so clang's SROA keeps each local register in an XMM register. This module + +- turns the direct forms into the accessors (c->v[n] = e -> VSET, c->v[n] -> VGET, lane forms -> VLANE/VLSET), +- picks the K registers the function names most often (VMASK), +- loads them at entry; before every call, rt_dispatch, rt_svc/rt_udf/rt_brk, rt_spin_gate and exit stores the ones + the function writes (VSYNC_OUT_D: only if the dirty mask may have the bit; the mask folds to a constant on most + paths); after a call reloads them, except V8-V15 with trust (AAPCS64: a callee preserves their low 64 bits, and a + conforming caller does not read their upper halves after the call). + +Why the cap: 32 guest registers on 16 XMM registers spill (all 32 cost +20% instructions, the stack traffic tripled), +while K = 6..8 removed a quarter of the c->v traffic and kept dependent FP chains in registers; build/v143-vloc +NOTES.md has the measurements. +""" +import re +from collections import Counter + +# runtime/simd.h helpers that take VCTX, with their parameters after c. Register-number parameters are d (written, +# also read by some), n, m, a (read) and t (the first of up to 4 consecutive registers; written by the sl_ld* loads). +HELPERS = { + 'fp_op_s': ['op', 'd', 'n', 'm'], + 'fp_op_d': ['op', 'd', 'n', 'm'], + 'fp_fma_s': ['na', 'nn', 'd', 'n', 'm', 'a'], + 'fp_fma_d': ['na', 'nn', 'd', 'n', 'm', 'a'], + 'sl_gen': ['op', 'sz', 'ne', 'd', 'szd', 'offd', 'keep', 'n', 'szn', 'offn', 'm', 'szm', 'offm', 'bidx', 'bimm'], + 'sl_addlp': ['u', 'acc', 'Q', 'sz', 'd', 'n'], + 'sl_across': ['op', 'u', 'Q', 'sz', 'd', 'n'], + 'sl_pmull64': ['part', 'd', 'n', 'm'], + 'sv_fred_s': ['op', 'd', 'n'], + 'sv_fcvtl': ['sz', 'part', 'd', 'n'], + 'sv_fcvtn': ['sz', 'part', 'odd', 'd', 'n'], + 'sv_tbl': ['Q', 'len', 'tbx', 'd', 'n', 'm'], + 'sv_aes': ['op', 'd', 'n'], + 'sv_sha1hash': ['op', 'd', 'n', 'm'], + 'sv_sha256hash': ['part1', 'd', 'n', 'm'], + 'sv_sha_misc': ['op', 'd', 'n', 'm'], + 'sl_ldm': ['Q', 'sz', 'selem', 'rpt', 't', 'ea'], + 'sl_stm': ['Q', 'sz', 'selem', 'rpt', 't', 'ea'], + 'sl_lds': ['sz', 'selem', 'idx', 't', 'ea'], + 'sl_sts': ['sz', 'selem', 'idx', 't', 'ea'], + 'sl_ldr': ['Q', 'sz', 'selem', 't', 'ea'], +} +REG_PARAMS = ('d', 'n', 'm', 'a', 't') +LANE_T = {'sa16': 'vu16', 'sa32': 'vu32', 'sa64': 'vu64', 'saf32': 'vf32', 'saf64': 'vf64', 'uint8_t': 'vu8', + 'uint16_t': 'vu16', 'uint32_t': 'vu32', 'uint64_t': 'vu64', 'float': 'vf32', 'double': 'vf64'} +CALLEE_SAVED = frozenset(range(8, 16)) +PLAIN = '\n const unsigned VMASK = 0; u128 *const vr = c->v; unsigned vd_ = 0, *const vdp = &vd_; (void)vr; (void)vdp;' + +FN = re.compile(r'^(?:static )?void (?:f|fb)_[0-9a-f]+\(Cpu \*c\) \{$', re.M) +CALL = re.compile(r'\b(?:f|fb)_[0-9a-f]+\(c\);|\brt_dispatch\(c\);|\brt_(?:svc|udf|brk|spin_gate)\(c,[^;]*;') +RET = re.compile(r'(?:__attribute__\(\(musttail\)\)\s*)?\breturn\b[^;]*;') +RET_BLOCK = re.compile(r'(\{ [^{}]*?(?:__attribute__\(\(musttail\)\) )?return\b[^;]*; \})') +LANE = re.compile(r'\(\((\w+) \*\)&c->v\[') +WHOLE = re.compile(r'c->v\[') +USES = re.compile(r'\b(' + '|'.join(['VSET', 'VLSET', 'VGET', 'VLANE', 'VS8', 'VS16', 'VS32', 'VS64', 'VSF32', 'VSF64', + 'VHI64', 'VREG', 'VQ', 'VW', 'FP_SETS', 'FP_SETD', 'SV_FOP_S', 'SV_FOP_D'] + + sorted(HELPERS)) + r')\(') + + +class Unsupported(ValueError): + pass + + +def _close(s, i): + """s[i-1] opens a bracket; the index after its match""" + d = 1 + while d: + d += {'[': 1, ']': -1}.get(s[i], 0) + i += 1 + return i + + +def _stmt_end(s, i): + """index of the ';' that ends the expression starting at s[i]""" + d = 0 + while True: + ch = s[i] + if ch in '([{': + d += 1 + elif ch in ')]}': + d -= 1 + elif ch == ';' and d == 0: + return i + i += 1 + + +def _args(s, i): + """arguments of the call whose '(' is s[i]""" + d, cur, out = 0, '', [] + while True: + ch = s[i] + if ch == '(': + d += 1 + if d == 1: + i += 1 + continue + elif ch == ')': + d -= 1 + if d == 0: + out.append(cur.strip()) + return out + elif ch == ',' and d == 1: + out.append(cur.strip()) + cur = '' + i += 1 + continue + cur += ch + i += 1 + + +def accessors(t): + """c->v forms of emitted C -> the RT_VLOCALS accessors of runtime/simd.h""" + out, i = [], 0 + while True: + m1, m2 = LANE.search(t, i), WHOLE.search(t, i) + m = min((x for x in (m1, m2) if x), key=lambda x: x.start(), default=None) + if not m: + out.append(t[i:]) + return ''.join(out) + out.append(t[i:m.start()]) + if m is m1: + e = _close(t, m.end()) + if m.group(1) not in LANE_T or t[e:e + 2] != ')[': + raise Unsupported(f'V register pointer form {t[m.start():e + 10]!r}') + je = _close(t, e + 2) + reg, lane, vt = accessors(t[m.end():e - 1]), t[e + 2:je - 1], LANE_T[m.group(1)] + a = re.match(r'\s*=(?!=)', t[je:]) + if a: + se = _stmt_end(t, je + a.end()) + out.append(f'VLSET({reg}, {vt}, {lane}, {accessors(t[je + a.end():se]).strip()})') + i = se + else: + out.append(f'VLANE({reg}, {vt}, {lane})') + i = je + continue + if t[m.start() - 1:m.start()] == '&': + raise Unsupported(f'V register address {t[m.start() - 1:m.start() + 20]!r}') + e = _close(t, m.end()) + reg = accessors(t[m.end():e - 1]) + a = re.match(r'\s*=(?!=)', t[e:]) + if a: + se = _stmt_end(t, e + a.end()) + out.append(f'VSET({reg}, {accessors(t[e + a.end():se]).strip()})') + i = se + else: + out.append(f'VGET({reg})') + i = e + + +def _lit(a): + a = a.strip().rstrip('uUlL') + if re.fullmatch(r'-\d+', a): + return -1 + return int(a, 0) if re.fullmatch(r'0x[0-9a-fA-F]+|\d+', a) else None + + +def registers(body): + """(Counter of register uses, set of written registers) of a body in accessor form""" + used, wr = Counter(), set() + for m in USES.finditer(body): + name = m.group(1) + args = _args(body, m.end() - 1) + if name in HELPERS: + for p, a in zip(['c'] + HELPERS[name], args): + if p not in REG_PARAMS: + continue + v = _lit(a) + if v == -1: + continue + if v is None: + raise Unsupported(f'register argument {a!r} of {name}') + span = 4 if p == 't' or (name == 'sv_tbl' and p == 'n') else 1 + rs = [(v + k) & 31 for k in range(span)] + used.update(rs) + if p == 'd' or (p == 't' and name.startswith('sl_ld')): + wr.update(rs) + continue + pos, write = {'VREG': (1, False), 'VQ': (2, False), 'VW': (2, True), 'SV_FOP_S': (2, True), + 'SV_FOP_D': (2, True), 'VSET': (0, True), 'VLSET': (0, True), 'FP_SETS': (0, True), + 'FP_SETD': (0, True)}.get(name, (0, False)) + v = _lit(args[pos]) + if v is None or v < 0: + raise Unsupported(f'register {args[pos]!r} of {name}') + used[v] += 1 + if write: + wr.add(v) + return used, wr + + +def rewrite_one(body, k, trust, stats): + """Body of one C function (text between its braces).""" + try: + new = accessors(body) + cnt, written = registers(new) + except Unsupported: + # a form this rewrite does not know: every register stays in c->v (VMASK 0) + stats['v_unsupported_functions'] += 1 + return PLAIN + body + body = new + if not cnt: + return body + local = {r for r, _ in sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[:k]} + written &= local + load = ''.join(f'VSYNC_IN({r}); ' for r in sorted(local)) + reload = ''.join(f'VSYNC_IN({r}); ' for r in sorted(local - (CALLEE_SAVED if trust else set()))) + store = ''.join(f'VSYNC_OUT_D({r}); ' for r in sorted(written)) + stats['v_local_functions'] += 1 + stats['v_local_registers'] += len(local) + + def at_ret(m): + stats['v_exit_syncs'] += 1 + return '{ ' + store + m.group(0) + ' }' + + def at_call(m): + stats['v_call_syncs'] += 1 + return '{ ' + store + m.group(0) + ' ' + reload + 'vd_ = 0; }' + + body = RET.sub(at_ret, body) + parts = RET_BLOCK.split(body) + body = ''.join(p if i % 2 else CALL.sub(at_call, p) for i, p in enumerate(parts)) + mask = sum(1 << r for r in local) + return (f'\n const unsigned VMASK = {mask:#x}u; u128 vr[32]; unsigned vd_ = 0, *const vdp = &vd_; {load.rstrip()}' + + body + ('\n ' + store.rstrip() if store else '')) + + +def rewrite(text, k, trust, stats): + """C text of one emitted guest function (an f_ and, for a computed branch, its fb_) with K registers local.""" + out, pos = [], 0 + for m in FN.finditer(text): + d, i = 1, m.end() + while d: + d += {'{': 1, '}': -1}.get(text[i], 0) + i += 1 + out.append(text[pos:m.end()]) + out.append(rewrite_one(text[m.end():i - 1], k, trust, stats) + text[i - 1]) + pos = i + out.append(text[pos:]) + return ''.join(out) diff --git a/tests/diff/func.py b/tests/diff/func.py index 62c34068..b26e5f75 100644 --- a/tests/diff/func.py +++ b/tests/diff/func.py @@ -83,7 +83,7 @@ def simd_mask(w): return ((w & 0x0E000000) == 0x0E000000) | (((w & 0x0A000000) == 0x08000000) & (((w >> 26) & 1) == 1)) -def pick_functions(name, mod, count, r, spins=None): +def pick_functions(name, mod, count, r, spins=None, **opts): """[(entry, end, tags)] of up to count functions of mod, plus the Emitter that will emit them. spins: {spin wait back-edge: head}, emitted with gates.""" starts, _ = gen.discover(mod) @@ -107,7 +107,7 @@ def pick_functions(name, mod, count, r, spins=None): with_spin = sorted({int(i) for i in gated} & set(cand.tolist())) for pool in (with_spin, with_br, with_call, rest): r.shuffle(pool) - emitter = gen.Emitter(mod, set((mod.lo + starts * 4).tolist()), spins=spins) + emitter = gen.Emitter(mod, set((mod.lo + starts * 4).tolist()), spins=spins, **opts) picks, tags_seen = [], Counter() # Functions with a spin wait first, then with a br (jump tables, fallbacks), at most a quarter of the picks each; # then functions with calls, at most half. @@ -346,12 +346,16 @@ def main(): ap.add_argument('--show', type=int, default=20) ap.add_argument('--patches', type=Path, help='patch table whose spin waits are emitted with their gates') ap.add_argument('--cflags', default='-march=x86-64-v3' if platform.machine().lower() in ('amd64', 'x86_64') else '') + ap.add_argument('--v-locals', type=int, default=0, help='gen.py --v-locals: V registers per function in locals') + ap.add_argument('--v-trust', action='store_true', help='gen.py --v-trust: V8-V15 not reloaded after a call') + ap.add_argument('--work', type=Path, default=ROOT / 'build' / 'test' / 'func', + help='work directory (chunks, harness, images); one per concurrent run') args = ap.parse_args() r = random.Random(args.seed) t0 = time.time() want = args.modules.split(',') - work = ROOT / 'build' / 'test' / 'func' + work = args.work work.mkdir(parents=True, exist_ok=True) images, emitted, tags = [], [], [] mods = layout(args.exefs) @@ -363,7 +367,8 @@ def main(): img = work / f'{name}.img' img.write_bytes(mod.image) images.append((base, img, mod.image)) - picks, emitter = pick_functions(name, mod, args.functions, r, table.spins_for(name, base)) + picks, emitter = pick_functions(name, mod, args.functions, r, table.spins_for(name, base), + v_locals=args.v_locals, v_trust=args.v_trust) for entry, end, t in picks: emitted.append((name, entry, end, emitter)) tags.append(t)