Skip to content

cpu: make the data-side nutlb two-way set-associative - #944

Open
Samueltgg12 wants to merge 2 commits into
techomancer:mainfrom
Samueltgg12:perf/nutlb-2way
Open

Samueltgg12 wants to merge 2 commits into
techomancer:mainfrom
Samueltgg12:perf/nutlb-2way

Conversation

@Samueltgg12

Copy link
Copy Markdown
Contributor

Summary

Next guest-memory/TLB perf change after #943. docs/nutlb-design.md §8/§9 measured 95–100% of nutlb misses as same-set conflicts and §10 listed 2-way associativity as "still the obvious next move". This makes the data-side nutlb two-way set-associative.

Design

  • MipsCore::nutlb / nutlb_valid gain a way dimension: [Read|Write][way][set], NUM_NUTLB_WAYS = 2. New nutlb_lru[arr][word] holds one next-victim bit per set. Arrays grow 8 KiB → 16 KiB.
  • nutlb_translate probes both ways with a branchless tag == va_page & valid_bit per way, ORs the hits, and selects the matching way's phys. On a miss it fills the victim way and flips the victim bit — so the hot hit path pays no LRU store, only one extra tag load / valid probe / select.
  • nutlb_fill takes the way; nutlb_clear clears both ways' valid bits and the LRU words; nutlb_is_valid/nutlb_set_valid take a way.
  • JIT mirror kept in lockstep: emit_inline_mem_guard computes way 1 as way0 + NUM_NUTLB_ENTRIES * 16, reads both valid words, ORs the hits and selects e_phys. It does not touch the victim bit (fills are Rust-only).
  • tlbstats conflict counting checks either way for a valid foreign tag.

SMP

nutlb is per guest CPU; both ways are probed before any result is used, and every existing barrier (exception, ERET, mode/ASID change, TLB write, restore) still flushes both ways. No shared state added. Safe for 4-vCPU Fuel/Tezro; please verify on the Tezro.

Verification

cargo check -p iris --features jitv2 clean (also jitv2,jitv2_lockstep). cargo test -p iris --lib --features jitv2: jitv2:: 523 passed, mips_exec_test:: 218 passed, nutlb 2 passed; jitv2,jitv2_lockstep lockstep 15 passed. Not guest-measured — please run iris-bench / gltest --bench, compare against #943 and main, and check tlbstats conflict rate on live IRIX.

Caveat

docs/nutlb-design.md §0 warns that a cheaper probe has beaten a smarter structure here twice before (4-way/AVX2 nanotlb lost; tagged nutlb lost). The added tag compare and 16 KiB footprint must earn their keep on a real guest. If it regresses, revert this commit wholesale — the Rust probe and the JIT mirror must stay in lockstep. No config/feature change; docs/nutlb-design.md §10/§11.1, CHANGELOG.md, clanker/cpu/nutlb-two-way.md + clanker/INDEX.md updated.

A jitv2 build paid ~10% on every guest store from a shared
gen[phys >> 12].fetch_add(1, Release), which also ping-ponged the cache
line between guest CPUs and compile workers. Each 4KB page now has 16
counters: slot 0 for bus/DMA/monitor writes, slots 1..8 for the guest
CPUs. A CPU store bumps only its own slot with a plain Release store
(no atomic RMW, single writer); PhysicalCodePage::current_gen() takes
the max of all slots with Acquire, so any CPU's or a device's write is
observed before code is published. CodePage::gen_slots bounds the read
(16 for RAM, 1 for ROM/MMIO); NEVER_COMPILABLE_GEN is an array.

ppMem GEN_RATIO 512 -> 32 (16 counters per page), GEN_WINDOW_SIZE
128MB; all window map/scrub/mark sites and the JIT inline store's gen
index scaled by GEN_SLOTS; MipsCore::cpu_id selects the slot. bump_gen_all
bumps every slot; install_jit_mem_ptrs stamps each cache's CPU id.

SMP-safe: single-writer slots + Release/Acquire + max over all CPUs.
cargo check (jitv2, +rex-jit/gr4-jit/gr2-jit/wgpu/gputrace) clean;
ppmem 51 passed; jitv2 523 passed. Not yet guest-measured.
docs/nutlb-design.md §8/§9 measured 95-100% of nutlb misses as same-set
conflicts and §10 listed 2-way as the obvious next move. Each set now
holds two entries: MipsCore::nutlb/nutlb_valid gain a [way] dimension and
nutlb_lru records the next victim bit, flipped only on a fill so the hot
hit path pays no LRU store.

nutlb_translate probes both ways with a branchless AND of tag compare and
valid bit, ORs the hits, and selects the matching phys; on a miss it fills
the victim way. The JIT inline probe in codegen.rs mirrors this exactly:
way 1 is one array past way 0, both valid words are read, hits OR-ed and
e_phys selected. No victim-bit write in compiled code (fills are Rust-only).
tlbstats conflict checking looks at either way.

Flushing at all the existing barriers still clears both ways; nutlb is
per-CPU, so SMP is unaffected. Arrays grow 8 KiB -> 16 KiB.

docs/nutlb-design.md updated (§10 no longer open, §11.1 shape).
cargo check (+jitv2_lockstep) clean; jitv2 523, mips_exec_test 218,
nutlb 2, lockstep 15 tests passed. Not yet guest-measured.
@Samueltgg12

Copy link
Copy Markdown
Contributor Author

@techomancer looks good but it fails tests. fix and change whatever you want from it and put in the upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant