mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-22 17:19:58 -04:00
Triforce v0.7.0 "Sage Of Courage"
The 0.6 crashes after warps and area changes fixed (no write-back into memory the capture leaves out), invisible objects fixed (occlusion gate), DLSS frame generation flicker fixed, achievements, the renderer built with a play profile, faster translated game code, helper threads judged on settled scenes, memory, shader preparation, audio effects, launcher, controls and Linux fixes.
This commit is contained in:
129
nx2/build.py
129
nx2/build.py
@@ -39,6 +39,7 @@ import time
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # (python -I leaves the script folder off)
|
||||
import kit
|
||||
import cold_parts
|
||||
import lifted_c
|
||||
import rtgen
|
||||
import logo_rgba
|
||||
@@ -58,6 +59,26 @@ HOST_ZSTD_SOURCES = ("common/debug.c", "common/entropy_common.c", "common/error_
|
||||
OBJ_CACHE = os.path.join(kit.WORK, "objcache")
|
||||
# (hookshot) work/hot_order.txt: the functions that run, hottest first (sampled), packed together by the linker; RX_NO_ORDER=1: off
|
||||
PGO = os.environ.get("RX_PGO", "")
|
||||
# ((Triforce, 11-21, the performance audit's LIFT-10, after Source A's) RX_VK_PGO: the renderer's own code (gpu/vk/*.cpp, not
|
||||
# the vendored recompiler, ImGui or zstd) with a play profile. The back end runs the replay on the game's render thread, and
|
||||
# was built at plain -O2 whatever it does most. "gen": it counts its branches (host/pgo_rt.c in the DLL, RX_VK_PGO_FILE=<path>
|
||||
# in the run: <path>.reset clears the counters, <path>.dump writes them to <path>; llvm-profdata merge makes the profile).
|
||||
# <file>: built with that profile. Not set, empty or 0: neither, as before (nx2/make_game.py passes a shipped profile).
|
||||
VK_PGO = os.environ.get("RX_VK_PGO", "")
|
||||
|
||||
|
||||
def vk_pgo_flags(asked=None):
|
||||
"""The flags RX_VK_PGO adds to the renderer's own files. asked: its value (the environment's when not given)"""
|
||||
asked = VK_PGO if asked is None else asked
|
||||
if asked in ("", "0"):
|
||||
return []
|
||||
if asked == "gen":
|
||||
return ["-fprofile-instr-generate", "-mllvm", "-disable-vp=true"]
|
||||
# (the profile's contents in the flags the object key hashes, as the hot code's: its path names no key, flag_text)
|
||||
with open(asked, "rb") as f:
|
||||
sha = hashlib.sha1(f.read(), usedforsecurity=False).hexdigest()[:16]
|
||||
return ["-fprofile-instr-use=" + asked, "-DTRIFORCE_VK_PROFILE_SHA1=" + sha, "-Wno-profile-instr-out-of-date",
|
||||
"-Wno-profile-instr-unprofiled"]
|
||||
|
||||
|
||||
# (linux) Linux builds: an ELF executable and triforce_vk.so, the Win32 calls of host/ and gpu/vk on POSIX
|
||||
@@ -71,6 +92,18 @@ EXE = "triforce" if LINUX else "Triforce.exe"
|
||||
VK_LIB = "triforce_vk.so" if LINUX else "triforce_vk.dll"
|
||||
|
||||
|
||||
def repro_link(asked=None, linux=None):
|
||||
"""(Triforce, 11-22, the performance audit's IO-45, after Source A's) What HOOKSHOT_LINK_REPRO=1 adds to the Windows
|
||||
links of the game's exe and of the renderer: /Brepro, with which lld writes a hash of the file where it otherwise
|
||||
writes the time of the link, so that two links of the same objects are the same bytes and make_game.py's finish can
|
||||
pass the unchanged exe over (INSTALLED_NOTE there). Off unless asked: it changes the bytes of every linked file once.
|
||||
asked: the switch's value (the environment's when not given)"""
|
||||
asked = os.environ.get("HOOKSHOT_LINK_REPRO", "") if asked is None else asked
|
||||
if asked != "1" or (LINUX if linux is None else linux):
|
||||
return []
|
||||
return [toolchain.link_opt("/Brepro")]
|
||||
|
||||
|
||||
def check_vk_symbols(so):
|
||||
"""The back end is dlopened with RTLD_NOW, so one unresolved symbol stops the whole thing
|
||||
loading -- and the only sign of that at run time is "the renderer did not start: the window
|
||||
@@ -666,7 +699,8 @@ def mem_need(src):
|
||||
with open(src, "rb") as f:
|
||||
text = f.read()
|
||||
# (each function from the newline before its definition, as this was first measured)
|
||||
starts = [m.start() - 1 for m in lifted_c.HEAD_BYTES.finditer(text)] + [len(text)]
|
||||
# ((Triforce, 11-22, the performance audit's IO-40) lifted_c.definitions: HEAD_BYTES's matches, found by jumps)
|
||||
starts = [m.start() - 1 for m in lifted_c.definitions(text)] + [len(text)]
|
||||
sizes = [b - a for a, b in zip(starts, starts[1:])]
|
||||
giant = GIANT_MB * sum((s / (1 << 20)) ** 2 for s in sizes if s >= GIANT_BYTES) if giants_counted() else 0
|
||||
if name.startswith("code_hot"):
|
||||
@@ -676,6 +710,11 @@ def mem_need(src):
|
||||
return 1000 if src.endswith(".cpp") else 400
|
||||
|
||||
|
||||
class CompileStopped(Exception):
|
||||
"""((Triforce, 11-21, the performance audit's LIFT-31) The build is stopping: a compile that waited at the memory gate
|
||||
is not started (MemoryGate.stop)"""
|
||||
|
||||
|
||||
class MemoryGate:
|
||||
"""(issue 418) Compilers started by the memory there is, not only by --jobs: one per CPU thread ran PCs out of memory (the kernel
|
||||
stopped a compiler, or the whole build), and a count low enough for the biggest files and the PC's other programs left most of
|
||||
@@ -684,10 +723,15 @@ class MemoryGate:
|
||||
on Windows up to 4 (half the threads on smaller CPUs) always run, as Windows pages rather than fails (issues 502, 503, 511: 1-2
|
||||
compilers on 16 GB PCs took hours); on Linux one (issue 531: Linux stops a compiler that does not fit, and it is compiled again
|
||||
alone). The free memory is read again as compiles start and end, so a game or browser opened during the build makes later
|
||||
compiles wait"""
|
||||
compiles wait
|
||||
|
||||
(Triforce, 11-21, the performance audit's LIFT-31, after Source A's) stop(): the build is ending on an exception. The
|
||||
pool's workers, one a CPU thread, wait here, and a compile that has left the pool's queue cannot be cancelled there:
|
||||
one that waits, or comes after, raises CompileStopped and takes no lease"""
|
||||
|
||||
def __init__(self):
|
||||
self.cv = threading.Condition()
|
||||
self.stopped = False
|
||||
self.running = {} # (per-compile lease: [need, Popen or None]; one source can have several objects)
|
||||
self.reserve = float(os.environ.get("HOOKSHOT_MEM_RESERVE_GB") or 1.5) * 1024
|
||||
self.floor = max(1, min(4, (os.cpu_count() or 2) // 2)) if os.name == "nt" else 1
|
||||
@@ -695,7 +739,7 @@ class MemoryGate:
|
||||
def enter(self, src):
|
||||
need = mem_need(src)
|
||||
with self.cv:
|
||||
while len(self.running) >= self.floor:
|
||||
while not self.stopped and len(self.running) >= self.floor:
|
||||
free = free_mb()
|
||||
if free is None:
|
||||
break
|
||||
@@ -703,10 +747,17 @@ class MemoryGate:
|
||||
if free - owed - self.reserve >= need:
|
||||
break
|
||||
self.cv.wait(1.0)
|
||||
if self.stopped:
|
||||
raise CompileStopped(src)
|
||||
lease = object()
|
||||
self.running[lease] = [need, None]
|
||||
return lease
|
||||
|
||||
def stop(self):
|
||||
with self.cv:
|
||||
self.stopped = True
|
||||
self.cv.notify_all()
|
||||
|
||||
def started(self, lease, proc):
|
||||
with self.cv:
|
||||
self.running[lease][1] = proc
|
||||
@@ -830,6 +881,10 @@ def flag_text(fl):
|
||||
continue
|
||||
if f.startswith("--sysroot="):
|
||||
f = "--sysroot=<sysroot>"
|
||||
elif VK_PGO not in ("", "0", "gen") and f == "-fprofile-instr-use=" + VK_PGO:
|
||||
# ((Triforce, 11-21, LIFT-10) the renderer's profile by its contents (TRIFORCE_VK_PROFILE_SHA1, beside it), not by
|
||||
# where it is: another work folder's copy of the same profile is no new key)
|
||||
f = "-fprofile-instr-use=<vk profile>"
|
||||
else:
|
||||
f = f.replace(kit.ROOT, "<root>")
|
||||
kept.append(f)
|
||||
@@ -1075,6 +1130,14 @@ def main():
|
||||
srcs = []
|
||||
if a.dry_run: # (the lift's files: there is no game here)
|
||||
srcs = [s for s in srcs if os.path.dirname(s) != gen]
|
||||
# ((Triforce, 11-22, the performance audit's LIFT-17, after Source A's) RX_COLD_OPT=O2, or a file that lists chunks: those
|
||||
# lifted chunks at -O2 too (never the hot pieces), each compiled as parts of RX_COLD_PART_MB under <build>/split in place
|
||||
# of the chunk (nx2/cold_parts.py). Unset: srcs as they are, and no file takes cold_fl below
|
||||
cold_stats = {}
|
||||
srcs, cold = cold_parts.expand(srcs, os.path.join(out, "split"), os.environ, stats=cold_stats)
|
||||
if cold:
|
||||
print("RX_COLD_OPT: %d files at -O2 (parts of %d MB; %d chunks cut again, %d kept as cut before)" % (
|
||||
len(cold), cold_parts.part_bytes(os.environ) >> 20, cold_stats.get("written", 0), cold_stats.get("kept", 0)), flush=True)
|
||||
# the longest first, so the long ones do not end up alone at the end (compile_cost)
|
||||
costs = chunk_costs(gen)
|
||||
srcs.sort(key=lambda s: -compile_cost(s, costs))
|
||||
@@ -1099,8 +1162,9 @@ def main():
|
||||
# new profile kept the hot code built from the old one)
|
||||
with open(PGO, "rb") as f:
|
||||
hot_fl = hot_fl + ["-fprofile-instr-use=" + PGO, "-DHOOKSHOT_PROFILE_SHA1=" + hashlib.sha1(f.read(), usedforsecurity=False).hexdigest()[:16]]
|
||||
cold_fl = cold_parts.cold_flags(fl, a.opt, os.environ) if cold else fl # ((Triforce, LIFT-17) -O2 and the part size)
|
||||
jobs = [(cc, s, os.path.join(obj_dir, os.path.basename(s)[:-2] + ".o"),
|
||||
hot_fl if os.path.basename(s).startswith("code_hot") else fl,
|
||||
hot_fl if os.path.basename(s).startswith("code_hot") else cold_fl if s in cold else fl,
|
||||
shim_deps if os.path.dirname(s) == NX2_RT else deps) for s in srcs]
|
||||
# (issue 624) what the game code's object keys are made of, as short fingerprints: two builds' logs side by side say which part
|
||||
# changed when an update compiled everything again (the headers, the flags, the profile, or the lifted code itself)
|
||||
@@ -1213,7 +1277,10 @@ def main():
|
||||
# (what the code takes for granted, which newer C++ libraries bring in by themselves and older ones do not: issue 48)
|
||||
"-include", "algorithm", "-include", "cstdio", "-include", "cstdlib", "-include", "cstring", "-include", "functional",
|
||||
"-include", "string", "-include", "vector",
|
||||
"-std=c++20", "-O2", "-g0", "-DNDEBUG", "-Wno-everything", "-DVK_NO_PROTOTYPES", "-DMKWVK_BUILDING",
|
||||
# ((Triforce, 11-21) HOOKSHOT_VK_LINES=1: a line table in the renderer (the same code, DWARF lines only) for a profile's
|
||||
# exact functions, inlined ones too, through llvm-symbolizer: the linker map lacks the file-local functions)
|
||||
"-std=c++20", "-O2", "-gline-tables-only" if os.environ.get("HOOKSHOT_VK_LINES") == "1" else "-g0", "-DNDEBUG",
|
||||
"-Wno-everything", "-DVK_NO_PROTOTYPES", "-DMKWVK_BUILDING",
|
||||
*(["-DHOOKSHOT_RELEASE"] if os.environ.get("RX_RELEASE") == "1" else []), # (the shaders precompiled before the game)
|
||||
# (Eden's recompiler before suyu's root: suyu has its own shader_recompiler/ headers, and a Shader::Info
|
||||
# of the other layout in some objects is what crashed the descriptor walk. MKWVK_INCLUDES changes the
|
||||
@@ -1285,11 +1352,16 @@ def main():
|
||||
# (the bundled fonts, compiled in: nx2/ui_fonts.py's file of the build's own)
|
||||
vk_jobs.append((cxx, fonts_cpp, os.path.join(obj_dir, "vk_ui_fonts.o"), target() + (["-fPIC"] if LINUX else []) + ["-O0", "-g0", "-w"],
|
||||
b""))
|
||||
vk_pgo = vk_pgo_flags() # ((Triforce, 11-21, LIFT-10) the renderer's own files alone)
|
||||
if VK_PGO == "gen" and not LINUX: # (what writes the counters, the DLL's own copy: host/pgo_rt.c)
|
||||
vk_jobs.append((cc, os.path.join(HOST, "pgo_rt.c"), os.path.join(obj_dir, "vk_pgo_rt.o"),
|
||||
target() + ["-O2", "-g0", '-DPGO_ENV="RX_VK_PGO_FILE"'], b""))
|
||||
for src in vk_sources():
|
||||
name = os.path.relpath(src, vk).replace(os.sep, "_")[:-4]
|
||||
lib = os.path.relpath(src, vk).split(os.sep)[0] in ("eden", "sirit")
|
||||
vk_jobs.append((cxx, src, os.path.join(obj_dir, "vk_" + name + ".o"),
|
||||
vflags + ([shader_stamp, translation_def] if os.path.basename(src) == "vk_shader.cpp" else []) +
|
||||
vflags + ([] if lib else vk_pgo) +
|
||||
([shader_stamp, translation_def] if os.path.basename(src) == "vk_shader.cpp" else []) +
|
||||
([stamp_def] if os.path.basename(src) == "vk_menu.cpp" else []) +
|
||||
(ngx_flags if os.path.basename(src) == "vk_dlss.cpp" else []) +
|
||||
(sl_flags if os.path.basename(src) == "vk_dlssg.cpp" else []) +
|
||||
@@ -1351,21 +1423,33 @@ def main():
|
||||
with concurrent.futures.ThreadPoolExecutor(a.jobs) as ex:
|
||||
# (as each ends, not in order: the builder app shows progress from these lines, and the biggest file comes first)
|
||||
submitted = {ex.submit(compile_one, job): job for job in jobs}
|
||||
for future in concurrent.futures.as_completed(submitted):
|
||||
src, secs, err = future.result()
|
||||
size = os.path.getsize(src) >> 20
|
||||
if err:
|
||||
failed.append(submitted[future])
|
||||
print("not compiled yet: %s (%d MB, %.0fs), tried again alone at the end:\n%s"
|
||||
% (os.path.basename(src), size, secs, err[-1500:]), flush=True)
|
||||
else:
|
||||
print(" %-16s %4d MB %6.0fs (%.0fs)" % (os.path.basename(src), size, secs, time.time() - t0), flush=True)
|
||||
if counted:
|
||||
print("@@file %s %.1f %s" % (os.path.basename(src), secs,
|
||||
"failed" if err else "cached" if secs <= 0 else "new"), flush=True)
|
||||
if not err and expected.get(src):
|
||||
pending.remove(expected[src].pop())
|
||||
print("@@eta %d" % eta_left(pending, eta_jobs), flush=True)
|
||||
try:
|
||||
for future in concurrent.futures.as_completed(submitted):
|
||||
src, secs, err = future.result()
|
||||
size = os.path.getsize(src) >> 20
|
||||
if err:
|
||||
failed.append(submitted[future])
|
||||
print("not compiled yet: %s (%d MB, %.0fs), tried again alone at the end:\n%s"
|
||||
% (os.path.basename(src), size, secs, err[-1500:]), flush=True)
|
||||
else:
|
||||
print(" %-16s %4d MB %6.0fs (%.0fs)" % (os.path.basename(src), size, secs, time.time() - t0), flush=True)
|
||||
if counted:
|
||||
print("@@file %s %.1f %s" % (os.path.basename(src), secs,
|
||||
"failed" if err else "cached" if secs <= 0 else "new"), flush=True)
|
||||
if not err and expected.get(src):
|
||||
pending.remove(expected[src].pop())
|
||||
print("@@eta %d" % eta_left(pending, eta_jobs), flush=True)
|
||||
except BaseException:
|
||||
# ((Triforce, 11-21, the performance audit's LIFT-31, after Source A's) A compiler's error comes back as a value and
|
||||
# is tried again alone. An exception does not: a compiler that is missing or blocked, an object or its stamp that
|
||||
# cannot be written, a full disk in cache_put, a file gone before its size is asked, Ctrl-C. It leaves this block,
|
||||
# and the pool's exit then ran every compile still queued first, the whole compile with nothing said, before the
|
||||
# build stopped; and make_game deletes those objects, as no "compiled in" line follows. So: the queued ones are
|
||||
# cancelled, and those waiting at the memory gate are told to go (MemoryGate.stop); the compilers running end)
|
||||
if GATE:
|
||||
GATE.stop()
|
||||
ex.shutdown(wait=False, cancel_futures=True)
|
||||
raise
|
||||
# (a compiler that ran out of memory beside the others dies "due to signal": a player's 24 MB code_021.c, 2026-11-08. The
|
||||
# files that failed are compiled again one at a time, with all the memory, before the build gives up)
|
||||
still = 0
|
||||
@@ -1421,6 +1505,7 @@ def main():
|
||||
t = time.time()
|
||||
r = subprocess.run([cc] + toolchain.native_target() + toolchain.native_link() +
|
||||
["-o", exe, toolchain.link_opt("/map:" + exe[:-4] + ".map"), toolchain.link_opt("/stack:8388608"), "@" + rsp] + HOT_ORDER() + HOST_LIBS +
|
||||
repro_link() + # ((Triforce, 11-22, IO-45) HOOKSHOT_LINK_REPRO=1: no link time in the file)
|
||||
([toolchain.link_opt("/nodefaultlib:clang_rt.profile.lib")] if PGO == "gen" else []) +
|
||||
([os.path.join(kit.WORK, "hookshot.res")] if os.path.exists(os.path.join(kit.WORK, "hookshot.res")) else []) + # (nx2/make_icon.py)
|
||||
[win_manifest.write(os.path.join(out, "hookshot_manifest.res"))] + # (UTF-8 paths: nx2/win_manifest.py)
|
||||
@@ -1475,7 +1560,7 @@ def main():
|
||||
([ngx_library(NGX_SDK, True)] if NGX_SDK else []) + ["-ldl", "-lpthread", "-lm"]
|
||||
else: # (Triforce: advapi32 for RivaTuner Statistics Server's folder in the registry, gpu/vk/rtss_sys_win.cpp)
|
||||
link = [cc] + toolchain.native_target() + toolchain.native_link() + \
|
||||
["-shared", "-o", vdll, toolchain.link_opt("/map:" + vdll[:-4] + ".map"), "@" + vrsp, "-luser32", "-ladvapi32"] + (["-lc++", "-lunwind"] if toolchain.mingw() else []) + ngx_link(NGX_SDK, toolchain.mingw())
|
||||
["-shared", "-o", vdll, toolchain.link_opt("/map:" + vdll[:-4] + ".map"), "@" + vrsp, "-luser32", "-ladvapi32"] + (["-lc++", "-lunwind"] if toolchain.mingw() else []) + ngx_link(NGX_SDK, toolchain.mingw()) + repro_link()
|
||||
r = subprocess.run(link, capture_output=True, text=True)
|
||||
if r.returncode:
|
||||
undefined = sorted({l.strip() for l in (r.stdout + r.stderr).splitlines() if "undefined symbol" in l})
|
||||
|
||||
@@ -321,7 +321,8 @@ def fn_cost(size):
|
||||
|
||||
def text_cost(text):
|
||||
"""The estimated compile cost of a C file of lifted functions"""
|
||||
marks = [m.start() for m in HEAD.finditer(text)] + [len(text)]
|
||||
# ((Triforce, 11-22, the performance audit's IO-40) lifted_c.noted_starts: where HEAD's matches start, found by jumps)
|
||||
marks = list(lifted_c.noted_starts(text)) + [len(text)]
|
||||
return marks[0] + sum(fn_cost(b - a) for a, b in zip(marks, marks[1:]))
|
||||
|
||||
|
||||
@@ -346,7 +347,7 @@ def split_one(gen_dir, path, budget):
|
||||
made, costs = [], {}
|
||||
with open(path, encoding="utf-8", newline="") as f:
|
||||
text = f.read()
|
||||
marks = [h.start() for h in HEAD.finditer(text)]
|
||||
marks = list(lifted_c.noted_starts(text)) # ((Triforce, 11-22, IO-40) as [h.start() for h in HEAD.finditer(text)])
|
||||
ends = marks[1:] + [len(text)]
|
||||
cost = [fn_cost(b - a) for a, b in zip(marks, ends)]
|
||||
total = sum(cost)
|
||||
|
||||
164
nx2/cold_parts.py
Normal file
164
nx2/cold_parts.py
Normal file
@@ -0,0 +1,164 @@
|
||||
"""(Triforce, 11-22, the performance audit's LIFT-17, after Source A's) An opt-in -O2 for every lifted chunk, each cut
|
||||
into parts of a few MB for the compile.
|
||||
|
||||
The build compiles the hot pieces (code_hot_*.c) at -O2 with the play profile and every other lifted chunk (code_NNN.c,
|
||||
code_NNN_M.c) at -O1. RX_COLD_OPT asks for -O2 on those too:
|
||||
|
||||
RX_COLD_OPT=O2 every lifted chunk but the hot pieces at -O2
|
||||
RX_COLD_OPT=<file> only the chunks the file lists, one file name a line (code_012.c; # starts a comment)
|
||||
unset, empty, O1 as before: nothing here runs
|
||||
|
||||
-O2's time grows with the square of a function's size and its passes look across a whole file, so each -O2 chunk is
|
||||
compiled as parts: RX_COLD_PART_MB (6 when unset; 0: no cut) is a part's size by the lift's own estimate
|
||||
(nx2/chunks.py fn_cost). A part is the chunk's declarations and a run of its functions, cut only where a function
|
||||
begins; the parts together hold the chunk's functions once, in order. They are written under <build>/split as
|
||||
code_NNN.p0.c, code_NNN.p1.c ..., with a key file a chunk (the chunk's text and the part size): a build that finds
|
||||
the key and the parts as it wrote them writes nothing. The chunk itself, the lift's file, is not touched, so nothing
|
||||
of the lift or its key changes.
|
||||
|
||||
Source A's figures: +4.4% and +5.5% fps on two PCs where the game's update bounds the frame, for a lifted compile of
|
||||
about twice the processor time (and 23 minutes against 5 without the cut); this build's hot list already runs most of
|
||||
the lifted time at -O2. Opt-in for that cost. Inlining sees one part at a time.
|
||||
|
||||
This file is not part of the lift key (nx2/make_game.py LIFT_KEY_NOT): the lift reads none of it. nx2/build.py is the
|
||||
one caller; tests/test_chunk_parts.py runs the rules on made-up chunks."""
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
|
||||
import lifted_c
|
||||
|
||||
CHUNK = re.compile(r"code_\d{3}(?:_\d+)?\.c") # (a lifted chunk or a part of one the lift cut: never code_hot*.c)
|
||||
PART_MB_DEFAULT = 6
|
||||
CUT_VERSION = "cold_parts 1" # (in every key: a change to how chunk_parts cuts writes the parts again)
|
||||
|
||||
|
||||
def cold_level(env):
|
||||
"""What RX_COLD_OPT asks: None (nothing), "all", or the set of chunk file names its file lists"""
|
||||
v = (env.get("RX_COLD_OPT") or "").strip()
|
||||
if v in ("", "O1"):
|
||||
return None
|
||||
if v == "O2":
|
||||
return "all"
|
||||
if not os.path.isfile(v): # (a level this does not know, or a list that is not there: not taken for "nothing")
|
||||
raise SystemExit("build: RX_COLD_OPT=%s: not O2, O1 or a file that lists chunks" % v)
|
||||
with open(v, encoding="utf-8") as f:
|
||||
names = {line.split("#", 1)[0].strip() for line in f}
|
||||
names.discard("")
|
||||
bad = sorted(n for n in names if not CHUNK.fullmatch(n))
|
||||
if bad:
|
||||
raise SystemExit("build: RX_COLD_OPT=%s: not a lifted chunk's file name: %s" % (v, ", ".join(bad[:5])))
|
||||
return frozenset(names)
|
||||
|
||||
|
||||
def chunk_opt_o2(name, level):
|
||||
"""Whether the lifted file `name` is compiled at -O2 under that level: a chunk it covers, never a hot piece"""
|
||||
if level is None or not CHUNK.fullmatch(name):
|
||||
return False
|
||||
return level == "all" or name in level
|
||||
|
||||
|
||||
def part_bytes(env):
|
||||
"""A part's size in bytes of estimated cost, from RX_COLD_PART_MB (6 when unset or empty); 0: no cut"""
|
||||
v = (env.get("RX_COLD_PART_MB") or "").strip()
|
||||
try:
|
||||
mb = float(v) if v else float(PART_MB_DEFAULT)
|
||||
except ValueError:
|
||||
mb = -1.0
|
||||
if not 0 <= mb <= 1024:
|
||||
raise SystemExit("build: RX_COLD_PART_MB=%s: not a size in MB (0: no cut)" % v)
|
||||
return int(mb * (1 << 20))
|
||||
|
||||
|
||||
def cold_flags(fl, opt, env):
|
||||
"""The chunks' flags for a -O2 chunk: -O2 where the level was, and the part size (so it is in every object's key)"""
|
||||
return ["-O2" if f == "-" + opt else f for f in fl] + ["-DRX_COLD_PART_MB=%d" % (part_bytes(env) >> 20)]
|
||||
|
||||
|
||||
def chunk_parts(text, budget, fn_cost):
|
||||
"""A chunk's text as parts of about `budget` cost each (fn_cost of each function's size): the text before the first
|
||||
function in every part, then a run of whole functions. A function dearer than the budget is a part by itself. One
|
||||
part, the text itself, when the chunk fits the budget, has no two functions, or budget is 0"""
|
||||
marks = [m.start() for m in lifted_c.HEAD_NOTED.finditer(text)]
|
||||
if budget <= 0 or len(marks) < 2:
|
||||
return [text]
|
||||
ends = marks[1:] + [len(text)]
|
||||
cuts, run = [0], 0 # (indexes into marks where a part starts)
|
||||
for i, (a, b) in enumerate(zip(marks, ends)):
|
||||
cost = fn_cost(b - a)
|
||||
if i > cuts[-1] and run + cost > budget:
|
||||
cuts.append(i)
|
||||
run = 0
|
||||
run += cost
|
||||
if len(cuts) == 1:
|
||||
return [text]
|
||||
preamble = text[:marks[0]]
|
||||
bounds = [marks[i] for i in cuts] + [len(text)]
|
||||
return [preamble + text[a:b] for a, b in zip(bounds, bounds[1:])]
|
||||
|
||||
|
||||
def parts_key(text, budget):
|
||||
"""What a chunk's parts depend on: its text, the part size, and how this file cuts"""
|
||||
return hashlib.sha1(("%s %d\n" % (CUT_VERSION, budget)).encode() + text.encode("utf-8"), usedforsecurity=False).hexdigest()
|
||||
|
||||
|
||||
def part_name(chunk_name, i):
|
||||
return "%s.p%d.c" % (chunk_name[:-2], i)
|
||||
|
||||
|
||||
def write_parts(src, split_dir, budget, fn_cost, stats=None):
|
||||
"""The parts of the chunk file `src` under split_dir, written unless its key file says these are they: their paths, in
|
||||
order. Parts an earlier cut of this chunk left and this one does not make are removed. stats: counts "written" and
|
||||
"kept" chunks, for the build's log and the test"""
|
||||
name = os.path.basename(src)
|
||||
with open(src, encoding="utf-8", newline="") as f:
|
||||
text = f.read()
|
||||
key = parts_key(text, budget)
|
||||
key_path = os.path.join(split_dir, name + ".key")
|
||||
try:
|
||||
with open(key_path, encoding="utf-8") as f:
|
||||
had = f.read().split()
|
||||
except OSError:
|
||||
had = []
|
||||
if had[:1] == [key] and len(had) > 1 and all(os.path.isfile(os.path.join(split_dir, p)) for p in had[1:]):
|
||||
if stats is not None:
|
||||
stats["kept"] = stats.get("kept", 0) + 1
|
||||
return [os.path.join(split_dir, p) for p in had[1:]]
|
||||
os.makedirs(split_dir, exist_ok=True)
|
||||
if os.path.exists(key_path): # (first: a build stopped while it writes leaves no key that vouches for half the parts)
|
||||
os.remove(key_path)
|
||||
parts = chunk_parts(text, budget, fn_cost)
|
||||
names = [part_name(name, i) for i in range(len(parts))]
|
||||
for part, piece in zip(names, parts):
|
||||
with open(os.path.join(split_dir, part), "w", encoding="utf-8", newline="") as f:
|
||||
f.write(piece)
|
||||
stale = re.compile(re.escape(name[:-2]) + r"\.p\d+\.c")
|
||||
for old in os.listdir(split_dir):
|
||||
if stale.fullmatch(old) and old not in names:
|
||||
os.remove(os.path.join(split_dir, old))
|
||||
with open(key_path, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(key + "\n" + "".join(n + "\n" for n in names))
|
||||
if stats is not None:
|
||||
stats["written"] = stats.get("written", 0) + 1
|
||||
return [os.path.join(split_dir, n) for n in names]
|
||||
|
||||
|
||||
def expand(srcs, split_dir, env, fn_cost=None, stats=None):
|
||||
"""The build's sources with each -O2 chunk replaced by its parts, and the set of paths that take the -O2 flags (a
|
||||
chunk's parts, or the chunk itself with RX_COLD_PART_MB=0). With RX_COLD_OPT unset: (srcs as given, nothing)"""
|
||||
level = cold_level(env)
|
||||
if level is None:
|
||||
return srcs, frozenset()
|
||||
budget = part_bytes(env)
|
||||
if fn_cost is None:
|
||||
import chunks # (the lift's own estimate; imported only here, when asked: the default build never loads the lifter)
|
||||
fn_cost = chunks.fn_cost
|
||||
out, cold = [], set()
|
||||
for s in srcs:
|
||||
if not chunk_opt_o2(os.path.basename(s), level):
|
||||
out.append(s)
|
||||
continue
|
||||
parts = [s] if budget <= 0 else write_parts(s, split_dir, budget, fn_cost, stats)
|
||||
out += parts
|
||||
cold.update(parts)
|
||||
return out, frozenset(cold)
|
||||
167
nx2/fastvec.py
Normal file
167
nx2/fastvec.py
Normal file
@@ -0,0 +1,167 @@
|
||||
"""(Triforce, 11-21, the performance audit's LIFT-02, after Source A's) Vector moves the kit's translator writes slowly,
|
||||
rewritten on the lifted text: two passes over one instruction's C, each with its helpers appended to a64_fpx.h. The
|
||||
kit's files stay as they are (third_party/ is not edited: CONTRIBUTING.md); install(), called from nx2/lift.py beside
|
||||
insn's and lift_fixes', wraps the kit's functions:
|
||||
|
||||
- a64_lift.vectorize_fpx: vectorize_bytes before it, on the emitter's own text (TBL with a one-register table, EXT),
|
||||
so that widen_vstores, which comes after, no longer meets the loops' tails;
|
||||
- a64_lift.op_text: vectorize_moves on its result (128-bit vector loads, DUP by element);
|
||||
- a64_lift.fpx_header: VBYTE_HEADER and VMOVE_HEADER after the kit's VSET_HEADER, whose A64_OPAQUE and a64_vset64
|
||||
they use.
|
||||
|
||||
Each rewrite leaves the same bytes in the same registers; tests/test_vbytes.py runs every shape both ways. Source A
|
||||
counted, in its market's game code, TBL 2,995 sites, EXT 29,451, 128-bit loads 139,843 and DUP 26,529, and its game
|
||||
work a frame went from 15.46 to about 14.3 ms with these and one more rewrite that Triforce had already
|
||||
(a64_lift.py's vectorize_elem).
|
||||
|
||||
Switches, read at lift time (the lift key holds the A64_ variables): A64_NO_VBYTES=1 and A64_NO_VMOVES=1 keep the kit's
|
||||
text for that pass. The lift's emit processes run nx2/lift.py's module level again (nx2/emit_pool.py), so they have
|
||||
the wrappers too."""
|
||||
import os
|
||||
import re
|
||||
|
||||
import a64_lift
|
||||
|
||||
# TBL with a one-register table, and EXT. The emitter writes each as a loop of 8 or 16 single bytes through arrays on
|
||||
# the stack, then a 16-byte reload of the result that cannot be forwarded from the byte stores. On x86 each is one SSSE3
|
||||
# instruction. TBL: an index of 16 or more gives 0, which PSHUFB does when the index byte's top bit is set; the index
|
||||
# plus 0x70, saturated, keeps 0 to 15 below 0x80 with the same low four bits and takes 16 to 255 to 0x80 or more.
|
||||
# EXT of 16 bytes is PALIGNR, of 8 bytes two shifts of the low halves. Both sources are read before Vd is written, so
|
||||
# Vd may be Vn or Vm.
|
||||
VBYTE_TBL = re.compile(
|
||||
r"\{ uint8_t _t\[16\],_x\[(8|16)\],_r\[\1\]; memcpy\(_t\+0,c->vreg\[(\d+)\],16\); memcpy\(_x,c->vreg\[(\d+)\],\1\); "
|
||||
r"for\(int _i=0;_i<\1;_i\+\+\)\{ unsigned _k=_x\[_i\]; if\(_k<16U\) _r\[_i\]=_t\[_k\]; else _r\[_i\]=0; \} "
|
||||
r"c->vreg\[(\d+)\]\[0\]=0; c->vreg\[\4\]\[1\]=0; memcpy\(c->vreg\[\4\],_r,\1\); \}")
|
||||
VBYTE_EXT = re.compile(
|
||||
r"\{ uint8_t _a\[(8|16)\],_b\[\1\],_r\[\1\]; memcpy\(_a,c->vreg\[(\d+)\],\1\); memcpy\(_b,c->vreg\[(\d+)\],\1\); "
|
||||
r"for\(int _i=0;_i<\1;_i\+\+\)\{ int _k=_i\+(\d+); _r\[_i\] = \(_k<\1\) \? _a\[_k\] : _b\[_k-\1\]; \} "
|
||||
r"c->vreg\[(\d+)\]\[0\]=0; c->vreg\[\5\]\[1\]=0; memcpy\(c->vreg\[\5\],_r,\1\); \}")
|
||||
|
||||
|
||||
def vectorize_bytes(text):
|
||||
if "uint8_t _" not in text or os.environ.get("A64_NO_VBYTES"):
|
||||
return text
|
||||
text = VBYTE_TBL.sub(lambda m: "a64_tbl1(c->vreg[%s],c->vreg[%s],c->vreg[%s],%d);"
|
||||
% (m.group(4), m.group(2), m.group(3), m.group(1) == "16"), text)
|
||||
return VBYTE_EXT.sub(lambda m: "A64_EXT%s(c->vreg[%s],c->vreg[%s],c->vreg[%s],%s);"
|
||||
% (m.group(1), m.group(5), m.group(2), m.group(3), m.group(4)), text)
|
||||
|
||||
|
||||
VBYTE_HEADER = r"""
|
||||
/* ---- (Triforce, LIFT-02) TBL with one table register, and EXT, as one SSSE3 instruction each (nx2/fastvec.py
|
||||
vectorize_bytes). Without SSSE3 (a baseline x86-64 build): plain C, the same bytes ---- */
|
||||
#if defined(__SSSE3__)
|
||||
#include <tmmintrin.h>
|
||||
static inline __attribute__((always_inline)) void a64_tbl1(uint64_t *d, const uint64_t *t, const uint64_t *x, int q) {
|
||||
const __m128i tv = _mm_loadu_si128((const __m128i *)t);
|
||||
const __m128i xv = q ? _mm_loadu_si128((const __m128i *)x) : _mm_loadl_epi64((const __m128i *)x);
|
||||
__m128i r = _mm_shuffle_epi8(tv, _mm_adds_epu8(xv, _mm_set1_epi8(0x70)));
|
||||
if (!q) r = _mm_move_epi64(r);
|
||||
A64_OPAQUE(r);
|
||||
_mm_storeu_si128((__m128i *)d, r);
|
||||
}
|
||||
/* (EXT of 16 bytes: Vn's bytes from imm up, then Vm's; PALIGNR takes the high source first) */
|
||||
#define A64_EXT16(d, n, m, imm) do { \
|
||||
const __m128i a64_e_n_ = _mm_loadu_si128((const __m128i *)(n)), a64_e_m_ = _mm_loadu_si128((const __m128i *)(m)); \
|
||||
__m128i a64_e_r_ = _mm_alignr_epi8(a64_e_m_, a64_e_n_, imm); \
|
||||
A64_OPAQUE(a64_e_r_); \
|
||||
_mm_storeu_si128((__m128i *)(d), a64_e_r_); } while (0)
|
||||
#else
|
||||
static inline void a64_tbl1(uint64_t *d, const uint64_t *t, const uint64_t *x, int q) {
|
||||
uint8_t tb[16], xb[16], r[16] = {0};
|
||||
memcpy(tb, t, 16); memcpy(xb, x, 16);
|
||||
for (int i = 0; i < (q ? 16 : 8); i++) r[i] = xb[i] < 16 ? tb[xb[i]] : 0;
|
||||
memcpy(d, r, 16);
|
||||
}
|
||||
#define A64_EXT16(d, n, m, imm) do { uint8_t a64_e_a_[32], a64_e_r_[16]; memcpy(a64_e_a_, (n), 16); \
|
||||
memcpy(a64_e_a_ + 16, (m), 16); memcpy(a64_e_r_, a64_e_a_ + (imm), 16); memcpy((d), a64_e_r_, 16); } while (0)
|
||||
#endif
|
||||
/* (EXT of 8 bytes: the low halves, Vn's from byte imm up, then Vm's; the top half zero) */
|
||||
#define A64_EXT8(d, n, m, imm) do { uint64_t a64_e_n_ = (n)[0], a64_e_m_ = (m)[0]; \
|
||||
const uint64_t a64_e_r_ = (imm) ? (a64_e_n_ >> (8 * (imm))) | (a64_e_m_ << ((64 - 8 * (imm)) & 63)) : a64_e_n_; \
|
||||
a64_vset64((d), a64_e_r_); } while (0)
|
||||
"""
|
||||
|
||||
|
||||
# Two moves the compiler leaves slow, on the final text (after widen_vstores):
|
||||
# - a 128-bit vector load (LDR Q, LDP Q, LD1 of one register) is recomp_ldp64 into the register's two halves: two
|
||||
# 8-byte stores, from which the next 16-byte read of the register cannot be forwarded. Now one 16-byte load and one
|
||||
# 16-byte store (a64_vld16); guest memory is read the same way, recomp_load64 being a plain copy.
|
||||
# - DUP Vd, Vn.T[i] is a loop through the stack and a 16-byte reload: one broadcast (a64_vdup). The element is read
|
||||
# before Vd is written, so Vd may be Vn.
|
||||
VMOVE_LDQ = re.compile(r"recomp_ldp64\(c,([^;]*?),&c->vreg\[(\d+)\]\[0\],&c->vreg\[\2\]\[1\]\);")
|
||||
VMOVE_DUP = re.compile(
|
||||
r"\{ uint8_t _s\[16\]; memcpy\(_s,c->vreg\[(\d+)\],16\); uint64_t _e=0; memcpy\(&_e,_s\+(\d+),(1|2|4|8)\); "
|
||||
r"uint8_t _d\[16\]; memset\(_d,0,16\); for\(int _i=0;_i<(\d+);_i\+\+\) memcpy\(_d\+_i\*\3,&_e,\3\); "
|
||||
r"a64_vst16\(c->vreg\[(\d+)\],_d\); \}")
|
||||
|
||||
|
||||
def vectorize_moves(text):
|
||||
if "c->vreg" not in text or os.environ.get("A64_NO_VMOVES"):
|
||||
return text
|
||||
text = VMOVE_LDQ.sub(lambda m: "a64_vld16(c->vreg[%s],%s);" % (m.group(2), m.group(1)), text)
|
||||
|
||||
def dup(m):
|
||||
s, off, w, n, d = m.groups()
|
||||
if int(w) * int(n) not in (8, 16): # (a shape that fills neither half nor the whole register: left alone)
|
||||
return m.group(0)
|
||||
return "a64_vdup(c->vreg[%s],c->vreg[%s],%s,%s,%d);" % (d, s, off, w, int(w) * int(n) == 16)
|
||||
return VMOVE_DUP.sub(dup, text)
|
||||
|
||||
|
||||
VMOVE_HEADER = r"""
|
||||
/* ---- (Triforce, LIFT-02) a whole vector register loaded at once, and DUP by element as one broadcast (nx2/fastvec.py
|
||||
vectorize_moves). Without the SSE2 host path: plain C, the same bytes ---- */
|
||||
#if RECOMP_FPX_HOST == 2
|
||||
static inline __attribute__((always_inline)) void a64_vld16(uint64_t *d, uint64_t a) {
|
||||
__m128i x = _mm_loadu_si128((const __m128i *)A64_PTR(a));
|
||||
A64_OPAQUE(x);
|
||||
_mm_storeu_si128((__m128i *)d, x);
|
||||
}
|
||||
static inline __attribute__((always_inline)) void a64_vdup(uint64_t *d, const uint64_t *s, int off, int w, int q) {
|
||||
uint64_t e = 0;
|
||||
memcpy(&e, (const uint8_t *)s + off, w);
|
||||
__m128i r = w == 1 ? _mm_set1_epi8((char)e) : w == 2 ? _mm_set1_epi16((short)e) : w == 4 ? _mm_set1_epi32((int)e)
|
||||
: _mm_set1_epi64x((long long)e);
|
||||
if (!q) r = _mm_move_epi64(r);
|
||||
A64_OPAQUE(r);
|
||||
_mm_storeu_si128((__m128i *)d, r);
|
||||
}
|
||||
#else
|
||||
static inline void a64_vld16(uint64_t *d, uint64_t a) { memcpy(d, A64_PTR(a), 16); }
|
||||
static inline void a64_vdup(uint64_t *d, const uint64_t *s, int off, int w, int q) {
|
||||
uint8_t r[16] = {0};
|
||||
uint64_t e = 0;
|
||||
memcpy(&e, (const uint8_t *)s + off, w);
|
||||
for (int i = 0; i < (q ? 16 : 8) / w; i++) memcpy(r + i * w, &e, w);
|
||||
memcpy(d, r, 16);
|
||||
}
|
||||
#endif
|
||||
"""
|
||||
|
||||
|
||||
_installed = False
|
||||
|
||||
|
||||
def install():
|
||||
"""The two passes around the kit's vectorize_fpx and op_text, and their helpers into its fpx_header. Once"""
|
||||
global _installed
|
||||
if _installed:
|
||||
return
|
||||
_installed = True
|
||||
base_vfpx, base_op_text, base_header = a64_lift.vectorize_fpx, a64_lift.op_text, a64_lift.fpx_header
|
||||
|
||||
def vectorize_fpx(text):
|
||||
return base_vfpx(vectorize_bytes(text))
|
||||
|
||||
def op_text(text, fpx):
|
||||
return vectorize_moves(base_op_text(text, fpx))
|
||||
|
||||
def fpx_header(used):
|
||||
text = base_header(used)
|
||||
at = a64_lift.VSET_HEADER + "\n"
|
||||
if text.count(at) != 1: # (the kit changed: the helpers have nowhere known to go, and the lift must not go on)
|
||||
raise SystemExit("fastvec: a64_fpx.h no longer holds the kit's VSET_HEADER once: where its helpers go")
|
||||
return text.replace(at, at + VBYTE_HEADER + "\n" + VMOVE_HEADER + "\n")
|
||||
|
||||
a64_lift.vectorize_fpx, a64_lift.op_text, a64_lift.fpx_header = vectorize_fpx, op_text, fpx_header
|
||||
147
nx2/fma_soft.py
Normal file
147
nx2/fma_soft.py
Normal file
@@ -0,0 +1,147 @@
|
||||
"""(Triforce, 11-22, the performance audit's LIFT-06, after Source A's) A short way out of the exact fused multiply-add
|
||||
bodies on a CPU without FMA3.
|
||||
|
||||
Where the build has no hardware FMA (x86-64-v2: RECOMP_FPX_FMA64 is 0) the kit's double-precision fast path declines every
|
||||
FMADD, and the single-precision one declines what its binary64 form cannot prove (a binary32 midpoint, a closed gate), so
|
||||
those calls run the exact bodies recomp_fpe_fma64 and recomp_fpe_fma32: integer code of many words, about 180 ns a call
|
||||
by Source A's figure on an i7-3770, 13.7% of a core in its walk. Most of those calls are the plain case, which this
|
||||
answers first, in the body's first lines:
|
||||
|
||||
- only under the default FPCR (no bit of RECOMP_FPX_FPCR_MASK; FZ may be set), with three normal operands (no zero,
|
||||
subnormal, infinity or NaN) and, for binary64, factors whose exponents leave the product and its error exact;
|
||||
- binary64: the product as an exact pair (Dekker's, from Veltkamp's split), the addend added without error (TwoSum), the
|
||||
two small terms added and rounded to odd, then one rounding to nearest (Boldo and Melquiond's emulation of an FMA);
|
||||
- binary32: the product is exact in binary64; the addend is added there with its error, the sum rounded to odd, and one
|
||||
rounding to binary32;
|
||||
- kept only when the result is a normal finite number above the smallest normal (so neither overflow nor underflow, and
|
||||
FZ changes nothing); FPSR.IXC is set when the result is not the exact value, and no other flag can be raised;
|
||||
- anything else goes on into the exact body, unchanged.
|
||||
|
||||
The kit's files stay as they are (third_party/ is not edited): install(), called from nx2/lift.py, wraps the kit's
|
||||
a64_rtgen.runtime_c and puts HEADER after its `#include "a64.h"` and one EARLY line group after each body's head, each
|
||||
place required to be there exactly once, so a refreshed kit that moved them stops the lift with a message.
|
||||
|
||||
Switches: A64_NO_FMA_SOFT=1 at lift time (in the lift key, an A64_ name) leaves the kit's runtime text as it is.
|
||||
HOS_FMA_FAST=0 when the game runs (read once) sends every call to the exact body. Where the build has FMA3 (x86-64-v3)
|
||||
none of this is compiled. tests/test_fma_soft.py runs both bodies with the way out against without it."""
|
||||
import os
|
||||
|
||||
import a64_rtgen
|
||||
|
||||
HEADER = r"""
|
||||
/* ---- (Triforce, 11-22, the performance audit's LIFT-06, after Source A's) the exact fused multiply-add bodies' short way
|
||||
out where the CPU has no FMA3 (nx2/fma_soft.py): the plain case (default FPCR, FZ or not; normal operands; a normal
|
||||
result) by an exact product and sum in binary64, rounded to odd, then rounded once. 1 and the result's bits when it
|
||||
answers (FPSR.IXC set when inexact), 0 for the exact body. HOS_FMA_FAST=0: never. ---- */
|
||||
#if RECOMP_FPX_HOST == 2 && !RECOMP_FPX_FMA64
|
||||
#define RECOMP_FMA_SOFT 1
|
||||
#include <stdlib.h>
|
||||
#if defined(__clang__)
|
||||
#pragma STDC FP_CONTRACT OFF
|
||||
#endif
|
||||
#ifndef RECOMP_FMA_SOFT_PROBE
|
||||
#define RECOMP_FMA_SOFT_PROBE() ((void)0) /* (tests/test_fma_soft.py counts the calls answered here) */
|
||||
#endif
|
||||
#ifndef RECOMP_FMA_SOFT_ON
|
||||
static int recomp_fma_soft_on(void) { /* (read once; a race reads the same value twice) */
|
||||
static int on = -1;
|
||||
int v = __atomic_load_n(&on, __ATOMIC_RELAXED);
|
||||
if (v < 0) {
|
||||
const char *e = getenv("HOS_FMA_FAST");
|
||||
v = !(e && e[0] == '0' && !e[1]);
|
||||
__atomic_store_n(&on, v, __ATOMIC_RELAXED);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
#define RECOMP_FMA_SOFT_ON() recomp_fma_soft_on()
|
||||
#endif
|
||||
/* (a sum's result moved to the neighbour with an odd last bit, on the side its error lies, when it is inexact and even) */
|
||||
static inline double recomp_fma_soft_odd(double s, double err) {
|
||||
uint64_t u = recomp_fpx_db(s);
|
||||
if (!(u & 1u)) u += ((err > 0.0) == (s > 0.0)) ? 1u : (uint64_t)-1;
|
||||
return recomp_fpx_d(u);
|
||||
}
|
||||
static inline int recomp_fma_soft64(GuestContext *c, uint64_t a, uint64_t b, uint64_t z, uint64_t *out) {
|
||||
/* (the factors' biased exponents 543 to 1503: the split cannot overflow, the product is 2^-960 to 2^962 and its error
|
||||
term is exact; the addend any normal number) */
|
||||
if ((c->fpcr & RECOMP_FPX_FPCR_MASK) || (uint32_t)(((uint32_t)(a >> 52) & 0x7ffu) - 543u) > 960u ||
|
||||
(uint32_t)(((uint32_t)(b >> 52) & 0x7ffu) - 543u) > 960u || (uint32_t)(((uint32_t)(z >> 52) & 0x7ffu) - 1u) > 2045u ||
|
||||
!RECOMP_FMA_SOFT_ON())
|
||||
return 0;
|
||||
const double x = recomp_fpx_d(a), y = recomp_fpx_d(b), w = recomp_fpx_d(z);
|
||||
/* x * y = ph + pl exactly */
|
||||
const double ph = x * y;
|
||||
const double xt = x * 134217729.0, xh = xt - (xt - x), xl = x - xh;
|
||||
const double yt = y * 134217729.0, yh = yt - (yt - y), yl = y - yh;
|
||||
const double pl = (((xh * yh - ph) + xh * yl) + xl * yh) + xl * yl;
|
||||
/* w + ph = th + tl exactly */
|
||||
const double th = w + ph, tv = th - w, tl = (w - (th - tv)) + (ph - tv);
|
||||
/* v = tl + pl rounded to odd */
|
||||
double v = tl + pl;
|
||||
const double vv = v - tl, ve = (tl - (v - vv)) + (pl - vv);
|
||||
if (ve != 0.0) v = recomp_fma_soft_odd(v, ve);
|
||||
/* the result, rounded once; its own error for IXC */
|
||||
const double r = th + v, rv = r - th, re = (th - (r - rv)) + (v - rv);
|
||||
const uint64_t t = recomp_fpx_db(r);
|
||||
if (!RECOMP_FPX_BIG64(t)) return 0;
|
||||
if (ve != 0.0 || re != 0.0) c->fpsr |= 16u;
|
||||
RECOMP_FMA_SOFT_PROBE();
|
||||
*out = t;
|
||||
return 1;
|
||||
}
|
||||
static inline int recomp_fma_soft32(GuestContext *c, uint64_t a, uint64_t b, uint64_t z, uint64_t *out) {
|
||||
if ((c->fpcr & RECOMP_FPX_FPCR_MASK) || (uint32_t)(((uint32_t)a & 0x7f800000u) - 0x00800000u) >= 0x7f000000u ||
|
||||
(uint32_t)(((uint32_t)b & 0x7f800000u) - 0x00800000u) >= 0x7f000000u ||
|
||||
(uint32_t)(((uint32_t)z & 0x7f800000u) - 0x00800000u) >= 0x7f000000u || ((a | b | z) >> 32) || !RECOMP_FMA_SOFT_ON())
|
||||
return 0;
|
||||
const double p = (double)recomp_fpx_f(a) * (double)recomp_fpx_f(b); /* (exact: 48 bits) */
|
||||
const double w = (double)recomp_fpx_f(z);
|
||||
double s = p + w;
|
||||
const double sv = s - p, se = (p - (s - sv)) + (w - sv);
|
||||
if (se != 0.0) s = recomp_fma_soft_odd(s, se);
|
||||
const float r = (float)s;
|
||||
const uint32_t t = (uint32_t)recomp_fpx_fb(r);
|
||||
if (!RECOMP_FPX_BIG32(t)) return 0;
|
||||
if (se != 0.0 || (double)r != s) c->fpsr |= 16u;
|
||||
RECOMP_FMA_SOFT_PROBE();
|
||||
*out = t;
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
"""
|
||||
|
||||
INCLUDE = '#include "a64.h"\n'
|
||||
HEAD = "RECOMP_FPE_COLD uint64_t recomp_fpe_fma%d(GuestContext* c,uint64_t _a,uint64_t _b,uint64_t _z){uint64_t _v=0;\n"
|
||||
EARLY = ("#ifdef RECOMP_FMA_SOFT\n"
|
||||
"{ uint64_t _s; if (recomp_fma_soft%d(c, _a, _b, _z, &_s)) return _s; } /* (Triforce, LIFT-06: nx2/fma_soft.py) */\n"
|
||||
"#endif\n")
|
||||
|
||||
|
||||
def patch(text):
|
||||
"""The kit's runtime C with the header and the two ways out; the kit's own text with A64_NO_FMA_SOFT=1"""
|
||||
if os.environ.get("A64_NO_FMA_SOFT"):
|
||||
return text
|
||||
places = [(INCLUDE, INCLUDE + HEADER)] + [(HEAD % n, HEAD % n + EARLY % n) for n in (32, 64)]
|
||||
for old, new in places:
|
||||
if text.count(old) != 1: # (the kit changed: the lift must not go on with half of it)
|
||||
raise SystemExit("fma_soft: %r is %d times in the kit's runtime C (expected once): update nx2/fma_soft.py"
|
||||
% (old.strip()[:70], text.count(old)))
|
||||
text = text.replace(old, new)
|
||||
return text
|
||||
|
||||
|
||||
_installed = False
|
||||
|
||||
|
||||
def install():
|
||||
"""patch() around the kit's a64_rtgen.runtime_c, which its write() calls by name. Once"""
|
||||
global _installed
|
||||
if _installed:
|
||||
return
|
||||
_installed = True
|
||||
base = a64_rtgen.runtime_c
|
||||
|
||||
def runtime_c():
|
||||
return patch(base())
|
||||
|
||||
a64_rtgen.runtime_c = runtime_c
|
||||
62
nx2/fma_steps.py
Normal file
62
nx2/fma_steps.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""(Triforce, 11-22, the performance audit's LIFT-05, after Source A's) The single-precision FRECPS and FRSQRTS steps as
|
||||
one fused instruction where the build has FMA3.
|
||||
|
||||
The kit's fast path for the Newton-Raphson steps, recomp_fpx_recps32 (2 - a*b) and recomp_fpx_rsqrts32 ((3 - a*b) / 2),
|
||||
has two forms: on an Arm host one fused multiply-add, the step rounded once as the guest rounds it; on x86-64 the step in
|
||||
binary64 (two widenings, a multiply, a subtraction, a narrowing) and a test for a binary32 midpoint, which sends the call
|
||||
to the exact body, because x86-64's baseline has no FMA. The game is built for x86-64-v3 where the CPU has it, and there
|
||||
one FMA instruction is that same correctly rounded step. The kit already took this step for the fused multiply-add itself
|
||||
(a64_rtgen.fma32_native, recomp_fpx_fma32); this gives the two steps the same guard. The tests that decide whether the
|
||||
native result is kept (a normal result; under FZ no subnormal operand) are unchanged, and halving a normal result is
|
||||
exact.
|
||||
|
||||
The kit's files stay as they are (third_party/ is not edited): install(), called from nx2/lift.py, wraps the kit's
|
||||
a64_rtgen.runtime_h. Each function's head must be in its text exactly once, so a refreshed kit that changed one stops the
|
||||
lift with a message, not the game.
|
||||
|
||||
Switches: A64_NO_FMA_STEPS=1 at lift time (in the lift key, an A64_ name) leaves the kit's text for the two steps. The
|
||||
build define -DA64_FPX_NO_FMA32, the kit's own for the fused multiply-add, now returns all three to the binary64 form.
|
||||
Nothing changes in a build without FMA3 (x86-64-v2). tests/test_fma_steps.py runs the guest instructions both ways."""
|
||||
import os
|
||||
|
||||
import a64_rtgen
|
||||
|
||||
GUARD_OLD = "#if RECOMP_FPX_HOST == 1\n"
|
||||
GUARD_NEW = "#if RECOMP_FPX_HOST == 1 || (RECOMP_FPX_HOST == 2 && defined(__FMA__) && !defined(A64_FPX_NO_FMA32))\n"
|
||||
# (each function's head line through its #if line, and the line after it, so the form that follows is the one known here)
|
||||
RECPS32_OLD = ("static RECOMP_INLINE int recomp_fpx_recps32(uint64_t a, uint64_t b, uint32_t fz, uint64_t* v) {\n" + GUARD_OLD +
|
||||
" const uint64_t t = recomp_fpx_fb(__builtin_fmaf(-recomp_fpx_f(a), recomp_fpx_f(b), 2.0f));\n")
|
||||
RSQRTS32_OLD = ("static RECOMP_INLINE int recomp_fpx_rsqrts32(uint64_t a, uint64_t b, uint32_t fz, uint64_t* v) {\n" + GUARD_OLD +
|
||||
" /* Halving a normal result is exact, and a normal result is all that is kept. */\n"
|
||||
" const uint64_t t = recomp_fpx_fb(__builtin_fmaf(-recomp_fpx_f(a), recomp_fpx_f(b), 3.0f) * 0.5f);\n")
|
||||
RECPS32_NEW = RECPS32_OLD.replace(GUARD_OLD, GUARD_NEW)
|
||||
RSQRTS32_NEW = RSQRTS32_OLD.replace(GUARD_OLD, GUARD_NEW)
|
||||
|
||||
|
||||
def patch(h):
|
||||
"""The kit's runtime header with the two steps' fused form on x86-64 with FMA3; the kit's text with A64_NO_FMA_STEPS=1"""
|
||||
if os.environ.get("A64_NO_FMA_STEPS"):
|
||||
return h
|
||||
for name, old, new in (("recomp_fpx_recps32", RECPS32_OLD, RECPS32_NEW), ("recomp_fpx_rsqrts32", RSQRTS32_OLD, RSQRTS32_NEW)):
|
||||
if h.count(old) != 1: # (the kit changed: the lift must not go on)
|
||||
raise SystemExit("fma_steps: the kit's %s changed (its head is %d times in the runtime header): update "
|
||||
"nx2/fma_steps.py" % (name, h.count(old)))
|
||||
h = h.replace(old, new)
|
||||
return h
|
||||
|
||||
|
||||
_installed = False
|
||||
|
||||
|
||||
def install():
|
||||
"""patch() around the kit's a64_rtgen.runtime_h, which its write() calls by name. Once"""
|
||||
global _installed
|
||||
if _installed:
|
||||
return
|
||||
_installed = True
|
||||
base = a64_rtgen.runtime_h
|
||||
|
||||
def runtime_h():
|
||||
return patch(base())
|
||||
|
||||
a64_rtgen.runtime_h = runtime_h
|
||||
16
nx2/insn.py
16
nx2/insn.py
@@ -19,6 +19,8 @@ Model:
|
||||
Xd unchanged, PACGA writes 0. Nothing in the game's own code can tell, since no pointer is ever signed.
|
||||
- MTE: tags are always 0. STG/ST2G only do their base-register writeback.
|
||||
"""
|
||||
import os
|
||||
|
||||
import a64emit
|
||||
import a64_lift
|
||||
|
||||
@@ -219,8 +221,22 @@ def ldar_stlr(w):
|
||||
% (rn, T, T, xr(rt), 1 << size))
|
||||
|
||||
|
||||
# (Triforce, 11-22, the performance audit's LIFT-47, after Source B's) DMB of the load or the store kind: ISHLD, ISHST and
|
||||
# the OSH, NSH and full-system forms, eight words (CRm<1:0> = 01 or 10; op2 = 101, so a DSB of those kinds is not one).
|
||||
# The kit writes every DMB and DSB as recomp_barrier(), an out-of-line full fence. x86-64 keeps load-load, load-store and
|
||||
# store-store order in hardware, so there such a DMB needs only that the compiler moves no guest access across it:
|
||||
# A64_DMB_LDST() (nx2/rtgen.py: nothing but a compiler barrier on x86-64, the full fence on any other host). A full DMB
|
||||
# (ISH, SY, ...), every DSB, ISB and STLR's locked store keep what they had, so store-load order holds as before.
|
||||
# Only when the lift runs with A64_DMB_LIGHT=1 (unset, empty or 0: the kit's fence; an A64_ name, so in the lift key).
|
||||
def dmb_light(w):
|
||||
"""Whether w is a DMB of the load or store kind that A64_DMB_LIGHT=1 turns into A64_DMB_LDST()"""
|
||||
return (w & 0xFFFFF0FF) == 0xD50330BF and ((w >> 8) & 3) in (1, 2) and os.environ.get("A64_DMB_LIGHT", "0") not in ("", "0")
|
||||
|
||||
|
||||
def emit(w):
|
||||
"""C for one of the forms above, or None (then suyu translates it)"""
|
||||
if (w & 0xFFFFF0FF) == 0xD50330BF: # DMB (LIFT-47: the LD and ST kinds, with A64_DMB_LIGHT=1)
|
||||
return "A64_DMB_LDST();" if dmb_light(w) else None
|
||||
if (w & 0x3FBF7C00) == 0x089F7C00: # LDAR, STLR, LDLAR, STLLR (o0 = bit 15)
|
||||
return ldar_stlr(w)
|
||||
if (w & 0xBF20FC00) == 0x0E209000 or (w & 0xBF00F400) == 0x0F003000:
|
||||
|
||||
@@ -29,6 +29,10 @@ import kit
|
||||
import nso2 # noqa: F401 (DT_RELR)
|
||||
import insn
|
||||
import lift_fixes
|
||||
import fastvec
|
||||
import fma_soft
|
||||
import fma_steps
|
||||
import vecfp
|
||||
import chunks
|
||||
import standalone
|
||||
import adr_entries # noqa: E402
|
||||
@@ -42,6 +46,10 @@ import a64_rtgen
|
||||
|
||||
insn.install()
|
||||
lift_fixes.install()
|
||||
fastvec.install() # ((Triforce, 11-21, LIFT-02) TBL, EXT, 128-bit loads and DUP as one instruction each: nx2/fastvec.py)
|
||||
fma_soft.install() # ((Triforce, 11-22, LIFT-06) the exact FMA bodies' short way out where the CPU has no FMA3: nx2/fma_soft.py)
|
||||
fma_steps.install() # ((Triforce, 11-22, LIFT-05) FRECPS and FRSQRTS S as one fused instruction where the build has FMA3: nx2/fma_steps.py)
|
||||
vecfp.install() # ((Triforce, 11-22, LIFT-03) SSE fast paths before float forms lifted lane by lane, with A64_VECFP=1: nx2/vecfp.py)
|
||||
|
||||
|
||||
def module_of(img, pc):
|
||||
|
||||
@@ -7,10 +7,60 @@ HEAD_BYTES HEAD, for bytes (build.py's memory estimate reads the files
|
||||
|
||||
The lift writes its C with \n line ends; a file with \r\n ones (written in text mode on Windows) is read the same: none of the
|
||||
patterns needs the end of the definition's line, and a note's line may end in \r\n.
|
||||
|
||||
(Triforce, 11-22, the performance audit's IO-40, after Source A's) The scans themselves, for the callers:
|
||||
|
||||
definitions(text) HEAD's matches (HEAD_BYTES's for bytes), one after the other, as HEAD.finditer gives them
|
||||
noted_starts(text) where HEAD_NOTED's matches start, as [m.start() for m in HEAD_NOTED.finditer(text)]
|
||||
|
||||
A pattern that begins with ^ is tried by finditer at every line of 1.5 GB of C. These jump from one "\nvoid f_" to the
|
||||
next with str.find and try the pattern only there: the same matches, far fewer tries (Source A: two scans over 1.56 GB in
|
||||
1.49 s where they took 11.49 s). HOOKSHOT_LIFTED_C_REGEX=1 (read once): both are the patterns' own finditer, as before.
|
||||
tests/test_lifted_c_scan.py holds them to the patterns.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
|
||||
_DEF = r"void (f_[0-9a-f]+)\(Ctx \*__restrict A64_NOESC c\) \{"
|
||||
HEAD = re.compile(r"^" + _DEF, re.M)
|
||||
HEAD_NOTED = re.compile(r"^(?:/\* .* \*/\r?\n)?" + _DEF, re.M)
|
||||
HEAD_BYTES = re.compile((r"^" + _DEF).encode(), re.M)
|
||||
|
||||
REGEX_SCAN = os.environ.get("HOOKSHOT_LIFTED_C_REGEX", "0") not in ("", "0")
|
||||
_NOTE = re.compile(r"/\* .* \*/\r?\n") # (HEAD_NOTED's optional first line, by itself)
|
||||
|
||||
|
||||
def definitions(text):
|
||||
"""Each definition in text (str or bytes), in order: the match HEAD (or HEAD_BYTES) has for it"""
|
||||
head, mark = (HEAD_BYTES, b"\nvoid f_") if isinstance(text, (bytes, bytearray, memoryview)) else (HEAD, "\nvoid f_")
|
||||
if REGEX_SCAN:
|
||||
yield from head.finditer(text)
|
||||
return
|
||||
# (^ is the start of the text or the place after a \n: the first line, then every "\nvoid f_". A definition is one
|
||||
# line and holds no "\n", so the next one is looked for from where this one's line began)
|
||||
at = 0
|
||||
while at >= 0:
|
||||
m = head.match(text, at)
|
||||
if m:
|
||||
yield m
|
||||
at = text.find(mark, at)
|
||||
if at >= 0:
|
||||
at += 1
|
||||
|
||||
|
||||
def noted_starts(text):
|
||||
"""Where each definition in text (str) starts, its note's line counted in when the line just above is one: the starts
|
||||
of HEAD_NOTED's matches, in order"""
|
||||
if REGEX_SCAN:
|
||||
for m in HEAD_NOTED.finditer(text):
|
||||
yield m.start()
|
||||
return
|
||||
# (HEAD_NOTED matches from the note's line when the line above the definition, whole, is "/* ... */" and its line
|
||||
# end; a match never reaches into the line after a definition's, so that line is free to be the next one's note)
|
||||
for m in definitions(text):
|
||||
start = m.start()
|
||||
if start:
|
||||
above = text.rfind("\n", 0, start - 1) + 1
|
||||
if _NOTE.fullmatch(text, above, start):
|
||||
start = above
|
||||
yield start
|
||||
|
||||
459
nx2/make_game.py
459
nx2/make_game.py
@@ -27,6 +27,7 @@ build's last step, the shaders' preparation, was skipped or failed and is left t
|
||||
import argparse
|
||||
import atexit
|
||||
import contextlib
|
||||
import errno
|
||||
import filecmp
|
||||
import hashlib
|
||||
import importlib.util
|
||||
@@ -838,10 +839,100 @@ def stop_stray_compilers(work, default_work):
|
||||
print("stopped %d process(es) an earlier build left running on this work folder: %s"
|
||||
% (len(doomed), " ".join(sorted(set(doomed.values())))), flush=True)
|
||||
time.sleep(2)
|
||||
return len(doomed) # (lock_work waits for a stopped build's lock: Windows frees it late)
|
||||
except Exception as e: # (not finding them is no reason to stop)
|
||||
print("could not look for an earlier build's processes: %s" % e, flush=True)
|
||||
|
||||
|
||||
# (Triforce, 11-22, the performance audit's IO-47, after Source A's) One build at a time per work folder: make_game.py
|
||||
# holds an OS lock on <work>/make_game.lock for as long as it runs. A second build on the folder stopped nothing on Linux
|
||||
# (two builders, a builder beside the launcher, AppRun --cli): both compiled the same objects and raced on the staged
|
||||
# game folder. On Windows stop_stray_compilers still stops an earlier build first, and the lock is what is left when it
|
||||
# finds none. The lock is the operating system's and ends with its process, so a build that crashed never blocks the
|
||||
# next one. The first byte is the locked one; the holder's process id is written after it, for the refusal to name.
|
||||
# HOOKSHOT_WORK_LOCK=0: no lock, as before.
|
||||
WORK_LOCK = "make_game.lock"
|
||||
WORK_LOCK_HELD = (errno.EWOULDBLOCK, errno.EAGAIN, errno.EACCES, getattr(errno, "EDEADLOCK", errno.EDEADLK))
|
||||
_work_lock = None # ((the open file, its work folder) while this process holds one)
|
||||
|
||||
|
||||
def _lock_first_byte(f):
|
||||
"""The OS lock itself, without waiting: an OSError of WORK_LOCK_HELD when another process holds it"""
|
||||
if os.name == "nt":
|
||||
import msvcrt
|
||||
f.seek(0)
|
||||
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
||||
else:
|
||||
import fcntl
|
||||
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
|
||||
|
||||
def work_lock_holder(path):
|
||||
"""The process id the holder of a work folder's lock wrote after the locked byte, or None"""
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
f.seek(1)
|
||||
return int(f.read(32).split()[0])
|
||||
except (OSError, ValueError, IndexError):
|
||||
return None
|
||||
|
||||
|
||||
def lock_work(work, wait=0.0):
|
||||
"""Takes the work folder's lock for this process, or stops the build (fail) when another build holds it. wait: the
|
||||
seconds to keep trying first, for after stop_stray_compilers stopped a build (Windows frees a killed process's lock
|
||||
a little late). Asked again for the same folder it does nothing; for another folder the old lock is let go first.
|
||||
Only "held by another" refuses: any other error is said and the build goes on without the lock, as before"""
|
||||
global _work_lock
|
||||
if os.environ.get("HOOKSHOT_WORK_LOCK") == "0":
|
||||
return False
|
||||
work = os.path.abspath(work)
|
||||
if _work_lock:
|
||||
if os.path.normcase(_work_lock[1]) == os.path.normcase(work):
|
||||
return True
|
||||
release_work()
|
||||
path = os.path.join(work, WORK_LOCK)
|
||||
try: # (made when it is not there, never emptied: the holder's process id stays readable)
|
||||
f = os.fdopen(os.open(path, os.O_RDWR | os.O_CREAT | getattr(os, "O_BINARY", 0), 0o666), "r+b")
|
||||
except OSError as e:
|
||||
print("could not open %s (%s): this build runs without the work folder's lock" % (path, e), flush=True)
|
||||
return False
|
||||
until = time.monotonic() + wait
|
||||
while True:
|
||||
try:
|
||||
_lock_first_byte(f)
|
||||
break
|
||||
except OSError as e:
|
||||
if e.errno not in WORK_LOCK_HELD:
|
||||
f.close()
|
||||
print("could not lock %s (%s): this build runs without the work folder's lock" % (path, e), flush=True)
|
||||
return False
|
||||
if time.monotonic() >= until:
|
||||
f.close()
|
||||
pid = work_lock_holder(path)
|
||||
fail("Another build is already working in %s%s. Wait for it to finish, or stop it, then build again."
|
||||
% (work, " (process %d)" % pid if pid else ""))
|
||||
time.sleep(0.2)
|
||||
try: # (after the locked byte, where another process may read on Windows)
|
||||
f.seek(1)
|
||||
f.truncate()
|
||||
f.write(b"%d\n" % os.getpid())
|
||||
f.flush()
|
||||
except OSError:
|
||||
pass
|
||||
_work_lock = (f, work)
|
||||
return True
|
||||
|
||||
|
||||
def release_work():
|
||||
"""Lets this process's work folder lock go (the file stays): for a lock on another folder, and for the tests that run
|
||||
main() in temporary folders. A build never calls it: its lock ends with the process"""
|
||||
global _work_lock
|
||||
if _work_lock:
|
||||
f, _work_lock = _work_lock[0], None
|
||||
with contextlib.suppress(OSError, ValueError):
|
||||
f.close() # (closing the file frees its lock, on Windows and with flock alike)
|
||||
|
||||
|
||||
def makefile_words(text):
|
||||
"""Decode clang -M words, preserving ordinary Windows backslashes."""
|
||||
text = re.sub(r"\\\r?\n", " ", text)
|
||||
@@ -1176,9 +1267,117 @@ def copy_writable(src, dst):
|
||||
shutil.copyfile(src, dst)
|
||||
|
||||
|
||||
# (Triforce, 11-22, the performance audit's IO-45, after Source A's) What the finish put into the game folder, noted in
|
||||
# the folder: for each file it installed, the SHA-1 of the source it was copied from and the installed file's size,
|
||||
# modification time, file number and mode right after the swap. The next finish passes over a file whose source hashes
|
||||
# the same and whose installed copy still has that size, time, number and mode: every build copied and swapped every
|
||||
# file, image.bin and the prebuilt DLLs included, and the installed file is never read to find out (a cold read of the
|
||||
# exe is seconds). Anything not noted, not readable or not as noted is copied as before, and so is everything on a file
|
||||
# system that gives no file number (FAT, exFAT). HOOKSHOT_FINISH_COPY_ALL=1: every file copied and swapped, no note.
|
||||
INSTALLED_NOTE = ".triforce_installed.json"
|
||||
|
||||
|
||||
def file_sha1(path):
|
||||
"""The SHA-1 of a file's bytes, as hex; None when it cannot be read"""
|
||||
h = hashlib.sha1()
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
for piece in iter(lambda: f.read(1 << 20), b""):
|
||||
h.update(piece)
|
||||
except OSError:
|
||||
return None
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def installed_stat(path):
|
||||
"""What tells an installed file is still the one the finish put there, without reading it: [size, modification time
|
||||
in ns, file number, mode]. None when it is not there, is no plain file, or the file system gives no file number"""
|
||||
try:
|
||||
st = os.stat(path)
|
||||
except OSError:
|
||||
return None
|
||||
if not stat.S_ISREG(st.st_mode) or st.st_ino == 0:
|
||||
return None
|
||||
return [st.st_size, st.st_mtime_ns, st.st_ino, st.st_mode]
|
||||
|
||||
|
||||
def read_installed(out):
|
||||
"""The game folder's note: {a file's path under the folder, with /: {"sha1": its source's, "stat": installed_stat's}}.
|
||||
Empty when there is none or it is not what write_installed writes (then everything is copied)"""
|
||||
try:
|
||||
with open(os.path.join(out, INSTALLED_NOTE), encoding="utf-8") as f:
|
||||
note = json.load(f)
|
||||
files = note["files"] if note.get("format") == 1 else {}
|
||||
if not isinstance(files, dict):
|
||||
return {}
|
||||
return {k: v for k, v in files.items()
|
||||
if isinstance(v, dict) and isinstance(v.get("sha1"), str) and isinstance(v.get("stat"), list)}
|
||||
except (OSError, ValueError, KeyError, AttributeError, TypeError):
|
||||
return {}
|
||||
|
||||
|
||||
def installed_name(out, dst):
|
||||
"""An installed file's name in the note: its path under the game folder, with / between the parts"""
|
||||
return os.path.relpath(dst, out).replace(os.sep, "/")
|
||||
|
||||
|
||||
def installed_same(note, out, dst, digest):
|
||||
"""Whether dst in the game folder is still the copy of a source with this SHA-1 that the note says the finish
|
||||
installed: the note's hash is this one and the file's size, time, number and mode are the noted ones"""
|
||||
was = note.get(installed_name(out, dst))
|
||||
now = installed_stat(dst)
|
||||
return bool(digest and was and now and was["sha1"] == digest and was["stat"] == now)
|
||||
|
||||
|
||||
def write_installed(out, files):
|
||||
"""The note, whole: written beside itself and moved in, so a build stopped here leaves the old note or the new one.
|
||||
A note that cannot be written is said and costs the next build its copies, nothing more"""
|
||||
path = os.path.join(out, INSTALLED_NOTE)
|
||||
try:
|
||||
with open(path + ".new", "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump({"format": 1, "files": files}, f, indent=1, sort_keys=True)
|
||||
os.replace(path + ".new", path)
|
||||
except OSError as e:
|
||||
print("could not note what was installed in %s (%s): the next build copies every file again" % (out, e), flush=True)
|
||||
with contextlib.suppress(OSError):
|
||||
os.remove(path + ".new")
|
||||
|
||||
|
||||
def refuse_dump_links(folder):
|
||||
"""An extracted dump holds only files and folders: a link in it was followed, and hard-linked or copied the file it
|
||||
names (anywhere on this PC) into the game folder. The folder itself may be one. (Walked as a long path: #1468)"""
|
||||
names (anywhere on this PC) into the game folder. The folder itself may be one. (Walked as a long path: #1468)
|
||||
|
||||
(Triforce, 11-21, the performance audit's IO-41, after Source A's) From the folders' listings: os.scandir gives each
|
||||
entry's kind and, on Windows, its attributes with the listing itself (the reparse-point bit is in them), so no file is
|
||||
opened or asked about one by one. An os.lstat per entry took 36 to 49 s over the game's 186,583 files where the
|
||||
listings take 1.3 to 1.5 s (Source A's, Windows 11, NTFS). And a folder that cannot be listed stops the build: os.walk
|
||||
passed over it silently, and what it held was never looked at. HOOKSHOT_DUMP_LINKS_LSTAT=1: an os.lstat each, as before"""
|
||||
if os.environ.get("HOOKSHOT_DUMP_LINKS_LSTAT", "") not in ("", "0"):
|
||||
return refuse_dump_links_lstat(folder)
|
||||
reparse = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400)
|
||||
stack = [long_path(folder)]
|
||||
while stack:
|
||||
d = stack.pop()
|
||||
try:
|
||||
listing = os.scandir(d)
|
||||
except OSError as e:
|
||||
fail("Could not read the game files at %s (%s: %s)." % (folder, d, e.strerror or e))
|
||||
with listing:
|
||||
for entry in listing:
|
||||
try:
|
||||
link = entry.is_symlink() or getattr(entry.stat(follow_symlinks=False), "st_file_attributes", 0) & reparse
|
||||
folder_too = not link and entry.is_dir(follow_symlinks=False)
|
||||
except OSError as e:
|
||||
fail("Could not read the game files at %s (%s: %s)." % (folder, entry.path, e.strerror or e))
|
||||
if link:
|
||||
fail("The game files at %s contain a link (%s): copy them without links and choose that folder."
|
||||
% (folder, entry.path))
|
||||
if folder_too:
|
||||
stack.append(entry.path)
|
||||
|
||||
|
||||
def refuse_dump_links_lstat(folder):
|
||||
"""(HOOKSHOT_DUMP_LINKS_LSTAT=1) refuse_dump_links as it was: os.walk, an os.lstat per entry"""
|
||||
for d, dirs, files in os.walk(long_path(folder)):
|
||||
for name in dirs + files:
|
||||
path = os.path.join(d, name)
|
||||
@@ -1214,31 +1413,43 @@ def long_path(p):
|
||||
|
||||
|
||||
def link_or_copy(src, dst):
|
||||
"""the romfs by hard links where the disk allows: 26 GB not written twice"""
|
||||
"""the romfs by hard links where the disk allows: 26 GB not written twice
|
||||
|
||||
(Triforce, 11-21, the performance audit's IO-41, after Source A's) One walk; each folder made once, not asked for at
|
||||
every file of it; and no os.path.exists before each link: os.link says FileExistsError itself for a file a retry left,
|
||||
which is passed over. The staged copy, for a disk that cannot link, is as it was"""
|
||||
src, dst = long_path(src), long_path(dst)
|
||||
files = [os.path.join(d, f) for d, _, fs in os.walk(src) for f in fs]
|
||||
for i, s in enumerate(files):
|
||||
t = os.path.join(dst, os.path.relpath(s, src))
|
||||
os.makedirs(os.path.dirname(t), exist_ok=True)
|
||||
if not os.path.exists(t):
|
||||
walked = [(d, fs) for d, _, fs in os.walk(src)]
|
||||
total, i = sum(len(fs) for _, fs in walked), 0
|
||||
for d, fs in walked:
|
||||
rel = os.path.relpath(d, src)
|
||||
# (the top folder is dst itself: an extended-length path keeps "dst\\." as it is written, a folder named ".")
|
||||
to = dst if rel == os.curdir else os.path.join(dst, rel)
|
||||
os.makedirs(to, exist_ok=True)
|
||||
for f in fs:
|
||||
s, t = os.path.join(d, f), os.path.join(to, f)
|
||||
try:
|
||||
os.link(s, t)
|
||||
except FileExistsError:
|
||||
pass # (a retry: what an earlier try put there stays)
|
||||
except OSError:
|
||||
# A retry skips existing targets, so publish the copy only after it is complete.
|
||||
fd, staged = tempfile.mkstemp(prefix=".romfs-", dir=os.path.dirname(t))
|
||||
try:
|
||||
os.close(fd)
|
||||
shutil.copy2(s, staged)
|
||||
os.replace(staged, t)
|
||||
except BaseException:
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
if os.name == "nt":
|
||||
# copy2 can carry the source's read-only attribute over.
|
||||
os.chmod(staged, stat.S_IWRITE)
|
||||
os.unlink(staged)
|
||||
raise
|
||||
if i % 500 == 0:
|
||||
say("progress", "%.3f" % (i / max(1, len(files))))
|
||||
if not os.path.exists(t):
|
||||
# A retry skips existing targets, so publish the copy only after it is complete.
|
||||
fd, staged = tempfile.mkstemp(prefix=".romfs-", dir=to)
|
||||
try:
|
||||
os.close(fd)
|
||||
shutil.copy2(s, staged)
|
||||
os.replace(staged, t)
|
||||
except BaseException:
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
if os.name == "nt":
|
||||
# copy2 can carry the source's read-only attribute over.
|
||||
os.chmod(staged, stat.S_IWRITE)
|
||||
os.unlink(staged)
|
||||
raise
|
||||
if i % 500 == 0:
|
||||
say("progress", "%.3f" % (i / max(1, total)))
|
||||
i += 1
|
||||
|
||||
|
||||
def shader_census_cmd(romfs, out):
|
||||
@@ -1266,24 +1477,111 @@ def compile_alone_failed(lines):
|
||||
elif "link FAILED" in line:
|
||||
return False
|
||||
return bool(failed)
|
||||
|
||||
|
||||
LIFT_KEPT_ONCE = ".lift_kept_after_failure" # (in <work>/sa, not in sa/gen, whose files gen_print vouches for: the lift was kept after a failed compile, once)
|
||||
|
||||
|
||||
def failed_only_own_sources(lines, work):
|
||||
"""(Triforce, 11-22, the performance audit's LIFT-27, after Source A's) Whether a build that failed in its compile
|
||||
(compile_alone_failed) failed only in the program's own sources, host/ and the renderers, and in nothing the lift
|
||||
wrote: then the translated game is not what failed, and its key can stay, so that the next build does not translate
|
||||
the game again (a compiler out of memory on a renderer's file cost the retry the whole lift, a minute to several).
|
||||
lines: build.py's own, "FAILED <file> (...)" for each file that failed alone too and "compiled in Ns, K failed".
|
||||
False unless every one of the K files is named, none is a lifted file (code_*) and none is a file of the lift's
|
||||
folder or of the build's generated one (<work>/sa/gen, <work>/sa/build/gen): a lifted file cut short or left by
|
||||
another builder fails there or at the link (issues 1197, 1176), and the lift is then forgotten as before"""
|
||||
names, count = [], None
|
||||
for line in lines:
|
||||
m = re.match(r"FAILED (\S+) \(", line)
|
||||
if m:
|
||||
names.append(m.group(1))
|
||||
m = re.match(r"compiled in \d+s, (\d+) failed", line)
|
||||
if m:
|
||||
count = int(m.group(1))
|
||||
if not names or count != len(names):
|
||||
return False
|
||||
made = (os.path.join(work, "sa", "gen"), os.path.join(work, "sa", "build", "gen"))
|
||||
return not any(n.startswith("code_") or any(os.path.exists(os.path.join(d, n)) for d in made) for n in names)
|
||||
|
||||
|
||||
def keep_lift_after_failure(lines, work, environ=None):
|
||||
"""Whether a failed build keeps the lift's key: HOOKSHOT_KEEP_LIFT is not 0, the compile alone failed, only the
|
||||
program's own sources failed (failed_only_own_sources), and the build before this one did not keep it the same way.
|
||||
That last: a generated header cut short fails the program's own files, which name nothing of the lift; kept twice,
|
||||
such a lift would fail at every try, so the second failure in a row forgets it, as every failure did before.
|
||||
Leaves LIFT_KEPT_ONCE in <work>/sa when it says yes and removes it when it says no (main removes it after a build
|
||||
that compiled)"""
|
||||
environ = os.environ if environ is None else environ
|
||||
mark = os.path.join(work, "sa", LIFT_KEPT_ONCE)
|
||||
again = os.path.exists(mark)
|
||||
keep = environ.get("HOOKSHOT_KEEP_LIFT", "1") != "0" and not again and compile_alone_failed(lines) and \
|
||||
failed_only_own_sources(lines, work)
|
||||
try:
|
||||
if keep:
|
||||
open(mark, "w").close()
|
||||
elif again:
|
||||
os.remove(mark)
|
||||
except OSError:
|
||||
keep = False # (the mark cannot be kept: not twice in a row cannot be told, so the lift is forgotten)
|
||||
return keep
|
||||
PROMPT_KEY = ".made_from" # (in <out>/prompts: prompts_key of what that folder was made from)
|
||||
|
||||
|
||||
def prompts_key(out):
|
||||
def prompt_game_files(romfs):
|
||||
"""((Triforce, 11-21, the performance audit's IO-43) The game's files nx2/prompt_assets.py reads, as names under the
|
||||
romfs, each once and in a fixed order: the zstd dictionaries' pack, the font, the textures, the layouts, the camera's
|
||||
pack, the resource size table and every language's messages. None when the tool's own names cannot be had.
|
||||
tests/test_prompts_background.py holds this list against what the tool opens"""
|
||||
try:
|
||||
import prompt_assets
|
||||
except Exception: # (the tool is not beside this one, or does not load: the prompts are made at every build, as before)
|
||||
return None
|
||||
names = ["Pack/ZsDic.pack.zs", prompt_assets.FONT, prompt_assets.TEXTURES, prompt_assets.LAYOUTS, prompt_assets.CAMERA]
|
||||
try: # (the first by name, as prompt_assets.read_size_table takes it)
|
||||
names += ["System/Resource/" + n for n in sorted(os.listdir(os.path.join(romfs, "System", "Resource")))
|
||||
if n.startswith("ResourceSizeTable.") and n.endswith(".rsizetable.zs")][:1]
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
names += [prompt_assets.MALS + "/" + n for n in sorted(os.listdir(os.path.join(romfs, prompt_assets.MALS)))
|
||||
if n.endswith(".sarc.zs")]
|
||||
except OSError:
|
||||
pass
|
||||
return names
|
||||
|
||||
|
||||
def prompts_key(out, romfs=None):
|
||||
"""What the button prompts of other controllers are made from, as one hash: the tools that draw them (nx2/prompt_*.py,
|
||||
the faces' drawings and parts in nx2/prompt_faces, the icon fonts in third_party/kenney_input), the packages they are
|
||||
drawn with (nx2/prompt_requirements.txt) and the game's files, by the unpack's stamp (.triforce_romfs: the .nup's name
|
||||
and size). None when the romfs has no such stamp (the player's own folders, linked in): its prompts are made at every
|
||||
build, as before. (Triforce) The keyboard and mouse face's too: nx2/prompt_keys.py, which draws it, and the font its
|
||||
labels are set in (third_party/fonts/libertinus: that one file, the menus' other fonts are not the prompts')"""
|
||||
and size). (Triforce) The keyboard and mouse face's too: nx2/prompt_keys.py, which draws it, and the font its
|
||||
labels are set in (third_party/fonts/libertinus: that one file, the menus' other fonts are not the prompts')
|
||||
|
||||
(Triforce, 11-21, the performance audit's IO-43, after Source A's) A romfs with no such stamp (the player's own
|
||||
folders, linked in) had no key, and its prompts were drawn at every build and every update, 60 to 87 s each time:
|
||||
its key is now the content of the game's files the tool reads (prompt_game_files, about 100 MB hashed). And for
|
||||
every game the key holds host/prompts.c, which reads what is drawn, and the release these tools are (version.txt or
|
||||
VERSION; a checkout has neither). None, the prompts made at every build as before: with no stamp and no romfs given,
|
||||
when the tool's names cannot be had, or with HOOKSHOT_PROMPTS_FOLDER_KEY=0"""
|
||||
stamp = os.path.join(out, ".triforce_romfs")
|
||||
game = [] # (the game's files by content, where no stamp says which dump they are)
|
||||
if not os.path.isfile(stamp):
|
||||
return None
|
||||
if not romfs or os.environ.get("HOOKSHOT_PROMPTS_FOLDER_KEY", "") == "0":
|
||||
return None
|
||||
names = prompt_game_files(romfs)
|
||||
if names is None:
|
||||
return None
|
||||
game = [("romfs/" + n, os.path.join(long_path(romfs), *n.split("/"))) for n in names]
|
||||
stamp = None
|
||||
# (each file under a name that does not depend on where the tools are installed)
|
||||
files = [("nx2/" + n, os.path.join(HERE, n))
|
||||
for n in ("prompt_assets.py", "prompt_draw.py", "prompt_faces.py", "prompt_requirements.txt", "prompt_keys.py")]
|
||||
labels = "third_party/fonts/libertinus/LibertinusSans-Bold.ttf"
|
||||
files.append((labels, os.path.join(ROOT, *labels.split("/"))))
|
||||
# ((Triforce, 11-21, IO-43) what reads the drawn files in the game, and which release these tools are)
|
||||
files.append(("host/prompts.c", os.path.join(ROOT, "host", "prompts.c")))
|
||||
files += [("release/" + n, os.path.join(ROOT, n)) for n in ("version.txt", "VERSION")]
|
||||
for name, root in (("nx2/prompt_faces", os.path.join(HERE, "prompt_faces")),
|
||||
("third_party/kenney_input", os.path.join(ROOT, "third_party", "kenney_input"))):
|
||||
for folder, dirs, names in os.walk(root):
|
||||
@@ -1291,13 +1589,19 @@ def prompts_key(out):
|
||||
files += [(name + "/" + os.path.relpath(os.path.join(folder, n), root).replace(os.sep, "/"), os.path.join(folder, n))
|
||||
for n in sorted(names)]
|
||||
h = hashlib.sha1()
|
||||
with open(stamp, "rb") as f:
|
||||
h.update(f.read())
|
||||
for name, path in files:
|
||||
if stamp:
|
||||
with open(stamp, "rb") as f:
|
||||
h.update(f.read())
|
||||
else:
|
||||
h.update(b"the game's files, by content\n")
|
||||
for name, path in game + files:
|
||||
if os.path.isfile(path):
|
||||
h.update(name.encode())
|
||||
with open(path, "rb") as f:
|
||||
h.update(f.read())
|
||||
for piece in iter(lambda: f.read(1 << 20), b""):
|
||||
h.update(piece)
|
||||
elif (name, path) in game:
|
||||
h.update(name.encode() + b": none\n") # (a file the tool would miss: not the key of a romfs that has it)
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
@@ -1355,7 +1659,7 @@ class Prompts:
|
||||
|
||||
def __init__(self, romfs, out, work):
|
||||
self.out, self.folder, self.new = out, os.path.join(out, "prompts"), os.path.join(out, "prompts.new")
|
||||
self.key = prompts_key(out)
|
||||
self.key = prompts_key(out, romfs) # ((Triforce, 11-21, IO-43) the romfs: its files' content where it has no stamp)
|
||||
self.proc = self.log = None
|
||||
self.state = "no fontTools" if importlib.util.find_spec("fontTools") is None else None
|
||||
if self.state:
|
||||
@@ -1418,7 +1722,12 @@ class Prompts:
|
||||
# the lift can import and to what its files name.
|
||||
LIFT_KEY_NOT = ("make_game.py", "build.py", "nup.py", "version.py", "shader_census.py", "prompt_assets.py", "prompt_draw.py",
|
||||
"prompt_faces.py", "prompt_requirements.txt", "prompt_keys.py",
|
||||
"code_mods.py") # (the mods' code patches: what it does to the game's code is in the key, the exefs's files)
|
||||
"code_mods.py", # (the mods' code patches: what it does to the game's code is in the key, the exefs's files)
|
||||
# ((Triforce, 11-22, the performance audit's LIFT-27) the last release's shader translation stamp, which
|
||||
# build.py alone reads (RELEASED_STAMP): a release that changed the shader translator lifted the game again.
|
||||
# HOOKSHOT_KEEP_LIFT=0 puts it back in the key: lift_key)
|
||||
"released_translation_stamp.json",
|
||||
"cold_parts.py") # ((Triforce, 11-22, LIFT-17) the compile's parts of a -O2 chunk: nx2/build.py's alone
|
||||
|
||||
|
||||
def lift_key_file(rel):
|
||||
@@ -1437,13 +1746,18 @@ def lift_key(exefs, work, env, chunks=True):
|
||||
import standalone
|
||||
h = hashlib.sha1(sys.version.encode(), usedforsecurity=False)
|
||||
h.update(" ".join(sorted(standalone.implemented())).encode())
|
||||
# ((Triforce, 11-22, the performance audit's LIFT-27, after Source A's) HOOKSHOT_KEEP_LIFT=0: the key as it was, with
|
||||
# every A64_, OOT_ and RX_ variable and the paths in it, and the release's translation stamp among its files. Read
|
||||
# here, inside the function, like COMPILE_ONLY_ENV below)
|
||||
key_as_before = os.environ.get("HOOKSHOT_KEEP_LIFT", "1") == "0"
|
||||
# (each file named from its own base: the exefs and the work folder may be on another drive than the source, which relpath refuses)
|
||||
files = [(exefs, os.path.join(exefs, f)) for f in sorted(os.listdir(exefs))]
|
||||
for top in (HERE, os.path.join(ROOT, "third_party", "a64-kit")):
|
||||
for d, dirs, fs in os.walk(top):
|
||||
dirs[:] = sorted(x for x in dirs if x != "__pycache__")
|
||||
files += [(ROOT, os.path.join(d, f)) for f in sorted(fs) if f.endswith((".py", ".c", ".h", ".cpp", ".hpp", ".txt", ".json"))
|
||||
and (top != HERE or lift_key_file(os.path.relpath(os.path.join(d, f), HERE).replace(os.sep, "/")))]
|
||||
and (top != HERE or lift_key_file(os.path.relpath(os.path.join(d, f), HERE).replace(os.sep, "/"))
|
||||
or (key_as_before and d == HERE and f == "released_translation_stamp.json"))]
|
||||
files.append((work, os.path.join(work, "hot_funcs.txt")))
|
||||
if chunks:
|
||||
files.append((work, os.path.join(work, "image", "chunks.txt")))
|
||||
@@ -1453,6 +1767,19 @@ def lift_key(exefs, work, env, chunks=True):
|
||||
with open(f, "rb") as src:
|
||||
h.update(hashlib.sha1(src.read(), usedforsecurity=False).digest())
|
||||
lift_env = dict(env, **{k: v for k, v in os.environ.items() if k.startswith(("A64_", "OOT_", "RX_"))})
|
||||
# ((Triforce, 11-21, LIFT-10) what only the compile after the lift reads: the renderer's profile translates no game code.
|
||||
# Named here, inside the function: tests/test_lift_key_files.py and test_spawn_emit.py run lift_key cut out by itself)
|
||||
COMPILE_ONLY_ENV = ("RX_VK_PGO", "RX_VK_PGO_FILE")
|
||||
for name in COMPILE_ONLY_ENV:
|
||||
lift_env.pop(name, None)
|
||||
# ((Triforce, 11-22, LIFT-27) only what shapes the C: the lifter's own switches (A64_*: nx2/fastvec.py and the kit's
|
||||
# a64_lift.py read no others) and the target (RX_TARGET: the kit's toolchain.windows). The rest that the lift's files
|
||||
# read are folders (OOT_WORK, OOT_DUMP, OOT_EXEFS, OOT_ROMFS: the files there are hashed above by their contents),
|
||||
# job counts and where the compiler is, and what the compile after the lift reads (RX_RELEASE, the profile's path):
|
||||
# a build from another folder, or one more variable in the environment, lifted the same C again.
|
||||
# tests/test_lift_key_env.py holds the lift's files to that list)
|
||||
if not key_as_before:
|
||||
lift_env = {k: v for k, v in lift_env.items() if k.startswith("A64_") or k == "RX_TARGET"}
|
||||
h.update(repr(sorted(lift_env.items())).encode())
|
||||
return h.hexdigest()
|
||||
|
||||
@@ -1843,7 +2170,9 @@ def main(a=None):
|
||||
check_cpu() # (no game read yet: the AppImage on a CPU without AVX2 stops here)
|
||||
if not LINUX:
|
||||
check_windows_sdk()
|
||||
stop_stray_compilers(work, a.default_work)
|
||||
stopped = stop_stray_compilers(work, a.default_work)
|
||||
# ((Triforce, 11-22, the performance audit's IO-47) after the stop, so that Windows still replaces a build left running)
|
||||
lock_work(work, wait=10 if stopped else 0)
|
||||
drop_stale_objects(work)
|
||||
if getattr(a, "fetch_ngx_sdk", False):
|
||||
fetch_ngx_sdk(a)
|
||||
@@ -1853,9 +2182,9 @@ def main(a=None):
|
||||
os.environ["HOOKSHOT_FSR"] = "1"
|
||||
fetch_fsr_runtime(work) # (AMD's two libraries, for the game folder: place_fsr_runtime at the end)
|
||||
open(os.path.join(out, FSR_MARK), "w").close()
|
||||
if os.environ.get("HOOKSHOT_VK_PREBUILT"): # (the AppImage's back end, built without FSR 4)
|
||||
print("--fsr: this build uses the prebuilt Vulkan back end (HOOKSHOT_VK_PREBUILT), which has no AMD FSR 4: the "
|
||||
"Upscaler row will not offer it", flush=True)
|
||||
if os.environ.get("HOOKSHOT_VK_PREBUILT"): # (the AppImage's back end: built with FSR 4 since 11-20, appimage.sh)
|
||||
print("--fsr: this build uses the prebuilt Vulkan back end (HOOKSHOT_VK_PREBUILT), which has AMD FSR 4 built in "
|
||||
"already: the Upscaler row offers it once amd_fidelityfx_upscaler_vk.so is in the game folder", flush=True)
|
||||
else:
|
||||
os.environ.pop("HOOKSHOT_FSR", None)
|
||||
if os.path.isfile(os.path.join(out, FSR_MARK)):
|
||||
@@ -1966,7 +2295,8 @@ def main(a=None):
|
||||
code_patches = _code_mods.build_step(exefs, out, say=lambda line: print(line, flush=True))
|
||||
|
||||
# (made once on the development PC: lists of addresses and counts, no game code)
|
||||
for name in ("hot_funcs.txt", "hot_order.txt", "hookshot_pieces.prof", "hookshot.res"):
|
||||
# ((Triforce, 11-21, the performance audit's LIFT-10) and the renderer's play profile, triforce_vk.profdata)
|
||||
for name in ("hot_funcs.txt", "hot_order.txt", "hookshot_pieces.prof", "triforce_vk.profdata", "hookshot.res"):
|
||||
src = os.path.join(ROOT, "work", name)
|
||||
dst = os.path.join(work, name)
|
||||
# (copied only when it differs: a compiler still ending from a cancelled build keeps the profile mapped, WinError 1224)
|
||||
@@ -1982,6 +2312,11 @@ def main(a=None):
|
||||
env["A64_HOT_FUNCS"] = os.path.join(work, "hot_funcs.txt")
|
||||
if os.path.exists(os.path.join(work, "hookshot_pieces.prof")) and profile_readable(os.path.join(work, "hookshot_pieces.prof")):
|
||||
env["RX_PGO"] = os.path.join(work, "hookshot_pieces.prof")
|
||||
# ((Triforce, 11-21, LIFT-10) the renderer's own code with the shipped profile, unless RX_VK_PGO says what to do (gen, a
|
||||
# file, 0) or this compiler cannot read it: nx2/build.py vk_pgo_flags)
|
||||
vk_profile = os.path.join(work, "triforce_vk.profdata")
|
||||
if "RX_VK_PGO" not in os.environ and os.path.exists(vk_profile) and profile_readable(vk_profile):
|
||||
env["RX_VK_PGO"] = vk_profile
|
||||
|
||||
prompts = Prompts(romfs, out, work) # (drawn beside the lift and the compile: finish() below)
|
||||
atexit.register(prompts.stop) # (a build that stops for any reason takes its drawing with it)
|
||||
@@ -2060,7 +2395,8 @@ def main(a=None):
|
||||
said = [] # (build.py's lines about how its compile and its links went: compile_alone_failed)
|
||||
|
||||
def compiled(line):
|
||||
if line.startswith("compiled in ") or "link FAILED" in line:
|
||||
# ((Triforce, 11-22, LIFT-27) and which files failed, "FAILED <file> (...": failed_only_own_sources)
|
||||
if line.startswith("compiled in ") or "link FAILED" in line or line.startswith("FAILED "):
|
||||
said.append(line)
|
||||
m = re.match(r"(\d+) files, ", line)
|
||||
if m:
|
||||
@@ -2093,13 +2429,21 @@ def main(a=None):
|
||||
# (hookshot) a compile or link that failed forgets the lift: the next build translates the game again. A lifted file cut short
|
||||
# (an antivirus, a full disk, a stopped run: "g_module_b" for g_module_base, issue 1197) or lifted C another builder left (20
|
||||
# undefined game functions at the link, issue 1176) otherwise failed the same way at every try, the key still matching
|
||||
# ((Triforce, 11-22, the performance audit's LIFT-27, after Source A's) but for a compile that failed only in the
|
||||
# program's own sources, once: the translated game is not what failed. keep_lift_after_failure; HOOKSHOT_KEEP_LIFT=0:
|
||||
# forgotten after every failed build, as before)
|
||||
lift_kept = keep_lift_after_failure(said, work)
|
||||
key_file = os.path.join(work, "sa", "gen", LIFT_KEY)
|
||||
if os.path.exists(key_file):
|
||||
if os.path.exists(key_file) and not lift_kept:
|
||||
os.remove(key_file)
|
||||
# (and its compiled objects go too: a cached runtime object from another version linked with every object "compiled in 0s"
|
||||
# and a64_dirty, a64_icall, a64_lookup, recomp_cntpct undefined, at every try (issue 1197, Linux, v2.0.10))
|
||||
# (Triforce: but not when the compile alone failed, before any link: compile_alone_failed)
|
||||
if compile_alone_failed(said):
|
||||
if lift_kept:
|
||||
print("the build failed in its compile, in the program's own files only: the translated game and the files that "
|
||||
"did compile are kept, and the next build compiles only the rest (a second failure like it translates the "
|
||||
"game again)", flush=True)
|
||||
elif compile_alone_failed(said):
|
||||
print("the build failed in its compile: the files that did compile are kept, and the next build translates the game "
|
||||
"again and compiles only the rest", flush=True)
|
||||
else:
|
||||
@@ -2108,6 +2452,8 @@ def main(a=None):
|
||||
print("the build failed: the next one translates and compiles the game again (nothing of this one is reused)", flush=True)
|
||||
prompts.stop()
|
||||
raise
|
||||
with contextlib.suppress(OSError): # ((Triforce, 11-22, LIFT-27) a build that compiled: no failure in a row any more)
|
||||
os.remove(os.path.join(work, "sa", LIFT_KEPT_ONCE))
|
||||
compile_seconds = time.time() - compile_started
|
||||
|
||||
say("stage", "finish", "Putting the game folder together")
|
||||
@@ -2137,6 +2483,25 @@ def main(a=None):
|
||||
staged.append((os.path.join(work, "sa", "image", "image.bin"), os.path.join(out, "image.bin")))
|
||||
if launcher:
|
||||
staged.append((launcher, os.path.join(out, BESIDE[0])))
|
||||
# ((Triforce, 11-22, the performance audit's IO-45) a file the folder already has from the same source is passed
|
||||
# over: INSTALLED_NOTE above. What is left in staged is copied and swapped as before)
|
||||
copy_all = os.environ.get("HOOKSHOT_FINISH_COPY_ALL", "0") not in ("", "0")
|
||||
installed_note = {} if copy_all else read_installed(out)
|
||||
installed_now, wanted, there = {}, staged, []
|
||||
staged = []
|
||||
for src, dst in wanted:
|
||||
digest = None if copy_all else file_sha1(src)
|
||||
if installed_same(installed_note, out, dst, digest):
|
||||
installed_now[installed_name(out, dst)] = installed_note[installed_name(out, dst)]
|
||||
there.append(installed_name(out, dst))
|
||||
with contextlib.suppress(OSError): # (a <name>.new an interrupted build left: gone when it can go, never unlocked)
|
||||
os.remove(dst + ".new")
|
||||
else:
|
||||
staged.append((src, dst))
|
||||
installed_now[installed_name(out, dst)] = {"sha1": digest, "stat": None}
|
||||
if there:
|
||||
print("the game folder has %d of this build's %d files already (%s): not copied again"
|
||||
% (len(there), len(wanted), ", ".join(there)), flush=True)
|
||||
for src, dst in staged:
|
||||
try:
|
||||
if os.path.exists(dst + ".new"):
|
||||
@@ -2152,6 +2517,16 @@ def main(a=None):
|
||||
except PermissionError:
|
||||
fail("Could not replace %s. Close any running copy of Triforce and check that the file and folder are writable, then retry. "
|
||||
"Staged replacements remain beside the installed files; the compiled source remains at %s." % (dst, src))
|
||||
# ((Triforce, 11-22, IO-45) noted only now, with every swap done: each file just installed as it is in the folder)
|
||||
if copy_all:
|
||||
with contextlib.suppress(OSError):
|
||||
os.remove(os.path.join(out, INSTALLED_NOTE))
|
||||
else:
|
||||
for src, dst in staged:
|
||||
installed_now[installed_name(out, dst)]["stat"] = installed_stat(dst)
|
||||
installed_now = {k: v for k, v in installed_now.items() if v["sha1"] and v["stat"]}
|
||||
if installed_now != installed_note: # (nothing installed and nothing gone: the note is the one there)
|
||||
write_installed(out, installed_now)
|
||||
if launcher: # (a folder built before the launcher: its libraries left the folder for bin\; hookshot is the launcher now)
|
||||
stale = [] if LINUX else sorted(f for f in os.listdir(out) if f.endswith(".dll") and f.startswith(("av", "sw")))
|
||||
for n in BESIDE[1:] + stale + ["Triforce.map", "hookshot.map", "hookshot-game.exe"]:
|
||||
|
||||
127
nx2/nup.py
127
nx2/nup.py
@@ -395,13 +395,82 @@ def long_path(p):
|
||||
return "\\\\?\\" + p
|
||||
|
||||
|
||||
def extraction_path(out, part, rel, temporary_suffix=".part"):
|
||||
"""A package path stays under its part, without following existing output links."""
|
||||
def package_parts(rel):
|
||||
"""A package path's parts ("/" between them), or the build stops: no empty part, no "." or "..", no backslash, drive
|
||||
or stream, nothing Windows would rename or open as a device. No file is touched"""
|
||||
parts = rel.split("/")
|
||||
if (not rel or "\\" in rel or any(not p or p in (".", "..") or p[-1] in " ." or
|
||||
any(c in '<>:"|?*' or ord(c) < 32 for c in p) or p.split(".")[0].rstrip(" ").upper() in WINDOWS_DEVICES
|
||||
for p in parts)):
|
||||
raise SystemExit("unsafe path in the package: %r" % rel)
|
||||
return parts
|
||||
|
||||
|
||||
def is_link(info):
|
||||
"""Whether what os.lstat found is a symbolic link or, on Windows, any reparse point (a junction, a mount)"""
|
||||
return bool(stat.S_ISLNK(info.st_mode) or
|
||||
getattr(info, "st_file_attributes", 0) & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0))
|
||||
|
||||
|
||||
# (Triforce, 11-22, the performance audit's IO-51, after Source A's) HOOKSHOT_NUP_IN_PLACE=0: extract writes each member
|
||||
# to <member>.part and renames it, and resolves the output folder and looks at every folder above a member for every
|
||||
# member, as before. Read once.
|
||||
IN_PLACE = os.environ.get("HOOKSHOT_NUP_IN_PLACE", "1") != "0"
|
||||
|
||||
|
||||
class Destination:
|
||||
"""(Triforce, 11-22, the performance audit's IO-51, after Source A's) Where one extraction writes: the output folder
|
||||
resolved once, and each folder under it looked at once (one os.lstat: there and no link, or made). extraction_path
|
||||
did both again for every member, 186,583 times over the game's package, and under a virus scanner on NTFS each look
|
||||
costs. A member's own name is still looked at by extract, once.
|
||||
|
||||
made: the folders this run made itself, which can hold nothing an older run left behind."""
|
||||
def __init__(self, out):
|
||||
self.root = long_path(os.path.realpath(os.path.abspath(out)))
|
||||
self.checked = set()
|
||||
self.made = set()
|
||||
|
||||
def folder(self, path, base):
|
||||
"""path (the root, or a folder under it) is a real folder from here on: its parent first, then itself"""
|
||||
if path in self.checked:
|
||||
return
|
||||
if path != self.root:
|
||||
if os.path.commonpath((os.path.normcase(base), os.path.normcase(os.path.abspath(path)))) != os.path.normcase(base):
|
||||
raise SystemExit("path leaves the extraction folder: %r" % path)
|
||||
self.folder(os.path.dirname(path), base)
|
||||
try:
|
||||
info = os.lstat(path)
|
||||
except FileNotFoundError:
|
||||
info = None
|
||||
if info is None:
|
||||
try:
|
||||
if path == self.root:
|
||||
os.makedirs(path)
|
||||
else:
|
||||
os.mkdir(path)
|
||||
self.made.add(path)
|
||||
except FileExistsError: # (made meanwhile by another unpack of the same package: looked at like any other)
|
||||
info = os.lstat(path)
|
||||
if info is not None:
|
||||
if is_link(info):
|
||||
raise SystemExit("extraction path is a link or reparse point: %s" % path)
|
||||
if not stat.S_ISDIR(info.st_mode):
|
||||
raise SystemExit("extraction path is not a folder: %s" % path)
|
||||
self.checked.add(path)
|
||||
|
||||
def member(self, part, rel):
|
||||
"""Where the package's rel of part goes, its folder there: (the path, whether this run made that folder)"""
|
||||
parts = package_parts(rel)
|
||||
base = os.path.join(self.root, part)
|
||||
folder = os.path.join(base, *parts[:-1])
|
||||
if folder not in self.checked:
|
||||
self.folder(folder, base)
|
||||
return os.path.join(folder, parts[-1]), folder in self.made
|
||||
|
||||
|
||||
def extraction_path(out, part, rel, temporary_suffix=".part"):
|
||||
"""A package path stays under its part, without following existing output links."""
|
||||
parts = package_parts(rel)
|
||||
root = long_path(os.path.realpath(os.path.abspath(out)))
|
||||
base = os.path.join(root, part)
|
||||
dst = os.path.join(base, *parts)
|
||||
@@ -419,7 +488,7 @@ def extraction_path(out, part, rel, temporary_suffix=".part"):
|
||||
info = os.lstat(path)
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
if stat.S_ISLNK(info.st_mode) or getattr(info, "st_file_attributes", 0) & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0):
|
||||
if is_link(info):
|
||||
raise SystemExit("extraction path is a link or reparse point: %s" % path)
|
||||
if path == temporary and info.st_nlink > 1:
|
||||
raise SystemExit("extraction temporary file has other hard links: %s" % path)
|
||||
@@ -502,8 +571,58 @@ def extract(nup, t, out, only=None, quiet=False, zs=None):
|
||||
print(" %.1f MiB written (%d files completed)" % (written / (1 << 20), n), flush=True)
|
||||
next_report = now + interval
|
||||
|
||||
# (Triforce, 11-22, the performance audit's IO-51, after Source A's) Each member is written straight to its name:
|
||||
# one os.lstat, then a create that fails if anything is there (O_EXCL), no <member>.part and no rename. Under a
|
||||
# virus scanner on NTFS the temporary's create and rename and extraction_path's looks at every folder were most of
|
||||
# a small member's cost. A member that is there whole is kept (a re-run picks up where it stopped); one of another
|
||||
# size is unlinked, never truncated (another hard link to it keeps its data); one whose write fails is deleted, so
|
||||
# the next run writes it again. HOOKSHOT_NUP_IN_PLACE=0: the loop below this one, as before.
|
||||
where = Destination(out) if IN_PLACE else None
|
||||
for part in ("exefs", "romfs"):
|
||||
if only and part != only:
|
||||
if where is None or (only and part != only):
|
||||
continue
|
||||
for rel, r in nup.walk(t[part]):
|
||||
dst, fresh = where.member(part, rel)
|
||||
looked = zs is not None and part == "romfs" and rel.endswith(".zs")
|
||||
try:
|
||||
info = os.lstat(dst)
|
||||
except FileNotFoundError:
|
||||
info = None
|
||||
if info is not None:
|
||||
if is_link(info):
|
||||
raise SystemExit("extraction path is a link or reparse point: %s" % dst)
|
||||
if stat.S_ISREG(info.st_mode) and info.st_size == r[2]:
|
||||
if looked:
|
||||
with open(dst, "rb") as f:
|
||||
zs["files"] += 1
|
||||
if not zstd_start(f.read(4)):
|
||||
zs["damaged"].append(rel)
|
||||
continue # (re-runs pick up where they stopped)
|
||||
os.unlink(dst)
|
||||
if not fresh: # (a <member>.part an interrupted older run left: a folder this run made has none)
|
||||
with suppress(FileNotFoundError):
|
||||
os.unlink(dst + ".part")
|
||||
g = os.fdopen(os.open(dst, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0), 0o666), "wb")
|
||||
try:
|
||||
with g:
|
||||
sink = HeadSink(g) if looked else g
|
||||
nup.copy_to(r, sink if quiet else ProgressSink(sink, progress), rel)
|
||||
if looked:
|
||||
zs["files"] += 1
|
||||
if not zstd_start(sink.head):
|
||||
zs["damaged"].append(rel)
|
||||
except BaseException as error:
|
||||
full = out_of_space(error, dst, part, rel, r[2]) # (measured before the member's space is freed)
|
||||
# Free the failed member's space without hiding the extraction error.
|
||||
with suppress(OSError):
|
||||
os.unlink(dst)
|
||||
if full:
|
||||
raise full from error
|
||||
raise
|
||||
total += r[2]
|
||||
n += 1
|
||||
for part in ("exefs", "romfs"):
|
||||
if where is not None or (only and part != only):
|
||||
continue
|
||||
for rel, r in nup.walk(t[part]):
|
||||
dst = extraction_path(out, part, rel)
|
||||
|
||||
@@ -22,6 +22,13 @@ Each is a runtime switch where it can be, so the game's own path stays reachable
|
||||
- ((Triforce) 11-19) The texture data compaction thread's step called through the host (host/oot_tex_compact.c): f_807da3f0's
|
||||
call of f_807da480. The host counts the cycles it starts and, with HOS_OOT_TEX_COMPACT_MS set, holds them to a pace; unset,
|
||||
the step runs as shipped. The supported build only.
|
||||
- ((Triforce) 11-22) The texture data manager's ask for a compaction round (f_80797240) also asks the host, which lets one through
|
||||
at most once a second (host/oot_tex_compact.c hos_oot_tex_compact_due, HOS_OOT_TEX_ASK_MS; 0: as shipped). The supported
|
||||
build only.
|
||||
- ((Triforce) 11-21, the performance audit's GAME-05) A character's or an object's shadow map's side through the host
|
||||
(host/oot_shadow_step.h): f_802640a0 reads it once before it asks for the texture; hos_oot_shadow_side rounds it up to a
|
||||
multiple of 64 (HOS_OOT_SHADOW_STEP), so the sides come round again and the render targets' pool serves them. The supported
|
||||
build only.
|
||||
"""
|
||||
import glob
|
||||
import os
|
||||
@@ -805,6 +812,67 @@ def patch_tex_compact(s):
|
||||
return s, 1
|
||||
|
||||
|
||||
# ((Triforce) f_80797240, the game's texture data manager's step, runs every frame: with no compaction round asked for (+776)
|
||||
# and bit 1 of +960 set, it asks for one and sends the compaction thread the message, so a round could start every frame. The
|
||||
# ask's test also asks the host (host/oot_tex_compact.c hos_oot_tex_compact_due: at most once a second, HOS_OOT_TEX_ASK_MS);
|
||||
# a round held back is asked for again at the next step.
|
||||
TEX_ASK_HEAD = "void f_80797240(Ctx *__restrict A64_NOESC c) {\n"
|
||||
TEX_ASK_OLD = " c->x[8]=recomp_load8(c,c->x[19] + 960);\nif (!((c->x[8] >> 1) & 1)) { goto L_80797288; }"
|
||||
TEX_ASK_NEW = (" c->x[8]=recomp_load8(c,c->x[19] + 960);\nif (!((c->x[8] >> 1) & 1) || !hos_oot_tex_compact_due()) { goto L_80797288; }"
|
||||
" /* (nx2/oot_patches.py: host/oot_tex_compact.c) */")
|
||||
TEX_ASK_DECL = "int hos_oot_tex_compact_due(void);\n"
|
||||
|
||||
|
||||
def patch_tex_ask(s):
|
||||
"""The ask's test above in one file's text: (text, 1 when made now or found made). apply() wants 1 over the whole lift"""
|
||||
at = _function(s, TEX_ASK_HEAD)
|
||||
if at is None:
|
||||
return s, 0
|
||||
head, end = at
|
||||
body = s[head:end]
|
||||
if body.count(TEX_ASK_NEW) == 1:
|
||||
return s, 1
|
||||
if body.count(TEX_ASK_OLD) != 1 or s.count('#include "a64.h"\n') != 1:
|
||||
return s, 0
|
||||
s = s[:head] + body.replace(TEX_ASK_OLD, TEX_ASK_NEW, 1) + s[end:]
|
||||
if TEX_ASK_DECL not in s:
|
||||
s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + TEX_ASK_DECL, 1)
|
||||
return s, 1
|
||||
|
||||
|
||||
# ((Triforce, 11-21, the performance audit's GAME-05, after Source B's) f_802640a0 (the game's 0x80264100 is in it) sizes a
|
||||
# character's or an object's shadow map: it reads the side once (ldr w10, [x21, #0x4e8]) before it asks for the texture, and
|
||||
# its later uses read the same register. That one read goes through the host, which rounds it up to a multiple of 64
|
||||
# (host/oot_shadow_step.h; HOS_OOT_SHADOW_STEP). The lifter writes a register as c->x[n] or, in a function whose registers
|
||||
# it keeps in locals, as xn_: both forms are looked for.
|
||||
SHADOW_STEP_HEAD = "void f_802640a0(Ctx *__restrict A64_NOESC c) {\n"
|
||||
SHADOW_STEP_FORMS = (("c->x[10]=recomp_load32(c,c->x[21] + 1256);",
|
||||
"c->x[10]=hos_oot_shadow_side(recomp_load32(c,c->x[21] + 1256));"),
|
||||
("x10_=recomp_load32(c,x21_ + 1256);",
|
||||
"x10_=hos_oot_shadow_side(recomp_load32(c,x21_ + 1256));"))
|
||||
SHADOW_STEP_MARK = " /* (nx2/oot_patches.py: a shadow map's side in steps, host/oot_shadow_step.h) */"
|
||||
SHADOW_STEP_DECL = "extern uint32_t hos_oot_shadow_side(uint32_t side);\n"
|
||||
|
||||
|
||||
def patch_shadow_step(s):
|
||||
"""The shadow map's side through the host: (text, 1) when the function is in s with it (made now or before), (s, 0) when
|
||||
the function is not here or its code differs (another game version: left as it is)"""
|
||||
at = _function(s, SHADOW_STEP_HEAD)
|
||||
if at is None:
|
||||
return s, 0
|
||||
head, end = at
|
||||
body = s[head:end]
|
||||
for old, new in SHADOW_STEP_FORMS:
|
||||
if body.count(new + SHADOW_STEP_MARK) == 1:
|
||||
return s, 1
|
||||
if body.count(old) == 1 and s.count('#include "a64.h"\n') == 1:
|
||||
s = s[:head] + body.replace(old, new + SHADOW_STEP_MARK, 1) + s[end:]
|
||||
if SHADOW_STEP_DECL not in s:
|
||||
s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + SHADOW_STEP_DECL, 1)
|
||||
return s, 1
|
||||
return s, 0
|
||||
|
||||
|
||||
def apply(gen, *, protect_hearts=False, required=False):
|
||||
"""required: the lift of the supported build (LIFE_BUILD_ID), whole: a missing output-size edit stops it (apply_output_size).
|
||||
The other patches only warn when their place is not found once."""
|
||||
@@ -830,6 +898,8 @@ def apply(gen, *, protect_hearts=False, required=False):
|
||||
mod_size_sites = 0 # ((Triforce) patch_mod_sizes: its one call, the supported build's alone)
|
||||
heap_sites = [0, 0] # (the GPU memory's sites, the CPU buffers': patch_heap_retry, the supported build only)
|
||||
tex_compact_sites = 0 # ((Triforce) patch_tex_compact: its one call, the supported build's alone)
|
||||
tex_ask_sites = 0 # ((Triforce) patch_tex_ask: its one test, the supported build's alone)
|
||||
shadow_step_sites = 0 # ((Triforce, 11-21, GAME-05) patch_shadow_step: the one read of a shadow map's side)
|
||||
for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))):
|
||||
with open(path, encoding="utf-8", newline="") as f:
|
||||
s = f.read()
|
||||
@@ -962,6 +1032,16 @@ def apply(gen, *, protect_hearts=False, required=False):
|
||||
if compacted != out:
|
||||
print("oot_patches: the texture compaction's step through the host in", os.path.basename(path))
|
||||
out = compacted
|
||||
asked, found = patch_tex_ask(out)
|
||||
tex_ask_sites += found
|
||||
if asked != out:
|
||||
print("oot_patches: the texture compaction's ask at most once a second in", os.path.basename(path))
|
||||
out = asked
|
||||
stepped, found = patch_shadow_step(out)
|
||||
shadow_step_sites += found
|
||||
if stepped != out:
|
||||
print("oot_patches: the shadow maps' side through the host in", os.path.basename(path))
|
||||
out = stepped
|
||||
if out != s:
|
||||
whole_write.write_whole(path, out, newline="")
|
||||
if sound_jobs != 1: # (without it, above 60 fps sounds and then the music can stop after a few loading zones)
|
||||
@@ -983,6 +1063,10 @@ def apply(gen, *, protect_hearts=False, required=False):
|
||||
print("oot_patches: WARNING: the resource size table's lookup was found at %d of 1 place" % mod_size_sites)
|
||||
if protect_hearts and tex_compact_sites != 1: # (without it the compaction is neither counted nor paced: host/oot_tex_compact.c)
|
||||
print("oot_patches: WARNING: the texture compaction's step was found at %d of 1 place" % tex_compact_sites)
|
||||
if protect_hearts and shadow_step_sites != 1: # (without it every shadow map has a side of its own, as shipped: host/oot_shadow_step.h)
|
||||
print("oot_patches: WARNING: the shadow maps' side was found at %d of 1 place" % shadow_step_sites)
|
||||
if protect_hearts and tex_ask_sites != 1: # (without it the compaction may be asked for every frame: host/oot_tex_compact.c)
|
||||
print("oot_patches: WARNING: the texture compaction's ask was found at %d of 1 place" % tex_ask_sites)
|
||||
if protect_hearts and zstd_sites != len(ZSTD_SITES): # (without them HOS_HOST_ZSTD does nothing, or half: host/hos_zstd.c)
|
||||
print("oot_patches: WARNING: the host's zstd decoder's calls were found at %d of %d places" % (zstd_sites, len(ZSTD_SITES)))
|
||||
if protect_hearts and heap_sites != [len(GPU_ALLOC_SITES), len(HEAP_ALLOC_SITES)]: # (the supported build's functions)
|
||||
|
||||
@@ -4,6 +4,19 @@ triforce_pipeline_states_shipped.bin, whose pipelines the first start's shader p
|
||||
|
||||
python -I nx2/pipeline_states.py <list>... what they hold
|
||||
python -I nx2/pipeline_states.py --out work/pipeline_states.bin <list>... merged, each pipeline once
|
||||
python -I nx2/pipeline_states.py --check <list>... each list checked (exit 1 when one is not sound)
|
||||
python -I nx2/pipeline_states.py --out work/pipeline_states.bin --contrib contrib <list>...
|
||||
and the players' lists in contrib/ (*.bin.xz, a note each), checked one by one,
|
||||
with what each source added
|
||||
python -I nx2/pipeline_states.py --export <list> <file.zip> a player's list checked and packed with a report
|
||||
|
||||
(Triforce, 11-22, the performance audit's PIPE-11, after Source A's.) The shipped list grows from more than one PC's play:
|
||||
--check says whether a list is sound (its head, its size, each record's counts and topology); --contrib merges the lists
|
||||
players handed in, each checked first (a list that is not sound is left out whole, with why), in a fixed order (the
|
||||
positional lists, then contrib/'s by name), and says for each source how many pipelines it added that the ones before
|
||||
lacked; --export makes what a player hands in (the list as .bin.xz and a report of what it holds, nothing of the PC's).
|
||||
Point-list records are counted as suspect: --check refuses them and --contrib leaves them out (Source A found 728 of its
|
||||
734 wrong) unless --keep-points; the plain merge keeps every record, as before.
|
||||
|
||||
A list holds no game code: each record is a pipeline's stage code keys (hashes of the game's shaders, which the player's own
|
||||
triforce_shaders.bin turns back into the shaders), the program variant's hash, and the formats and draw state baked into the pipeline.
|
||||
@@ -17,8 +30,12 @@ triforce_shaders.bin turns back into the shaders), the program variant's hash, a
|
||||
"""
|
||||
import argparse
|
||||
import collections
|
||||
import io
|
||||
import lzma
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
MAGIC = b"HKSTATE1"
|
||||
SIZE = 528
|
||||
@@ -33,6 +50,131 @@ def read(path):
|
||||
return [body[i:i + SIZE] for i in range(0, len(body) - SIZE + 1, SIZE)]
|
||||
|
||||
|
||||
# ---- (Triforce, 11-22, PIPE-11) checking a list
|
||||
|
||||
TOPOLOGY_MOST = 10 # (VK_PRIMITIVE_TOPOLOGY_PATCH_LIST)
|
||||
POINT_LIST = 0 # (VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
|
||||
|
||||
|
||||
def record_faults(r):
|
||||
"""What is wrong with one record, as words (none: sound). Its counts within their arrays, a first stage, flags 0 or 1"""
|
||||
faults = []
|
||||
code0 = struct.unpack_from("<Q", r, 0)[0]
|
||||
mesh, topo, n, samples, _depth, has_depth = struct.unpack_from("<6I", r, 40)
|
||||
nvattr = struct.unpack_from("<I", r, 232)[0]
|
||||
nanswers, nquestions = struct.unpack_from("<2I", r, 388)
|
||||
if not code0:
|
||||
faults.append("no vertex or mesh stage")
|
||||
if mesh > 1 or has_depth > 1:
|
||||
faults.append("a flag other than 0 or 1")
|
||||
if topo > TOPOLOGY_MOST:
|
||||
faults.append("topology %d" % topo)
|
||||
if n > 8:
|
||||
faults.append("%d colour targets" % n)
|
||||
if samples not in (1, 2, 4, 8, 16):
|
||||
faults.append("%d samples" % samples)
|
||||
if nvattr > 16:
|
||||
faults.append("%d vertex attributes" % nvattr)
|
||||
if nanswers > 24 or nquestions > 32:
|
||||
faults.append("%d answers, %d questions" % (nanswers, nquestions))
|
||||
return faults
|
||||
|
||||
|
||||
def is_point(r):
|
||||
return struct.unpack_from("<I", r, 44)[0] == POINT_LIST
|
||||
|
||||
|
||||
def check_bytes(data, name="list"):
|
||||
"""(records, faults, points): the records of a list's bytes, what is wrong with it (empty: sound), and how many records
|
||||
draw point lists"""
|
||||
if data[:8] != MAGIC:
|
||||
return [], ["%s: not a pipeline state list (no %s head)" % (name, MAGIC.decode())], 0
|
||||
if len(data) < 12 or struct.unpack_from("<I", data, 8)[0] != SIZE:
|
||||
return [], ["%s: record size %s, not %d" % (name, struct.unpack_from("<I", data, 8)[0] if len(data) >= 12 else "?", SIZE)], 0
|
||||
body = data[12:]
|
||||
faults = []
|
||||
if len(body) % SIZE:
|
||||
faults.append("%s: %d bytes past its last whole record (cut short)" % (name, len(body) % SIZE))
|
||||
recs = [body[i:i + SIZE] for i in range(0, len(body) - SIZE + 1, SIZE)]
|
||||
bad = 0
|
||||
for k, r in enumerate(recs):
|
||||
f = record_faults(r)
|
||||
if f:
|
||||
bad += 1
|
||||
if bad <= 8:
|
||||
faults.append("%s: record %d: %s" % (name, k, ", ".join(f)))
|
||||
if bad > 8:
|
||||
faults.append("%s: %d records not sound in all" % (name, bad))
|
||||
return recs, faults, sum(1 for r in recs if is_point(r))
|
||||
|
||||
|
||||
def read_any(path):
|
||||
"""A list's bytes, plain or xz-packed (.xz)"""
|
||||
with open(path, "rb") as f:
|
||||
data = f.read()
|
||||
return lzma.decompress(data) if path.endswith(".xz") else data
|
||||
|
||||
|
||||
def contrib_sources(folder):
|
||||
"""The players' lists in a folder: (name, path, note), by name; a list's note is <name>.note beside it ("" without)"""
|
||||
out = []
|
||||
for name in sorted(os.listdir(folder)):
|
||||
if not name.endswith(".bin.xz"):
|
||||
continue
|
||||
note_path = os.path.join(folder, name[:-len(".bin.xz")] + ".note")
|
||||
note = ""
|
||||
if os.path.exists(note_path):
|
||||
with open(note_path, encoding="utf-8", errors="replace") as f:
|
||||
note = f.read().strip()
|
||||
out.append((name, os.path.join(folder, name), note))
|
||||
return out
|
||||
|
||||
|
||||
def merge_sources(sources, keep_points=False):
|
||||
"""sources: (name, bytes, note) in order. The merged records (each once, in the order first met) and a report line a
|
||||
source: its records, what it added, its repeats, the point-list records left out, or why it was left out whole"""
|
||||
merged, seen, report = [], set(), []
|
||||
for name, data, note in sources:
|
||||
recs, faults, points = check_bytes(data, name)
|
||||
if faults:
|
||||
report.append("%s: left out: %s" % (name, "; ".join(faults)))
|
||||
continue
|
||||
added = repeats = dropped = 0
|
||||
for r in recs:
|
||||
if is_point(r) and not keep_points:
|
||||
dropped += 1
|
||||
continue
|
||||
if r in seen:
|
||||
repeats += 1
|
||||
continue
|
||||
seen.add(r)
|
||||
merged.append(r)
|
||||
added += 1
|
||||
report.append("%s: %d pipelines, %d added, %d already in, %d point-list records left out%s" %
|
||||
(name, len(recs), added, repeats, dropped, (" (" + note + ")") if note else ""))
|
||||
return merged, report
|
||||
|
||||
|
||||
def export_zip(list_path, zip_path):
|
||||
"""A player's list checked and packed: the list as triforce_pipeline_states.bin.xz and report.txt (what it holds, and
|
||||
the check's words). Nothing of the PC's. False, with the faults printed, when the list is not sound"""
|
||||
data = read_any(list_path)
|
||||
recs, faults, points = check_bytes(data, os.path.basename(list_path))
|
||||
if faults:
|
||||
for f in faults:
|
||||
print(f)
|
||||
return False
|
||||
report = io.StringIO()
|
||||
report.write("Triforce pipeline state list, for the shipped list (hashes and draw state only, no game code)\n")
|
||||
report.write("%s\n" % describe(recs))
|
||||
report.write("point-list records: %d\n" % points)
|
||||
report.write("record size %d, %s\n" % (SIZE, MAGIC.decode()))
|
||||
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_STORED) as z:
|
||||
z.writestr("triforce_pipeline_states.bin.xz", lzma.compress(data))
|
||||
z.writestr("report.txt", report.getvalue())
|
||||
return True
|
||||
|
||||
|
||||
def describe(recs):
|
||||
programs = collections.Counter()
|
||||
kinds = collections.Counter()
|
||||
@@ -46,9 +188,37 @@ def describe(recs):
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
||||
p.add_argument("lists", nargs="+")
|
||||
p.add_argument("lists", nargs="*")
|
||||
p.add_argument("--out")
|
||||
p.add_argument("--check", action="store_true", help="check each list: exit 1 when one is not sound")
|
||||
p.add_argument("--contrib", help="a folder of players' lists (*.bin.xz, a .note each), checked and merged after the lists")
|
||||
p.add_argument("--keep-points", action="store_true", help="keep point-list records (refused by --check, left out by --contrib)")
|
||||
p.add_argument("--export", nargs=2, metavar=("LIST", "ZIP"), help="a list checked and packed with its report")
|
||||
a = p.parse_args()
|
||||
if a.export:
|
||||
return 0 if export_zip(*a.export) else 1
|
||||
if a.check:
|
||||
bad = False
|
||||
for path in a.lists:
|
||||
recs, faults, points = check_bytes(read_any(path), path)
|
||||
if points and not a.keep_points:
|
||||
faults.append("%s: %d point-list records (suspect: --keep-points to allow)" % (path, points))
|
||||
print("%s: %s" % (path, "; ".join(faults) if faults else "sound, " + describe(recs)))
|
||||
bad |= bool(faults)
|
||||
return 1 if bad else 0
|
||||
if a.contrib:
|
||||
sources = [(os.path.basename(x), read_any(x), "") for x in a.lists]
|
||||
sources += [(name, read_any(path), note) for name, path, note in contrib_sources(a.contrib)]
|
||||
merged, report = merge_sources(sources, a.keep_points)
|
||||
for line in report:
|
||||
print(line)
|
||||
if a.out:
|
||||
with open(a.out, "wb") as f:
|
||||
f.write(MAGIC + struct.pack("<I", SIZE) + b"".join(merged))
|
||||
print("%s: %s" % (a.out, describe(merged)))
|
||||
return 0
|
||||
if not a.lists:
|
||||
p.error("no list")
|
||||
merged, seen = [], set()
|
||||
for path in a.lists:
|
||||
recs = read(path)
|
||||
|
||||
@@ -269,11 +269,16 @@ PICTURE_BUTTONS = {"east": (869, 227, 31), "south": (800, 286, 31), "north": (80
|
||||
# the labels' bars (BARS: the left ones' right edge, the right ones' left edge, kept from the game's) to the picture's parts
|
||||
# (nx2/prompt_faces.py): each line's texture, bar (0 left, 1 right), row (its top; 6 pixels thick) and end: a Switch button (the
|
||||
# part of the pad input bound to it: one line per pad input, chosen by the controls; a face button or the d-pad: a frame round the
|
||||
# group, as the game's round its face buttons, the label's icon telling which) or a pad input (fixed: the sticks)
|
||||
GUIDES = ("ControllerGuide_Pro_00", "ControllerGuide_Pro_01")
|
||||
# group, as the game's round its face buttons, the label's icon telling which) or a pad input (fixed: the sticks).
|
||||
# (Triforce) The page has three more of them, drawn with the two first: _02 ZR's line, _03 the D-pad's frame round the Pro's d-pad
|
||||
# and _04 L's and R's lines. Left the game's, on a face's picture they pointed at the Pro's places: on the PlayStation pads, whose
|
||||
# left stick is where the Pro's d-pad is, the D-pad's frame went round the left stick (the ticket "Put d-pad arrows in the right
|
||||
# place"). Now made for each face as the others are; the D-pad's label follows Switch Up's pad input
|
||||
GUIDES = ("ControllerGuide_Pro_00", "ControllerGuide_Pro_01", "ControllerGuide_Pro_02", "ControllerGuide_Pro_03",
|
||||
"ControllerGuide_Pro_04")
|
||||
GUIDE_SCALE, GUIDE_AT, BARS = 1.1136, (131.3, 462.6), (147, 1407)
|
||||
GUIDE_LINES = [(0, 0, 333, "zl"), (1, 0, 561, "minus"), (0, 0, 667, "lstick"), (0, 1, 558, "plus"), (0, 1, 713, "a"),
|
||||
(0, 1, 1225, "rstick")]
|
||||
(0, 1, 1225, "rstick"), (2, 1, 333, "zr"), (3, 0, 1015, "up"), (4, 0, 447, "l"), (4, 1, 446, "r")]
|
||||
|
||||
|
||||
def bntx_textures(b):
|
||||
@@ -545,6 +550,30 @@ def guide_frame_at(where, group):
|
||||
return cx, cy, max(max(abs(x - cx), abs(y - cy)) + r for x, y, r in pts) + 8 # (from the centre to a corner, as the game's)
|
||||
|
||||
|
||||
# (Triforce, 11-22, the performance audit's IO-42, after Source A's) The lines that stay, widened (a 9 by 9 maximum, what a
|
||||
# route pays to cross), kept for the last image asked: every route of one line is given the same image (guide_layers: the
|
||||
# frames and each pad input's line), and each widened it again, 37 times for one face where 5 images differ. One entry,
|
||||
# by the image's id, holding the image itself: the entry is used only when it is that very object, so an id given again
|
||||
# to another image misses, and no step here changes an image in place. HOOKSHOT_PROMPTS_WIDEN_EACH=1 (read once):
|
||||
# widened at every route, as before.
|
||||
_WIDENED = {}
|
||||
WIDEN_EACH = os.environ.get("HOOKSHOT_PROMPTS_WIDEN_EACH", "0") not in ("", "0")
|
||||
|
||||
|
||||
def guide_widened(stays):
|
||||
"""the lines that stay (L) with every lit pixel grown to 9 by 9, as an array: _WIDENED above"""
|
||||
import numpy as np
|
||||
from PIL import ImageFilter
|
||||
held = None if WIDEN_EACH else _WIDENED.get(id(stays))
|
||||
if held is not None and held[0] is stays:
|
||||
return held[1]
|
||||
st = np.asarray(stays.filter(ImageFilter.MaxFilter(9)))
|
||||
_WIDENED.clear()
|
||||
if not WIDEN_EACH:
|
||||
_WIDENED[id(stays)] = (stays, st)
|
||||
return st
|
||||
|
||||
|
||||
def guide_route(size, bar, row, where, pad, stays, frame=None):
|
||||
"""the way (points, the lines' pixels) of a line from a bar's row to the pad input's part, round the picture's other parts and
|
||||
with the fewest bends, crossing the lines that stay (L) as little as it can: it comes to a stick's centre, to a part just off
|
||||
@@ -552,7 +581,6 @@ def guide_route(size, bar, row, where, pad, stays, frame=None):
|
||||
import heapq
|
||||
import math
|
||||
import numpy as np
|
||||
from PIL import ImageFilter
|
||||
g = GUIDE_GRID
|
||||
x0, y0 = BARS[0] if bar == 0 else BARS[1], row + 3
|
||||
ilo, ihi = (0, (BARS[1] - x0) // g) if bar == 0 else (-((x0 - BARS[0]) // g), 0)
|
||||
@@ -580,7 +608,7 @@ def guide_route(size, bar, row, where, pad, stays, frame=None):
|
||||
else: # (the frame: a square on its corner, its sides 1.15 corners' distance off in x + y)
|
||||
blocked |= np.abs(X - cx) + np.abs(Y - cy) < 1.15 * e + GUIDE_CLEAR
|
||||
blocked |= ((np.abs(X - BARS[0]) < 14) | (np.abs(X - BARS[1]) < 14)) & (Y != y0) # (off the bars but on its own row)
|
||||
st = np.asarray(stays.filter(ImageFilter.MaxFilter(9)))
|
||||
st = guide_widened(stays) # ((Triforce, 11-22, IO-42) once per image, not once per route)
|
||||
pen = np.where(st[np.clip(Y, 0, size[1] - 1), np.clip(X, 0, size[0] - 1)] > 0, GUIDE_CROSS, 0)
|
||||
dirs = ((1, 0), (-1, 0), (0, 1), (0, -1))
|
||||
goals = {} # ((i, j, dir): (the end, extra cost)): the cell two before the end, coming the way it ends
|
||||
|
||||
@@ -12,8 +12,8 @@ what it is made from, or draw it by hand and put it here.
|
||||
| `KeyTexFont<A B X Y>_00.<pad input>.png` | the white letters on black (the Talk ring's small buttons) | the icon's letter (its holes), or its shape when it has none |
|
||||
| `Controller_Pro.png` | the controller in the pause menu's controls page (System) | the face's `drawing` in `nx2/prompt_faces.py`, drawn by `nx2/prompt_draw.py` |
|
||||
| `Controller_Pro1.png`, `ControllerSh_Pro1.png` | the page's tinted copy of the controller, and its shadow | `Controller_Pro.png` (yours if you made one) |
|
||||
| `ControllerGuide_Pro_0<0 1>.png` | the page's leader lines that stay, whatever the controls | the face's `parts` in `nx2/prompt_faces.py` (where each control is in the picture) and the router in `prompt_assets.py` (`GUIDE_*`) |
|
||||
| `ControllerGuide_Pro_0<0 1>.<Switch button>.<pad input>.png` | the line of that Switch button's label when it is on that pad input, drawn over the lines that stay | the same |
|
||||
| `ControllerGuide_Pro_0<0-4>.png` | the page's leader lines that stay, whatever the controls | the face's `parts` in `nx2/prompt_faces.py` (where each control is in the picture) and the router in `prompt_assets.py` (`GUIDE_*`) |
|
||||
| `ControllerGuide_Pro_0<0-4>.<Switch button>.<pad input>.png` | the line of that Switch button's label when it is on that pad input, drawn over the lines that stay (`_02`: ZR's, `_03`: the D-pad's frame, by Switch Up's input, `_04`: L's and R's) | the same |
|
||||
|
||||
The text icons (dialogue, the control hints, the menus) are not images: they are the face's font, the game's
|
||||
`External.bfotf` with the face's Kenney glyphs, picked by `icons` in `nx2/prompt_faces.py`.
|
||||
|
||||
13
nx2/rtgen.py
13
nx2/rtgen.py
@@ -35,6 +35,17 @@ void a64_poll_cur(void);
|
||||
#define A64_POLL(c) ((void)0)
|
||||
#endif"""
|
||||
|
||||
# (Triforce, 11-22, the performance audit's LIFT-47, after Source B's) what a DMB of the load or the store kind is in code
|
||||
# lifted with A64_DMB_LIGHT=1 (nx2/insn.py dmb_light). x86-64 orders loads with loads, loads with later stores and stores
|
||||
# with stores itself; the empty asm with a "memory" clobber keeps the compiler from moving or merging guest accesses across
|
||||
# it (a C11 fence alone would not order the plain accesses), and no instruction is emitted. Any other host: the full fence.
|
||||
DMB_LDST = r"""
|
||||
#if defined(__x86_64__)
|
||||
#define A64_DMB_LDST() __asm__ __volatile__("" ::: "memory")
|
||||
#else
|
||||
#define A64_DMB_LDST() recomp_barrier()
|
||||
#endif"""
|
||||
|
||||
ADDR64 = r"""#ifndef A64_ADDR64
|
||||
#define A64_ADDR64 1 /* (nx2/rtgen.py: 64-bit guest window) */
|
||||
#endif
|
||||
@@ -73,7 +84,7 @@ SUBST = {
|
||||
"a64.h": [
|
||||
(THREAD, THREAD.replace("\n} GuestContext;", "\n uint32_t poll; "
|
||||
"/* (nx2/rtgen.py) interrupt request: A64_POLL clears it and calls a64_poll_cur when set */\n} GuestContext;")),
|
||||
(RET_MISMATCH, RET_MISMATCH + POLL_DECL),
|
||||
(RET_MISMATCH, RET_MISMATCH + POLL_DECL + DMB_LDST),
|
||||
("#define A64_PTR(a) (A64_MEM + (uint32_t)(a))", ADDR64),
|
||||
(MARK_NATIVE, MARK_NATIVE_64),
|
||||
],
|
||||
|
||||
218
nx2/split_hot.py
218
nx2/split_hot.py
@@ -5,6 +5,22 @@
|
||||
Functions that call each other stay together, heaviest calls first, so what used to inline still can. The pieces cost about the
|
||||
same to compile (chunks.fn_cost). code_hot.c stays as it was, renamed code_hot.c.whole, and build.py compiles code_hot_*.c at the
|
||||
hot flags. A profile from the whole file fits the pieces: its counters go by function name and shape.
|
||||
|
||||
(Triforce, 11-22, the performance audit's LIFT-30, after Source A's) The pieces are sticky, and each declares only what it
|
||||
uses. Every split dealt the functions out afresh by their sizes, so a few more lines in one hot function could move
|
||||
functions between most of the pieces, and every piece began with code_hot.c's whole preamble, so one new declaration
|
||||
changed all of them: an update that touched a few hot functions compiled up to eight pieces of about ten MB again, the
|
||||
longest files of the build. Now:
|
||||
- <work>/image/hot_pieces.txt keeps each hot function's piece. The next split leaves the functions it knows where they
|
||||
are and puts a new one in the piece it shares the most calls with, else in the cheapest; when that leaves the costliest
|
||||
piece more than STICKY_SLACK times a fresh split's costliest, or the file is not there, or it names more pieces than
|
||||
this split makes, the split is the fresh one, as before, and the file is written again from it;
|
||||
- a piece keeps a declaration of code_hot.c's preamble only if it uses the name: the lift's `void f_<address>(Ctx
|
||||
*A64_NOESC);` lines and the patches' one-line `hos_` declarations (a function's, or a variable's with extern), each a
|
||||
whole statement at the top level (declared()). Every other line of the preamble stays in every piece.
|
||||
A64_HOT_STICKY=0 (at lift time; an A64_ name, in the lift key): the fresh split and the whole preamble, byte for byte as
|
||||
before, and hot_pieces.txt is neither read nor written. The file is not in the lift key: any map gives the same functions.
|
||||
tests/test_sticky_hot_pieces.py.
|
||||
"""
|
||||
import collections
|
||||
import glob
|
||||
@@ -21,21 +37,39 @@ HEAD = lifted_c.HEAD
|
||||
CALL = re.compile(r"\b(f_[0-9a-f]{8})\(c\)")
|
||||
|
||||
|
||||
def split(gen, pieces):
|
||||
path = os.path.join(gen, "code_hot.c")
|
||||
whole = path + ".whole"
|
||||
if os.path.exists(path):
|
||||
os.replace(path, whole)
|
||||
for old in glob.glob(os.path.join(glob.escape(gen), "code_hot_*.c")):
|
||||
os.remove(old)
|
||||
with open(whole, encoding="utf-8", newline="") as f:
|
||||
text = f.read()
|
||||
marks = list(HEAD.finditer(text))
|
||||
if not marks:
|
||||
raise SystemExit("split_hot: no functions in %s" % whole)
|
||||
preamble = text[:marks[0].start()]
|
||||
bodies = {m.group(1): text[m.start():(marks[i + 1].start() if i + 1 < len(marks) else len(text))] for i, m in enumerate(marks)}
|
||||
STICKY_SLACK = 1.15 # (how much dearer than a fresh split's the costliest sticky piece may be)
|
||||
DECL = re.compile(r"void (f_[0-9a-f]+)\(Ctx \*A64_NOESC\);") # (the lift's declaration of a function a hot one names)
|
||||
# (a one-line declaration of a hos_ name, nx2/oot_patches.py's: a function's, extern or not, or a variable's with extern. A
|
||||
# variable's without extern is a definition, and stays in every piece as before)
|
||||
EXTERN = re.compile(r"(?P<ext>extern\s+)?(?:[A-Za-z_]\w*[\s*]+)+(?P<name>hos_\w+)\s*(?P<args>\([^;{}=#]*\))?\s*;")
|
||||
NAME = re.compile(r"\b(?:f_[0-9a-f]+|hos_\w+)\b")
|
||||
TOKEN = re.compile(r'/\*|\*/|//|"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'|[{}()]')
|
||||
|
||||
|
||||
def sticky():
|
||||
"""Whether the pieces are sticky with their own declarations: unless A64_HOT_STICKY=0"""
|
||||
return os.environ.get("A64_HOT_STICKY", "1").strip() != "0"
|
||||
|
||||
|
||||
def pieces_file(gen):
|
||||
"""<work>/image/hot_pieces.txt for a gen folder (<work>/gen or <work>/sa/gen): beside chunks.txt, which a player's build keeps"""
|
||||
up = os.path.dirname(os.path.abspath(gen))
|
||||
work = os.path.dirname(up) if os.path.basename(up) == "sa" else up
|
||||
return os.path.join(work, "image", "hot_pieces.txt")
|
||||
|
||||
|
||||
def call_edges(bodies):
|
||||
"""How often each hot function calls each other one: {(caller, callee): calls}"""
|
||||
edges = collections.Counter()
|
||||
for f, b in bodies.items():
|
||||
for callee in CALL.findall(b):
|
||||
if callee in bodies and callee != f:
|
||||
edges[(f, callee)] += 1
|
||||
return edges
|
||||
|
||||
|
||||
def fresh_split(bodies, pieces, edges):
|
||||
"""The split from the sizes alone, as every split was: (the pieces' function names, the calls that stay in one group)"""
|
||||
# (groups joined along the calls: small callees first, those are what the optimiser inlines)
|
||||
limit = sum(len(b) for b in bodies.values()) * 1.1 / pieces
|
||||
parent = {f: f for f in bodies}
|
||||
@@ -46,11 +80,6 @@ def split(gen, pieces):
|
||||
parent[f] = parent[parent[f]]
|
||||
f = parent[f]
|
||||
return f
|
||||
edges = collections.Counter()
|
||||
for f, b in bodies.items():
|
||||
for callee in CALL.findall(b):
|
||||
if callee in bodies and callee != f:
|
||||
edges[(f, callee)] += 1
|
||||
for (f, callee), n in sorted(edges.items(), key=lambda e: (len(bodies[e[0][1]]) / e[1], e[0])):
|
||||
a, b = root(f), root(callee)
|
||||
if a != b and size[a] + size[b] <= limit:
|
||||
@@ -68,17 +97,158 @@ def split(gen, pieces):
|
||||
b = min(bins, key=lambda x: x[0])
|
||||
b[0] += sum(cost[f] for f in g)
|
||||
b[1].extend(g)
|
||||
kept = sum(n for (f, c), n in edges.items() if root(f) == root(c))
|
||||
return [names for _, names in bins], kept
|
||||
|
||||
|
||||
def read_pieces(path):
|
||||
"""{function: piece} from hot_pieces.txt, or {} when it is not there or not what save_pieces writes"""
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
rows = [line.split() for line in f if line.strip()]
|
||||
except OSError:
|
||||
return {}
|
||||
if not all(len(r) == 2 and r[1].isdigit() for r in rows):
|
||||
return {}
|
||||
return {name: int(piece) for name, piece in rows}
|
||||
|
||||
|
||||
def save_pieces(path, bins):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write("".join("%s %d\n" % (name, i) for i, names in enumerate(bins) for name in sorted(names)))
|
||||
|
||||
|
||||
def sticky_split(bodies, pieces, saved, edges, fresh):
|
||||
"""The pieces with the functions `saved` knows left where they are: (the pieces' names, why), or (fresh, why) when
|
||||
there is nothing to keep or keeping it costs more than STICKY_SLACK times the fresh split's costliest piece"""
|
||||
if not saved:
|
||||
return fresh, "fresh (no pieces file)"
|
||||
if max(saved.values()) >= pieces:
|
||||
return fresh, "fresh (the pieces file is for another number of pieces)" # (fewer: the slack below decides)
|
||||
cost = {f: chunks.fn_cost(len(b)) for f, b in bodies.items()}
|
||||
bins = [[] for _ in range(pieces)]
|
||||
where = {}
|
||||
for f in sorted(bodies):
|
||||
if f in saved:
|
||||
bins[saved[f]].append(f)
|
||||
where[f] = saved[f]
|
||||
if not where:
|
||||
return fresh, "fresh (none of the pieces file's functions is hot now)"
|
||||
known = len(where)
|
||||
load = [sum(cost[f] for f in names) for names in bins]
|
||||
calls = collections.defaultdict(collections.Counter) # (each function's calls with every other, either way)
|
||||
for (f, callee), n in edges.items():
|
||||
calls[f][callee] += n
|
||||
calls[callee][f] += n
|
||||
for f in sorted(bodies): # (a new one: beside the function it shares the most calls with, else in the cheapest piece)
|
||||
if f in where:
|
||||
continue
|
||||
shared = collections.Counter()
|
||||
for other, n in calls[f].items():
|
||||
if other in where:
|
||||
shared[where[other]] += n
|
||||
i = min(range(pieces), key=lambda i: (-shared[i], load[i], i))
|
||||
bins[i].append(f)
|
||||
where[f] = i
|
||||
load[i] += cost[f]
|
||||
fresh_top = max(sum(cost[f] for f in names) for names in fresh)
|
||||
if max(load) > STICKY_SLACK * fresh_top:
|
||||
return fresh, "fresh (kept where they were, the costliest piece is %.2f times a fresh split's)" % (max(load) / max(1, fresh_top))
|
||||
return bins, "sticky (%d functions where they were, %d new)" % (known, len(where) - known)
|
||||
|
||||
|
||||
def declared(lines):
|
||||
"""For each line of code_hot.c's preamble the name it declares when a piece may leave it out, else None. Only a whole
|
||||
statement at the top level is taken: not a line inside braces or parentheses, a comment, a macro's continued lines, or
|
||||
after an unfinished statement (an attribute on the line before), where a line that reads like a declaration is code"""
|
||||
out = []
|
||||
depth = paren = 0
|
||||
comment = cont = False
|
||||
done = True
|
||||
for line in lines:
|
||||
s = line.strip()
|
||||
name = None
|
||||
if depth == 0 and paren == 0 and not comment and not cont and done and not s.startswith(("typedef", "static")):
|
||||
m = DECL.fullmatch(s)
|
||||
e = None if m else EXTERN.fullmatch(s)
|
||||
name = m.group(1) if m else e.group("name") if e and (e.group("ext") or e.group("args")) else None
|
||||
out.append(name)
|
||||
directive = cont or s.startswith("#")
|
||||
cont = line.rstrip("\r\n").endswith("\\")
|
||||
code, pos = [], 0 # (the line without its comments, and the braces and parentheses it opens and closes)
|
||||
for t in TOKEN.finditer(line):
|
||||
g = t.group()
|
||||
if comment:
|
||||
if g == "*/":
|
||||
comment, pos = False, t.end()
|
||||
elif g in ("//", "/*"):
|
||||
code.append(line[pos:t.start()])
|
||||
pos = len(line)
|
||||
if g == "//":
|
||||
break
|
||||
comment = True
|
||||
elif g in ("{", "}", "(", ")") and not directive:
|
||||
depth += (g == "{") - (g == "}")
|
||||
paren += (g == "(") - (g == ")")
|
||||
if not comment:
|
||||
code.append(line[pos:])
|
||||
code = " ".join(code).strip()
|
||||
if not directive and code:
|
||||
done = code.endswith((";", "{", "}"))
|
||||
return out
|
||||
|
||||
|
||||
def piece_preamble(preamble, names_used):
|
||||
"""code_hot.c's preamble for one piece: a declaration of a function or of a hos_ name stays only if the piece uses the
|
||||
name (names_used: every f_ and hos_ word of its functions); every other line stays"""
|
||||
lines = preamble.splitlines(keepends=True)
|
||||
names = declared(lines)
|
||||
# (a name a line that always stays uses is used too: a macro or an inline helper the preamble may carry)
|
||||
used = set(names_used)
|
||||
for line, name in zip(lines, names):
|
||||
if name is None:
|
||||
used.update(NAME.findall(line))
|
||||
return "".join(line for line, name in zip(lines, names) if name is None or name in used)
|
||||
|
||||
|
||||
def split(gen, pieces):
|
||||
path = os.path.join(gen, "code_hot.c")
|
||||
whole = path + ".whole"
|
||||
if os.path.exists(path):
|
||||
os.replace(path, whole)
|
||||
for old in glob.glob(os.path.join(glob.escape(gen), "code_hot_*.c")):
|
||||
os.remove(old)
|
||||
with open(whole, encoding="utf-8", newline="") as f:
|
||||
text = f.read()
|
||||
marks = list(lifted_c.definitions(text)) # ((Triforce, 11-22, the performance audit's IO-40) HEAD's matches, found by jumps)
|
||||
if not marks:
|
||||
raise SystemExit("split_hot: no functions in %s" % whole)
|
||||
preamble = text[:marks[0].start()]
|
||||
bodies = {m.group(1): text[m.start():(marks[i + 1].start() if i + 1 < len(marks) else len(text))] for i, m in enumerate(marks)}
|
||||
|
||||
edges = call_edges(bodies)
|
||||
bins, kept = fresh_split(bodies, pieces, edges)
|
||||
keep = sticky()
|
||||
if keep: # ((Triforce, LIFT-30) the functions the last split placed stay where they are)
|
||||
bins, how = sticky_split(bodies, pieces, read_pieces(pieces_file(gen)), edges, bins)
|
||||
where = {f: i for i, names in enumerate(bins) for f in names}
|
||||
kept = sum(n for (f, c), n in edges.items() if where[f] == where[c])
|
||||
save_pieces(pieces_file(gen), bins)
|
||||
print("split_hot: %s" % how)
|
||||
cost = {f: chunks.fn_cost(len(b)) for f, b in bodies.items()}
|
||||
costs = {}
|
||||
for i, (c, names) in enumerate(bins):
|
||||
for i, names in enumerate(bins):
|
||||
c = sum(cost[f] for f in names)
|
||||
head = piece_preamble(preamble, {n for name in names for n in NAME.findall(bodies[name])}) if keep else preamble
|
||||
with open(os.path.join(gen, "code_hot_%d.c" % i), "w", encoding="utf-8", newline="") as f:
|
||||
f.write(preamble)
|
||||
f.write(head)
|
||||
for name in sorted(names):
|
||||
f.write(bodies[name])
|
||||
costs["code_hot_%d.c" % i] = len(preamble) + c
|
||||
costs["code_hot_%d.c" % i] = len(head) + c
|
||||
print("split_hot: piece %d: %d functions, %d MB, cost %d MB" % (i, len(names), sum(len(bodies[f]) for f in names) >> 20, c >> 20))
|
||||
chunks.write_costs(gen, costs)
|
||||
kept = sum(n for (f, c), n in edges.items() if root(f) == root(c))
|
||||
print("split_hot: %d of %d calls stay in one group" % (kept, sum(edges.values())))
|
||||
print("split_hot: %d of %d calls stay in one %s" % (kept, sum(edges.values()), "piece" if keep else "group"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
286
nx2/test_nup_in_place.py
Normal file
286
nx2/test_nup_in_place.py
Normal file
@@ -0,0 +1,286 @@
|
||||
"""Game-free: a package's unpack writes each member in place and looks at each folder once (nx2/nup.py extract,
|
||||
Destination).
|
||||
|
||||
(Triforce, 11-22, the performance audit's IO-51, after Source A's.) extract wrote every member to <member>.part and
|
||||
renamed it, and extraction_path resolved the output folder and looked at every folder above the member for every one of
|
||||
the game's 186,583 members. Under a virus scanner on NTFS those creates, renames and looks were most of a small member's
|
||||
cost.
|
||||
|
||||
On made-up packages in a temporary folder:
|
||||
- a fresh unpack calls no os.replace, makes no <member>.part, unlinks nothing, and asks os.lstat once for each folder
|
||||
and once for each member; the old loop asks more than that and renames every member;
|
||||
- the files and the .zs notes are the same as the old loop's, byte for byte, nested and non-ASCII names included;
|
||||
- a member that is there whole is kept and not opened for writing (its .zs head is read back);
|
||||
- a member whose write fails is removed, and the next run writes it;
|
||||
- a short member is written again;
|
||||
- a hard-linked member of another size is replaced and its twin keeps its data;
|
||||
- a file that appears between the look and the create is not written over (the create is exclusive);
|
||||
- a <member>.part an older run left in a folder that was there is removed; a link is refused as before;
|
||||
- HOOKSHOT_NUP_IN_PLACE=0 brings the old loop back, and the switches table says so.
|
||||
|
||||
python -I -B nx2/test_nup_in_place.py
|
||||
"""
|
||||
import errno
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
import nup
|
||||
|
||||
ZSTD = b"\x28\xb5\x2f\xfd"
|
||||
|
||||
|
||||
class Package:
|
||||
"""Made-up members: name -> bytes."""
|
||||
def __init__(self, members):
|
||||
self.members = dict(members)
|
||||
|
||||
def walk(self, directory):
|
||||
for name, data in self.members.items():
|
||||
yield name, (name, 0, len(data), 0, len(data))
|
||||
|
||||
def copy_to(self, record, output, name):
|
||||
output.write(self.members[name][:5])
|
||||
output.write(self.members[name][5:])
|
||||
|
||||
|
||||
MEMBERS = {
|
||||
"Model/Forest/tree.bfres.zs": ZSTD + b"a tree" * 9,
|
||||
"Model/Forest/rock.bfres.zs": b"\0\0\0\0 a damaged file",
|
||||
"Model/Forest/Deep/leaf.bin": b"leaf",
|
||||
"Model/empty.bin": b"",
|
||||
"Text/Français.msbt.zs": b"\x50\x2a\x4d\x18 a skippable frame",
|
||||
"top.bin": b"top level",
|
||||
".hidden": b"h",
|
||||
}
|
||||
FOLDERS = {"", "Model", "Model/Forest", "Model/Forest/Deep", "Text"} # (under romfs; "" is romfs itself)
|
||||
|
||||
|
||||
def tree(root):
|
||||
out = {}
|
||||
for folder, dirs, files in os.walk(root):
|
||||
for name in files:
|
||||
path = os.path.join(folder, name)
|
||||
with open(path, "rb") as f:
|
||||
out[os.path.relpath(path, root).replace(os.sep, "/")] = f.read()
|
||||
return out
|
||||
|
||||
|
||||
def plain(path):
|
||||
"""A path without Windows' extended-length prefix, in one case, to compare what os.lstat was asked"""
|
||||
path = os.path.normcase(os.path.abspath(path))
|
||||
return path[4:] if path.startswith("\\\\?\\") else path
|
||||
|
||||
|
||||
class InPlace(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temporary = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temporary.cleanup)
|
||||
self.root = Path(os.path.realpath(self.temporary.name))
|
||||
self.out = self.root / "out"
|
||||
self.out.mkdir()
|
||||
self.assertTrue(nup.IN_PLACE)
|
||||
|
||||
def extract(self, members=MEMBERS, out=None, zs=None):
|
||||
return nup.extract(Package(members), {"romfs": 0}, str(out or self.out), only="romfs", quiet=True, zs=zs)
|
||||
|
||||
def test_fresh_unpack_one_look_each_and_no_rename(self):
|
||||
asked = []
|
||||
real_lstat = os.lstat
|
||||
|
||||
def lstat(path, *args, **kwargs):
|
||||
asked.append(plain(path))
|
||||
return real_lstat(path, *args, **kwargs)
|
||||
|
||||
with patch.object(nup.os, "lstat", side_effect=lstat), \
|
||||
patch.object(nup.os, "replace", side_effect=AssertionError("a rename")) as replace, \
|
||||
patch.object(nup.os, "unlink", side_effect=AssertionError("an unlink")) as unlink:
|
||||
self.assertEqual(self.extract(), (len(MEMBERS), sum(len(d) for d in MEMBERS.values())))
|
||||
replace.assert_not_called()
|
||||
unlink.assert_not_called() # (romfs was made by this run: no stale .part can be in it)
|
||||
base = plain(str(self.out / "romfs"))
|
||||
under = [p for p in asked if p == base or p.startswith(base + os.sep)]
|
||||
want = sorted(plain(str(self.out / "romfs" / f)) for f in FOLDERS) + \
|
||||
sorted(plain(str(self.out / "romfs" / m)) for m in MEMBERS)
|
||||
self.assertEqual(sorted(under), sorted(want)) # (each folder once, each member once, nothing twice)
|
||||
self.assertEqual(tree(self.out / "romfs"), MEMBERS)
|
||||
self.assertEqual([p for p in tree(self.out) if p.endswith(".part")], [])
|
||||
|
||||
def test_the_old_loop_looks_more_and_renames(self):
|
||||
# (what the count above is compared with, and the switch back)
|
||||
asked = []
|
||||
real_lstat = os.lstat
|
||||
|
||||
def lstat(path, *args, **kwargs):
|
||||
asked.append(plain(path))
|
||||
return real_lstat(path, *args, **kwargs)
|
||||
|
||||
with patch.object(nup, "IN_PLACE", False), patch.object(nup.os, "lstat", side_effect=lstat), \
|
||||
patch.object(nup.os, "replace", wraps=os.replace) as replace:
|
||||
self.extract()
|
||||
self.assertEqual(replace.call_count, len(MEMBERS))
|
||||
base = plain(str(self.out / "romfs"))
|
||||
under = [p for p in asked if p == base or p.startswith(base + os.sep)]
|
||||
self.assertGreater(len(under), 2 * (len(FOLDERS) + len(MEMBERS)))
|
||||
self.assertEqual(tree(self.out / "romfs"), MEMBERS)
|
||||
|
||||
def test_same_files_and_notes_as_the_old_loop(self):
|
||||
new, old = self.root / "new", self.root / "old"
|
||||
zs_new, zs_old = {}, {}
|
||||
counts_new = self.extract(out=new, zs=zs_new)
|
||||
with patch.object(nup, "IN_PLACE", False):
|
||||
counts_old = self.extract(out=old, zs=zs_old)
|
||||
self.assertEqual(counts_new, counts_old)
|
||||
self.assertEqual(tree(new), tree(old))
|
||||
self.assertEqual(tree(new), {"romfs/" + k: v for k, v in MEMBERS.items()})
|
||||
self.assertEqual(zs_new, zs_old)
|
||||
self.assertEqual(zs_new, dict(files=3, damaged=["Model/Forest/rock.bfres.zs"]))
|
||||
# and a second run over each: nothing written, the notes read back from the files
|
||||
again_new, again_old = {}, {}
|
||||
self.assertEqual(self.extract(out=new, zs=again_new), (0, 0))
|
||||
with patch.object(nup, "IN_PLACE", False):
|
||||
self.assertEqual(self.extract(out=old, zs=again_old), (0, 0))
|
||||
self.assertEqual(again_new, zs_new)
|
||||
self.assertEqual(again_old, zs_new)
|
||||
|
||||
def test_whole_member_is_kept_and_not_opened_for_writing(self):
|
||||
self.extract()
|
||||
before = {p: os.stat(self.out / "romfs" / p).st_mtime_ns for p in MEMBERS}
|
||||
with patch.object(nup.os, "open", side_effect=AssertionError("a create")), \
|
||||
patch.object(nup.os, "unlink", wraps=os.unlink) as unlink:
|
||||
self.assertEqual(self.extract(), (0, 0))
|
||||
self.assertEqual(before, {p: os.stat(self.out / "romfs" / p).st_mtime_ns for p in MEMBERS})
|
||||
# (romfs was there this time: nothing was written, so no <member>.part was looked for either)
|
||||
unlink.assert_not_called()
|
||||
|
||||
def test_failed_member_is_removed_then_written_on_retry(self):
|
||||
package = Package(MEMBERS)
|
||||
failing = "Model/Forest/rock.bfres.zs"
|
||||
real = package.copy_to
|
||||
|
||||
def copy_to(record, output, name):
|
||||
if name == failing:
|
||||
output.write(b"half")
|
||||
raise OSError(errno.EIO, "a made-up read error")
|
||||
real(record, output, name)
|
||||
|
||||
with patch.object(package, "copy_to", side_effect=copy_to), self.assertRaises(OSError) as error:
|
||||
nup.extract(package, {"romfs": 0}, str(self.out), only="romfs", quiet=True)
|
||||
self.assertEqual(error.exception.errno, errno.EIO)
|
||||
self.assertFalse((self.out / "romfs" / failing).exists())
|
||||
done = tree(self.out / "romfs")
|
||||
self.assertEqual(done, {k: v for k, v in MEMBERS.items() if k in done}) # (what was written is whole)
|
||||
n, total = nup.extract(package, {"romfs": 0}, str(self.out), only="romfs", quiet=True)
|
||||
self.assertEqual(n, len(MEMBERS) - len(done))
|
||||
self.assertEqual(tree(self.out / "romfs"), MEMBERS)
|
||||
|
||||
def test_short_member_is_written_again(self):
|
||||
self.extract()
|
||||
short = self.out / "romfs" / "top.bin"
|
||||
short.write_bytes(b"top")
|
||||
long = self.out / "romfs" / "Model" / "Forest" / "Deep" / "leaf.bin"
|
||||
long.write_bytes(b"leaf and more than the package has")
|
||||
self.assertEqual(self.extract(), (2, len(b"top level") + len(b"leaf")))
|
||||
self.assertEqual(tree(self.out / "romfs"), MEMBERS)
|
||||
|
||||
def test_hard_linked_member_is_replaced_and_its_twin_keeps_its_data(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
twin = self.root / "twin.bin"
|
||||
twin.write_bytes(b"another file's data, of another size")
|
||||
os.link(twin, self.out / "romfs" / "top.bin")
|
||||
self.extract({"top.bin": b"top level"})
|
||||
self.assertEqual((self.out / "romfs" / "top.bin").read_bytes(), b"top level")
|
||||
self.assertEqual(twin.read_bytes(), b"another file's data, of another size")
|
||||
self.assertEqual(os.stat(twin).st_nlink, 1)
|
||||
|
||||
def test_member_link_is_refused(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
outside = self.root / "outside.bin"
|
||||
outside.write_bytes(b"keep outside data")
|
||||
try:
|
||||
os.symlink(outside, self.out / "romfs" / "top.bin")
|
||||
except OSError as error:
|
||||
self.skipTest("no symbolic links here: %s" % error)
|
||||
with self.assertRaisesRegex(SystemExit, "^extraction path is a link or reparse point: "):
|
||||
self.extract({"top.bin": b"top level"})
|
||||
self.assertEqual(outside.read_bytes(), b"keep outside data")
|
||||
self.assertTrue((self.out / "romfs" / "top.bin").is_symlink())
|
||||
|
||||
def test_file_that_appears_after_the_look_is_not_written_over(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
planted = self.out / "romfs" / "top.bin"
|
||||
real_lstat = os.lstat
|
||||
|
||||
def lstat(path, *args, **kwargs):
|
||||
if plain(path) == plain(str(planted)): # (the look sees nothing; then the file is there)
|
||||
planted.write_bytes(b"planted")
|
||||
raise FileNotFoundError(errno.ENOENT, "not there yet", str(path))
|
||||
return real_lstat(path, *args, **kwargs)
|
||||
|
||||
with patch.object(nup.os, "lstat", side_effect=lstat), self.assertRaises(FileExistsError):
|
||||
self.extract({"top.bin": b"top level"})
|
||||
self.assertEqual(planted.read_bytes(), b"planted")
|
||||
|
||||
def test_stale_part_in_a_folder_that_was_there_is_removed(self):
|
||||
(self.out / "romfs" / "Model").mkdir(parents=True)
|
||||
stale = self.out / "romfs" / "Model" / "empty.bin.part"
|
||||
stale.write_bytes(b"an interrupted older run")
|
||||
self.extract()
|
||||
self.assertFalse(stale.exists())
|
||||
self.assertEqual(tree(self.out / "romfs"), MEMBERS)
|
||||
|
||||
def test_folder_that_is_a_file_stops_the_unpack(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
(self.out / "romfs" / "Model").write_bytes(b"not a folder")
|
||||
with self.assertRaisesRegex(SystemExit, "^extraction path is not a folder: "):
|
||||
self.extract()
|
||||
self.assertEqual((self.out / "romfs" / "Model").read_bytes(), b"not a folder")
|
||||
|
||||
def test_destination_checks_each_folder_once(self):
|
||||
where = nup.Destination(str(self.out / "new" / "deeper"))
|
||||
with patch.object(nup.os, "lstat", wraps=os.lstat) as lstat:
|
||||
first, fresh = where.member("romfs", "A/B/one.bin")
|
||||
calls = lstat.call_count
|
||||
second, fresh_again = where.member("romfs", "A/B/two.bin")
|
||||
self.assertEqual(lstat.call_count, calls) # (the same folder: no further look)
|
||||
where.member("romfs", "A/C/three.bin")
|
||||
self.assertEqual(lstat.call_count, calls + 1) # (one new folder: one look)
|
||||
self.assertTrue(fresh and fresh_again)
|
||||
self.assertEqual(os.path.dirname(first), os.path.dirname(second))
|
||||
self.assertTrue(os.path.isdir(self.out / "new" / "deeper" / "romfs" / "A" / "C"))
|
||||
with self.assertRaisesRegex(SystemExit, "^unsafe path in the package: "):
|
||||
where.member("romfs", "A/../../outside.bin")
|
||||
|
||||
def test_switch_is_read_from_the_environment(self):
|
||||
for value, want in ((None, "True"), ("1", "True"), ("0", "False")):
|
||||
env = {k: v for k, v in os.environ.items() if k != "HOOKSHOT_NUP_IN_PLACE"}
|
||||
if value is not None:
|
||||
env["HOOKSHOT_NUP_IN_PLACE"] = value
|
||||
said = subprocess.run([sys.executable, "-I", "-B", "-c",
|
||||
"import sys; sys.path.insert(0, sys.argv[1]); import nup; print(nup.IN_PLACE)", HERE],
|
||||
env=env, capture_output=True, text=True)
|
||||
self.assertEqual(said.stdout.strip(), want, said.stderr)
|
||||
|
||||
def test_source_and_docs(self):
|
||||
source = Path(HERE, "nup.py").read_text(encoding="utf-8")
|
||||
extract = source[source.index("def extract("):source.index("def heal(")]
|
||||
self.assertIn("where = Destination(out) if IN_PLACE else None", extract)
|
||||
self.assertIn("os.O_WRONLY | os.O_CREAT | os.O_EXCL", extract)
|
||||
self.assertIn('IN_PLACE = os.environ.get("HOOKSHOT_NUP_IN_PLACE", "1") != "0"', source)
|
||||
# (heal and the .plain path keep the per-member check)
|
||||
self.assertIn('p = extraction_path(out, "exefs", m)', source)
|
||||
self.assertIn('extraction_path(a.extract, "exefs", m, temporary_suffix=".plain")', source)
|
||||
docs = Path(HERE).parent.joinpath("docs", "development.md").read_text(encoding="utf-8")
|
||||
row = [line for line in docs.splitlines() if line.startswith("| `HOOKSHOT_NUP_IN_PLACE=0`")]
|
||||
self.assertEqual(len(row), 1)
|
||||
self.assertIn("IO-51", row[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -34,11 +34,11 @@ class OutOfSpace(unittest.TestCase):
|
||||
def test_full_drive_names_the_member_and_the_drive(self):
|
||||
with tempfile.TemporaryDirectory() as out:
|
||||
member = Path(os.path.realpath(out)) / "romfs" / "Model" / "authored.zs"
|
||||
partial = member.with_name("authored.zs.part")
|
||||
seen = []
|
||||
|
||||
def usage(path):
|
||||
seen.append((path, partial.exists()))
|
||||
# (the member itself: it is written straight to its name since the performance audit's IO-51)
|
||||
seen.append((path, member.exists()))
|
||||
return SimpleNamespace(free=196 * 10 ** 9)
|
||||
|
||||
original = OSError(errno.ENOSPC, "authored disk-full failure")
|
||||
@@ -51,8 +51,9 @@ class OutOfSpace(unittest.TestCase):
|
||||
"No space left on device writing romfs/Model/authored.zs (12345 bytes): 196.0 GB free there, NTFS")
|
||||
self.assertIs(error.exception.__cause__, original)
|
||||
# (measured while the failed member still holds its space)
|
||||
self.assertEqual(seen, [(str(member.parent), True)])
|
||||
self.assertFalse(partial.exists())
|
||||
self.assertEqual(seen, [(nup.long_path(str(member.parent)), True)])
|
||||
self.assertFalse(member.exists())
|
||||
self.assertFalse(member.with_name("authored.zs.part").exists())
|
||||
|
||||
def test_other_errors_pass_through(self):
|
||||
with tempfile.TemporaryDirectory() as out:
|
||||
|
||||
@@ -207,10 +207,11 @@ class ExtractionPaths(unittest.TestCase):
|
||||
with self.assertRaisesRegex(SystemExit, "^" + re.escape("unsafe path in the package: %r" % name) + "$"):
|
||||
self.extract([name])
|
||||
self.assertEqual(attempts, [])
|
||||
# In this exact context, a valid name must pass validation and reach a write.
|
||||
with self.assertRaisesRegex(WriteAttempt, "^os\\.makedirs$"):
|
||||
# In this exact context, a valid name must pass validation and reach a write (the part's folder,
|
||||
# made once by nup.Destination since the performance audit's IO-51).
|
||||
with self.assertRaisesRegex(WriteAttempt, "^os\\.mkdir$"):
|
||||
self.extract(["valid.bin"])
|
||||
self.assertEqual(attempts, ["os.makedirs"])
|
||||
self.assertEqual(attempts, ["os.mkdir"])
|
||||
self.assertEqual(before, snapshot(self.root))
|
||||
|
||||
def test_write_guards_block_alternative_io(self):
|
||||
@@ -255,7 +256,9 @@ class ExtractionPaths(unittest.TestCase):
|
||||
self.assertEqual((self.out / "romfs" / "file.bin").read_bytes(), b"authored fixture")
|
||||
self.assertFalse(partial.exists())
|
||||
|
||||
def test_failed_member_keeps_previous_file_and_can_be_retried(self):
|
||||
def test_failed_member_is_removed_and_can_be_retried(self):
|
||||
# (the performance audit's IO-51: a member is written straight to its name, so a file of another size that was
|
||||
# there is unlinked first and a write that fails leaves nothing; before, the previous file stayed)
|
||||
target = self.out / "romfs" / "file.bin"
|
||||
target.parent.mkdir()
|
||||
partial = target.with_name("file.bin.part")
|
||||
@@ -266,18 +269,16 @@ class ExtractionPaths(unittest.TestCase):
|
||||
output.write(b"some bytes")
|
||||
raise OSError(errno.ENOSPC, "authored disk-full failure")
|
||||
|
||||
failures = (
|
||||
(patch.object(package, "copy_to", side_effect=out_of_space), errno.ENOSPC),
|
||||
(patch.object(nup.os, "replace", side_effect=PermissionError(errno.EACCES, "authored replacement failure")),
|
||||
errno.EACCES),
|
||||
)
|
||||
for failure, code in failures:
|
||||
def refused(record, output, name):
|
||||
raise PermissionError(errno.EACCES, "authored write failure")
|
||||
|
||||
for failure, code in ((out_of_space, errno.ENOSPC), (refused, errno.EACCES)):
|
||||
with self.subTest(error=code):
|
||||
target.write_bytes(b"previous file")
|
||||
with failure, self.assertRaises(OSError) as error:
|
||||
with patch.object(package, "copy_to", side_effect=failure), self.assertRaises(OSError) as error:
|
||||
nup.extract(package, title, str(self.out), only="romfs", quiet=True)
|
||||
self.assertEqual(error.exception.errno, code)
|
||||
self.assertEqual(target.read_bytes(), b"previous file")
|
||||
self.assertFalse(target.exists())
|
||||
self.assertFalse(partial.exists())
|
||||
self.assertEqual(nup.extract(package, title, str(self.out), only="romfs", quiet=True),
|
||||
(1, len(package.data)))
|
||||
@@ -317,18 +318,23 @@ class ExtractionPaths(unittest.TestCase):
|
||||
self.fail("Cannot create the file-link fixture: %s" % error)
|
||||
self.reject(["file.bin"], "extraction path is a link or reparse point: ")
|
||||
|
||||
def test_temporary_link_cannot_truncate_an_outside_file(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
try:
|
||||
os.symlink(self.sentinel, self.out / "romfs" / "file.bin.part")
|
||||
except OSError as error:
|
||||
self.fail("Cannot create the file-link fixture: %s" % error)
|
||||
self.reject(["file.bin"], "extraction path is a link or reparse point: ")
|
||||
|
||||
def test_temporary_hard_link_cannot_truncate_an_outside_file(self):
|
||||
(self.out / "romfs").mkdir()
|
||||
os.link(self.sentinel, self.out / "romfs" / "file.bin.part")
|
||||
self.reject(["file.bin"], "extraction temporary file has other hard links: ")
|
||||
def test_stale_temporary_link_is_removed_without_touching_an_outside_file(self):
|
||||
# (the performance audit's IO-51: no <member>.part is written any more, so one an older run left, a link or a
|
||||
# hard link included, is only unlinked, which never reaches what it points at; before, both were refused)
|
||||
for kind, make in (("link", os.symlink), ("hard link", os.link)):
|
||||
with self.subTest(kind=kind):
|
||||
folder = self.out / "romfs" / kind.replace(" ", "_")
|
||||
folder.mkdir(parents=True)
|
||||
partial = folder / "file.bin.part"
|
||||
try:
|
||||
make(self.sentinel, partial)
|
||||
except OSError as error:
|
||||
self.fail("Cannot create the file-link fixture: %s" % error)
|
||||
name = folder.name + "/file.bin"
|
||||
self.assertEqual(self.extract([name]), (1, len(b"authored fixture")))
|
||||
self.assertEqual((folder / "file.bin").read_bytes(), b"authored fixture")
|
||||
self.assertFalse(os.path.lexists(partial))
|
||||
self.assertEqual(self.sentinel.read_bytes(), b"keep outside data")
|
||||
|
||||
def test_heal_missing_module_and_resume(self):
|
||||
(self.out / "exefs").mkdir()
|
||||
|
||||
308
nx2/vecfp.py
Normal file
308
nx2/vecfp.py
Normal file
@@ -0,0 +1,308 @@
|
||||
"""(Triforce, 11-22, the performance audit's LIFT-03, after Source A's) SSE fast paths for the float forms the lifter
|
||||
still writes lane by lane, behind A64_VECFP (off by default).
|
||||
|
||||
The kit turns straight FADD / FSUB, FMUL .4S and FMUL / FMLA / FMLS .4S by element into one SSE op with the scalar fast
|
||||
path's tests on every lane (a64_lift.py's vectorize_fpx and vectorize_elem). These stay loops of one helper call a lane,
|
||||
or exact integer code a lane:
|
||||
|
||||
elem FMUL, FMLA, FMLS .2S by element
|
||||
vec FMUL .2S; FMLA and FMLS .2S and .4S; FDIV .2S and .4S
|
||||
minmax FMAX, FMIN, FMAXNM, FMINNM: scalar S, .2S and .4S
|
||||
fcvt FCVT S to D and D to S (scalar); FCVTL and FCVTL2 (.2S to .2D); FCVTN (.2D to .2S)
|
||||
|
||||
With A64_VECFP=1 (or a list of the groups above) each such instruction is lifted as
|
||||
|
||||
if (!a64_vfpx_q_<form>(c, ...)) { <its C as before> }
|
||||
|
||||
The helper (HEADER, appended to a64_fpx.h) does the op in SSE and writes the register only when the result is provably
|
||||
Arm's with no new FPSR bit, by the rules the kit's own helpers use (RECOMP_FPX_OPEN, RECOMP_FPX_KEEP32 / KEEPDIV32, no
|
||||
subnormal operand under FZ); otherwise it writes nothing, returns 0, and the C as before runs:
|
||||
- multiplies, multiply-adds and divides: under the open gate (default FPCR but FZ, IXC already raised); a lane is kept
|
||||
when its result is a normal number above the smallest, or a finite result with a zero factor (dividend, for a divide);
|
||||
the multiply-adds are one fused op and need FMA3, without it they always decline;
|
||||
- min and max: under the default FPCR but FZ; kept unless a lane is a NaN or, under FZ, a subnormal. Zeros of both signs
|
||||
come out as Arm's (max of +0 and -0 is +0) by taking the op both ways round;
|
||||
- S to D: exact; kept unless the input is a NaN or, under FZ, a subnormal;
|
||||
- D to S: under the open gate; kept when the result is a normal number above the smallest, or the input is a zero.
|
||||
|
||||
The kit's files stay as they are (third_party/ is not edited): install(), called from nx2/lift.py after insn's, wraps
|
||||
a64emit.translate (the word decides the form; the text behind the test is whatever the translator wrote, so no pattern
|
||||
of its text is assumed) and a64_lift.fpx_header. A word whose text the kit's own passes already turn into a vector
|
||||
helper is left alone (main_handles), so nothing is wrapped twice. The closing brace goes on the text's last line, never
|
||||
at a line's start: nx2/oot_patches.py ends a function at the first `}` that starts a line.
|
||||
|
||||
A64_VECFP is read at lift time and is in the lift key (an A64_ name). Unset, empty or 0: the kit's text, the default
|
||||
until it is measured in the game. tests/test_vecfp.py runs every form both ways and against the exact body."""
|
||||
import os
|
||||
import re
|
||||
|
||||
import a64emit
|
||||
import a64_lift
|
||||
|
||||
GROUPS = ("elem", "vec", "minmax", "fcvt")
|
||||
|
||||
|
||||
def groups():
|
||||
"""The groups A64_VECFP turns on: none when unset, empty or 0; all for 1; else the listed ones"""
|
||||
v = os.environ.get("A64_VECFP", "0").strip()
|
||||
if v in ("", "0"):
|
||||
return frozenset()
|
||||
if v == "1":
|
||||
return frozenset(GROUPS)
|
||||
got = frozenset(p.strip() for p in v.split(",") if p.strip())
|
||||
if got - set(GROUPS):
|
||||
raise SystemExit("vecfp: A64_VECFP=%s: not 0, 1 or a list of %s" % (v, ", ".join(GROUPS)))
|
||||
return got
|
||||
|
||||
|
||||
def fast_call(w, on):
|
||||
"""The helper call for guest word w (C, nonzero when it did the op), or None: not one of the forms, or its group is off"""
|
||||
top = w & 0x1F000000
|
||||
if top not in (0x0E000000, 0x0F000000, 0x1E000000) or not on:
|
||||
return None
|
||||
rd, rn, rm = w & 31, (w >> 5) & 31, (w >> 16) & 31
|
||||
d, n, m = "c->vreg[%d]" % rd, "c->vreg[%d]" % rn, "c->vreg[%d]" % rm
|
||||
k = 4 if w & 0x40000000 else 2
|
||||
v = w & 0xBFE0FC00 # (the three-register vector forms of single precision, Q left out)
|
||||
if "vec" in on:
|
||||
if v == 0x2E20DC00 and k == 2: # FMUL .2S (.4S is the kit's a64_vfpx_mul32)
|
||||
return "a64_vfpx_q_mul(c,%s,%s,%s,2)" % (d, n, m)
|
||||
if v in (0x0E20CC00, 0x0EA0CC00): # FMLA, FMLS .2S / .4S
|
||||
return "a64_vfpx_q_fma(c,%s,%s,%s,%d,%d)" % (d, n, m, v == 0x0EA0CC00, k)
|
||||
if v == 0x2E20FC00: # FDIV .2S / .4S
|
||||
return "a64_vfpx_q_div(c,%s,%s,%s,%d)" % (d, n, m, k)
|
||||
if "minmax" in on:
|
||||
if v in (0x0E20F400, 0x0EA0F400, 0x0E20C400, 0x0EA0C400): # FMAX, FMIN, FMAXNM, FMINNM .2S / .4S
|
||||
return "a64_vfpx_q_minmax(c,%s,%s,%s,%d,%d)" % (d, n, m, bool(v & 0x00800000), k)
|
||||
s = w & 0xFFE0FC00
|
||||
if s in (0x1E204800, 0x1E205800, 0x1E206800, 0x1E207800): # FMAX, FMIN, FMAXNM, FMINNM S
|
||||
return "a64_vfpx_q_minmax(c,%s,%s,%s,%d,1)" % (d, n, m, bool(s & 0x00001000))
|
||||
if "elem" in on:
|
||||
e = w & 0xFFC0F400 # (.2S by element: Q = 0, size = 10; L, M:Rm and H left out)
|
||||
if e in (0x0F809000, 0x0F801000, 0x0F805000): # FMUL, FMLA, FMLS .2S by element
|
||||
index = ((w >> 11) & 1) << 1 | ((w >> 21) & 1)
|
||||
if e == 0x0F809000:
|
||||
return "a64_vfpx_q_mule(c,%s,%s,%s,%d,2)" % (d, n, m, index)
|
||||
return "a64_vfpx_q_fmae(c,%s,%s,%s,%d,%d,2)" % (d, n, m, index, e == 0x0F805000)
|
||||
if "fcvt" in on:
|
||||
u = w & 0xFFFFFC00
|
||||
if u == 0x1E22C000: # FCVT Dd, Sn
|
||||
return "a64_vfpx_q_cvtsd(c,%s,%s)" % (d, n)
|
||||
if u == 0x1E624000: # FCVT Sd, Dn
|
||||
return "a64_vfpx_q_cvtds(c,%s,%s)" % (d, n)
|
||||
if (w & 0xBFFFFC00) == 0x0E617800: # FCVTL / FCVTL2 .2D from .2S
|
||||
return "a64_vfpx_q_cvtl(c,%s,%s,%d)" % (d, n, k == 4)
|
||||
if u == 0x0E616800: # FCVTN .2S from .2D (FCVTN2 keeps the low half: not one)
|
||||
return "a64_vfpx_q_cvtn(c,%s,%s)" % (d, n)
|
||||
return None
|
||||
|
||||
|
||||
KIT_VECTOR = re.compile(r"\ba64_vfpx_(?!q_)\w+\(|\ba64_vfcmp\(")
|
||||
|
||||
|
||||
def main_handles(text):
|
||||
"""Whether the kit's own passes already turn this instruction's text into a vector helper (then it is not wrapped)"""
|
||||
return KIT_VECTOR.search(a64_lift.vectorize_elem(a64_lift.widen_vstores(a64_lift.vectorize_fpx(
|
||||
a64_lift.outline_fpx(text, set()))))) is not None
|
||||
|
||||
|
||||
def wrap(text, call):
|
||||
"""The instruction's text behind the helper's test. The closing brace stays on the text's last line"""
|
||||
return "if (!%s) { %s }\n" % (call, text.strip())
|
||||
|
||||
|
||||
HEADER = r"""
|
||||
/* ---- (Triforce, 11-22, the performance audit's LIFT-03, after Source A's) SSE fast paths for float forms lifted lane by
|
||||
lane (nx2/vecfp.py, A64_VECFP): each returns 1 when it did the op and wrote Vd, 0 with nothing written when the C behind
|
||||
it must run. k: the lanes (4, 2, or 1 for a scalar). A lane is kept by the scalar fast paths' rules. ---- */
|
||||
#ifndef A64_VFPX_Q_PROBE
|
||||
#define A64_VFPX_Q_PROBE() ((void)0) /* (tests/test_vecfp.py counts the ops done here) */
|
||||
#endif
|
||||
#if RECOMP_FPX_HOST == 2
|
||||
#if defined(__FMA__)
|
||||
#include <immintrin.h>
|
||||
#endif
|
||||
static inline __m128i a64_vq_ult(__m128i x, uint32_t k) { /* unsigned x < k, per lane */
|
||||
return _mm_cmplt_epi32(_mm_xor_si128(x, _mm_set1_epi32((int)0x80000000u)), _mm_set1_epi32((int)(k ^ 0x80000000u)));
|
||||
}
|
||||
static inline __m128i a64_vq_abs(__m128i x) { return _mm_and_si128(x, _mm_set1_epi32(0x7fffffff)); }
|
||||
static inline __m128i a64_vq_big(__m128i t) { return a64_vq_ult(_mm_sub_epi32(a64_vq_abs(t), _mm_set1_epi32(0x00800001)), 0x7effffffu); }
|
||||
static inline __m128i a64_vq_zero(__m128i x) { return _mm_cmpeq_epi32(a64_vq_abs(x), _mm_setzero_si128()); }
|
||||
static inline __m128i a64_vq_nonfin(__m128i t) { const __m128i e = _mm_set1_epi32(0x7f800000); return _mm_cmpeq_epi32(_mm_and_si128(t, e), e); }
|
||||
static inline __m128i a64_vq_nan(__m128i x) { return _mm_cmpgt_epi32(a64_vq_abs(x), _mm_set1_epi32(0x7f800000)); }
|
||||
static inline __m128i a64_vq_subn(__m128i x) { return a64_vq_ult(_mm_sub_epi32(_mm_slli_epi32(x, 1), _mm_set1_epi32(1)), 0x00ffffffu); }
|
||||
/* an operand's low k lanes, repeated to fill the register: the unused lanes then hold what the used ones do, so every
|
||||
test is made on all four and no lane computes on bytes the instruction does not read */
|
||||
static inline __attribute__((always_inline)) __m128i a64_vq_load(const uint64_t *p, int k) {
|
||||
if (k == 4) return _mm_loadu_si128((const __m128i *)p);
|
||||
if (k == 2) { const __m128i x = _mm_loadl_epi64((const __m128i *)p); return _mm_unpacklo_epi64(x, x); }
|
||||
return _mm_set1_epi32((int)(uint32_t)p[0]);
|
||||
}
|
||||
static inline __attribute__((always_inline)) __m128i a64_vq_lane(const uint64_t *p, int index) { /* one lane in all four */
|
||||
uint32_t e;
|
||||
memcpy(&e, (const uint8_t *)p + 4 * index, 4);
|
||||
return _mm_set1_epi32((int)e);
|
||||
}
|
||||
/* the result's low k lanes into Vd, the rest of the register zero */
|
||||
static inline __attribute__((always_inline)) void a64_vq_store(uint64_t *d, __m128i r, int k) {
|
||||
if (k == 4) _mm_storeu_si128((__m128i *)d, r);
|
||||
else if (k == 2) a64_vst_lo64(d, r);
|
||||
else a64_vset64(d, (uint32_t)_mm_cvtsi128_si32(r));
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vq_mul(const Ctx *c, uint64_t *d, __m128i a, __m128i b, int k) {
|
||||
if (!RECOMP_FPX_OPEN(c)) return 0;
|
||||
const __m128i t = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b)));
|
||||
/* kept: big, or a finite product of a zero (RECOMP_FPX_KEEP32); under FZ no subnormal operand */
|
||||
const __m128i keep = _mm_or_si128(a64_vq_big(t), _mm_andnot_si128(a64_vq_nonfin(t), _mm_or_si128(a64_vq_zero(a), a64_vq_zero(b))));
|
||||
__m128i bad = _mm_andnot_si128(keep, _mm_set1_epi32(-1));
|
||||
if (RECOMP_FPX_FZ(c)) bad = _mm_or_si128(bad, _mm_or_si128(a64_vq_subn(a), a64_vq_subn(b)));
|
||||
if (_mm_movemask_epi8(bad)) return 0;
|
||||
a64_vq_store(d, t, k);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vq_fma(const Ctx *c, uint64_t *d, __m128i a, __m128i b, int neg, int k) {
|
||||
#if defined(__FMA__)
|
||||
if (!RECOMP_FPX_OPEN(c)) return 0;
|
||||
const __m128i z = a64_vq_load(d, k);
|
||||
if (neg) a = _mm_xor_si128(a, _mm_set1_epi32((int)0x80000000u));
|
||||
const __m128i t = _mm_castps_si128(_mm_fmadd_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(z)));
|
||||
/* kept: big, or a finite result of a zero factor (RECOMP_FPX_KEEP32); under FZ no subnormal operand */
|
||||
const __m128i keep = _mm_or_si128(a64_vq_big(t), _mm_andnot_si128(a64_vq_nonfin(t), _mm_or_si128(a64_vq_zero(a), a64_vq_zero(b))));
|
||||
__m128i bad = _mm_andnot_si128(keep, _mm_set1_epi32(-1));
|
||||
if (RECOMP_FPX_FZ(c)) bad = _mm_or_si128(bad, _mm_or_si128(_mm_or_si128(a64_vq_subn(a), a64_vq_subn(b)), a64_vq_subn(z)));
|
||||
if (_mm_movemask_epi8(bad)) return 0;
|
||||
a64_vq_store(d, t, k);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
#else
|
||||
(void)c; (void)d; (void)a; (void)b; (void)neg; (void)k; return 0; /* (no FMA3: a fused op is not one instruction here) */
|
||||
#endif
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_mul(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) {
|
||||
return a64_vq_mul(c, d, a64_vq_load(n, k), a64_vq_load(m, k), k);
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_mule(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int index, int k) {
|
||||
return a64_vq_mul(c, d, a64_vq_load(n, k), a64_vq_lane(m, index), k);
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_fma(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int neg, int k) {
|
||||
return a64_vq_fma(c, d, a64_vq_load(n, k), a64_vq_load(m, k), neg, k);
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_fmae(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int index, int neg, int k) {
|
||||
return a64_vq_fma(c, d, a64_vq_load(n, k), a64_vq_lane(m, index), neg, k);
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_div(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) {
|
||||
if (!RECOMP_FPX_OPEN(c)) return 0;
|
||||
const __m128i a = a64_vq_load(n, k), b = a64_vq_load(m, k);
|
||||
const __m128i t = _mm_castps_si128(_mm_div_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b)));
|
||||
/* kept: big, or a finite quotient of a zero (RECOMP_FPX_KEEPDIV32); under FZ no subnormal operand */
|
||||
const __m128i keep = _mm_or_si128(a64_vq_big(t), _mm_andnot_si128(a64_vq_nonfin(t), a64_vq_zero(a)));
|
||||
__m128i bad = _mm_andnot_si128(keep, _mm_set1_epi32(-1));
|
||||
if (RECOMP_FPX_FZ(c)) bad = _mm_or_si128(bad, _mm_or_si128(a64_vq_subn(a), a64_vq_subn(b)));
|
||||
if (_mm_movemask_epi8(bad)) return 0;
|
||||
a64_vq_store(d, t, k);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
/* FMAX / FMIN / FMAXNM / FMINNM (the NM forms differ only for a NaN, which is never kept). x86's MAXPS and MINPS give
|
||||
their second operand for two zeros; taking the op both ways round and combining (and for max, or for min) gives Arm's
|
||||
zero, and changes nothing where the operands are not both zeros (equal numbers have equal bits). No FPSR bit. */
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_minmax(const Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int min, int k) {
|
||||
if (c->fpcr & RECOMP_FPX_FPCR_MASK) return 0;
|
||||
const __m128i a = a64_vq_load(n, k), b = a64_vq_load(m, k);
|
||||
__m128i bad = _mm_or_si128(a64_vq_nan(a), a64_vq_nan(b));
|
||||
if (RECOMP_FPX_FZ(c)) bad = _mm_or_si128(bad, _mm_or_si128(a64_vq_subn(a), a64_vq_subn(b)));
|
||||
if (_mm_movemask_epi8(bad)) return 0;
|
||||
const __m128 x = _mm_castsi128_ps(a), y = _mm_castsi128_ps(b);
|
||||
const __m128 r = min ? _mm_or_ps(_mm_min_ps(x, y), _mm_min_ps(y, x)) : _mm_and_ps(_mm_max_ps(x, y), _mm_max_ps(y, x));
|
||||
a64_vq_store(d, _mm_castps_si128(r), k);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
/* FCVT Dd, Sn: every binary32 number is a binary64 number. A NaN (quieted, IOC) and, under FZ, a subnormal (flushed, IDC)
|
||||
are left to the C behind it */
|
||||
static inline __attribute__((always_inline)) int a64_vq_s_to_d(const Ctx *c, uint32_t x, uint64_t *r) {
|
||||
if ((x & 0x7fffffffu) > 0x7f800000u || (RECOMP_FPX_FZ(c) && RECOMP_FPX_SUBN32(x))) return 0;
|
||||
*r = recomp_fpx_db((double)recomp_fpx_f(x));
|
||||
return 1;
|
||||
}
|
||||
/* FCVT Sd, Dn: one rounding to nearest. Kept when the result is a normal number above the smallest (no overflow, not
|
||||
tiny; an inexact result raises IXC, already raised under the open gate), or the input is a zero */
|
||||
static inline __attribute__((always_inline)) int a64_vq_d_to_s(uint64_t x, uint32_t *r) {
|
||||
const uint32_t t = (uint32_t)recomp_fpx_fb((float)recomp_fpx_d(x));
|
||||
if (!(RECOMP_FPX_BIG32(t) || RECOMP_FPX_ZERO64(x))) return 0;
|
||||
*r = t;
|
||||
return 1;
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_cvtsd(const Ctx *c, uint64_t *d, const uint64_t *n) {
|
||||
uint64_t r;
|
||||
if ((c->fpcr & RECOMP_FPX_FPCR_MASK) || !a64_vq_s_to_d(c, (uint32_t)n[0], &r)) return 0;
|
||||
a64_vset64(d, r);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_cvtds(const Ctx *c, uint64_t *d, const uint64_t *n) {
|
||||
uint32_t r;
|
||||
if (!RECOMP_FPX_OPEN(c) || !a64_vq_d_to_s(n[0], &r)) return 0;
|
||||
a64_vset64(d, r);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_cvtl(const Ctx *c, uint64_t *d, const uint64_t *n, int hi) {
|
||||
const uint64_t x = n[hi ? 1 : 0];
|
||||
uint64_t r0, r1;
|
||||
if ((c->fpcr & RECOMP_FPX_FPCR_MASK) || !a64_vq_s_to_d(c, (uint32_t)x, &r0) || !a64_vq_s_to_d(c, (uint32_t)(x >> 32), &r1)) return 0;
|
||||
a64_vset128(d, r0, r1);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
static inline __attribute__((always_inline)) int a64_vfpx_q_cvtn(const Ctx *c, uint64_t *d, const uint64_t *n) {
|
||||
const uint64_t x0 = n[0], x1 = n[1];
|
||||
uint32_t r0, r1;
|
||||
if (!RECOMP_FPX_OPEN(c) || !a64_vq_d_to_s(x0, &r0) || !a64_vq_d_to_s(x1, &r1)) return 0;
|
||||
a64_vset64(d, (uint64_t)r1 << 32 | r0);
|
||||
A64_VFPX_Q_PROBE();
|
||||
return 1;
|
||||
}
|
||||
#else /* (no SSE2 host path: the C behind each test always runs) */
|
||||
#define a64_vfpx_q_mul(c, d, n, m, k) 0
|
||||
#define a64_vfpx_q_mule(c, d, n, m, index, k) 0
|
||||
#define a64_vfpx_q_fma(c, d, n, m, neg, k) 0
|
||||
#define a64_vfpx_q_fmae(c, d, n, m, index, neg, k) 0
|
||||
#define a64_vfpx_q_div(c, d, n, m, k) 0
|
||||
#define a64_vfpx_q_minmax(c, d, n, m, min, k) 0
|
||||
#define a64_vfpx_q_cvtsd(c, d, n) 0
|
||||
#define a64_vfpx_q_cvtds(c, d, n) 0
|
||||
#define a64_vfpx_q_cvtl(c, d, n, hi) 0
|
||||
#define a64_vfpx_q_cvtn(c, d, n) 0
|
||||
#endif
|
||||
"""
|
||||
|
||||
|
||||
_installed = False
|
||||
|
||||
|
||||
def install():
|
||||
"""The wrap around a64emit.translate (insn's, installed before) and HEADER at the end of the kit's fpx_header. Once"""
|
||||
global _installed
|
||||
if _installed:
|
||||
return
|
||||
_installed = True
|
||||
base_translate, base_header = a64emit.translate, a64_lift.fpx_header
|
||||
|
||||
def translate(items, batch=65536):
|
||||
texts, flags = base_translate(items, batch)
|
||||
on = groups()
|
||||
if not on:
|
||||
return texts, flags
|
||||
texts = list(texts)
|
||||
for k, (pc, w) in enumerate(items):
|
||||
call = fast_call(w, on)
|
||||
if call and not flags[k] and not main_handles(texts[k]):
|
||||
texts[k] = wrap(texts[k], call)
|
||||
return texts, flags
|
||||
|
||||
def fpx_header(used):
|
||||
return base_header(used) + HEADER # (after the kit's VSET_HEADER, whose a64_vst_lo64, a64_vset64 and a64_vset128 it uses)
|
||||
|
||||
a64emit.translate, a64_lift.fpx_header = translate, fpx_header
|
||||
@@ -12,6 +12,14 @@ W functions and the C runtime's file functions take paths of MAX_PATH (260) and
|
||||
folder (the game's own nn::fs paths add the long-path prefix themselves, host/nn_fs.c fs_wide; host/hos_paths.h's and gpu/vk/vk_paths.h's
|
||||
probe too). Without that setting it changes nothing.
|
||||
|
||||
(Triforce) HOOKSHOT_SEGMENT_HEAP=1 at the build: heapType SegmentHeap (Windows 10 2004 and newer) puts the whole process on
|
||||
Windows' segment heap in place of the NT heap, its DLLs and the graphics driver too. Measured on an RX 6700 XT (AMD's Windows
|
||||
driver, a cold shader preparation): 76% of the driver's compile threads' samples waited on the NT heap's lock, 12 workers ran
|
||||
slower than 6, and the first part of the preparation took 1,478 s against 982 s on the segment heap; play, four pairs of
|
||||
runs, within the noise. Not measured on NVIDIA, Intel or Windows 10, and the heap is fixed when the process starts, so the
|
||||
arms are two exes. On by default since the lab's play A/B on an RTX card (11-22: no fps lost); HOOKSHOT_SEGMENT_HEAP=0: off. The log's "the shaders' preparation runs on" line says which heap the process has
|
||||
(gpu/vk/prepare_threads.h segment_heap).
|
||||
|
||||
python nx2/win_manifest.py [out.res]
|
||||
"""
|
||||
import os
|
||||
@@ -33,6 +41,27 @@ MANIFEST = b"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
</assembly>
|
||||
"""
|
||||
|
||||
# ((Triforce) HOOKSHOT_SEGMENT_HEAP=1: the element manifest() puts after longPathAware)
|
||||
SEGMENT_HEAP = b""" <heapType xmlns="http://schemas.microsoft.com/SMI/2020/WindowsSettings">SegmentHeap</heapType>
|
||||
"""
|
||||
_SEGMENT_HEAP_AFTER = b"""<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
|
||||
"""
|
||||
|
||||
|
||||
def segment_heap_asked(environ=None):
|
||||
"""(Triforce) The exe on Windows' segment heap unless HOOKSHOT_SEGMENT_HEAP=0 (the NT heap, as before). On by default since the
|
||||
lab A/B of 11-22 (the Market, 3+3, one build each way on an RTX card: 81.4 against 80.6 fps, 1% low 59.9 against 58.0)"""
|
||||
return (os.environ if environ is None else environ).get("HOOKSHOT_SEGMENT_HEAP", "").strip() != "0"
|
||||
|
||||
|
||||
def manifest(segment_heap=False):
|
||||
"""MANIFEST, with SEGMENT_HEAP in its windowsSettings when segment_heap"""
|
||||
if not segment_heap:
|
||||
return MANIFEST
|
||||
at = MANIFEST.index(_SEGMENT_HEAP_AFTER) + len(_SEGMENT_HEAP_AFTER)
|
||||
return MANIFEST[:at] + SEGMENT_HEAP + MANIFEST[at:]
|
||||
|
||||
|
||||
RT_MANIFEST = 24
|
||||
CREATEPROCESS_MANIFEST_RESOURCE_ID = 1 # (the one an exe's loader reads)
|
||||
|
||||
@@ -48,9 +77,10 @@ def res_bytes(manifest=MANIFEST):
|
||||
return res_entry(0, 0, b"", 0, 0) + res_entry(RT_MANIFEST, CREATEPROCESS_MANIFEST_RESOURCE_ID, manifest)
|
||||
|
||||
|
||||
def write(path):
|
||||
"""hookshot_manifest.res at path, rewritten only when its bytes change (the link reads it each time)"""
|
||||
data = res_bytes()
|
||||
def write(path, segment_heap=None):
|
||||
"""hookshot_manifest.res at path, rewritten only when its bytes change (the link reads it each time). segment_heap: None,
|
||||
as HOOKSHOT_SEGMENT_HEAP says (segment_heap_asked)"""
|
||||
data = res_bytes(manifest(segment_heap_asked() if segment_heap is None else segment_heap))
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
if f.read() == data:
|
||||
|
||||
Reference in New Issue
Block a user