Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ hashes are given where a change is easiest to understand by reading the commit.
- IRIX spends less host time on address translation: raising or lowering the
interrupt mask no longer empties the CPU's translation cache, which halves
translation misses on a busy 4-CPU Tezro. All machines.
- Address translation keeps twice as many recent pages per set on every
machine, so workloads that alternate between pages no longer evict each
other on every access. SMP guests keep a separate translation cache per
CPU.

### jitv2 memory

- jitv2 no longer bumps a shared per-page counter on every guest store. Each
CPU bumps its own counter slot and a compile reads the maximum of all slots,
so this also removes the cross-CPU cache-line contention. All machines;
especially SMP guests on small hosts.

### jitv2 compile order

Expand Down
2 changes: 2 additions & 0 deletions clanker/INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CPU architecture: CP0, TLB and its fast paths, caches, FPU, LL/SC, CPU models. B
| [nanotlb-asid-mutation-flush.md](cpu/nanotlb-asid-mutation-flush.md) | rule | nanotlb must be flushed on ASID mutation, not just on privilege transition |
| [translation-status-flush-filter.md](cpu/translation-status-flush-filter.md) | rule | skip the nutlb/nanotlb flush on a CP0 Status write that changes no translation-relevant bit (and on a no-op write) |
| [nutlb-erl-guard-defeats-permission-test.md](cpu/nutlb-erl-guard-defeats-permission-test.md) | history | nutlb: the ERL guard bit defeats the permission test entirely |
| [nutlb-two-way.md](cpu/nutlb-two-way.md) | design | two ways per nutlb set, branchless both-way probe in Rust and the JIT mirror; victim bit flipped on fill only |
| [r5000-taglo-is-the-r4400-pstate.md](cpu/r5000-taglo-is-the-r4400-pstate.md) | rule | R5000 L1 TagLo uses the R4400 PState encoding |
| [r5k-l1i-cache-bugs.md](cpu/r5k-l1i-cache-bugs.md) | open | R5000 secondary cache: no working configuration currently exists |
| [sc-succeeds-on-llbit-alone.md](cpu/sc-succeeds-on-llbit-alone.md) | rule | `SC` succeeds on the LLbit alone — `LLAddr` is not a gate |
Expand Down Expand Up @@ -85,6 +86,7 @@ The MIPS JIT (`src/cpu/jitv2/`): design, Cranelift codegen, delay slots, invalid
| [dirty-cache-page-probe.md](jitv2/dirty-cache-page-probe.md) | history | jitv2: the dirty-cache blind spot, and the probe that closes it |
| [dispatch-gate-shared-writes.md](jitv2/dispatch-gate-shared-writes.md) | rule | The dispatch gate must not write shared per-page state on every arrival: five atomic RMWs per arrival ping-ponged with the compile worker; test-then-set, count per CPU, reuse the gate's translation |
| [emit_absolute_pc_exit-in_delay_slot-followup.md](jitv2/emit_absolute_pc_exit-in_delay_slot-followup.md) | open | Follow-up: `emit_absolute_pc_exit` should own its own `in_delay_slot=false` clear |
| [per-cpu-page-generation.md](jitv2/per-cpu-page-generation.md) | design | Per-CPU page generation slots (16/page): a store bumps only its CPU's slot (no shared RMW); a compile reads the max of all slots; SMP-safe |
| [entry-word-bail-must-not-touch-pc-or-bd.md](jitv2/entry-word-bail-must-not-touch-pc-or-bd.md) | rule | An entry-word bail must not touch `pc` or `in_delay_slot` |
| [flush-preserved-entry-point-inflation.md](jitv2/flush-preserved-entry-point-inflation.md) | rule | Flush-preserved pages inherited the previous program's entry points |
| [fpu-flags-are-computed-not-read.md](jitv2/fpu-flags-are-computed-not-read.md) | rule | FPU exception flags are computed from bit patterns, never read from the host |
Expand Down
54 changes: 54 additions & 0 deletions clanker/cpu/nutlb-two-way.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# nutlb: two ways per set, branchless both-way probe

**Status:** implemented 2026-11-30, awaiting guest measurement.

## Symptom / motivation

