diff --git a/.agents/kwemu.md b/.agents/kwemu.md index 31e1999..56c2b7a 100644 --- a/.agents/kwemu.md +++ b/.agents/kwemu.md @@ -221,7 +221,9 @@ names the member a region left from. both, `member_op` the low 16 bits of each member's op index. - **Membership.** When a block gets hot, `compile` takes it, the members of an open region of the same virtual page that has room (that region - is replaced), the blocks of that page that could be members and have + is replaced; with chaining, only regions with one live home or stale + code in the block's slot qualify), + the blocks of that page that could be members and have counted at least a quarter of the threshold (`pageWarm`, which probes every pc of the page; without it most of them got hot later, one at a time, and each compiled the region again to join it), then diff --git a/src/emu/jitcore.zig b/src/emu/jitcore.zig index 102e25b..d632b76 100644 --- a/src/emu/jitcore.zig +++ b/src/emu/jitcore.zig @@ -910,12 +910,13 @@ pub const Error = error{ Unsupported, Overflow }; // on host addresses, generations or the pool entry: a revalidated member // keeps its code. Regions are formed when a block gets hot (`compile`): // that block, the members of an open region of its page that has room -// (which is then replaced, so a page tends to have one module), the blocks -// of its page that have counted a quarter of the threshold (`pageWarm`: -// otherwise each of them, when hot, compiles the region again), then -// blocks reachable from those through branch and call-return targets -// (`I.targets`) that are already decoded, valid under their page's current -// fill and not compiled elsewhere, on any page, in breadth-first order. +// (replaced; with chaining, only regions with one live home or stale +// code in that block's slot), the blocks of its page that have counted a +// quarter of the threshold (`pageWarm`: otherwise each of them, when hot, +// compiles the region again), then blocks reachable from those through +// branch and call-return targets (`I.targets`) that are already decoded, +// valid under their page's current fill and not compiled elsewhere, on any +// page, in breadth-first order. // They grow later along hot exits (`noteHot`): a block compiled in another // region (a self-loop's own module included), on any page, that `run` // often enters right after the region left joins it as a guest. A member that is re-decoded only loses its own @@ -963,7 +964,8 @@ inline fn memberSt(i: usize, m: usize) u32 { } /// Pool entry of an open region of b's page with room for another member -/// whose live (home) members on that page are on b's host page. +/// whose live (home) members on that page are on b's host page; with +/// chaining, one with a single live member or stale code in b's slot. fn findRegion(cpu: *Cpu, b: *const Block, limit: usize) ?usize { const st = &cpu.priv.jit; for (0..npool) |i| { @@ -971,14 +973,24 @@ fn findRegion(cpu: *Cpu, b: *const Block, limit: usize) ?usize { var n: usize = 0; var live: usize = 0; var same = true; + var stale = false; for (st.members[i]) |m| { if (m == 0) continue; n += 1; const o = &cpu.priv.blocks[m - 1]; + // b is not compiled: a member in its slot is stale code + stale = stale or o == b; if (slotOf(o) != i + 1) continue; live += 1; if ((o.pc ^ b.pc) >> 12 == 0) same = same and o.host >> 12 == b.host >> 12; } + // Chaining can enter an existing multi-block module while the + // newly hot block compiles separately. Avoid recompiling those + // live members unless the edges become hot enough to grow later, + // or the region holds stale code in b's slot (its members' + // transfers to b would fail the op check and leave to `run`, + // unchained, until an exit grow). + if (st.chain and live > 1 and !stale) continue; if (same and live > 0 and n < limit) return i; } return null;