mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-23 17:49:57 -04:00
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.
197 lines
10 KiB
Python
197 lines
10 KiB
Python
"""TBL with a one-register table, EXT, 128-bit vector loads and DUP by element as nx2/fastvec.py rewrites them, bit-exact
|
|
with what the kit's translator writes for them (game-free).
|
|
|
|
(Triforce, 11-21, the performance audit's LIFT-02, after Source A's.) The translator writes TBL and EXT as byte loops
|
|
through the stack, a 128-bit load as two halves and DUP by element as a loop, each followed by a 16-byte read that
|
|
cannot be forwarded from those stores. nx2/fastvec.py turns each into one instruction's helper on the lifted text.
|
|
|
|
Guest words (TBL of 8 and 16 bytes; EXT of 16 bytes at every imm 0-15 and of 8 bytes at every imm 0-7; LDR Q, LDP Q
|
|
and a post-indexed LDR Q (LD1 is written byte by byte and is not one of them); DUP of every element size, index and width; with Vd apart, Vd = Vn, Vd = Vm, one register
|
|
for all, and registers above 15) go through a64emit.translate and a64_lift.op_text twice: as lifted now, and with
|
|
A64_NO_VBYTES=1 and A64_NO_VMOVES=1 (the kit's text). The first must call the helper and keep no byte loop; the second
|
|
must not have the helper. tests/vbytes_test.c then runs both from the same random Ctx and guest memory (index bytes
|
|
biased to the edges: 15, 16, 0x6f-0x90, 0xff) and compares every vector and general register. Built at -march=x86-64-v2
|
|
(SSSE3: PSHUFB and PALIGNR), at -march=x86-64 (SSE2 only: the plain C fallback) and at -march=native.
|
|
|
|
And reads the source: nx2/lift.py installs the passes, and the switches are in the switches table.
|
|
|
|
python -I -B tests/test_vbytes.py [--cc clang] [--cxx clang++] [--random 40000] [--time]
|
|
|
|
Skipped (exit 0) on a host that is not x86-64 or has no C or C++ compiler.
|
|
"""
|
|
import argparse
|
|
import os
|
|
import platform
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tests"))
|
|
sys.path.insert(0, os.path.join(ROOT, "nx2"))
|
|
import test_fpx_sticky as fs # noqa: E402 (the build helpers the kit's other lifted-code test uses)
|
|
|
|
RUN_ALL_ARGS = ["--cc", "clang", "--cxx", "{cxx}"]
|
|
SEED = 20261121
|
|
MARCHES = ("x86-64-v2", "x86-64", "native")
|
|
REGS = ((0, 5, 1), (5, 5, 1), (1, 5, 1), (2, 2, 2), (3, 30, 17)) # (rd, rn, rm)
|
|
|
|
|
|
def tbl(q, rd, rn, rm):
|
|
return 0x0E000000 | (q << 30) | (rm << 16) | (rn << 5) | rd
|
|
|
|
|
|
def ext(q, rd, rn, rm, imm):
|
|
return 0x2E000000 | (q << 30) | (rm << 16) | (imm << 11) | (rn << 5) | rd
|
|
|
|
|
|
def ldr_q(rt, rn, imm):
|
|
return 0x3DC00000 | ((imm // 16) << 10) | (rn << 5) | rt
|
|
|
|
|
|
def ldp_q(rt, rt2, rn, imm):
|
|
return 0xAD400000 | (((imm // 16) & 0x7f) << 15) | (rt2 << 10) | (rn << 5) | rt
|
|
|
|
|
|
def ldr_q_post(rt, rn, imm9):
|
|
return 0x3CC00400 | ((imm9 & 0x1ff) << 12) | (rn << 5) | rt
|
|
|
|
|
|
def dup_elem(q, size, idx, rd, rn):
|
|
imm5 = (idx << (size + 1)) | (1 << size)
|
|
return 0x0E000400 | (q << 30) | (imm5 << 16) | (rn << 5) | rd
|
|
|
|
|
|
def cases():
|
|
"""(name, guest word, the helper its lifted text must call)"""
|
|
out = []
|
|
for q in (0, 1):
|
|
for r in REGS:
|
|
out.append(("TBL v%d.%s, {v%d.16b}, v%d" % (r[0], "16b" if q else "8b", r[1], r[2]), tbl(q, *r), "a64_tbl1("))
|
|
for q, n in ((1, 16), (0, 8)):
|
|
for imm in range(n):
|
|
for r in (REGS if imm in (0, 1, n // 2, n - 1) else REGS[:1]):
|
|
out.append(("EXT v%d.%s, v%d, v%d, #%d" % (r[0], "16b" if q else "8b", r[1], r[2], imm), ext(q, *r, imm),
|
|
"A64_EXT%d(" % n))
|
|
out += [("LDR q3, [x1, #32]", ldr_q(3, 1, 32), "a64_vld16("), ("LDR q31, [x7]", ldr_q(31, 7, 0), "a64_vld16("),
|
|
("LDP q2, q9, [x1, #-64]", ldp_q(2, 9, 1, -64), "a64_vld16("), ("LDR q5, [x3], #48", ldr_q_post(5, 3, 48), "a64_vld16(")]
|
|
for size, lanes in ((0, 16), (1, 8), (2, 4), (3, 2)):
|
|
for q in ((1,) if size == 3 else (0, 1)):
|
|
for idx in sorted({0, 1, lanes - 1}):
|
|
for rd, rn in ((6, 2), (2, 2), (17, 30)):
|
|
out.append(("DUP v%d.%d%s, v%d[%d]" % (rd, lanes if q else lanes // 2, "bhsd"[size], rn, idx),
|
|
dup_elem(q, size, idx, rd, rn), "a64_vdup("))
|
|
return out
|
|
|
|
|
|
def lifted(a64emit, a64_lift, used):
|
|
"""vbytes_lifted.h: each guest word as lifted now and as the kit writes it, as functions, and the CASES table"""
|
|
all_cases = cases()
|
|
texts, flags = a64emit.translate([(0x1000 + 4 * i, w) for i, (_, w, _) in enumerate(all_cases)])
|
|
if any(flags):
|
|
raise SystemExit("vbytes: a64emit did not translate the guest words: %s" % flags)
|
|
now = [a64_lift.op_text(t, used) for t in texts]
|
|
os.environ["A64_NO_VBYTES"] = os.environ["A64_NO_VMOVES"] = "1"
|
|
try:
|
|
old = [a64_lift.op_text(t, used) for t in texts]
|
|
finally:
|
|
del os.environ["A64_NO_VBYTES"], os.environ["A64_NO_VMOVES"]
|
|
out = ["/* Generated by tests/test_vbytes.py: each guest word as lifted now and as the kit's translator writes it. */"]
|
|
for i, (n, o, (name, _, helper)) in enumerate(zip(now, old, all_cases)):
|
|
if helper not in n or "for(int _i" in n:
|
|
raise SystemExit("vbytes: %s is not rewritten: %s" % (name, n[:300]))
|
|
if helper in o:
|
|
raise SystemExit("vbytes: %s has the helper with the switch set: %s" % (name, o[:300]))
|
|
out.append("static void g_now%d(Ctx *__restrict c) { %s }" % (i, n))
|
|
out.append("static void g_old%d(Ctx *__restrict c) { %s }" % (i, o))
|
|
out.append("static const struct { const char *name; void (*now)(Ctx *), (*old)(Ctx *); } CASES[] = {%s};"
|
|
% ", ".join('{"%s", g_now%d, g_old%d}' % (c[0], i, i) for i, c in enumerate(all_cases)))
|
|
return "\n".join(out) + "\n", len(all_cases)
|
|
|
|
|
|
def generate(cxx, tmp, flags):
|
|
"""a64_fpx.h, a64_suyu.h, a64_suyu.c and vbytes_lifted.h, by the generators the lift runs (a64emit built into tmp)"""
|
|
dll = os.path.join(tmp, "a64emit.dll" if os.name == "nt" else "liba64emit.so")
|
|
fs.build([cxx, "-std=c++20", "-O0", "-shared", "-Wno-everything", "-o", dll, os.path.join(fs.KIT, "lift", "a64emit.cpp")]
|
|
+ flags + ([] if os.name == "nt" else ["-fPIC"]), "a64emit")
|
|
import build_emit
|
|
build_emit.build = lambda force=False: dll # (not a64emit.dll beside the source)
|
|
import a64_lift
|
|
import a64_rtgen
|
|
import a64emit
|
|
import fastvec # (the passes under test, around the kit's)
|
|
fastvec.install()
|
|
used = set()
|
|
try:
|
|
a64_rtgen.write(tmp)
|
|
text, n = lifted(a64emit, a64_lift, used)
|
|
finally:
|
|
if a64emit._lib is not None: # (unloaded, so the folder can go)
|
|
import _ctypes
|
|
(_ctypes.FreeLibrary if os.name == "nt" else _ctypes.dlclose)(a64emit._lib._handle)
|
|
a64emit._lib = None
|
|
with open(os.path.join(tmp, "vbytes_lifted.h"), "w", newline="\n") as f:
|
|
f.write(text)
|
|
header = a64_lift.fpx_header(used)
|
|
for helper in ("a64_tbl1(", "A64_EXT16(", "A64_EXT8(", "a64_vld16(", "a64_vdup("):
|
|
if header.count("void %s" % helper) + header.count("#define %s" % helper) < 1:
|
|
raise SystemExit("vbytes: a64_fpx.h has no %s: fpx_header no longer carries the helpers" % helper)
|
|
with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f:
|
|
f.write(header)
|
|
return n
|
|
|
|
|
|
def check_sources():
|
|
with open(os.path.join(ROOT, "nx2", "lift.py"), encoding="utf-8") as f:
|
|
lift = f.read()
|
|
at = [lift.find(p) for p in ("import fastvec", "lift_fixes.install()", "fastvec.install()")]
|
|
if min(at) < 0 or at[1] > at[2]:
|
|
raise SystemExit("vbytes: nx2/lift.py does not install nx2/fastvec.py's passes after the kit's fixes")
|
|
with open(os.path.join(ROOT, "docs", "development.md"), encoding="utf-8") as f:
|
|
if "`A64_NO_VBYTES=1`, `A64_NO_VMOVES=1`" not in f.read():
|
|
raise SystemExit("vbytes: docs/development.md: A64_NO_VBYTES and A64_NO_VMOVES are not in the switches table")
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
p.add_argument("--cc")
|
|
p.add_argument("--cxx")
|
|
p.add_argument("--random", type=int, default=40000)
|
|
p.add_argument("--time", action="store_true", help="also time each kind of guest word, now and as the kit writes it")
|
|
a = p.parse_args()
|
|
check_sources()
|
|
if platform.machine().lower() not in ("x86_64", "amd64"):
|
|
fs.skip("not an x86-64 host: %s" % platform.machine())
|
|
cc = fs.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang")
|
|
cxx = fs.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++")
|
|
if not cc or not cxx:
|
|
fs.skip("no C compiler" if not cc else "no C++ compiler")
|
|
t0 = time.time()
|
|
failed = []
|
|
with tempfile.TemporaryDirectory(prefix="triforce-vbytes-") as tmp:
|
|
n = generate(cxx, tmp, fs.target_flags(cxx, tmp))
|
|
for march in MARCHES:
|
|
exe = os.path.join(tmp, "vbytes_test_%s.exe" % march)
|
|
fs.build([cc] + fs.target_flags(cc, tmp) + ["-march=" + march] + fs.FLAGS +
|
|
["-D_CRT_SECURE_NO_WARNINGS", "-I" + os.path.join(fs.KIT, "rt"), "-I" + tmp,
|
|
os.path.join(ROOT, "tests", "vbytes_test.c"), os.path.join(tmp, "a64_suyu.c"), "-o", exe],
|
|
"the driver at " + march)
|
|
r = fs.run([exe, str(a.random), str(SEED)], timeout=1200)
|
|
print("%-10s %s" % (march, r.stdout.rstrip()))
|
|
if r.returncode:
|
|
failed.append("%s (exit %d)" % (march, r.returncode))
|
|
if a.time and not failed: # (at the game's own -O1: what each rewrite is worth by itself)
|
|
exe = os.path.join(tmp, "vbytes_time.exe")
|
|
fs.build([cc] + fs.target_flags(cc, tmp) + ["-march=native", "-ffp-contract=off", "-fno-strict-aliasing", "-O1",
|
|
"-D_CRT_SECURE_NO_WARNINGS", "-I" + os.path.join(fs.KIT, "rt"), "-I" + tmp,
|
|
os.path.join(ROOT, "tests", "vbytes_test.c"), os.path.join(tmp, "a64_suyu.c"), "-o", exe], "the timing driver")
|
|
print("timing at -O1 -march=native:")
|
|
print(fs.run([exe, "time"], timeout=1200).stdout.rstrip())
|
|
if failed:
|
|
raise SystemExit("FAIL: vbytes: %s" % ", ".join(failed))
|
|
print("PASS: %d guest words rewritten by nx2/fastvec.py leave the same registers as the kit's text (%.0f s)" % (n, time.time() - t0))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|