`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".
The one-way structure's cheap probe had already beaten a 4-way/AVX2 nanotlb
once, so the added tag compare had to be kept minimal.

## Design

- `MipsCore::nutlb` and `nutlb_valid` gain a **way** dimension:
`[Read|Write][way][set]`, `NUM_NUTLB_WAYS = 2`. `nutlb_lru[arr][word]` holds
one next-victim bit per set. Total arrays 16 KiB (was 8 KiB).
- `nutlb_translate` loads **both** ways' `virttag`, ANDs each with its valid
bit, ORs the two results (bitwise, branchless). On a hit it selects the
matching way's `phys`; on a miss it fills the set's victim way and flips
that bit. **No LRU store on a hit**, so the hot path is one extra tag load
and one extra valid-bit probe, plus a select.
- `nutlb_fill` takes the way chosen by the caller; `nutlb_clear` clears both
ways' valid bits and the LRU words; `nutlb_is_valid`/`nutlb_set_valid` take
a `way`.
- **JIT mirror** (`jitv2/codegen.rs::emit_inline_mem_guard`) computes way 1
as `way0 + NUM_NUTLB_ENTRIES * 16`, reads both valid words
(`nutlb_valid[arr][way][idx >> 6]`), ORs the two hits and `select`s `e_phys`.
It does **not** update the victim bit (fills are Rust-only), so the emitted
probe stays two loads + two bit tests + one select.
- `tlbstats` conflict counting now checks either way for a valid foreign tag.

## Why correct on SMP

nutlb is per CPU; both ways are probed before any result is used, and the
valid bit still gates every tag. Flushing at all the existing barriers still
clears both ways. The JIT probe selects the same physical address the Rust
path would. The only new shared state is `nutlb_lru`, per-CPU and written
only on a fill, never on the hot hit path.

## Verification

`cargo check -p iris --features jitv2` clean; also `jitv2_lockstep`.
`cargo test -p iris --lib --features jitv2`: `jitv2::` 523 passed,
`mips_exec_test::` 218 passed, `nutlb` 2 passed,
`--features jitv2,jitv2_lockstep lockstep` 15 passed.

## Caveats

- 16 KiB host D-cache footprint (was 8 KiB). §0's warning stands: on this hot
path a cheaper probe has beaten a smarter structure twice before; this
needs live-IRIX + `iris-bench` measurement before believing it.
- `IRIS_*` no new config; no feature flag; `docs/nutlb-design.md` updated.
- If it regresses, revert the whole commit (the struct layout, the Rust
probe and the JIT mirror must stay in lockstep).
73 changes: 73 additions & 0 deletions clanker/jitv2/per-cpu-page-generation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# per-CPU page generation counters remove the per-store shared RMW

**Status:** implemented, awaiting guest measurement. 2026-11-30.

## Symptom

A jitv2 build with no code compiling was ~10% slower than a `tcache` build
without jitv2. The tax was `PpMemory::bump_gen` / `Memory::bump_gen` /
`tc_bump_gen` / `mips_cache_shadow::bump_gen` / the JIT inline store bump:
`gen[phys >> 12].fetch_add(1, Release)` on a **shared** `AtomicU64` for every
guest store, on all four vCPUs plus the compile workers touching the same
cache lines.

## Rule / design

- Each 4KB page gets **`GEN_SLOTS = 16` counters**: slot 0 is the **external**
counter (bus writes with no guest CPU: DMA, devices, monitor), slots 1..=8
are **per guest CPU** (`MAX_CPUS = 8`).
- **A store from a CPU thread bumps only its own slot**, with a plain
`load(Relaxed) + store(Release)` — one writer, so **no atomic RMW** and no
shared-line ping-pong. Device/DMA writes keep `fetch_add(Release)` on slot 0.
- `PhysicalCodePage::current_gen()` reads **all** `gen_slots` counters with
`Acquire` and returns their **max**. Any CPU's write, or a device's, is
therefore observed before a snapshot publishes — SMP-correct. The Acquire
pairs with the store's Release so the compile sees the preceding bytes.
- `CodePage::gen_slots` says how many counters follow `gen`: 16 from
`CodeBank::page` (RAM), 1 from `CodeKey::flat_page` / a device with no bank
(ROM/MMIO). `PhysicalCodePage::gen_slots` bounds the read so a single
device counter is never read out of bounds. `NEVER_COMPILABLE_GEN` is an
array of 16 counters.
- `bump_gen_all()` (snapshot restore / power-on) bumps **every** slot of every
page, so all CPUs see the invalidation.
- ppMem layout: `GEN_RATIO = 4096 / 16 / 8 = 32` bytes of gen window per 32
bytes of guest RAM, so `GEN_WINDOW_SIZE = 128 MB`. The counter *index* for
physical `p` is `(p >> 12) * GEN_SLOTS` (pointer `.add()` multiplies by 8).
All window mapping/scrub/mark sites are scaled accordingly.
- The JIT inline store now loads `MipsCore::cpu_id`, computes
`(phys >> 12) << GEN_SHIFT | (cpu + 1)`, and does a plain store; `atomic_gen`
is no longer used for the generation bump (`emit_sync` still uses it).

## Why it is safe on SMP

The per-CPU slots are single-writer (one guest CPU thread per `cpu_id`), so
there is no data race; the cross-thread visibility the JIT needs is supplied
by Release-store / Acquire-load. `current_gen()`'s max means a write by CPU 3
is seen by a compile triggered on CPU 0. Device writes on external slot 0 are
also included. No per-page generation counter is reset by a CPU store.

## Caveats / gotchas

- `current_gen()` now issues up to **16 relaxed/Acquire loads** on any dispatch
that asks `is_runnable()` (each compiled-page arrival). This is a new cost
that must be measured against the removed atomic RMW on stores. Store-heavy
guests should win; dispatch-heavy ones may lose. If needed, reduce slots to
`MAX_CPUS + 1 = 9` (keeping a power-of-two window by padding to 16 but
reading only 9).
- `GEN_RATIO` changed from 512 to 32: **do not** revert it without reverting
all window mapping/scrub/mark scalings and the ppMem tests
(`gen_window_sizing`).
- `gen_ptr()` returns slot 0 (external). A device's `code_page()` with one
never-bumped counter sets `gen_slots = 1`; `current_gen()` then reads only
that counter.
- `PpMemory::bump_gen_cpu` is currently unused: CPU stores reach the slots
through the gen window pointer (`tc_gen`) already published into the cache
and into `MipsCore::jit_tc_gen`. Do not add a duplicate bump.

## Verification

- `cargo check -p iris --features jitv2` clean; also with
`rex-jit,gr4-jit,gr2-jit,wgpu,gputrace`.
- `cargo test -p iris --lib --features jitv2 ppmem::` — 51 passed.
- `cargo test -p iris --lib --features jitv2 jitv2::` — 523 passed.
- Not measured on a guest yet (the user's Tezro / impact profile).
26 changes: 16 additions & 10 deletions docs/nutlb-design.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# nutlb — direct-mapped data-side micro-TLB
# nutlb — two-way set-associative data-side micro-TLB

Status: **shipped, unconditional (no feature flag).** Replaces the Read/Write
slots of the old 3-entry `nanotlb`; the Fetch slot survives as a separate,
specialised one-entry structure.
specialised one-entry structure. Two ways per set were added in November
2026 (§10); the measurements in this document are from the earlier one-way
design unless a section says otherwise.

Companion reading: `HACKING.md` (data path), `clanker/testing/` (TLB findings),
and an earlier nanotlb-associativity note (never checked in) — a previous 4-way/AVX2
Expand Down Expand Up @@ -641,9 +643,11 @@ with `&`.
Ranked by expected value. Revised for Generation 2 — two entries that referred
to Generation 1 machinery are now moot and marked so.

- **2-way associativity.** Still the obvious next move, and cheaper to try
now: with a 16 B entry a 2-way set is 32 B, one cache line, and the probe it
competes against is only two operations. But note that Generation 2 is the
- **2-way associativity — implemented November 2026.** Both ways of a set are
probed branchlessly; a fill toggles the set's victim bit, so the hot path
pays no LRU store (the JIT's inline probe mirrors this). The arrays grew to
16 KiB. The measurement that follows is the original argument for trying it.
But note that Generation 2 is the
*second* time a cheaper hit test beat a smarter one here (§0), so weight the
extra tag comparison accordingly. Original reasoning: 95–100% of misses are
same-set conflicts, and more sets demonstrably don't fix that (§8). Caveat
Expand Down Expand Up @@ -694,7 +698,8 @@ to Generation 1 machinery are now moot and marked so.
### 11.1 Shape

```rust
pub const NUTLB_BITS: usize = 8; // 256 entries/array
pub const NUTLB_BITS: usize = 8; // 256 entries/way
pub const NUM_NUTLB_WAYS: usize = 2;
pub const NUM_NUTLB_WORDS: usize = NUM_NUTLB_ENTRIES / 64; // 4

