"""N, Z, C and V as locals of each lifted function, on by default (A64_LOCAL_FLAGS=0: bytes of the context, as before). (Triforce, 11-23, the fps pass's LX-2, after NX2navi 0.11.0-rc4's recomp/flags.py, which keeps the flags in a function's locals.) The kit has the rule as a lift-time switch, A64_LOCAL_FLAGS (third_party/a64-kit/lift/a64_lift.py local_flags, localize_regs): every c->n, c->z, c->c and c->v of a function's text becomes a local, read from the context once at the function's start and written back only where control leaves other than by a call or a return (tail calls, indirect jumps, SVCs and traps), and read again after an SVC. The AAPCS64 leaves the flags undefined across calls and returns, which the kit's dead-flag pass relies on already. Without it every flag a later instruction reads is a byte stored into the context at each compare, and the compiler must keep the store because the context outlives the function. Measured on the owner's PC (RTX 5090, 2560x1440, the Market after the save's load, the last 40 s of each run, nine runs a side interleaved over three batches, the shipped hot profile made without it): 139.4 -> 141.3 fps (+1.3%), the time a frame spends outside the replay 2.10 -> 1.98 ms. In the last batch, with HOS_THREAD_TIMES: 139.3 -> 142.3 fps (+2.1%, every run with it above every run without) and the game's workers and main thread 31.9 -> 30.4 ms of CPU a frame. The rule (on): unset, empty or any value but 0 turns it on; 0 turns it off. The kit's own reader takes any value that is set and not empty as on, so install() puts this rule in its place (a64_lift.local_flags) and leaves the environment as the player set it. Every process that runs install() decides the same way: the lift's own, and each process it starts for its emit (spawned on Windows: they run nx2/lift.py's module level again with the lift's environment). A64_LOCAL_FLAGS is an A64_ name, so nx2/make_game.py's lift key has the player's value, and this file is in the key (it is not in make_game.py's LIFT_KEY_NOT; tests/test_local_flags.py holds it there). (11-27) The kit's A64_PACKED_FLAGS (an experiment, off) writes a flag-setting instruction's four flags into the context in one store, which the flag locals do not see: every later flag read would take the flags the function started with. install() stops the lift when both are on. What the lifted text then has in place of c->z is fz_ (likewise fn_, fc_, fv_): an edit of the lifted C that matches or writes a flag by its text has to know both forms (nx2/oot_patches.py patch_scene_format). (11-27, on v0.8.4) The hot profile. The hot code is built with the shipped profile (RX_PGO, clang's -fprofile-instr-use), which clang matches to each function by a hash of its control flow (its ifs and elses, loops, gotos, returns, !, && and ||, comparisons, ?:). With the switch the kit lifts a scalar FCMP's fast path in a code form, "{ const uint32_t _fk = a64_fcmp32k(c,a,b); if (_fk) { the flags from _fk } else { the exact compare } }", where without it the lift has "{ if (!a64_fcmp32(c,a,b)) { the exact compare } }": an else in place of a !, so every function that holds an FCMP hashed differently and was compiled without its profile. That was 652 of the 1384 hot functions of v0.8.4 (17.7% of the profile's entry counts), and clang counted 49 functions with stale entries in the first hot piece where the lift without the switch has 21. install() therefore wraps the kit's vectorize_fcmp: the flags from _fk are written first and the exact compare, which writes every flag the dead-flag pass keeps on each of its paths, runs after them under "if (!_fk)", so the control flow is the one the profile was made from. The fast path computes the same three flags as before; the exact path's first writes are dead, and the compiler drops them. """ import os import re # the kit's code form of a scalar FCMP's fast path with the flags as locals (a64_lift.vectorize_fcmp), as far as its else KIT_FCMP = re.compile(r"\{ const uint32_t _fk = (a64_fcmp(?:32|64)k\(c,[^;]*\)); if \(_fk\) \{ c->n=\(uint8_t\)\(_fk&1\); " r"c->z=\(uint8_t\)\(_fk>>1&1\); c->c=\(uint8_t\)!\(_fk&1\); c->v=0; \} else ") # the same flags written first and the exact compare under "if (!_fk)": the control flow of the form without the switch PGO_FCMP = r"{ const uint32_t _fk = \1; c->n=(uint8_t)(_fk&1); c->z=(uint8_t)(_fk>>1&1); c->c=(uint8_t)((_fk&1)^1); c->v=0; if (!_fk) " _installed = False def on(value): """Whether the flags become locals for a value of A64_LOCAL_FLAGS (None: unset)""" return value is None or value.strip() != "0" def profile_form(text): """A lifted instruction's text with the kit's code form of the FCMP fast path (if any) in the profile's control flow""" return KIT_FCMP.sub(PGO_FCMP, text) if "_fk = a64_fcmp" in text else text def kit_reads(): """a64_lift.local_flags once installed: the rule (on) on the switch as the environment has it when the kit asks. By the variable's literal name, as tests/test_lift_key_env.py wants every read of the lift's environment""" return on(os.environ.get("A64_LOCAL_FLAGS")) def report(): """The lift's line on the flags' form""" return ("flags: N, Z, C and V as each function's locals (A64_LOCAL_FLAGS=0: bytes of the context)" if kit_reads() else "flags: bytes of the context (A64_LOCAL_FLAGS=0)") def install(): """The rule in place of the kit's reader (a64_lift.local_flags, which the kit calls by its name in the module), the environment left as it is, and a stop when the kit's A64_PACKED_FLAGS is on as well (the kit's own test: set and not empty). And, once, the wrap around the kit's vectorize_fcmp (profile_form), which a64_lift.vectorize_fpx calls by its name in the module""" global _installed import a64_lift a64_lift.local_flags = kit_reads if kit_reads() and os.environ.get("A64_PACKED_FLAGS"): raise SystemExit("nx2/local_flags.py: A64_PACKED_FLAGS writes the four flags into the context in one store, which " "the flag locals (A64_LOCAL_FLAGS, on by default) do not see: every later flag read would take the " "flags the function started with. Unset A64_PACKED_FLAGS, or set A64_LOCAL_FLAGS=0 with it") if _installed: return _installed = True base = a64_lift.vectorize_fcmp def vectorize_fcmp(text): return profile_form(base(text)) a64_lift.vectorize_fcmp = vectorize_fcmp