PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
VU programs that never read flags skip them at exit again, as before the delay fix
Samir Nuri2b934d38ecd6
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cppindex e8b8840..fae2638 100644--- a/recomp/vurecomp.cpp+++ b/recomp/vurecomp.cpp@@ -485,7 +485,7 @@ std::string generate(const Program &p, std::string &name) {const std::vector<std::uint32_t> pcs = ps2::vu::reachable(p.code.data(), kMask, p.entry);std::set<std::uint32_t> in(pcs.begin(), pcs.end());std::map<std::uint32_t, Lower> lowers;- bool any_status = false, any_q = false;+ bool any_status = false, any_flags = false, any_q = false;for (std::uint32_t pc : pcs) {const std::uint32_t lo = word(p.code, pc), hi = word(p.code, pc + 4);Lower l;@@ -493,6 +493,7 @@ std::string generate(const Program &p, std::string &name) {for (const std::uint32_t latency : {7u, 13u})replace_all(l.code, std::format("fl.end_q(r, qb, {}u)", latency), std::format("fl.end_q(r, qb, {}u)", ps2::vu::q_delay(p.code.data(), kMask, pc, latency)));any_status = any_status || l.reads_status;+ any_flags = any_flags || l.reads_flags;any_q = any_q || l.code.find("fl.end_q(") != std::string::npos;lowers[pc] = l;}@@ -541,7 +542,7 @@ std::string generate(const Program &p, std::string &name) {const std::uint32_t lo = word(p.code, pc), hi = word(p.code, pc + 4);bool out_live = false;for (std::int64_t s : successors(pc)) out_live = out_live || s < 0 || !in.count(static_cast<std::uint32_t>(s)) || live_in[static_cast<std::uint32_t>(s)];- const Upper u = upper(hi, any_status || out_live);+ const Upper u = upper(hi, any_status ? true : (any_flags && out_live));const Lower &l = lowers[pc];// Save the VI write for the next executed instruction, including// taken branches. Branch operands use the previous instruction's view.diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cppindex 22228d8..b447891 100644--- a/runtime/src/vu_jit.cpp+++ b/runtime/src/vu_jit.cpp@@ -390,7 +390,7 @@ private:std::set<std::uint32_t> keep_bk_, set_bi_;int delay_mode_ = 0;unsigned delay_reg_ = 0;- bool any_status_ = false;+ bool any_status_ = false, any_flags_ = false;std::uint32_t word(std::uint32_t at) const {std::uint32_t v;@@ -1699,7 +1699,7 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {void Translator::instruction(std::uint32_t pc, std::uint32_t following) {const std::uint32_t lo = word(pc), hi = word(pc + 4);const Up u = decode_upper(hi);- const bool flags_live = any_status_ || out_live(pc);+ const bool flags_live = any_status_ ? true : (any_flags_ && out_live(pc));const LowerInfo &li = lowers_[pc];c_.get(kT);@@ -1865,6 +1865,7 @@ bool Translator::run(std::vector<std::uint8_t> &out) {LowerInfo l;if (!(word(pc + 4) & 0x80000000u)) l = analyze_lower(word(pc), pc);any_status_ = any_status_ || l.reads_status;+ any_flags_ = any_flags_ || l.reads_flags;lowers_[pc] = l;}for (std::uint32_t pc : pcs_) live_in_[pc] = false;diff --git a/scripts/test_vu_codegen.py b/scripts/test_vu_codegen.pyindex e07f8c8..3cca261 100644--- a/scripts/test_vu_codegen.py+++ b/scripts/test_vu_codegen.py@@ -57,8 +57,9 @@ int main(int argc, char **argv) {put(0, addi(1, 0, 1));put(8, ((test == 6 ? 0x24u : 0x25u) << 25) | (1u << 11) | (1u << 16), unop | end);} else {- // Flags remain observable after exit even with no local reader.+ // Flags remain observable after exit in a program that reads them.put(0, nop, (0xFu << 21) | (2u << 16) | (1u << 11) | (3u << 6) | 0x28u | end);+ put(8, (0x16u << 25) | (2u << 16));}std::string name;native << generate(p, name);@@ -102,6 +103,7 @@ for (let test = 0; test < 8; ++test) {env[imp.name] = () => { throw Error(`Unexpected import: ${imp.name}`); };}env.clip = () => 1;+ env.status = () => 0;new WebAssembly.Instance(mod, {env}).exports.run(r, 0x4000);if (test >= 6) {if (view.getUint32(r + off.tpc, true) !== 8) throw Error(`Register jump regression ${test}`);