#[derive(Clone, Copy, Default)]
Expand All @@ -704,12 +709,13 @@ pub struct NuTlbEntry {
pub phys: u64, // PA[63:12] in [63:12]; C-field in [2:0]
}

pub nutlb: [[NuTlbEntry; NUM_NUTLB_ENTRIES]; 2], // [0]=Read [1]=Write
pub nutlb_valid: [[u64; NUM_NUTLB_WORDS]; 2], // one bit per set
pub nutlb: [[[NuTlbEntry; NUM_NUTLB_ENTRIES]; NUM_NUTLB_WAYS]; 2], // [Read|Write][way][set]
pub nutlb_valid: [[[u64; NUM_NUTLB_WORDS]; NUM_NUTLB_WAYS]; 2], // one bit per set/way
pub nutlb_lru: [[u64; NUM_NUTLB_WORDS]; 2], // next victim way, flipped on fill
```

16 B/entry, so both arrays together are **8 KiB** rather than Generation 1's
16 KiB. The Read/Write split (§2) and the `phys` encoding are unchanged.
16 B/entry, two ways, so all arrays together are **16 KiB** — the same as
Generation 1's one-way 32 B entries, but with twice the residency. The Read/Write split (§2) and the `phys` encoding are unchanged.

**A zero tag is no longer self-evidently invalid.** VA 0 is a legal page
number, so validity comes *only* from the bitmask. Never test `virttag` without
Expand Down
Loading
Loading