mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 05:05:56 +00:00
Merge the spin wait gates: they also end the fight hangs
The generator emits a guest poll such as `ldr w9, [x19]; cmp w9, #1; b.eq` as a plain C load, and clang hoists it out of the loop: the lock wait in f_7001fa0e2c compiled to `cmp; jne; jmp .`, so a thread that found the lock held spun forever after its holder released it. A fight run hung this way in 3 of 13 launches on an RX 9070, with the UBO snapshot on and off; gdb showed the thread on that `jmp` in every sample. The gate is an opaque call on every back-edge of the 868 loops tools/spin_waits.py lists, so each pass reloads memory: the exe goes from 182 self-jumps to 148, and 144 of those are the game's own `b .` instructions. 8 of 8 fight runs then passed; tests/diff/func.py with --patches passed 5470 cases on 878 functions, and the four probes passed on 1.4.3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
c2943970cc
12 files changed
+2370
-19
No files matched your search
+25
-9
@@ -15,7 +15,11 @@ stub folds the guest state at the call into a digest and sets the caller-saved r
|
||||
whether the emitted code had every register in the Cpu at the call and reads them back after it. Callers of another
|
||||
picked function are dropped, as the C side would run that function instead of the stub.
|
||||
|
||||
With --patches, the spin waits of the table (recomp/patches.py `spin`) are emitted with their gates, and functions
|
||||
that hold one come first (a quarter of the picks at most); a case whose loop does not leave runs into --steps.
|
||||
|
||||
Usage: func.py [--exefs DIR] [--modules main,subsdk0,sdk] [--functions N] [--cases K] [--seed S] [--jobs J]
|
||||
[--patches FILE]
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
@@ -34,6 +38,7 @@ from unicorn import arm64_const as A
|
||||
|
||||
from common import BUF, BUF_SIZE, MSYS_BIN, ROOT, Emu, compiler, diff_state, rand_fpcr, rand_v, rand_x, run_harness
|
||||
import gen
|
||||
import patches
|
||||
from layout import layout
|
||||
from module import Module
|
||||
|
||||
@@ -78,8 +83,9 @@ def simd_mask(w):
|
||||
return ((w & 0x0E000000) == 0x0E000000) | (((w & 0x0A000000) == 0x08000000) & (((w >> 26) & 1) == 1))
|
||||
|
||||
|
||||
def pick_functions(name, mod, count, r):
|
||||
"""[(entry, end, tags)] of up to count functions of mod, plus the Emitter that will emit them."""
|
||||
def pick_functions(name, mod, count, r, spins=None):
|
||||
"""[(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)
|
||||
w = mod.words
|
||||
ends = np.append(starts[1:], len(w))
|
||||
@@ -96,21 +102,28 @@ def pick_functions(name, mod, count, r):
|
||||
with_br = [int(i) for i in cand if has_br[i]]
|
||||
with_call = [int(i) for i in cand if has_call[i] and not has_br[i]]
|
||||
rest = [int(i) for i in cand if not has_br[i] and not has_call[i]]
|
||||
for pool in (with_br, with_call, rest):
|
||||
spins = spins or {}
|
||||
gated = np.searchsorted(mod.lo + starts * 4, np.array(sorted(spins), dtype=np.int64), side='right') - 1
|
||||
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()))
|
||||
emitter = gen.Emitter(mod, set((mod.lo + starts * 4).tolist()), spins=spins)
|
||||
picks, tags_seen = [], Counter()
|
||||
# Functions with a br first (jump tables, fallbacks), at most a quarter of the picks; then functions with calls,
|
||||
# at most half.
|
||||
for pool, quota in ((with_br, count // 4), (with_call, count // 2), (rest, count)):
|
||||
# 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.
|
||||
for pool, quota in ((with_spin, count // 4), (with_br, count // 4), (with_call, count // 2), (rest, count)):
|
||||
taken = 0
|
||||
for i in pool:
|
||||
if len(picks) >= count or taken >= quota:
|
||||
break
|
||||
entry, end = mod.lo + int(starts[i]) * 4, mod.lo + int(ends[i]) * 4
|
||||
if any(e == entry for e, _, _ in picks):
|
||||
continue
|
||||
tags = check_function(emitter, entry, end)
|
||||
if tags is None:
|
||||
continue
|
||||
if any(spins.get(x) is not None for x in range(entry, end, 4)):
|
||||
tags.add('spin_gate')
|
||||
picks.append((entry, end, tags))
|
||||
taken += 1
|
||||
picked = {e for e, _, _ in picks}
|
||||
@@ -330,6 +343,7 @@ def main():
|
||||
ap.add_argument('--seed', type=int, default=1)
|
||||
ap.add_argument('--jobs', type=int, default=os.cpu_count() or 4)
|
||||
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 '')
|
||||
args = ap.parse_args()
|
||||
r = random.Random(args.seed)
|
||||
@@ -339,14 +353,16 @@ def main():
|
||||
work = ROOT / 'build' / 'test' / 'func'
|
||||
work.mkdir(parents=True, exist_ok=True)
|
||||
images, emitted, tags = [], [], []
|
||||
for name, base, _ in layout(args.exefs):
|
||||
mods = layout(args.exefs)
|
||||
table = patches.load(args.patches, mods) if args.patches else patches.Table()
|
||||
for name, base, _ in mods:
|
||||
if name not in want:
|
||||
continue
|
||||
mod = Module(name, args.exefs / name, base, 'c')
|
||||
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)
|
||||
picks, emitter = pick_functions(name, mod, args.functions, r, table.spins_for(name, base))
|
||||
for entry, end, t in picks:
|
||||
emitted.append((name, entry, end, emitter))
|
||||
tags.append(t)
|
||||
|
||||
Reference in new issue
Block a user