Repository navigation
engine: shrink block cache to 8 KB and evict ways in FIFO order - #111
Open
bluetoe-99 wants to merge 3 commits into
Open
bluetoe-99 wants to merge 3 commits into
bluetoe-99 wants to merge 3 commits into
Conversation
bluetoe-99
force-pushed
the
feature/engine-block-cache-8kb
branch
2 times, most recently
from
October 29, 2026 19:21
8cbf9ec to
20a12e8
Compare
Reduce block cache sets from 4096 to 128 (issue pound-emu#45): 128 sets x 64 B = 8192 B exact, enforced by a static_assert. Replace the PC-hash way selection with a 2-bit FIFO counter per set, packed 4 sets per byte (32 B total). The lookup hot path is untouched. Because cached host code pointers only alias valid compiled blocks while the Tier 1 layout is intact, clear the block cache at every layout reset: the buffer-overflow reset (closing the TODO at that site), the clear-cache request, and bal_engine_reset. A 32x smaller cache makes evictions and recompiles common, which makes buffer exhaustion - and the dangling pointers - reachable. Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
bal_x86_assembler_reset memsets rx_buffer, which points at the RX mapping (Linux shm dual mapping, Windows file-mapping view): never writable, so the reset faulted on any platform that enforces W^X. On APPLE buffer == rx_buffer, so the memset was a redundant second zeroing of the same pages. Writes to the RW alias already cover the executable mapping (same physical backing), so zeroing assembler->buffer is sufficient. This path is reached by the Tier 1 buffer-overflow reset and bal_engine_clear_cache, both newly exercised by the engine block-cache tests. Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
Five blocks spaced 512 bytes apart all share one block cache set, because (pc >> 2) & 127 discards bit 9, so the fifth insert forces a FIFO way eviction. Assert every block produces identical results before and after eviction. Cover the clear-cache request path and the DISABLE_BLOCK_CACHE bypass flag. Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
bluetoe-99
force-pushed
the
feature/engine-block-cache-8kb
branch
from
October 29, 2026 19:46
20a12e8 to
aed331e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #45.
Cache shrink and FIFO eviction.
BLOCK_CACHE_SETSgoes from 4096 to 128, andstatic_assert(8192 == BLOCK_CACHE_SETS * sizeof(block_cache_set_t))pins the footprint at exactly 8 KB. Way eviction changes from a PC-hash ((pc >> 4) & 3, which evicts the same way for every PC sharing those bits) to the 2-bit FIFO counter per set that the issue asks for, packed four sets per byte into a 32 B array and pinned by a second static_assert at 2 bits per set. The lookup hot loop is untouched.About the
size -Aacceptance criterion: the block cache lives insideinternal_engine_state_t, which is heap-allocated through the engine's allocator, so it has never appeared in the static image:.bssis 8 B in both builds. The footprint is pinned by the two static_asserts instead, and confirmed from DWARF withpahole:Engine-state allocation drops from 262,656 B to 8,768 B (-96.7%).
Cache invalidation. Cached
host_codepointers only stay valid while the Tier 1 layout is intact, so the cache is now cleared at every layout reset: the buffer-overflow reset (this closes the// TODO: When the block cache is implemented, it must be cleared here.at that site, and this PR is what makes that path reachable), the clear-cache request, andbal_engine_reset(which memsets the executable buffer and has the same dangling-pointer problem). A 32x smaller cache makes eviction and recompile churn common, so running the code buffer out becomes reachable.One latent bug fixed (
bal_x86_assembler_reset): it memsetsrx_buffer, which points at the executable mapping:PROT_READ|PROT_EXECon Linux (shm dual mapping), aPAGE_EXECUTE_READview on Windows, and the same pointer asbufferon APPLE. That memset faulted on Linux and Windows and was redundant on APPLE (the RW dual mapping aliases the same physical pages). Running the reset paths in the new tests produced an immediate SIGSEGV at exactly that line on stock main. Removed.Tests (
test_bal_engine_block_cache, the first engine-level tests): fiveMOVZ X8,#k; RETblocks spaced 512 B apart.(pc >> 2) & 127discards bit 9, so all five land in one 4-way set and the fifth insert forces a FIFO eviction. Every block must give identical results before and after eviction, through abal_engine_clear_cachecycle, and withDISABLE_BLOCK_CACHE. ctest 15/15 (was 14), zero warnings (Clang 18, project strict flags).Cachegrind (the same 500-seed IPC workload piped byte-identically into
bal_fuzzer_worker_ballistic, stock vs this PR):The D1 write-miss delta is the per-engine-init zeroing of the old 262 KB state: 262,144/64 = 4,096 cache lines x 500 seeds = 2.048 M, which matches the measured 2,140,906 within other state touches.
perf stat (i9-13900K, pinned to a P-core, whole 3,000-seed fuzzer process tree including driver and Unicorn oracle): L1-dcache-load-misses 10,631,787 becomes 10,423,280 (-2.0%), cycles 1,569,667,093 becomes 1,552,376,645 (-1.1%). The engine is a small share of that process tree, so the Cachegrind per-process numbers are the sharper signal.
Fuzz gate (full corpus, 59,088 seeds, identical corpus and order, Debug build): unique-seed verdict sets against the stock-main baseline: 0 new fails, 0 new errors. Main's 149 unique fail seeds become 16; main's 18 error seeds become 6; every remaining fail or error seed is a member of main's set. (Fail-to-pass flips are order-sensitivity of fuzzer worker state across seeds, not claimed fixes.)